STPPaymentContext
@objc(STPPaymentContext)
public class STPPaymentContext: NSObject, STPAuthenticationContext,
STPPaymentOptionsViewControllerDelegate, STPShippingAddressViewControllerDelegate
An STPPaymentContext
keeps track of all of the state around a payment. It will manage fetching a user’s saved payment methods, tracking any information they select, and prompting them for required additional information before completing their purchase. It can be used to power your application’s “payment confirmation” page with just a few lines of code.
STPPaymentContext
also provides a unified interface to multiple payment methods - for example, you can write a single integration to accept both credit card payments and Apple Pay.
STPPaymentContext
saves information about a user’s payment methods to a Stripe customer object, and requires an STPCustomerContext
to manage retrieving and modifying the customer.
-
This is a convenience initializer; it is equivalent to calling
init(customerContext:customerContext configuration:STPPaymentConfiguration.shared theme:STPTheme.defaultTheme
.Declaration
Swift
@objc public convenience init(customerContext: STPCustomerContext)
Parameters
customerContext
The customer context the payment context will use to fetch and modify its Stripe customer. - seealso: STPCustomerContext.h
Return Value
the newly-instantiated payment context
-
Initializes a new Payment Context with the provided customer context, configuration, and theme. After this class is initialized, you should also make sure to set its
delegate
andhostViewController
properties.Seealso
STPPaymentConfiguration.h- theme: The theme describing the visual appearance of all UI that the payment context automatically creates for you. - seealso: STPTheme.h
Declaration
Swift
@objc public convenience init( customerContext: STPCustomerContext, configuration: STPPaymentConfiguration, theme: STPTheme )
Parameters
customerContext
The customer context the payment context will use to fetch and modify its Stripe customer. - seealso: STPCustomerContext.h
configuration
The configuration for the payment context to use. This lets you set your Stripe publishable API key, required billing address fields, etc.
Return Value
the newly-instantiated payment context
-
Note: Instead of providing your own backend API adapter, we recommend using
STPCustomerContext
, which will manage retrieving and updating a Stripe customer for you. - seealso: STPCustomerContext.h This is a convenience initializer; it is equivalent to callinginit(apiAdapter:apiAdapter configuration:STPPaymentConfiguration.shared theme:STPTheme.defaultTheme)
.Declaration
Swift
@objc public convenience init(apiAdapter: STPBackendAPIAdapter)
-
Note: Instead of providing your own backend API adapter, we recommend using
STPCustomerContext
, which will manage retrieving and updating a Stripe customer for you. - seealso: STPCustomerContext.h Initializes a new Payment Context with the provided API adapter and configuration. After this class is initialized, you should also make sure to set itsdelegate
andhostViewController
properties.Seealso
STPPaymentConfiguration.h- theme: The theme describing the visual appearance of all UI that the payment context automatically creates for you. - seealso: STPTheme.h
Declaration
Swift
@objc public init( apiAdapter: STPBackendAPIAdapter, configuration: STPPaymentConfiguration, theme: STPTheme )
Parameters
apiAdapter
The API adapter the payment context will use to fetch and modify its contents. You need to make a class conforming to this protocol that talks to your server. - seealso: STPBackendAPIAdapter.h
configuration
The configuration for the payment context to use. This lets you set your Stripe publishable API key, required billing address fields, etc.
Return Value
the newly-instantiated payment context
-
Note: Instead of providing your own backend API adapter, we recommend using
STPCustomerContext
, which will manage retrieving and updating a Stripe customer for you. - seealso: STPCustomerContext.h The API adapter the payment context will use to fetch and modify its contents. You need to make a class conforming to this protocol that talks to your server.Seealso
STPBackendAPIAdapter.hDeclaration
Swift
@objc public private(set) var apiAdapter: STPBackendAPIAdapter { get }
-
The configuration for the payment context to use internally. - seealso: STPPaymentConfiguration.h
Declaration
Swift
@objc public private(set) var configuration: STPPaymentConfiguration { get }
-
The visual appearance that will be used by any views that the context generates. - seealso: STPTheme.h
Declaration
Swift
@objc public private(set) var theme: STPTheme { get }
-
If you’ve already collected some information from your user, you can set it here and it’ll be automatically filled out when possible/appropriate in any UI that the payment context creates.
Declaration
Swift
@objc public var prefilledInformation: STPUserInformation? { get set }
-
The view controller that any additional UI will be presented on. If you have a “checkout view controller” in your app, that should be used as the host view controller.
Declaration
Swift
@objc public weak var hostViewController: UIViewController? { get set }
-
This delegate will be notified when the payment context’s contents change. - seealso: STPPaymentContextDelegate
Declaration
Swift
@objc public weak var delegate: STPPaymentContextDelegate? { get set }
-
Whether or not the payment context is currently loading information from the network.
Declaration
Swift
@objc public var loading: Bool { get }
-
@note This is no longer recommended as of v18.3.0 - the SDK automatically saves the Stripe ID of the last selected payment method using NSUserDefaults and displays it as the default pre-selected option. You can override this behavior by setting this property. The Stripe ID of a payment method to display as the default pre-selected option. @note Set this property immediately after initializing STPPaymentContext, or call
retryLoading
afterwards.Declaration
Swift
@objc public var defaultPaymentMethod: String?
-
The user’s currently selected payment option. May be nil.
Declaration
Swift
@objc public private(set) var selectedPaymentOption: STPPaymentOption? { get set }
-
The available payment options the user can choose between. May be nil.
Declaration
Swift
@objc public private(set) var paymentOptions: [STPPaymentOption]? { get set }
-
The user’s currently selected shipping method. May be nil.
Declaration
Swift
@objc public internal(set) var selectedShippingMethod: PKShippingMethod? { get }
-
An array of STPShippingMethod objects that describe the supported shipping methods. May be nil.
Declaration
Swift
@objc public private(set) var shippingMethods: [PKShippingMethod]? { get set }
-
The user’s shipping address. May be nil. If you’ve already collected a shipping address from your user, you may prefill it by setting a shippingAddress in PaymentContext’s prefilledInformation. When your user enters a new shipping address, PaymentContext will save it to the current customer object. When PaymentContext loads, if you haven’t manually set a prefilled value, any shipping information saved on the customer will be used to prefill the shipping address form. Note that because your customer’s email may not be the same as the email provided with their shipping info, PaymentContext will not prefill the shipping form’s email using your customer’s email. You should not rely on the shipping information stored on the Stripe customer for order fulfillment, as your user may change this information if they make multiple purchases. We recommend adding shipping information when you create a charge (which can also help prevent fraud), or saving it to your own database. https://stripe.com/docs/api/payment_intents/create#create_payment_intent-shipping Note: by default, your user will still be prompted to verify a prefilled shipping address. To change this behavior, you can set
verifyPrefilledShippingAddress
to NO in yourSTPPaymentConfiguration
.Declaration
Swift
@objc public private(set) var shippingAddress: STPAddress? { get }
-
The amount of money you’re requesting from the user, in the smallest currency unit for the selected currency. For example, to indicate $10 USD, use 1000 (i.e. 1000 cents). For more information, see https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount @note This value must be present and greater than zero in order for Apple Pay to be automatically enabled. @note You should only set either this or
paymentSummaryItems
, not both. The other will be automatically calculated on demand using yourpaymentCurrency
.Declaration
Swift
@objc public var paymentAmount: Int { get set }
-
The three-letter currency code for the currency of the payment (i.e. USD, GBP, JPY, etc). Defaults to “USD”. @note Changing this property may change the return value of
paymentAmount
orpaymentSummaryItems
(whichever one you didn’t directly set yourself).Declaration
Swift
@objc public var paymentCurrency: String
-
The two-letter country code for the country where the payment will be processed. You should set this to the country your Stripe account is in. Defaults to “US”. @note Changing this property will change the
countryCode
of your Apple Pay payment requests.Seealso
PKPaymentRequest for more information.Declaration
Swift
@objc public var paymentCountry: String
-
If you support Apple Pay, you can optionally set the PKPaymentSummaryItems you want to display here instead of using
paymentAmount
. Note that the grand total (the amount of the last summary item) must be greater than zero. If not set, a single summary item will be automatically generated usingpaymentAmount
and your configuration’scompanyName
.Seealso
PKPaymentRequest for more information @note You should only set either this orpaymentAmount
, not both. The other will be automatically calculated on demand using yourpaymentCurrency.
Declaration
Swift
@objc public var paymentSummaryItems: [PKPaymentSummaryItem] { get set }
-
The presentation style used for all view controllers presented modally by the context. Since custom transition styles are not supported, you should set this to either
UIModalPresentationFullScreen
,UIModalPresentationPageSheet
, orUIModalPresentationFormSheet
. The default value isUIModalPresentationFullScreen
.Declaration
Swift
@objc public var modalPresentationStyle: UIModalPresentationStyle
-
The mode to use when displaying the title of the navigation bar in all view controllers presented by the context. The default value is
automatic
, which causes the title to use the same styling as the previously displayed navigation item (if the view controller is pushed onto thehostViewController
). If theprefersLargeTitles
property of thehostViewController
‘s navigation bar is false, this property has no effect and the navigation item’s title is always displayed as a small title. If the view controller is presented modally,automatic
andnever
always result in a navigation bar with a small title.Declaration
Swift
@objc public var largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode
-
A view that will be placed as the footer of the payment options selection view controller. When the footer view needs to be resized, it will be sent a
sizeThatFits:
call. The view should respond correctly to this method in order to be sized and positioned properly.Declaration
Swift
@objc public var paymentOptionsViewControllerFooterView: UIView?
-
A view that will be placed as the footer of the add card view controller. When the footer view needs to be resized, it will be sent a
sizeThatFits:
call. The view should respond correctly to this method in order to be sized and positioned properly.Declaration
Swift
@objc public var addCardViewControllerFooterView: UIView?
-
The API Client to use to make requests. Defaults to STPAPIClient.shared
Declaration
Swift
public var apiClient: STPAPIClient
-
The STPAPIClient instance to use to make API requests. Defaults to
STPAPIClient.shared
.Declaration
Swift
@available(swift, deprecated: 0.0.1, renamed: "apiClient") @objc(apiClient) public var _objc_apiClient: _stpobjc_STPAPIClient { get set }
-
If
paymentContext:didFailToLoadWithError:
is called on your delegate, you can in turn call this method to try loading again (if that hasn’t been called, calling this will do nothing). If retrying in turn fails,paymentContext:didFailToLoadWithError:
will be called again (and you can again call this to keep retrying, etc).Declaration
Swift
@objc public func retryLoading()
-
This creates, configures, and appropriately presents an
STPPaymentOptionsViewController
on top of the payment context’shostViewController
. It’ll be dismissed automatically when the user is done selecting their payment method. @note This method will do nothing if it is called while STPPaymentContext is already showing a view controller or in the middle of requesting a payment.Declaration
Swift
@objc public func presentPaymentOptionsViewController()
-
This creates, configures, and appropriately pushes an
STPPaymentOptionsViewController
onto the navigation stack of the context’shostViewController
. It’ll be popped automatically when the user is done selecting their payment method. @note This method will do nothing if it is called while STPPaymentContext is already showing a view controller or in the middle of requesting a payment.Declaration
Swift
@objc public func pushPaymentOptionsViewController()
-
This creates, configures, and appropriately presents a view controller for collecting shipping address and shipping method on top of the payment context’s
hostViewController
. It’ll be dismissed automatically when the user is done entering their shipping info. @note This method will do nothing if it is called while STPPaymentContext is already showing a view controller or in the middle of requesting a payment.Declaration
Swift
@objc public func presentShippingViewController()
-
This creates, configures, and appropriately pushes a view controller for collecting shipping address and shipping method onto the navigation stack of the context’s
hostViewController
. It’ll be popped automatically when the user is done entering their shipping info. @note This method will do nothing if it is called while STPPaymentContext is already showing a view controller, or in the middle of requesting a payment.Declaration
Swift
@objc public func pushShippingViewController()
-
Requests payment from the user. This may need to present some supplemental UI to the user, in which case it will be presented on the payment context’s
hostViewController
. For instance, if they’ve selected Apple Pay as their payment method, calling this method will show the payment sheet. If the user has a card on file, this will use that without presenting any additional UI. After this is called, thepaymentContext:didCreatePaymentResult:completion:
andpaymentContext:didFinishWithStatus:error:
methods will be called on the context’sdelegate
. @note This method will do nothing if it is called while STPPaymentContext is already showing a view controller, or in the middle of requesting a payment.Declaration
Swift
@objc public func requestPayment()
-
Declaration
Swift
@objc public func paymentOptionsViewController( _ paymentOptionsViewController: STPPaymentOptionsViewController, didSelect paymentOption: STPPaymentOption )
-
Declaration
Swift
@objc public func paymentOptionsViewControllerDidFinish( _ paymentOptionsViewController: STPPaymentOptionsViewController )
-
Declaration
Swift
@objc public func paymentOptionsViewControllerDidCancel( _ paymentOptionsViewController: STPPaymentOptionsViewController )
-
Declaration
Swift
@objc public func paymentOptionsViewController( _ paymentOptionsViewController: STPPaymentOptionsViewController, didFailToLoadWithError error: Error )
-
Declaration
Swift
@objc public func shippingAddressViewControllerDidCancel( _ addressViewController: STPShippingAddressViewController )
-
Declaration
Swift
@objc public func shippingAddressViewController( _ addressViewController: STPShippingAddressViewController, didEnter address: STPAddress, completion: @escaping STPShippingMethodsCompletionBlock )
-
Declaration
Swift
@objc public func shippingAddressViewController( _ addressViewController: STPShippingAddressViewController, didFinishWith address: STPAddress, shippingMethod method: PKShippingMethod? )
-
Declaration
Swift
@objc public func authenticationPresentingViewController() -> UIViewController
-
Declaration
Swift
@objc public func prepare(forPresentation completion: @escaping STPVoidBlock)