If you're planning to use Tazapay exclusively for collecting card payments then you can further optimise the payment experience by using the Card Checkout.
Follow below steps to provide a delightful payment experience to your customers:
Step 1: Initialise Tazapay SDK
While initialising the SDK the checkout method can accept a payment object.
- The payment object requires a Public key. This can be found on Tazapay dashboard, under API Keys section.
- The currency resembles collection currency, that is the currency in which user needs to pay.
- The layout field can be either inline, two-rows or three-rows.
window.tazapay.checkout({
callbacks: {
onPaymentSuccess,
onPaymentFail,
onValidation: ({errors}) => {
errors = errors || [];
hasErrors = Boolean(errors.length);
// errors -> array of objects will consist of all the validations.
// before calling confirmCardPayment, You can disable or enable the payment button
// or display the custom error messages by checking the hasErrors variable and errors data.
}
...
},
payment: {
key: "<YOUR_PUBLIC_KEY>",
method: "card",
currency: "USD",
country: "US",
origins: 'https://your.site,https://your.host.site',
layout: 'inline',
}
}
Step 2: Listen for Click event on the Pay button
You can use the native inbuilt pay button or your own custom pay button
Step 2A: Using native inbuilt button
- Whenever user clicks on the inbuilt pay button a PaymentClicked event is triggered
- Add a callback for onPaymentClicked event
- Use this event to generate the client token by calling your backend services.
- Once Client Token is available, you can proceed with the payment (Step 3).
window.tazapay.checkout({
callbacks: {
onPaymentClicked: () => {
// Call your backend service to get the client_token
// Then use the client token to continue the payment > Step 3
},
...
},
payment: {
...
}
}
Step 2B: Using custom pay button
- Use the config option to hide the inbuilt button.
- When user clicks on your custom button, generate the client token by calling your backend services.
- Once Client Token is available, you can proceed with the payment (Step 3).
- You can update the state of your custom button by listening to PayButtonState event.
window.tazapay.checkout({
callbacks: {
onPayButtonState: ({ payload:state })=>{
console.log('State changed',state.disabled,state.progress);
},
...
},
payment: {
...
},
config: {
customPayButton: true
}
}
Step 3: Trigger the payment flow
Once the client_token is available, use the confirmCardPayment method from SDK to trigger the payment flow.
- Pass the client token to SDK via confirmCardPayment method
- Additionally pass the billing details object
window.tazapay.confirmCardPayment({
clientToken: '<CLIENT_TOKEN>',
billingDetails: {
name: "",
address: {
line1: "",
line2: "",
city: "",
state: "",
country: "US",
postal_code: "",
},
phone: {
country_code: "",
number: "<10_DIGITS>",
}
}
})
Step 4: Handle payment errors
There are various scenarios that can lead to an error while processing the payment. You can monitor such errors using the PaymentError event.
- The PaymentError event can be used to show custom styled error messages.
- It can also be used to update the application state.
- By default, the error messages will be displayed in the payment section. This can be disabled by setting hideErrors config option to true.
window.tazapay.checkout({
callbacks: {
onPaymentError: ({ payload:error })=>{
console.log('Error occured',error.code,error.title,error.message);
}
},
payment: {
...
},
config: {
hideErrors: true
}
Step 5: Use Style configuration
You can use style configurations to customise most of the user interface elements. Please check the style guide.