@protocol STPPaymentContextDelegate <NSObject>
STPPaymentContextDelegate to get notified when a payment context changes, finishes, encounters errors, etc. In practice, if your app has a “checkout screen view controller”, that is a good candidate to implement this protocol.
Called when the payment context encounters an error when fetching its initial set of data. A few ways to handle this are:
- If you’re showing the user a checkout page, dismiss the checkout page when this is called and present the error to the user.
- Present the error to the user using a
UIAlertControllerwith two buttons: Retry and Cancel. If they cancel, dismiss your UI. If they Retry, call
retryLoadingon the payment context.
To make it harder to get your UI into a bad state, this won’t be called until the context’s
hostViewControllerhas finished appearing.
- (void)paymentContext:(nonnull STPPaymentContext *)paymentContext didFailToLoadWithError:(nonnull NSError *)error;
func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error)
the payment context that encountered the error
the error that was encountered
This is called every time the contents of the payment context change. When this is called, you should update your app’s UI to reflect the current state of the payment context. For example, if you have a checkout page with a “selected payment method” row, you should update its payment method with
paymentContext.selectedPaymentOption.label. If that checkout page has a “buy” button, you should enable/disable it depending on the result of
- (void)paymentContextDidChange:(nonnull STPPaymentContext *)paymentContext;
func paymentContextDidChange(_ paymentContext: STPPaymentContext)
the payment context that changed
Inside this method, you should make a call to your backend API to make a PaymentIntent with that Customer + payment method, and invoke the
completionblock when that is done.
- (void)paymentContext:(nonnull STPPaymentContext *)paymentContext didCreatePaymentResult:(nonnull STPPaymentResult *)paymentResult completion:(nonnull STPPaymentStatusBlock)completion;
The context that succeeded
Information associated with the payment that you can pass to your server. You should go to your backend API with this payment result and use the PaymentIntent API to complete the payment. See https://stripe.com/docs/mobile/ios/standard#submit-payment-intents. Once that’s done call the
completionblock with any error that occurred (or none, if the payment succeeded). - see: STPPaymentResult.h
Call this block when you’re done creating a payment intent (or subscription, etc) on your backend. If it succeeded, call
completion(STPPaymentStatusSuccess, nil). If it failed with an error, call
completion(STPPaymentStatusError, error). If the user canceled, call
This is invoked by an
STPPaymentContextwhen it is finished. This will be called after the payment is done and all necessary UI has been dismissed. You should inspect the returned
statusand behave appropriately. For example: if it’s
STPPaymentStatusSuccess, show the user a receipt. If it’s
STPPaymentStatusError, inform the user of the error. If it’s
STPPaymentStatusUserCancellation, do nothing.
- (void)paymentContext:(nonnull STPPaymentContext *)paymentContext didFinishWithStatus:(STPPaymentStatus)status error:(nullable NSError *)error;
The payment context that finished
The status of the payment -
STPPaymentStatusSuccessif it succeeded,
STPPaymentStatusErrorif it failed with an error (in which case the
errorparameter will be non-nil),
STPPaymentStatusUserCancellationif the user canceled the payment.
An error that occurred, if any.
Inside this method, you should verify that you can ship to the given address. You should call the completion block with the results of your validation and the available shipping methods for the given address. If you don’t implement this method, the user won’t be prompted to select a shipping method and all addresses will be valid. If you call the completion block with nil or an empty array of shipping methods, the user won’t be prompted to select a shipping method.
If a user updates their shipping address within the Apple Pay dialog, this address will be anonymized. For example, in the US, it will only include the city, state, and zip code. The payment context will have the user’s complete shipping address by the time
- (void)paymentContext:(nonnull STPPaymentContext *)paymentContext didUpdateShippingAddress:(nonnull STPAddress *)address completion: (nonnull STPShippingMethodsCompletionBlock)completion;
The context that updated its shipping address
The current shipping address
Call this block when you’re done validating the shipping address and calculating available shipping methods. If you call the completion block with nil or an empty array of shipping methods, the user won’t be prompted to select a shipping method.