Showing Drop-in Checkout is the simplest way to integrate with Primer. With just a few lines of code, you can display a fully in-context checkout UI with all your payment methods.Availing Drop-In Checkout is as easy as implementing one line of code.
import { Primer } from "@primer-io/checkout-web";const checkout = await Primer.showUniversalCheckout(clientToken, { container: "#container", onCheckoutComplete(...args) { console.log("onCheckoutComplete", ...args); },});
Note that there are more options which can be passed to Drop-In Checkout.
Please refer to the section below for more information.
When calling showUniversalCheckout you can provide Drop-In Checkout with some configuration options. These
options range from callbacks notifying you of the current payment’s status, to styling options for certain UI
elements.
Optional domain override. Required for Apple Pay when the checkout is rendered in a cross-origin iframe.For example, if your main page is example.com and the iframe checkout is hosted at iframecheckout.com,
you must set this to example.com to ensure Apple Pay functions properly.Additional Requirements:
The iframe element must include allow="payment" attribute
Called when a payment will be created.
Use as an opportunity to update UI accordingly - for example, to display a “loading” component.
The handler must be used to then abort or continue with payment creation.
Primer will continue with payment creation if onBeforePaymentCreate is not implemented.
Show an error message.If handler is undefined, the SDK does not expect anything from you.If handler exists, you MUST call one of the functions of the handler.
TS
import { Primer } from "@primer-io/checkout-web";const checkout = await Primer.showUniversalCheckout(clientToken, { container: "#container", onCheckoutComplete(...args) { console.log("onCheckoutComplete", ...args); }, onCheckoutFail(error, data, handler) { if (!handler) { return; } // Show a default error message return handler.showErrorMessage(); // Or show a custom error message // return handler.showErrorMessage('This is my custom message'); },});
Called when the client session has been updated by the checkout. Returns the
updated client session which can be used to inform your UI. For example
updating tax, shipping or discount amounts displayed to your customers.
The placeholder text to display in the cardholder name input field. It
can be either a string or a function that returns a string. The
function receives an options object as first argument.
The placeholder text to display in the cardholder number input field.
It can be either a string or a function that returns a string. The
function receives an options object as first argument.
The placeholder text to display in the expiry date input field. It can
be either a string or a function that returns a string. The function
receives an options object as first argument.
The placeholder text to display in the CVV input field. It can be
either a string or a function that returns a string. The function
receives an options object as first argument.
If set to true, this specifies that Google Pay can only be used for payments if the user’s Google Pay wallet already contains allowed payment methods. When enabled, the SDK will check if IsReadyToPayResponse.paymentMethodPresent is also true before rendering the Google Pay button. Default value is false.
Whether the vaulted payment methods are visible or not.If this is not necessary for your use case, we recommend setting this to false to skip the retrieval of the vault and speed up the loading of the checkout.Default value is true.
Drop-In Checkout allows you to use your own submit button for submitting forms. By default, the built-in submit button will be favored.Note that when disabling the built-in submit button and using your own custom submit button, it is required to implement the submit function in order to notify Drop-In Checkout of form submissions.When using your own custom submit button, it’s important to use the following callbacks to ensure that your submit button is in the correct state and in sync with the checkout as your customers interact with it.
TS
const options = { /* Other options ... */ submitButton: { useBuiltInButton: false, // Default to true // Callback for receiving the submit button's visible state in the current scene onVisible(isVisible, context: { currentSceneId }) { // Show or hide your custom submit button }, // Callback for receiving the submit button's disabled state in the current scene onDisable(isDisabled, context: { currentSceneId }) { // Disable or enable your custom submit button }, // Callback for receiving the submit button's loading state in the current scene onLoading(isLoading, context: { currentSceneId }) { // Show your submit button in a loading state }, // Callback for receiving the submit button's content in the current scene onContentChange(content, context: { currentSceneId }) { // Set your submit button's content with either the content provided or your own custom content }, },};
When the checkout is successfully completed, Drop-In Checkout displays a success scene with a default success message “Your payment was successful!”.Set the option successScreen to modify the default behavior of the success scene.Remove the success screen:
The paymentHandling option defines how the SDK should handle payment creation and resume.The default is AUTO, set paymentHandling to MANUAL to turn off automatic payment handling. This disables the callbacks onBeforePayment and onCheckoutComplete.Two callbacks must be implemented:
onTokenizeSuccess() to create payments with the paymentMethodToken.
onResumeSuccess() to resume payments with resumeToken.
Additional information about the payment instrument.
Depending on the payment method type, only some of the following properties are present on the object.
Continue the payment flow using the given clientToken.
Called after a customer submitted their payment data and the payment details have been tokenized.
You’ll receive a paymentMethodToken in onTokenizeSuccess().
If the payment is successful, call handler.handleSuccess() in order to display a success screen
If the payment is unsuccessful, call handler.handleFailure(errorMessage) to display an error or failure message
Payments API may return a new clientToken for additional steps (in the requiredActions on the response). In this case, call handler.continueWithNewClientToken(clientToken) to continue with the payment flow
Called after a customer submitted their payment data and the payment details could not be tokenized.
Use the PrimerClientError data to display an error message.
Called when the resume token must be sent to your server to resume the payment flow, typically after the customer has completed some authorization step outside of the Primer SDK.
If you set paymentHandling to MANUAL, handling onResumeSuccess() is required to fully support 3DS and the majority of payment methods.
Called just before the payment method tokenization starts. Implement this to perform any custom action, for example, displaying a custom loading indicator.
Before enabling the flag to true it’s recommended to reach out to Primer support for additional verification since there are edge cases where it’s not advised.
Indicates whether client session caching is enabled.When set to true, responses from the server will be cached on the client side, allowing for faster subsequent
access to the same data within the cache duration. When set to false, every request to the server will be
processed without utilizing any client-side cache, ensuring that the client always receives the most up-to-date data.
A promise that is resolved when Drop-In Checkout has been properly rendered on the page, or rejected if any error occurred during initialisation.The promise is resolved with an instance of PrimerCheckout