STPRedirectContext


@interface STPRedirectContext : NSObject

This is a helper class for handling redirects associated with STPSource and STPPaymentIntents.

Init and retain an instance with the Source or PaymentIntent you want to handle, then choose a redirect method. The context will fire the completion handler when the redirect completes.

Due to the nature of iOS, very little concrete information can be gained during this process, as all actions take place in either the Safari app or the sandboxed SFSafariViewController class. The context attempts to detect when the user has completed the necessary redirect action by listening for both app foregrounds and url callbacks received in the app delegate. However, it is possible the when the redirect is completed, the user may have not actually completed the necessary actions to authorize the charge.

You should not use either this class, nor STPAPIClient, as a way to determine when you should charge the Source or to determine if the redirect was successful. Use Stripe webhooks on your backend server to listen for Source state changes and to make the charge.

Note

You must retain this instance for the duration of the redirect flow. This class dismisses any presented view controller upon deallocation.

See https://stripe.com/docs/sources/best-practices

  • The current state of the context.

    Declaration

    Objective-C

    @property (readonly, nonatomic) STPRedirectContextState state;

    Swift

    var state: STPRedirectContextState { get }
  • Initializer for context from an STPSource.

    Note

    You must ensure that the returnURL set up in the created source correctly goes to your app so that users can be returned once they complete the redirect in the web broswer.

    Note

    Execution of the completion block does not necessarily mean the user successfully performed the redirect action. You should listen for source status change webhooks on your backend to determine the result of a redirect.

    Declaration

    Objective-C

    - (nullable instancetype)
        initWithSource:(nonnull STPSource *)source
            completion:(nonnull STPRedirectContextSourceCompletionBlock)completion;

    Swift

    init?(source: STPSource, completion: @escaping STPRedirectContextSourceCompletionBlock)

    Parameters

    source

    The source that needs user redirect action to be taken.

    completion

    A block to fire when the action is believed to have been completed.

    Return Value

    nil if the specified source is not a redirect-flow source. Otherwise a new context object.

  • Initializer for context from an STPPaymentIntent.

    This should be used when the status is STPPaymentIntentStatusRequiresAction. If the next action involves a redirect, this init method will return a non-nil object.

    Note

    Execution of the completion block does not necessarily mean the user successfully performed the redirect action.

    Declaration

    Objective-C

    - (nullable instancetype)
        initWithPaymentIntent:(nonnull STPPaymentIntent *)paymentIntent
                   completion:
                       (nonnull STPRedirectContextPaymentIntentCompletionBlock)
                           completion;

    Swift

    init?(paymentIntent: STPPaymentIntent, completion: @escaping STPRedirectContextPaymentIntentCompletionBlock)

    Parameters

    paymentIntent

    The STPPaymentIntent that needs a redirect.

    completion

    A block to fire when the action is believed to have been completed.

    Return Value

    nil if the provided PaymentIntent does not need a redirect. Otherwise a new context object.

  • Unavailable

    Use initWithSource:completion:

    Declaration

    Objective-C

    - (nonnull instancetype)init;
  • Starts a redirect flow.

    You must ensure that your app delegate listens for the returnURL that you set on the Stripe object, and forwards it to the Stripe SDK so that the context can be notified when the redirect is completed and dismiss the view controller. See [Stripe handleStripeURLCallbackWithURL:]

    The context will listen for both received URLs and app open notifications and fire its completion block when either the URL is received, or the next time the app is foregrounded.

    The context will initiate the flow by presenting a SFSafariViewController instance from the passsed in view controller. If you want more manual control over the redirect method, you can use startSafariViewControllerRedirectFlowFromViewController or startSafariAppRedirectFlow

    If the redirect supports a native app, and that app is is installed on the user’s device, this call will do a direct app-to-app redirect instead of showing a web url.

    Note

    This method does nothing if the context is not in the STPRedirectContextStateNotStarted state.

    Declaration

    Objective-C

    - (void)startRedirectFlowFromViewController:
        (nonnull UIViewController *)presentingViewController;

    Swift

    func startRedirectFlow(from presentingViewController: UIViewController)

    Parameters

    presentingViewController

    The view controller to present the Safari view controller from.

  • Starts a redirect flow by presenting an SFSafariViewController in your app from the passed in view controller.

    You must ensure that your app delegate listens for the returnURL that you set on the Stripe object, and forwards it to the Stripe SDK so that the context can be notified when the redirect is completed and dismiss the view controller. See [Stripe handleStripeURLCallbackWithURL:]

    The context will listen for both received URLs and app open notifications and fire its completion block when either the URL is received, or the next time the app is foregrounded.

    Note

    This method does nothing if the context is not in the STPRedirectContextStateNotStarted state.

    Declaration

    Objective-C

    - (void)startSafariViewControllerRedirectFlowFromViewController:
        (nonnull UIViewController *)presentingViewController;

    Swift

    func startSafariViewControllerRedirectFlow(from presentingViewController: UIViewController)

    Parameters

    presentingViewController

    The view controller to present the Safari view controller from.

  • Starts a redirect flow by calling openURL to bounce the user out to the Safari app.

    The context will listen for app open notifications and fire its completion block the next time the user re-opens the app (either manually or via url)

    Note

    This method does nothing if the context is not in the STPRedirectContextStateNotStarted state.

    Declaration

    Objective-C

    - (void)startSafariAppRedirectFlow;

    Swift

    func startSafariAppRedirectFlow()
  • Dismisses any presented views and stops listening for any app opens or callbacks. The completion block will not be fired.

    Declaration

    Objective-C

    - (void)cancel;

    Swift

    func cancel()