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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line