Public preview
Display the related setting Messaging Public preview
Automatically explain buy now, pay later payment options.
To display promotional messaging for buy now, pay later payment options, use the Payment Method Messaging Element.
Set up Stripe Server-side Client-side
First, you need a Stripe account. Register now.
The Stripe iOS SDK is open source, fully documented, and compatible with apps supporting iOS 13 or above.
To install the SDK, follow these steps:
- In Xcode, select File > Add Package Dependencies… and enter https://github. the relevant part of the product as the repository URL.
- Select the latest version number from our releases page .
- Add the StripePaymentSheet product to the target of your app .
Note
For details on the latest SDK release and past versions, see the Releases page on GitHub. To receive notifications when a new release is published, watch releases for the repository.
Configure the SDK with your Stripe publishable key on app start. This enables your app to make requests to the Stripe API.
AppDelegate.swift
Swift
import UIKit
import StripePaymentSheet
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StripeAPI.defaultPublishableKey = "pk_test_TYooMQauvdEDq54NiTphI7jx"
// do any other necessary launch configuration
return true
}
}
Note
Use your test keys while you test and develop, and your live mode keys when you publish your app.
Initialize the Payment Method Messaging Element
Call create to instantiate the Payment Method Messaging Element with a PaymentMethodMessagingElement.Configuration. The Configuration object contains details about the potential purchase and how information about it should be displayed. After PaymentMethodMessagingElement has successfully initialized, put its view into your UI.
@_spi(PaymentMethodMessagingElementPreview) import StripePaymentSheet
class MyViewController: UIViewController {
private var amount = 1000 // $10.00
private var currency = "USD"
private var paymentMethodMessagingElementView: UIView?
func addPaymentMethodMessagingElement() {
Task { @MainActor in
// Create configuration object
let configuration = PaymentMethodMessagingElement.Configuration(
amount: amount,
currency: currency,
)
// Create PaymentMethodMessagingElement
switch await PaymentMethodMessagingElement.create(configuration: configuration) {
case .success(let element):
let elementView = element.view
elementView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(elementView)
self.paymentMethodMessagingElementView = elementView
NSLayoutConstraint.activate([
elementView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
elementView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
elementView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
])
case .noContent:
// No element is available to display with this configuration
// You may want to adapt your UI accordingly
// ...
case .failed(let error):
// An unrecoverable error has occurred while attempting to load the element
// You may want to log the error or take other action
// ...
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Set up the remainder of your page
// ...
addPaymentMethodMessagingElement()
}
}
