Getting started with Sources in the iOS SDK
Learn how to use Sources in your iOS application.
Creating a payment using Sources with the iOS SDK is a multi-step process:
- Create an STPSource object that represents your customer’s payment method.
- Check if further action is required from your customer.
If no further action is required:
- Confirm the source is ready to use.
- Create a charge request on your backend using the source.
If further action is required:
- Present the user with any information they may need to authorize the charge.
- On your backend, listen to Stripe webhooks to create a charge with the source.
- In your app, display the appropriate confirmation to your customer based on the source’s status.
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
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:
To determine whether further action is required from your customer, check the
flow property on the newly created
STPSource object. If
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.
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.
|Your customer must be redirected to the payment method’s website or app to confirm the charge. See the section below for more information.|
|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.|
|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. See best practices for more information on supporting different payment methods using webhooks.
Redirect 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.
STPRedirectContext's completion block is 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 webhooks on your own server to receive notification of a change in status of the source’s chargeable state. See best practices for more information on how to build a confirmation screen when using sources.
If you’d like more help, check out the example app that demonstrates creating a payment using several different payment methods.