简体   繁体   English

我如何获得费用以及如何通过令牌ID快速付款(IOS)

[英]How Can i get Charge and how to pass token id to charge in swift (IOS)

How to generate stripe token id, charge id in swift. 如何快速生成条带令牌ID,费用ID。 Please, could anyone help on the generation of stripe payment in swift? 请,任何人都可以迅速帮助产生条纹支付吗?

First do stripe payment configuration 首先做条纹支付配置

        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)

Code For ApiClient to generate ephermal key 代码供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)
            }
    }
}

} }

Then in delegate method 然后在委托方法

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

Try out this method. 试试这个方法。 From stripe document. 来自条带文档。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM