I want to implement Stripe Payment gateway. And need to generate token and pass it to backend.
private func getToken(){
let cardParams = STPCardParams()
cardParams.number = paymentCardTextField?.cardNumber
cardParams.expMonth = (paymentCardTextField?.expirationMonth)!
cardParams.expYear = (paymentCardTextField?.expirationYear)!
cardParams.cvc = paymentCardTextField?.cvc
STPAPIClient.shared().createToken(withCard: cardParams) { (token: STPToken?, error: Error?) in
guard let token = token, error == nil else {
// Present error to user...
return
}
self.dictPayData["stripe_token"] = token.tokenId
print(self.dictPayData)
}
}
Make sure you need to add your Stripe publishableKey (live/test)
in AppDelegate.swift
import Stripe
class AppDelegate: UIResponder, UIApplicationDelegate{
// var navCon: UINavigationController?
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application la
STPPaymentConfiguration.shared().publishableKey = "pk_test_DSFDSFSDFDFSDFSDFDSDSF"
return true
}
}
in my PaymentView.swift
Note: Use STPPaymentCardTextFieldDelegate
for card validation.
class PaymentView: UIViewController,STPPaymentCardTextFieldDelegate {
@IBOutlet weak var paymentCardTextField = STPPaymentCardTextField()
}
private func getToken(){
let cardParams = STPCardParams()
cardParams.number = paymentCardTextField?.cardNumber
cardParams.expMonth = (paymentCardTextField?.expirationMonth)!
cardParams.expYear = (paymentCardTextField?.expirationYear)!
cardParams.cvc = paymentCardTextField?.cvc
STPAPIClient.shared().createToken(withCard: cardParams) { (token: STPToken?, error: Error?) in
guard let token = token, error == nil else {
// Present error to user...
return
}
self.dictPayData["stripe_token"] = token.tokenId
print(self.dictPayData)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With