Using PaymentIntents on iOS Beta

    Learn how to build an iOS application that uses PaymentIntents to accept card payments.

    Accepting a card payment with PaymentIntents and the iOS SDK is a four-step process, with server-side and client-side actions:

    1. Create a PaymentIntent and make its client secret accessible to your application
    2. Collect card information and create an STPCardParams object
    3. Confirm the PaymentIntent
    4. Redirect and authenticate the payment if necessary

    Step 1: Create a PaymentIntent and make its client secret accessible to your application

    When a customer begins the checkout process, create a PaymentIntent on your server. To use the PaymentIntent, you need to make its client secret accessible to your mobile application. Typically, the best approach is to serve it from an HTTP endpoint on your server and then retrieve it on the client side.

    Step 2: Collect card information and create an STPCardParams object

    In your application, collect card details from the customer. You can use a STPPaymentCardTextField, retrieving an STPCardParams object from the cardParams property. Or you can collect payment information through your own form and pass the collected information into a new STPCardParams instance.

    Once you have the STPCardParams object, pass it to cardParamsWithCard to create a new STPSourceParams object. If you already have a source that you would like to associate with the PaymentIntent, you may skip this step.

    The following code demonstrates how to create the STPSourceParams object from an STPCardParams object:

    let sourceParams = STPSourceParams.cardParams(withCard: paymentTextField.cardParams)
    STPSourceParams *sourceParams = [STPSourceParams cardParamsWithCard:paymentTextField.cardParams];

    Step 3: Confirm the PaymentIntent

    To initiate payment collection, you must confirm that your customer intends to pay with the provided payment details. Add the desired source to an STPPaymentIntentParams object initialized with the PaymentIntent’s client secret. If you are using a new source, you can assign it to the sourceParams property. If you are attaching an existing source, use the sourceId property instead.

    If you are collecting details of a payment method that involves redirection as part of the authentication process, include a return URL to indicate where the PaymentIntent should return when authentication is complete.

    Refer to our iOS SDK documentation for more information about handling redirection to authenticate payment methods.

    let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
    paymentIntentParams.sourceParams = sourceParams
    paymentIntentParams.returnURL = "your-app://stripe-redirect"
    STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:client_secret];
    paymentIntentParams.sourceParams = sourceParams;
    paymentIntentParams.returnURL = "your-app://stripe-redirect";

    If you’d like to send additional data, you can assign values to the relevant properties on the STPPaymentIntentParams object. Any extra parameters that you add are included in the request to Stripe. To send an e-mail to the customer on payment confirmation, for example, you can use the receiptEmail property.

    To finish confirming the payment, pass the STPPaymentIntentParams object to the confirmPaymentIntentWithParams method on a STAPIClient sharedClient:

    let client = STPAPIClient.shared()
    client.confirmPaymentIntent(with: paymentIntentParams, completion: { (paymentIntent, error) in
      if let error = error {
          // handle error
      } else if let paymentIntent = paymentIntent {
          // see below to handle the confirmed PaymentIntent
      }
    })
    [[STPAPIClient sharedClient] confirmPaymentIntentWithParams:paymentIntentParams
        completion:^(STPPaymentIntent * _Nullable paymentIntent, NSError * _Nullable error) {
        if (error != nil) {
            // handle error
        } else {
            // see below to handle the confirmed PaymentIntent
        }
    }];

    Step 4: Redirect and authenticate the payment if necessary

    Some payment methods may require additional authentication steps in order to complete a payment. To determine whether further action is required from your customer, check the status property of the STPPaymentIntent object returned from the confirmation function in the previous step. If the status is STPPaymentIntentStatusRequiresSourceAction, further action is required from your customer in order to complete the payment.

    If the required source action is authorize_with_url, pass the STPPaymentIntent to a new instance of STPRedirectContext to complete this action:

    if paymentIntent.status == .requiresSourceAction {
      if let redirectContext = STPRedirectContext(paymentIntent: paymentIntent, completion: redirectCompletionBlock) {
          self.redirectContext = redirectContext
          // opens SFSafariViewController to the necessary URL
          redirectContext.startRedirectFlow(from: self)
      } else {
          // This PaymentIntent action is not yet supported by the SDK.
      }
    }
    if (paymentIntent.status == STPPaymentIntentStatusRequiresSourceAction) {
        self.redirectContext = [[STPRedirectContext alloc] initWithPaymentIntent:paymentIntent completion:redirectCompletionBlock];
        if (self.redirectContext) {
            // opens SFSafariViewController to the necessary URL
            [self.redirectContext startRedirectFlowFromViewController:self];
        } else {
            // This PaymentIntent action is not yet supported by the SDK.
        }
    }

    The STPRedirectContext completion block is called after your customer returns to the application. At this point, the user may or may not have completed the authorization process.

    If your application needs to know the status of the PaymentIntent, you can use retrievePaymentIntentWithClientSecret:completion: to fetch the STPPaymentIntent and check for changes.

    Using PaymentIntents with Apple Pay

    To integrate with Apple Pay, you can use the STPSource object returned by your Apple Pay integration to confirm the PaymentIntent. Set the sourceId property in the STPPaymentIntentParams object, and then confirm the PaymentIntent as normal.

    let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
    paymentIntentParams.sourceId = myApplePaySource.stripeID
    STPAPIClient.shared().confirmPaymentIntent(with: paymentIntentParams) { ... }
    STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:client_secret];
    paymentIntentParams.sourceId = myApplePaySource.stripeID;
    [[STPAPIClient sharedClient] confirmPaymentIntentWithParams:paymentIntentParams ...];

    Next steps

    You now have an iOS integration that can accept card payments with PaymentIntents, prompting customers for authentication if needed.

    Questions?

    We're always happy to help with code or other questions you might have! Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    Was this page helpful? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.