Backend API V2.4 enhances reliability and minimizes synchronization issues with external processors by significantly extending payment timeouts. This longer timeout accommodates external processor limits and internal operations, ensuring more reliable transaction handling. These improvements are fully supported starting with Primer iOS SDK 2.34.0, so upgrading your app to use the newest SDK version is highly recommended when creating client sessions with API V2.4.
Install the SDK
With CocoaPods
Add the following in your Podfile:use_frameworks!
target 'MyApp' do
# 👇 Add this line
pod 'PrimerSDK'
# ...
end
Then run pod install to install PrimerSDK on your workspace.In case you encounter an error that the bundle needs signing on Xcode 14, add the following post-install script in your podfile.post_install do |installer|
installer.pods_project.targets.each do |target|
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
end
end
end
end
With Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into Xcode. In order to add PrimerSDK with Swift Package Manager:
- Select your project, and then navigate to
Package Dependencies
- Click on the + button at the bottom-left of the
Packages section
- Paste https://github.com/primer-io/primer-sdk-ios.git into the Search Bar, or search for
primer-sdk-ios in the Search Bar.
- Press Add Package
- Let Xcode download the package and set everything up
For more details about SDK versions, please see our changelog. Initialize Universal Checkout
Import the PrimerSDK and set its delegate as shown in the following example:import PrimerSDK
class MyViewController: UIViewController, PrimerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Initialize the SDK with the default settings.
Primer.shared.configure(delegate: self)
}
func primerDidCompleteCheckoutWithData(_ data: PrimerCheckoutData) {
// Primer checkout completed with data
// do something...
}
}
Show Universal Checkout
At this step you should have a client token available - see Manage Client Sessions for more.Call the showUniversalCheckout(clientToken:) function (as shown below) to present Universal Checkout.class MyViewController: UIViewController, PrimerDelegate {
func startUniversalCheckout() {
Primer.shared.showUniversalCheckout(clientToken: self.clientToken)
}
}
You should now be able to see Universal Checkout! The user can now interact with Universal Checkout, and the SDK will create the payment. The payment’s data will be returned on primerDidCompleteCheckoutWithData(_:) configured in the previous step.
When the user interacts with the checkout and clicks to pay, the Universal Checkout:
- Shows the payment method screen if applicable
- Tokenizes the payment method for payment
- Creates the payment
- Handles any other interactions (e.g. 3DS)
Handle successful payments
Listen to delegate function
Listen to the primerDidCompleteCheckoutWithData(_:) delegate functionThis function will notify you of a successful payment.class MyViewController: UIViewController {
// ...
func primerDidCompleteCheckoutWithData(_ data: PrimerCheckoutData) {
// Primer checkout completed with data
// Do something with the data, e.g. show the payment id
}
}
Handle webhooks in your dashboard
To receive updates about the status of your payments you’ll need to listen to webhooks. This is particularly useful for updating an order or any other data stored server-side.Head to the Developers section of the Primer Dashboard to setup and test a webhook for PAYMENT.STATUS event.If you are not yet ready to receive webhooks, you can use https://webhook.site to test your implementation.Handle failed payments
Any errors, cancelled payment interactions or failed payments will trigger the primerDidFailWithError(_:data:decisionHandler:) delegate function.class MyViewController: UIViewController, PrimerDelegate {
// ...
func primerDidFailWithError(_ error: Error, data: PrimerCheckoutData?, decisionHandler: @escaping ((PrimerErrorDecision) -> Void)) {
// 👇 Call the decision handler to show a failure message
decisionHandler(.fail(withErrorMessage: message))
}
}
Backend API V2.4 enhances reliability and minimizes synchronization issues with external processors by significantly extending payment timeouts. This longer timeout accommodates external processor limits and internal operations, ensuring more reliable transaction handling. These improvements are fully supported starting with Primer Android SDK 2.35.0, so upgrading your app to use the newest SDK version is highly recommended when creating client sessions with API V2.4.
Install the SDK
Add the following to your app/build.gradle file:repositories {
mavenCentral()
}
dependencies {
implementation 'io.primer:android:latest.version'
}
For more details about SDK versions, please see our changelog. It is highly recommended adding the following settings to your app/build.gradle file:android {
kotlinOptions {
freeCompilerArgs += '-Xjvm-default=all'
}
}
Initialize Universal Checkout
Prepare the PrimerCheckoutListener that will handle the callbacks that happen during the lifecycle. Import the Primer SDK and set its listener as shown in the following example:class CheckoutActivity : AppCompatActivity() {
private val listener = object : PrimerCheckoutListener {
// 👇 [Required] This function is called when the checkout has been completed
override fun onCheckoutCompleted(checkoutData: PrimerCheckoutData) {
// Show an order confirmation screen...
// checkoutData contains the payment that has been created
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
configureCheckout()
}
private fun configureCheckout() {
// 👇 Initialize the SDK with the default settings.
Primer.instance.configure(settings = PrimerSettings(), listener = listener)
}
}
Show Universal Checkout
At this step you should have a client token available - see Manage Client Sessions for more.When the client token is retrieved, show Universal Checkout by calling showUniversalCheckout method:class CheckoutActivity : AppCompatActivity() {
// other code goes here
private lateinit var viewModel: CheckoutViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupViewModel()
setupObservers()
fetchClientToken()
}
private fun setupViewModel() {
viewModel = ViewModelProvider(this).get(CheckoutViewModel::class.java)
}
private fun fetchClientToken() = viewModel.fetchClientToken()
private fun setupObservers() {
viewModel.clientToken.observe(this) { clientToken ->
showUniversalCheckout(clientToken)
}
}
private fun showUniversalCheckout(clientToken: String) {
Primer.instance.showUniversalCheckout(this, clientToken)
}
}
You should now be able to see Universal Checkout! The user can now interact with Universal Checkout, and the SDK will create the payment. The payment’s data will be returned on onCheckoutCompleted() configured in the previous step.
When the user interacts with the checkout and clicks to pay, the Universal Checkout:
- Shows the payment method screen if applicable
- Tokenizes the payment method for payment
- Creates the payment
- Handles any other interactions (e.g. 3DS)
Handle successful payments
Listen to callback
Listen to onCheckoutCompleted callbackThis function will notify you of a successful paymentprivate val listener = object : PrimerCheckoutListener {
override fun onCheckoutCompleted(checkoutData: PrimerCheckoutData) {
// Show an order confirmation screen...
// checkoutData contains the payment that has been created
}
}
Handle webhooks in your dashboard
To receive updates about the status of your payments you’ll need to listen to webhooks. This is particularly useful for updating an order or any other data stored server-side.Head to the Developers section of the Primer Dashboard to setup and test a webhook for PAYMENT.STATUS event.If you are not yet ready to receive webhooks, you can use https://webhook.site to test your implementation.Handle failed payments
Any errors, cancelled payment interactions or failed payments will trigger the onFailed callback private val listener = object : PrimerCheckoutListener {
override fun onFailed(
error: PrimerError,
checkoutData: PrimerCheckoutData?,
errorHandler: PrimerErrorDecisionHandler?
) {
// Notifies you that the checkout flow has failed and a payment could not be created
// This callback can also be used to display an error state within your own UI.
// Show a default error message
errorHandler?.showErrorMessage(null)
// Show a custom error message
errorHandler?.showErrorMessage("This is my custom error message")
}
}
This SDK requires React Native v0.63 or higher. Backend API V2.4 enhances reliability and minimizes synchronization issues with external processors by significantly extending payment timeouts. This longer timeout accommodates external processor limits and internal operations, ensuring more reliable transaction handling. These improvements are fully supported starting with Primer React Native SDK 2.35.0, so upgrading your app to use the newest SDK version is highly recommended when creating client sessions with API V2.4.
Install the SDK
Add the SDK package# With yarn
yarn add @primer-io/react-native
# With npm
npm i @primer-io/react-native --save
Requirement for iOSOnce you are done, navigate to the /ios folder and run pod install.For more details about SDK versions, please see our changelog. Initialize Universal Checkout
Import the Primer SDK, construct your settings and call the SDK’s configure function.import {
Primer,
PrimerSettings,
PrimerCheckoutData,
} from "@primer-io/react-native";
const CheckoutScreen = (props: any) => {
const onCheckoutComplete = (checkoutData: PrimerCheckoutData) => {
// Perform an action based on the payment creation response
// ex. show success screen, redirect to order confirmation view, etc.
};
const onUniversalCheckoutButtonTapped = async () => {
try {
const settings: PrimerSettings = {
onCheckoutComplete,
};
await Primer.configure(settings);
} catch (err) {
// Handle error
}
};
};
Show Universal Checkout
First, make an API call to your backend to fetch a client token. Here is a simple example of how it can be done from your component. Once successful store your client token for future use.const CheckoutScreen = (props: any) => {
// ...
const onUniversalCheckoutButtonTapped = async () => {
try {
// ...
// Ask your backend to create a client session
const clientToken = await createClientSession();
} catch (err) {
// Handle error
}
};
};
See Manage Client Sessions for more.Now, call the showUniversalCheckout(clientToken) function, with the client token to present Universal Checkout.const CheckoutScreen = async (props: any) => {
// ...
const onUniversalCheckoutButtonTapped = async () => {
// ...
try {
// Ask your backend to create a client session
const clientToken = await createClientSession();
// Present Universal Checkout
await Primer.showUniversalCheckout(clientToken);
} catch (err) {
// Handle error
}
};
};
You should now be able to see Universal Checkout! The user can now interact with Universal Checkout, and the SDK will create the payment. The payment’s data will be returned on primerDidCompleteCheckoutWithData(_:) configured in the previous step.
When the user interacts with the checkout and clicks to pay, the Universal Checkout:
- Shows the payment method screen if applicable
- Tokenizes the payment method for payment
- Creates the payment
- Handles any other interactions (e.g. 3DS)
Handle successful payments
Listen to callback
This function will notify you of a successful paymentconst onCheckoutComplete = (checkoutData: PrimerCheckoutData) => {
// Show a success screen
};
Handle webhooks in your dashboard
To receive updates about the status of your payments you’ll need to listen to webhooks. This is particularly useful for updating an order or any other data stored server-side.Head to the Developers section of the Primer Dashboard to setup and test a webhook for PAYMENT.STATUS event.If you are not yet ready to receive webhooks, you can use https://webhook.site to test your implementation.Handle failed payments
Any errors, cancelled payment interactions or failed payments will trigger the onError callbackconst onError = (
error: PrimerError,
checkoutData: PrimerCheckoutData | null,
handler: PrimerErrorHandler | undefined,
) => {
// Notifies you that the checkout flow has failed and a payment could not be created
// This callback can also be used to display an error state within your own UI.
// ⚠️ `handler` is undefined if the SDK does not expect anything from you
if (!handler) {
return;
}
// ⚠️ If `handler` exists, you MUST call one of the functions of the handler
// Show a default error message
handler.showErrorMessage();
// Show a custom error message
handler.showErrorMessage('This is my custom error message');