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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?