Getting Started with Sources in the iOS SDK

    Make use of Sources with the iOS SDK. If you need help after reading this, search our documentation or check out answers to common questions. You can even chat live with other developers in #stripe on freenode.

    Creating a payment using Sources with the iOS SDK is a multi-step process:

    1. Create an STPSource object that represents your customer's payment method
    2. Check if further action is required from your customer

    If no further action is required:

    1. Confirm the source is ready to use
    2. Tell your backend to create a charge request using the source

    If further action is required:

    1. Present the user with any information they may need to authorize the charge
    2. In your backend, listen to Stripe webhooks to create a charge with the source
    3. In your app, display the appropriate confirmation to your customer based on the source's status

    Create an STPSource object

    Once you’ve collected your customer’s payment details, you can use the STPAPIClient class to create a source. First, assemble an STPSourceParams object with the payment information you’ve collected. Then, pass this object to STPAPIClient’s createSourceWithParams: method.

    To create an STPSourceParams object, we recommend that you use one of the helper constructors we provide, which specify the information needed for each payment method. For example, to create a source for a Sofort payment:

    let sourceParams = STPSourceParams.sofortParams(withAmount: 1099,
                                                    returnURL: "payments-example://stripe-redirect",
                                                    country: "DE",
                                                    statementDescriptor: "ORDER AT11990")
    STPAPIClient.shared().createSource(with: sourceParams, completion: completionBlock)
    STPSourceParams *sourceParams = [STPSourceParams sofortParamsWithAmount:1099
                                                                  returnURL:@"payments-example://stripe-redirect"
                                                                    country:@"DE"
                                                        statementDescriptor:@"ORDER AT11990"];
    [[STPAPIClient sharedClient] createSourceWithParams:sourceParams completion:completionBlock];

    Check if further action is required from your customer

    To determine whether further action is required from your customer, check the flow property on the newly created STPSource object. If flow is STPSourceFlowNone, no further action is required. For example, if you create a source for a card payment, its status is immediately set to STPSourceStatusChargeable. No additional customer action is needed, so you can tell your backend to create a charge with the source right away.

    let cardParams = STPCardParams()
    cardParams.name = "Jenny Rosen"
    cardParams.number = "4242424242424242"
    cardParams.expMonth = 12
    cardParams.expYear = 18
    cardParams.cvc = "424"
    
    let sourceParams = STPSourceParams.cardParams(withCard: cardParams)
    STPAPIClient.shared().createSource(with: sourceParams) { (source, error) in
        if let s = source, s.flow == .none && s.status == .chargeable {
            self.createBackendChargeWithSourceID(s.stripeID)
        }
    }
    STPCardParams *cardParams = [STPCardParams new];
    cardParams.name = @"Jenny Rosen";
    cardParams.number = @"4242424242424242";
    cardParams.expMonth = 12;
    cardParams.expYear = 18;
    cardParams.cvc = @"123";
    
    STPSourceParams *sourceParams = [STPSourceParams cardParamsWithCard:cardParams];
    
    [[STPAPIClient sharedClient] createSourceWithParams:sourceParams completion:^(STPSource *source, NSError *error) {
        if (source.flow == STPSourceFlowNone
            && source.status == STPSourceStatusChargeable) {
            [self createBackendChargeWithSourceID:source.stripeID];
        }
    }];

    If the source’s flow is not STPSourceFlowNone, then your customer needs to complete an action before the source can be used in a charge request.

    Flow Description
    STPSourceFlowRedirect Your customer must be redirected to the payment method’s website or app to confirm the charge. See the section below for more information.
    STPSourceFlowReceiver Your customer must push funds to the account information provided in the Source object. See the documentation for the specific payment method you are using for more information.
    STPSourceFlowVerification Your customer must verify ownership of their account by providing a code that you post to the Stripe API for authentication. See the documentation for the specific payment method you are using for more information.

    If the source requires further action from your customer, your iOS app should not tell your backend to create a charge request. Instead, your backend should listen for the source.chargeable webhook event to charge the source. This ensures that the source is charged even if the user never returns to your app after taking the required action. You can refer to our best practices for more information on supporting different payment methods using webhooks.

    Redirecting your customer to authorize a source

    For sources that require redirecting your customer to authorize the payment, you need to specify a return URL when you create the source. This allows your customer to be redirected back to your app after they authorize the payment. For this return URL, you can either use a custom URL scheme or a universal link supported by your app. For more information on registering and handling URLs in your app, refer to the Apple documentation:

    To handle redirecting your customer to the URL in the source object’s redirect.url parameter, we recommend using STPRedirectContext, which you can use to open the URL in SFSafariViewController, if available, or mobile Safari otherwise. To use STPRedirectContext, you’ll need to first set up your app delegate to forward URLs to the Stripe SDK.

    // This method handles opening native URLs (e.g., "yourexampleapp://")
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let stripeHandled = Stripe.handleURLCallback(with: url)
        if (stripeHandled) {
            return true
        } else {
            // This was not a stripe url – do whatever url handling your app
            // normally does, if any.
        }
        return false
    }
    
    // This method handles opening univeral link URLs (e.g., "https://example.com/stripe_ios_callback")
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            if let url = userActivity.webpageURL {
                let stripeHandled = Stripe.handleURLCallback(with: url)
                if (stripeHandled) {
                    return true
                } else {
                    // This was not a stripe url – do whatever url handling your app
                    // normally does, if any.
                }
            }
        }
        return false
    }
    // This method handles opening native URLs (e.g., "yourexampleapp://")
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
        BOOL stripeHandled = [Stripe handleStripeURLCallbackWithURL:url];
        if (stripeHandled) {
            return YES;
        } else {
            // This was not a stripe url – do whatever url handling your app
            // normally does, if any.
        }
        return NO;
    }
    
    // This method handles opening univeral link URLs (e.g., "https://example.com/stripe_ios_callback")
    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
        if (userActivity.activityType == NSUserActivityTypeBrowsingWeb) {
            if (userActivity.webpageURL) {
                BOOL stripeHandled = [Stripe handleStripeURLCallbackWithURL:url];
                if (stripeHandled) {
                    return YES;
                } else {
                    // This was not a stripe url – do whatever url handling your app
                    // normally does, if any.
                }
                return NO;
            }
        }
    }

    STPRedirectContext’s completion block will be called after your customer returns to your app. At this point, the user may or may not have completed the authorization process. You can use use webhooks on your own server to receive notification of a change in status of the source’s chargeable state. Please refer to our best practices guide for more information on how to build a confirmation screen when using sources.

    More information

    If you'd like more help, check out our example app that demonstrates creating a payment using several different payment methods. In addition, here's some documentation you might want to read next: