简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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