如何在 PayPal JavaScript 中传递 PHP 变量作为支付金额
How to pass PHP variable in PayPal JavaScript as payment amount
所以我有这个应用程序,我正在尝试实施 [PayPal Express][1]
[1]: https://developer.paypal.com/demo/checkout/#/pattern/client 但我的问题是我无法选择从 php 到 javascript 'value'
的价格变量
我试过了value: '<?php echo $order_amount;?>'
无济于事。
这是贝宝 JS:
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '88.44'
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
如何在 js 中将此 $order_amount
称为 value
以便我可以处理付款?
感谢@David 的指导,我能够解决我的问题。
我未能在 PayPal 的 js 上定义值 id。
我在顶部创建了一个隐藏的输入字段,其 ID 如下所示:
<input type="hidden" id="t_amount" value="<?php echo $orders['order_amount']; ?>">
这是我添加的:
var amountt= document.getElementById("t_amount").value;
console.log(amountt);
然后我将 amountt
作为值,它就像魅力一样工作。
这是我的全部代码:
<script>
var amountt= document.getElementById("t_amount").value;
console.log(amountt);
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: amountt
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
所以我有这个应用程序,我正在尝试实施 [PayPal Express][1]
[1]: https://developer.paypal.com/demo/checkout/#/pattern/client 但我的问题是我无法选择从 php 到 javascript 'value'
的价格变量我试过了value: '<?php echo $order_amount;?>'
无济于事。
这是贝宝 JS:
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '88.44'
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
如何在 js 中将此 $order_amount
称为 value
以便我可以处理付款?
感谢@David 的指导,我能够解决我的问题。
我未能在 PayPal 的 js 上定义值 id。
我在顶部创建了一个隐藏的输入字段,其 ID 如下所示:
<input type="hidden" id="t_amount" value="<?php echo $orders['order_amount']; ?>">
这是我添加的:
var amountt= document.getElementById("t_amount").value;
console.log(amountt);
然后我将 amountt
作为值,它就像魅力一样工作。
这是我的全部代码:
<script>
var amountt= document.getElementById("t_amount").value;
console.log(amountt);
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: amountt
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>