Show payment UI
Stay organized with collections
Save and categorize content based on your preferences.
After creating the PaymentRequest
object, the payment can be initiated by
calling the show()
method.
The following sample code shows how to implement a function used when a user
taps the Buy button in the UI. When a user clicks the Buy button, the
PaymentRequest
object is created and checks for the user’s readiness to make
payment.
Based on the parameters defined in create payment request object
and check user readiness, the data in the following code snippet is displayed.
In the code snippet, the canMakePaymentPromise()
method is used to check for
user readiness and displays the result. If the result shows that the Google Pay is a
supported payment method, a payment request UI is shown to the user.
/** Launches payment request flow when user taps on buy button. */
function onBuyClicked() {
if (!window.PaymentRequest) {
console.log('Web payments are not supported in this browser.');
return;
}
// Create supported payment method.
const supportedInstruments = [
{
supportedMethods: ['https://tez.google.com/pay'],
data: {
pa: 'merchant-vpa@xxx',
pn: 'Merchant Name',
tr: '1234ABCD', // Your custom transaction reference ID
url: 'https://url/of/the/order/in/your/website',
mc: '1234', //Your merchant category code
tn: 'Purchase in Merchant',
},
}
];
// Create order detail data.
const details = {
total: {
label: 'Total',
amount: {
currency: 'INR',
value: '10.01', // sample amount
},
},
displayItems: [{
label: 'Original Amount',
amount: {
currency: 'INR',
value: '10.01',
},
}],
};
// Create payment request object.
let request = null;
try {
request = new PaymentRequest(supportedInstruments, details);
} catch (e) {
console.log('Payment Request Error: ' + e.message);
return;
}
if (!request) {
console.log('Web payments are not supported in this browser.');
return;
}
var canMakePaymentPromise = checkCanMakePayment(request);
canMakePaymentPromise
.then((result) => {
showPaymentUI(request, result);
})
.catch((err) => {
console.log('Error calling checkCanMakePayment: ' + err);
});
}
Display Google Pay Play Store page
The following code shows how to display a payment request UI and handle the case
where the user cannot make payment with Google Pay.
/**
* Show the payment request UI.
*
* @private
* @param {PaymentRequest} request The payment request object.
* @param {Promise} canMakePayment The promise for whether can make payment.
*/
function showPaymentUI(request, canMakePayment) {
if (!canMakePayment) {
handleNotReadyToPay();
return;
}
// Set payment timeout.
let paymentTimeout = window.setTimeout(function() {
window.clearTimeout(paymentTimeout);
request.abort()
.then(function() {
console.log('Payment timed out after 20 minutes.');
})
.catch(function() {
console.log('Unable to abort, user is in the process of paying.');
});
}, 20 * 60 * 1000); /* 20 minutes */
request.show()
.then(function(instrument) {
window.clearTimeout(paymentTimeout);
processResponse(instrument); // Handle response from browser.
})
.catch(function(err) {
console.log(err);
});
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-16 UTC.
[null,null,["Last updated 2024-10-16 UTC."],[[["\u003cp\u003eWeb payments are initiated using the \u003ccode\u003ePaymentRequest\u003c/code\u003e object's \u003ccode\u003eshow()\u003c/code\u003e method, triggered by a user action like clicking a "Buy" button.\u003c/p\u003e\n"],["\u003cp\u003ePrior to initiating payment, the code checks for user readiness and support for Google Pay as a payment method using \u003ccode\u003ecanMakePaymentPromise()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIf Google Pay is supported and the user is ready, a payment request UI is displayed for the user to proceed with the transaction.\u003c/p\u003e\n"],["\u003cp\u003eIf the user cannot make payment using Google Pay, alternative options or guidance are provided through \u003ccode\u003ehandleNotReadyToPay()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eA 20-minute timeout is implemented for the payment process to prevent indefinite waiting and potential issues.\u003c/p\u003e\n"]]],["The payment process starts when a user clicks the \"Buy\" button, triggering the creation of a `PaymentRequest` object. This object checks user readiness using `canMakePaymentPromise()`. If Google Pay is supported, a payment request UI is displayed; otherwise, it handles the case where the user is not ready to pay. After the `show()` method is called, a 20-minute timeout is set. If completed, it goes to processResponse() function, else the request is aborted.\n"],null,["# Show payment UI\n\nAfter creating the `PaymentRequest` object, the payment can be initiated by\ncalling the `show()` method.\n\nThe following sample code shows how to implement a function used when a user\ntaps the Buy button in the UI. When a user clicks the Buy button, the\n`PaymentRequest` object is created and checks for the user's readiness to make\npayment.\n\nBased on the parameters defined in [create payment request object](/pay/india/api/web/payment-request-object)\nand [check user readiness](/pay/india/api/web/user-readiness), the data in the following code snippet is displayed.\n\nIn the code snippet, the `canMakePaymentPromise()` method is used to check for\nuser readiness and displays the result. If the result shows that the Google Pay is a\nsupported payment method, a payment request UI is shown to the user. \n\n /** Launches payment request flow when user taps on buy button. */\n function onBuyClicked() {\n if (!window.PaymentRequest) {\n console.log('Web payments are not supported in this browser.');\n return;\n }\n\n // Create supported payment method.\n const supportedInstruments = [\n {\n supportedMethods: ['https://tez.google.com/pay'],\n data: {\n pa: 'merchant-vpa@xxx',\n pn: 'Merchant Name',\n tr: '1234ABCD', // Your custom transaction reference ID\n url: 'https://url/of/the/order/in/your/website',\n mc: '1234', //Your merchant category code\n tn: 'Purchase in Merchant',\n },\n }\n ];\n\n // Create order detail data.\n const details = {\n total: {\n label: 'Total',\n amount: {\n currency: 'INR',\n value: '10.01', // sample amount\n },\n },\n displayItems: [{\n label: 'Original Amount',\n amount: {\n currency: 'INR',\n value: '10.01',\n },\n }],\n };\n\n // Create payment request object.\n let request = null;\n try {\n request = new PaymentRequest(supportedInstruments, details);\n } catch (e) {\n console.log('Payment Request Error: ' + e.message);\n return;\n }\n if (!request) {\n console.log('Web payments are not supported in this browser.');\n return;\n }\n\n var canMakePaymentPromise = checkCanMakePayment(request);\n canMakePaymentPromise\n .then((result) =\u003e {\n showPaymentUI(request, result);\n })\n .catch((err) =\u003e {\n console.log('Error calling checkCanMakePayment: ' + err);\n });\n }\n\nDisplay Google Pay Play Store page\n----------------------------------\n\nThe following code shows how to display a payment request UI and handle the case\nwhere the user cannot make payment with Google Pay. \n\n\n /**\n * Show the payment request UI.\n *\n * @private\n * @param {PaymentRequest} request The payment request object.\n * @param {Promise} canMakePayment The promise for whether can make payment.\n */\n function showPaymentUI(request, canMakePayment) {\n if (!canMakePayment) {\n handleNotReadyToPay();\n return;\n }\n\n // Set payment timeout.\n let paymentTimeout = window.setTimeout(function() {\n window.clearTimeout(paymentTimeout);\n request.abort()\n .then(function() {\n console.log('Payment timed out after 20 minutes.');\n })\n .catch(function() {\n console.log('Unable to abort, user is in the process of paying.');\n });\n }, 20 * 60 * 1000); /* 20 minutes */\n\n request.show()\n .then(function(instrument) {\n\n window.clearTimeout(paymentTimeout);\n processResponse(instrument); // Handle response from browser.\n })\n .catch(function(err) {\n console.log(err);\n });\n }"]]