【问题标题】:How to pass PHP variable in PayPal JavaScript as payment amount如何在 PayPal JavaScript 中传递 PHP 变量作为付款金额
【发布时间】:2021-11-25 17:15:25
【问题描述】:

所以我有这个应用程序,我正在尝试实现 [PayPal Express][1]

[1]: https://developer.paypal.com/demo/checkout/#/pattern/client 但我的问题是我无法将价格变量从 php 选择为 javascript 'value'

我试过value: '<?php echo $order_amount;?>' 无济于事。

这是 PayPal 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 以便处理付款?

【问题讨论】:

  • 当您尝试时,具体是如何失败的? PHP有错误吗?来自 JavaScript 的错误?出乎意料的结果?
  • 没有错误。但它得到的数量是错误的,而不是变量提供的数量。例如,我尝试过的可变金额是 21 美元,但当您登录贝宝时,它显示为 600 美元
  • 您进行了哪些调试以确认它正在输出您期望的值?当您将值从 PHP 输出到 JavaScript 代码时,当您检查浏览器页面源中的输出时,向客户端发出了什么值? JavaScript 代码的确切结果行是什么?
  • 在调试时,我仍然得到订单值为 600,这是错误的。这是因为我有一个默认按钮,它将值计算为 21。我试图从我的系统中找出它在哪里得到这个数字,但没有运气。
  • “在调试时,我的订单值仍然为 600” - 如果此代码:value: '&lt;?php echo $order_amount;?&gt;' 导致此输出:value: '600',则运行时值为 @ 987654328@ 是600。如果该值不正确,那么您将需要找到该值的来源以及为什么它不是您期望的值。显示的代码与此无关。基本上听起来问题出在其他地方,而不是您认为的问题。

标签: php paypal codeigniter-3


【解决方案1】:

感谢@David 的指导,我能够解决我的问题。

我未能在 PayPal 的 js 上定义值 id。

在顶部我创建了一个隐藏的输入字段,其 id 如下所示:

&lt;input type="hidden" id="t_amount" value="&lt;?php echo $orders['order_amount']; ?&gt;"&gt;

这是我添加的:

 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2020-09-02
    • 2020-03-20
    • 2020-08-25
    • 2020-08-16
    • 2020-09-18
    • 2014-02-18
    相关资源
    最近更新 更多