donglun2024 2018-11-20 12:07
浏览 91
已采纳

如何将PHP变量值传递给Paypal的客户端代码

I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code

Suppose I have a variable

$finalAmount = 220;

This value is dynamic and will change with each user. I need to pass it below inside the Paypal code

<script>

        paypal.Button.render({
          // Configure environment
          env: 'sandbox', // sandbox | production

           // PayPal Client IDs - replace with your own
           // Create a PayPal app: https://developer.paypal.com/developer/applications/create
          client: {
            sandbox: 'CODE_HERE',
        //    production: 'demo_production_client_id'
          },
          // Customize button (optional)
          locale: 'en_US',
          style: {
             layout: 'vertical',  // horizontal | vertical
             size:   'medium',    // medium | large | responsive
             shape:  'rect',      // pill | rect
             color:  'gold'
          },
          // Set up a payment
          payment: function (data, actions) {
            return actions.payment.create({
              transactions: [{
                amount: {
                  total: '1',
                  currency: 'USD'
                },
                invoice_number : 123111241, // invoice must be unique for each transaction.
                item_list: {
                      items: [
                      {
                        name: 'hat',
                        description: 'Brown hat.',
                        quantity: '2',
                        price: '3',             
                        sku: '1',
                        currency: 'INR'
                      },
                      {
                        name: 'handbag',
                        description: 'Black handbag.',
                        quantity: '1',
                        price: '7',             
                        sku: 'product34',
                        currency: 'INR'
                      }
                    ],
                    shipping_phone_number : '8888888888',
                    shipping_address: {           
                      recipient_name: 'Logan loga',
                      line1: '4th Floor',
                      line2: 'Unit #34',
                      city: 'Chennai',
                      country_code: 'IN',
                      postal_code: '600113',
                      phone: '9999999999',
                      state: 'Tamil Nadu'
                }
                }       
              }],
              payer: {
                payment_method: "paypal",
                payer_info : {
                email: 'test@gmail.com',
                first_name :'Logan',
                last_name  :'loga',
                billing_address : {                       
                      line1: '4th Floor',
                      line2: 'Unit #34',
                      city: 'Chennai',
                      country_code: 'IN',
                      postal_code: '600114',
                      phone: '7777777777',
                      state: 'Tamil Nadu'
                }       
                }
             },
            });
          },
          // Execute the payment
          onAuthorize: function (data, actions) {
            return actions.payment.execute()
              .then(function (res2) {
                // Show a confirmation message to the buyer
                console.log(res2);
                console.log(res2.transactions[0].related_resources[0].sale.state);
                //state codes - completed, partially_refunded, pending, refunded, denied
                if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
                alert("Payment received for the invoice number" + res2.transactions[0].invoice_number); 

                }   
              });
          },
          onCancel: function (data, actions) {
            // Show a cancel page or return to cart
            alert("Payment  cancelled");
          },
           onError: function (err) {
            // Show an error page here, when an error occurs
            alert("Payment  error");
          }
        }, '#paypal-button');
        </script>

I want to pass the value of $finalAmount to the PayPal script at

amount: {
   total: 'HERE'
}

How can I implement this?

  • 写回答

2条回答 默认 最新

  • dooo61733 2018-11-20 12:10
    关注

    You Can use...
    amount: { total : '<?php echo $finalAmount ?>' }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码