Stripe-ios: STPAddCardViewController Never stop spinning

Created on 9 Jul 2016  Â·  14Comments  Â·  Source: stripe/stripe-ios

Hello all, I'm implementing stripe STPAddCardViewController to add cards in my app. This is my code:

`

let addCardViewController = STPAddCardViewController()

override func viewDidLoad() {
    super.viewDidLoad()



    addCardViewController.delegate = self

    let info = STPUserInformation()
    info.email = FIRAuth.auth()?.currentUser?.email
    addCardViewController.prefilledInformation = info


    let navigationController = UINavigationController(rootViewController: addCardViewController)

    self.presentViewController(navigationController, animated: false, completion: {

    })



}

func addCardViewControllerDidCancel(addCardViewController: STPAddCardViewController) {
    print("CANCEL")
}



func addCardViewController(addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: STPErrorBlock) {


}

`
The problem is that when I tap on Done, the spinner at the top right never stop spinning!
Can anyone help me? Thank you in advance.

Most helpful comment

Hi @theseansy,

If you pass an error to the addCardViewController:didCreateToken:completion: completion block, the view controller will display that error in an alert view (as you have discovered). If you want to stop this you should be able to just pass in a nil error, and then present whatever message you would like. I believe calling completion(nil) should not dismiss the view (your code is responsible for dismissing the view, as noted above and in the documentation). You should be able to call completion(nil) to stop the spinner and then just not dismiss the view and present your own message if there was an error.

If you continue to have issues, please open a new Github issue instead of adding to this thread. This thread is almost a year old and things may have changed in the sdk since then. It will be easier to have a dedicated thread for your new issue.

Hope that helps!
Brian

All 14 comments

Hey there,I'd make sure you're using the latest version (v8.0.2) of our SDK. There was a bug in version 8.0.0 that could cause this issue. Let me know if you're still having problems after that!

On Sat, Jul 9, 2016 at 6:41 PM +0200, "Marco Carnevali" [email protected] wrote:

Hello all, I'm implementing stripe STPAddCardViewController to add cards in my app. This is my code:

` let STPAddCard = STPAddCardViewController()

let addCardViewController = STPAddCardViewController()

override func viewDidLoad() {
super.viewDidLoad()

addCardViewController.delegate = self

let info = STPUserInformation()
info.email = FIRAuth.auth()?.currentUser?.email
addCardViewController.prefilledInformation = info


let navigationController = UINavigationController(rootViewController: addCardViewController)

self.presentViewController(navigationController, animated: false, completion: {

})

}

func addCardViewControllerDidCancel(addCardViewController: STPAddCardViewController) {
print("CANCEL")
}

func addCardViewController(addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: STPErrorBlock) {

}

`

The problem is that when I tap on Done, the spinner at the top right never stop spinning!

Can anyone help me? Thank you in advance.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@jflinter Yeah, I have the last SDK : 8.0.2. Did I miss something in the code?

@MarcoCarnevali I had same problem. Please make sure YourAPIClient has clientID value

@alexbtlv I don't use a APIClient, do I need it only for adding card?

Hey @MarcoCarnevali, I think I've spotted the confusion here. STPAddCardViewController's loading indicator will continue spinning after it successfully creates a token – your delegate is responsible for dismissing the view controller.

In your implementation of the didCreateToken: delegate method, you should send the token to your backend to store it on a customer, and then call the provided completion block when that call is finished. If an error occurred while talking to your backend, call completion(error) – the add card view controller will present an alert view, and the user can try adding their card again. If no error occurs, you can dismiss the view controller – you've successfully saved the user's tokenized card, and now they can continue through your checkout flow.

func addCardViewController(addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: STPErrorBlock) {
    yourAPIClient.saveToken(token) { error in
        if error != nil {
            completion(error)
        }
        else {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
}

While we fully support using STPAddCardViewController on its own, you may want to consider using STPPaymentContext, which makes it simpler to accept both Apple Pay and credit cards. Our guide covers setting up your backend, using STPPaymentContext, and configuring the look and feel of our UI elements: https://stripe.com/docs/mobile/ios

Let us know if you have any further questions!

@benzguo Thank you for your reply, then How can I show all the list of cards I saved ? With addCardViewController I can only create a card, with STPPaymentmethods I can only choose my payment method as if I add a card I can't get the token. I know I can do it with STPPaymentContext but I don't need a Payment page for now, I only need a page to add card and see the list of cards added with the Stripe UI.

I may be misunderstanding, but I think STPPaymentContext is indeed what you want. STPPaymentContext can do two things:

  1. present a payment methods view controller, where a user will see a list of their saved cards and have the option to add a new card
  2. request a payment, using the currently selected payment method.

It sounds like you only want 1. for now – it's perfectly fine to use STPPaymentContext and never call requestPayment.

@benzguo Thank you, I'm working on that... I have only a problem, it's the push/present method view controller the way to show that view? Because I would like to show that UI as soon as the user select that view from a sidebar menu. So I don't want the animation of the modal view and don't want it to dismiss in a blank view when I add a card.

Thanks :)

Currently, pushing onto a navigation controller and presenting modally are the only presentation options using STPPaymentContext. We'll think about ways to update the API to better support this use case, but for now, using a modal presentation with paymentContext.presentPaymentMethodsViewController() is probably your best bet.

@benzguo thank you for all, but I thought you were able to save cards locally without connecting to APIClient, I am now able to save cards on the customer ID with the stripe api, but how can I retrieve all cards and show it in the Stripe UI? Thanks in advance

Hey there,

If you've set up everything correctly, when you call presentPaymentMethodsViewController(), you should see a list of cards associated with your customer.

For demonstration purposes, the example app will save cards locally, but in order to actually save cards on your Stripe customer, you'll need to set up your backend (we have an example backend that you can deploy to heroku here)

It'll be easiest for me to help if you can take a look at our integration guide, and provide some more detail about which steps you've taken.

Hope this helps!

@benzguo - I found this thread having the same issue with the loading spinner staying open in place of done. I fixed it by calling completion(error) with a non-nil error object. The problem is the STPAddCardViewController then immediately shows an alert with the localizedDescription of the error (or just the error domain if there is no userInfo on the error), but the alert disappears immediately after displaying.

Is anyone else seeing this? I would prefer the stripe SDK not try to express this error to the user with an alert, so I can show a customized message. Thus I tried calling completion(nil), but that dismisses the view controller, and I want it to stay open for the user to continue editing their payment info.

Hi @theseansy,

If you pass an error to the addCardViewController:didCreateToken:completion: completion block, the view controller will display that error in an alert view (as you have discovered). If you want to stop this you should be able to just pass in a nil error, and then present whatever message you would like. I believe calling completion(nil) should not dismiss the view (your code is responsible for dismissing the view, as noted above and in the documentation). You should be able to call completion(nil) to stop the spinner and then just not dismiss the view and present your own message if there was an error.

If you continue to have issues, please open a new Github issue instead of adding to this thread. This thread is almost a year old and things may have changed in the sdk since then. It will be easier to have a dedicated thread for your new issue.

Hope that helps!
Brian

@bdorfman-stripe - Thank you for the detailed response. It was my fault that the VC was being dismissed. I was doing this in one of the delegate calls back for completion

Was this page helpful?
0 / 5 - 0 ratings

Related issues

levisbotio picture levisbotio  Â·  6Comments

chrismanderson picture chrismanderson  Â·  4Comments

sumitmundra picture sumitmundra  Â·  5Comments

tommanuel92 picture tommanuel92  Â·  4Comments

coolcool1994 picture coolcool1994  Â·  4Comments