这是我用来使用 Stripe 开发 Apple Payment Request 按钮的 JavaScript 代码,我在上面放置了相关代码,我还添加了相关的 stripe v3 库,并在 html 中添加了一个带有 payment-request-button id 的 div
var stripe = Stripe('pk_test_ZjbcGwDeoJsycvIs7KGEFkVR00qOzHxlrX');
elements = stripe.elements();
// Config payment request
paymentRequest = stripe.paymentRequest({
country: 'US',
currency: 'usd',
total: {
label: 'Demo total',
amount: 100,
},
});
paymentRequest.on('source', function(event) {
console.log('Got source: ', event.source.id);
event.complete('success');
ChromeSamples.log(JSON.stringify(event.source, 2));
// Send the source to your server to charge it!
});
prButton = elements.create('paymentRequestButton', {
paymentRequest,
});
// Check the availability of the Payment Request API first.
paymentRequest.canMakePayment().then((result) => {
//console.log(prButton);
if (result) {
prButton.mount('#payment-request-button');
} else {
document.getElementById('payment-request-button').style.display = 'none';
ChromeSamples.setStatus("Not supported, please check: https://stripe.com/docs/elements/payment-request-button#testing");
}
});
// Helpers
var ChromeSamples = {
log: function() {
var line = Array.prototype.slice.call(arguments).map(function(argument) {
return typeof argument === 'string' ? argument : JSON.stringify(argument);
}).join(' ');
document.querySelector('#log').textContent += line + '\n';
},
clearLog: function() {
document.querySelector('#log').textContent = '';
},
setStatus: function(status) {
document.querySelector('#status').textContent = status;
},
setContent: function(newContent) {
var content = document.querySelector('#content');
while (content.hasChildNodes()) {
content.removeChild(content.lastChild);
}
content.appendChild(newContent);
}
};