我正在跟随客户端Client Side Rest API创建捐赠按钮。收费金额在代码中是静态的。在提示用户单击按钮输入金额后,我能够传入一个变量,然而,这非常笨重,如果没有输入任何值,则需要一个默认值来防止PayPal窗口一打开就关闭
// 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: promptMe2(), currency: 'USD' }
}
]
}
});
},
你可以看到我有var donationAmount
。这默认设置为一个值。但是,用户可以通过以下方式更改它:
<script>
var donationAmount = 16.11;
function promptMe(){
donationAmount = parseFloat(prompt("How much would you like to donate?"));
while (Number.isNaN(donationAmount)){
alert("Invalid number, try again!");
donationAmount = parseFloat(prompt("Amount to donate?"));
}
donationAmount = donationAmount.toFixed(2);
}
我尝试拨打promptMe()
代替donationAmount
变量(当然还添加了donationAmount的返回语句),并发现该页面只会将我的错误消息垃圾邮件无效。当我硬编码它的价值工作得很好。当我把错误检出时,它甚至没有提示我,它只是立即关闭了paypal窗口。我能够触发警报(“你好”);但这很无用,我需要能够接受用户输入,然后将其传递给Paypal。