Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Add payment service
Browse files Browse the repository at this point in the history
  • Loading branch information
aksalj committed Dec 7, 2017
1 parent 8c72164 commit d83316c
Show file tree
Hide file tree
Showing 8 changed files with 636 additions and 82 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ For more information on:

- How to listen for subscription notifications: [http://docs.africastalking.com/subscriptions/callback](http://docs.africastalking.com/subscriptions/callback)

### `Payment` **TODO**
### `Payment`

- `checkout(request: CheckoutRequest)`: Initiate checkout(mobile, card or bank).

- `validateCheckout(request: CheckoutValidateRequest)`: Validate checkout (card or bank).

- `payConsumers(productName: String, recipients: [Consumer])`: Send money to consumer.
- `mobileB2C(productName: String, recipients: [Consumer])`: Send money to consumer.

- `payBusiness(productName: String, recipient: Business)`: Send money to business.
- `mobileB2B(productName: String, recipient: Business)`: Send money to business.

### Voice

Expand Down
91 changes: 14 additions & 77 deletions Sources/AfricasTalking/AfricasTalking.swift
Original file line number Diff line number Diff line change
@@ -1,82 +1,11 @@
import Foundation
import Alamofire
//
// AfricasTalking.swift
// AfricasTalking
//
// Created by Salama Balekage on 05/12/2017.
//
import SwiftyJSON

extension Date {
var millisecondsSince1970:Int64 {
return Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}

init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
}
}

open class Service {

internal static let PRODUCTION_DOMAIN = "africastalking.com"
internal static let SANDBOX_DOMAIN = "sandbox.africastalking.com"
internal static var HOST = "localhost"
internal static var PORT = 35897

internal static var DISABLE_TLS = false

internal static var USERNAME: String? = nil
internal static var API_KEY: String? = nil
internal static var AUTH_TOKEN: Africastalking_ClientTokenResponse? = nil

internal static let GrpcClient: Africastalking_SdkServerServiceService = {
let address = "\(Service.HOST):\(Service.PORT)"
if (Service.DISABLE_TLS) {
return Africastalking_SdkServerServiceService(address: address)
} else {
return Africastalking_SdkServerServiceService(address: address, certificates: "", host: Service.HOST) // FIXME
}
}()

internal var baseUrl: String? = nil

internal var isSandbox: Bool {
get {
return Service.USERNAME == "sandbox"
}
}

internal var headers: HTTPHeaders {
get {
let authHeader = Service.API_KEY != nil ? "apiKey" : "authToken"
var authValue = Service.API_KEY
if (authValue == nil) {
if (Service.AUTH_TOKEN == nil || Service.AUTH_TOKEN!.expiration < Date().millisecondsSince1970) {
Service.fetchToken()
}
authValue = Service.AUTH_TOKEN?.token
}

return [
authHeader : authValue ?? "none",
"Accept": "application/json"
]
}
}

init() {
if (Service.API_KEY == nil && Service.AUTH_TOKEN == nil) {
// init token
Service.fetchToken()
}
}

private static func fetchToken() {
do {
let req = Africastalking_ClientTokenRequest()
Service.AUTH_TOKEN = try Service.GrpcClient.gettoken(req)
Service.USERNAME = Service.AUTH_TOKEN?.username ?? nil
} catch { }
}

}

public struct AfricasTalking {

public typealias Callback = (_ error: String?, _ data: JSON?) -> Void
Expand All @@ -86,6 +15,7 @@ public struct AfricasTalking {
private static var voiceService: VoiceService? = nil
private static var tokenService: TokenService? = nil
private static var smsService: SmsService? = nil
private static var paymentService: PaymentService? = nil

private init() { }

Expand Down Expand Up @@ -134,4 +64,11 @@ public struct AfricasTalking {
}
return smsService!
}

public static func getPaymentService() -> PaymentService {
if (paymentService == nil) {
paymentService = PaymentService()
}
return paymentService!
}
}
Loading

0 comments on commit d83316c

Please sign in to comment.