首页 > 解决方案 > 我如何获得 Charge 以及如何通过令牌 ID 快速收费(IOS)

问题描述

如何快速生成条带令牌ID,收费ID。请问,有人可以帮助快速生成条带支付吗?

标签: iosswiftiphonestripe-payments

解决方案


先做条带支付配置

        let configuration = STPPaymentConfiguration.shared()
    configuration.additionalPaymentMethods = .all
    configuration.appleMerchantIdentifier = "Your stripe identifier"
    configuration.canDeletePaymentMethods = true
    configuration.createCardSources = false

    let customerContext = STPCustomerContext(keyProvider: MyAPIClient.sharedClient)
    paymentMethodViewController = STPPaymentMethodsViewController(configuration: configuration,
                                                                  theme: STPTheme.init(),
                                                                  customerContext: customerContext,
                                                                  delegate: self)
    self.navigationController?.pushViewController(controller, animated: true)

ApiClient 生成临时密钥的代码

 class MyAPIClient: NSObject, STPEphemeralKeyProvider {

static let sharedClient = MyAPIClient()

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
    let url = AppConstant.Server.EPHERMAL_KEY

    let user = UserDefaultManager.shared.readUser()
    let header: HTTPHeaders = ["api_token":  user.apiToken ?? "",
                               "Content-Type": "application/json"]

    Alamofire.request(url,
                      method: .get,
                      parameters: [
        "api_version": apiVersion,
        "id": user.id ?? -1
        ],
        headers: header)
        .validate(statusCode: 200..<300)
        .responseJSON { responseJSON in
            switch responseJSON.result {
            case .success(let json):
                completion(json as? [String: AnyObject], nil)
            case .failure(let error):
                completion(nil, error)
            }
    }
}

}

然后在委托方法中

   func paymentMethodsViewController(_ paymentMethodsViewController: STPPaymentMethodsViewController, didSelect paymentMethod: STPPaymentMethod) {

    var paymentStripeId: String?
    if let source = paymentMethod as? STPSource {
        paymentStripeId = source.stripeID
    } else if let card = paymentMethod as? STPCard {
        self.stpCard = card
    }
}

推荐阅读