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

时间:2018-11-20 12:07:29

标签: javascript php paypal

我正在尝试将Paypal与我的网站集成,并面临与此相关的问题。问题是我似乎找不到发送PHP变量值的方法,也就是向Paypal代码发送TOTAL AMOUNT

假设我有一个变量

$finalAmount = 220;

此值是动态的,将随每个用户而变化。我需要在贝宝(Paypal)代码中将其传递到下面

<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>

我想将$ finalAmount的值传递给位于以下位置的PayPal脚本

amount: {
   total: 'HERE'
}

我该如何实现?

2 个答案:

答案 0 :(得分:0)

您可以使用...
amount: { total : '<?php echo $finalAmount ?>' }

答案 1 :(得分:0)

创建一个具有唯一ID的新隐藏输入字段,并在js中获取该字段的值。例如

在您的PHP页面中:

<input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">

在您的js代码中:

var finalAmount = $('#finalAmount').val();

希望您会得到所需的数量:)