我正在将 Paypal 按钮添加到我的网站。但是,我想做一些用户需要在按钮起作用之前填写某些信息(地址框)的事情。如果用户填写了信息,当点击Paypal按钮时,提示用户登录Paypal(正常流程)。否则,用户会收到alert
:alert('Please choose a delivery address');
我一直在尝试执行以下操作:
$('.paypal-button').on( "click", function() {
alert('Please choose a delivery address');
});
甚至尝试禁用该按钮(尽管这不是最佳选择),但发现它不会根据其他答案起作用:
$('.paypal-button').prop("disabled",true);
这是 Paypal 沙盒代码(您需要一个真正的沙盒凭证):
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>
<body>
<div id="paypal-button-container"></div>
<script>
paypal.Button.render({
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'ABCDEcFpQtjWTOUtWKbyN_bDt4OgqatestlewfBP4-33iV8e1GWU6liB2XYZ',
production: '<insert production client id>'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '0.01', currency: 'USD' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then(function() {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
</script>
</body>