【问题标题】:How Can i get Charge and how to pass token id to charge in swift (IOS)我如何获得 Charge 以及如何通过令牌 ID 快速收费(IOS)
【发布时间】:2019-11-03 23:13:15
【问题描述】:

如何快速生成条带token id,charge id。请问,有人可以帮助快速生成条带支付吗?

【问题讨论】:

    标签: ios swift iphone stripe-payments


    【解决方案1】:

    先做条带支付配置

            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
        }
    }
    

    【讨论】:

    • 我没有得到 AppConstant.Server.EPHERMAL_KEY
    • EPHERMAL_KEY 是你的后端开发者给你的 url。
    • 没有网址可以分条付款吗?
    • 我可以在stripe.com/docs/charges/android下面像在ios中那样做吗?
    【解决方案2】:

    试试这个方法。来自条带文档。

    let cardParams = STPCardParams()
    cardParams.number = "4242424242424242"
    cardParams.expMonth = 10
    cardParams.expYear = 2021
    cardParams.cvc = "123"
    
    STPAPIClient.shared().createToken(withCard: cardParams) { (token: STPToken?, error: Error?) in
        guard let token = token, error == nil else {
            // Present error to user...
            return
        }
    
        submitTokenToBackend(token, completion: { (error: Error?) in
            if let error = error {
                // Present error to user...
            }
            else {
                // Continue with payment...
            }
        })
    }
    

    【讨论】:

    • 得到这个错误 stripCard.validateCardReturningError(&underlyingError)
    • 你能给我一个示例项目吗
    猜你喜欢
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2021-12-30
    • 2018-06-27
    相关资源
    最近更新 更多