STPRedirectContext
@available(iOSApplicationExtension, unavailable)
@available(macCatalystApplicationExtension, unavailable)
public class STPRedirectContext: NSObject, SFSafariViewControllerDelegate, STPURLCallbackListener,
UIViewControllerTransitioningDelegate, STPSafariViewControllerDismissalDelegate
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 domain for NSErrors specific to
STPRedirectContext
Declaration
Swift
@objc public static let STPRedirectContextErrorDomain: String
-
The current state of the context.
Declaration
Swift
@objc public internal(set) 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.Declaration
Swift
@objc public convenience 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. @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.
-
Initializer for context from an
STPPaymentIntent
. This should be used when thestatus
isSTPPaymentIntentStatusRequiresAction
. If the next action involves a redirect, this init method will return a non-nil object.Declaration
Swift
@objc public convenience 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. @note Execution of the completion block does not necessarily mean the user successfully performed the redirect action.
-
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. SeeStripeAPI.handleURLCallback(with url:)
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 usestartSafariViewControllerRedirectFlowFromViewController
orstartSafariAppRedirectFlow
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 theSTPRedirectContextStateNotStarted
state.Declaration
Swift
@objc(startRedirectFlowFromViewController:) public 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. SeeStripeAPI.handleStripeURLCallback(with url:)]
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 theSTPRedirectContextStateNotStarted
state.Declaration
Swift
@objc(startSafariViewControllerRedirectFlowFromViewController:) public dynamic 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 theSTPRedirectContextStateNotStarted
state.Declaration
Swift
@objc public func startSafariAppRedirectFlow()
-
Dismisses any presented views and stops listening for any app opens or callbacks. The completion block will not be fired.
Declaration
Swift
@objc public func cancel()