这个问题在这里已有答案:
我正在将Paypal按钮添加到我的网站。但是,我想做一些事情,用户需要在按钮运行之前填写某些信息(地址框)。如果用户填写了信息,当点击Paypal按钮时,它会提示用户登录Paypal(正常流程)。否则,用户会收到alert
:alert('Please choose a delivery address');
我一直在努力做以下事情:
$('.paypal-button').on( "click", function() {
alert('Please choose a delivery address');
});
甚至试图禁用该按钮(虽然这不是最佳),但已发现它不会基于this other answer工作:
$('.paypal-button').prop("disabled",true);
这是Paypal Sandbox代码(您需要一个真正的沙箱凭证):
<!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>
new docs提供onInit
和onClick
选项,但是正如讨论的here你不能通过脚本触发弹出窗口,因为它会被阻止。因此,异步方法是错误的,因为它简要地显示了验证失败的弹出窗口。我用这种方式实现了同步验证:
onInit: function(data, actions) {
// Disable the buttons
actions.disable();
// Listen for changes
$("input[aria-required=true]").change(function() {
var valid=true;
$("input[aria-required=true]").each(function(){
if($(this).val()=="") valid=false;
});
if (valid==true) {
actions.enable();
} else {
actions.disable();
}
});
}
我试图通过监听器使用验证(wp cf7的内置),但似乎没有办法。
您可以使用PayPal按钮的validate:
和onClick:
属性。有一个example on the demo site这是如何工作的。