Reputation: 11042
I am using stpaddcardviewcontroller UI element to ask a customer to enter the new card information
let addCardViewController = STPAddCardViewController()
addCardViewController.delegate = self
present(navigationController, animated: true)
Above code will show the UI
but after tapping on the Done button, I need to get the token (generated from the added card information) for adding a new card/source to the existing customer.
I am using the following delegate method to get the token from stpaddcardviewcontroller
UI
extension viewController : STPAddCardViewControllerDelegate {
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
dismiss(animated: true)
}
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) {
dismiss(animated: true)
}
How can I receive the card token in delegate methods? So that I can pass to Add Card API ?
Upvotes: 4
Views: 2609
Reputation: 3311
You get the PaymentMethod token in your didCreatePaymentMethod
delegate.
Stripe-iOS (using your publishable key) cannot update Customers by itself. There are 2 options here:
1) Post the token to a backend server endpoint where you would use a Stripe API library (with your Stripe secret key) and attach the PaymentMethod to a Stripe Customer: https://stripe.com/docs/api/payment_methods/attach
2) If you use Ephemeral keys on your iOS app allows you to attach a PaymentMethod to a Stripe Customer. The SDK handles it for you. Stripe-iOS actually offers prebuilt UI to handle this too such as STPPaymentOptionsViewController. You would need to refer to the "standard integration" guide here which uses STPCustomerContext: https://stripe.com/docs/mobile/ios/standard
Upvotes: 1