Available in Chrome 68+ | View on GitHub | Browse Samples
PaymentRequest lets you accept payment from different payment methods.
This sample accepts the URL-based payment method.
/**
* Builds PaymentRequest for the BobBucks payment method, but does not show any
* UI yet.
*
* @return {PaymentRequest} The PaymentRequest object.
*/
function initPaymentRequest() {
const supportedInstruments = [{
supportedMethods: 'https://bobbucks.dev/pay',
}];
const details = {
total: {label: 'Donation', amount: {currency: 'USD', value: '55.00'}},
displayItems: [
{
label: 'Original donation amount',
amount: {currency: 'USD', value: '65.00'},
},
{
label: 'Friends and family discount',
amount: {currency: 'USD', value: '-10.00'},
},
],
};
return new PaymentRequest(supportedInstruments, details);
}
/**
* Invokes PaymentRequest for the BobBucks payment method.
*/
async function onBuyClicked() {
try {
const request = initPaymentRequest();
const instrumentResponse = await request.show();
await sendPaymentToServer(instrumentResponse);
} catch (err) {
ChromeSamples.setStatus(err);
}
}
/**
* Simulates processing the payment data on the server.
*
* @param {PaymentResponse} instrumentResponse The payment information to
* process.
*/
async function sendPaymentToServer(instrumentResponse) {
// There's no server-side component of these samples. No transactions are
// processed and no money exchanged hands. Instantaneous transactions are not
// realistic. Add a 2 second delay to make it seem more real.
await new Promise(resolve => window.setTimeout(resolve, 2000));
await instrumentResponse.complete('success');
document.getElementById('result').textContent =
JSON.stringify(instrumentResponse, undefined, 2);
}
const payButton = document.getElementById('buyButton');
payButton.setAttribute('style', 'display: none;');
if (window.PaymentRequest) {
initPaymentRequest().canMakePayment().then(result => {
if (result) {
payButton.setAttribute('style', 'display: inline;');
payButton.addEventListener('click', onBuyClicked);
} else {
ChromeSamples.setStatus('Cannot make payment');
}
}).catch(err => {
ChromeSamples.setStatus(err);
});
} else {
ChromeSamples.setStatus('This browser does not support web payments');
}