A client session is the starting point for integrating payments at Primer.
You can attach all the additional data associated with the order to the client session, and generate a client token, a temporary key used to initialize Universal Checkout.
The information you include in the client session is used in the Dashboard to conditionally route payments with Workflows, and activate payment methods and other features in Universal Checkout, so pass as much information as you can.
The X-Api-Version specifies the API version information. Earlier, this was supposed to be a date. For example, 2021-10-19.
This has changed post API version v2 which was represented by 2021-09-27 date.
Starting API version v2.1, the X-Api-Version needs to provide the API version as v2.1.
Depending upon the API version specified in the client-session request, your client-session will be processed accordingly with requisite features and options that are available for that version.
Make sure to keep track of orderId - you will later receive updates to the payment via Webhooks. The payment will contain the orderId specified in the client session.
The clientToken is a key concept within Primer. You may receive a client token from various places but as long as you pass it to the SDK, Universal Checkout knows where to start/resume the flow.
For more details about SDK versions, please see our changelog.
Step 2. Initialize the SDK
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.
1234567891011121314151617181920212223
publicclassCheckoutActivityextendsAppCompatActivity{privatefinalPrimerCheckoutListener listener =newPrimerCheckoutListener(){@OverridepublicvoidonCheckoutCompleted(@NonNullPrimerCheckoutData checkoutData){// Primer checkout completed with checkoutData// show an order confirmation screen, fulfil the order...}};@OverrideprotectedvoidonCreate(@NullableBundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);configureCheckout();}privatevoidconfigureCheckout(){// Initialize the SDK with the default settings.Primer.getInstance().configure(newPrimerSettings(), listener);}}
java
copy
🚀
Check the SDK API here to customize your SDK settings.
Step 3. Generate a client token
💡
Check our guide on how to set up the client session here.
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 activity:
Your view model code may look something like this:
12345678910
publicclassCheckoutViewModelextendsViewModel{privatefinalMutableLiveData<String> _clientToken =newMutableLiveData<>();publicLiveData<String> clientToken = _clientToken;publicvoidfetchClientToken(){// fetch your client token here (ask your backend to provide token) _clientToken.postValue("retrieved_token");}}
java
copy
Step 4. Show Universal Checkout
When the client token is retrieved, show Universal Checkout.
123456789101112
publicclassCheckoutActivityextendsAppCompatActivity{// other code goes hereprivatevoidsetupObservers(){ viewModel.clientToken.observe(this,this::showUniversalCheckout);}privatevoidshowUniversalCheckout(String clientToken){Primer.getInstance().showUniversalCheckout(this, clientToken);}}
java
copy
🔥
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(checkoutData) configured in Step 2
Use this event as a single source of truth to update your database and to fulfill the order.
12345678
app.post('/webhooks/payment-status',async(req, res)=>{const{ eventType, payment }= req.bodyconst{ id, orderId, status }= paymentif(status ==='AUTHORIZED'|| status ==='SETTLED'){// Fullfil the order for the Payment / Client Session labelled with `orderId`}})