Set up Alipay payment methods with Zuora JavaScript SDK
This article describes how to set up Alipay payment methods by integrating with a JavaScript SDK provided by Zuora. With this integration, Zuora renders the Alipay button in an iFrame. After the end user clicks the button, a QR code is displayed and the end user follows the instructions on the page to complete the authorization. A token is returned to Zuora. Zuora stores the token and uses it to create an Apliay payment method in Zuora.
The created Alipay payment method can be retrieved through the Zuora UI and API operations. You can also retrieve the following token information in the Electronic Payment Methods section of the customer account page. Token ID will be used in subsequent recurring payments.
|
Zuora UI field |
Zuora API field |
Value |
|---|---|---|
|
Token ID |
paymentMethodId |
The access token obtained from applyToken. |
Alipay Auto Debit does not support one-time payments. To handle one-time payments, you must implement your custom solution for processing one-time payments after the end user completes the authorization and a payment token is returned by Alipay.
If the Alipay payment method is deleted or closed in Zuora, Zuora sends the token to Alipay and Alipay will revoke the token.
Complete the following tasks to implement the integration:
Import the Zuora JavaScript client library to your page.
For sandbox environments, use
<script src="https://js.zuora.com/payment/sandbox/1.4.0/zuora.js"></script>
.
For production environments, use
<script src="https://js.zuora.com/payment/1.4.0/zuora.js"></script>
.
Create a container for the Alipay button on your page and place it where you want the button to be rendered. Replace
alipay-button-container
with the ID you want to use.
<div id="alipay-button-container">
<!-- The Alipay button will be inserted here. -->
</div>
Here is an example of the code implementation:
<script>
const publishableKey = "pk_rO0AB…";
const renderForm = () => {
const zuora = Zuora(publishableKey);
zuora.createAlipayButton({
buttonWidth: "300px",
createPaymentSession: () => {
return new Promise((resolve, reject) => {
fetch("/sdk-test/payment-session.json?seed=" + Math.random(), {
method: "GET",
}).then((response) => {
if (response.ok) {
return response.json();
} else {
return reject("Payment session request is rejected.");
}
}).then((data) => {
return resolve(data.token);
}).catch((error) => {
return reject(error);
});
});
},
onComplete: (result) => {
console.log("==========");
console.log("Payment Result");
console.log("==========");
console.log(`transaction result: ${JSON.stringify(result)}`);
alert(`transaction result: ${JSON.stringify(result)}`);
}
}).then(function(alipayButton) {
alipayButton.mount("#payment-form-container")
}).catch(function(error) {
console.error(error);
});
}
function resultMessage(message) {
const container = document.querySelector("#result-message");
container.innerHTML = message;
}
</script>
In your server, make an API request to the Create a payment session endpoint.
Because only the payment method creation flow is supported, specify the following required fields in the API request:
-
processPayment:false -
storePaymentMethod:true -
amount -
currency
For details about the API request parameters, see Create a payment session in the API Reference.
In client code, make a call to your backend API to create a payment session.