Callbacks instead of events
If you prefer not to use event emitters, you can supply callback functions directly in thecreateCheckout options for success, error, and status changes. This provides a quick way to handle outcomes without separately registering event listeners:
'success', 'error', and 'status-change' events on the CheckoutInstance.
Internally, the SDK will trigger these callbacks at the same time as the events. Choose the approach (callbacks vs. event listeners) that fits your application style; you can even mix them (for example, provide an onSuccess callback but also listen for 'status-change' events).
Manual session creation
For full control over the checkout flow, you can manually create a client session and then use it with Primer’s Headless Checkout. This is useful if you need to perform custom steps between tokenization and finalizing payment (for example, if you want to process the payment on your backend before confirming the UI).1. Create client session
Create a client session to get aclientToken (and orderId):
2. Use with Primer Headless Checkout
Use the obtainedclientToken with Primer’s Headless Checkout:
createClientSession, you initialize Primer’s Headless Checkout with the clientToken.
The onTokenizeSuccess callback gives you the opportunity to handle the payment token (paymentMethodTokenData.token) as needed.
Once your server confirms the payment or subscription creation, you call handler.handleSuccess() to complete the flow (or call handler.handleFailure() if an error occurred).
This advanced approach is useful for integrating additional custom business logic into the payment flow.