【问题标题】:Passing price variable to PayPal with custom button使用自定义按钮将价格变量传递给 PayPal
【发布时间】:2011-07-12 10:50:20
【问题描述】:

我有一个表单和一个自定义 PayPal 按钮,但如何将值/价格变量传递给 PayPal?

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="ZEFZFYBY2SZB8">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

我有一个变量$total = "238.00";

【问题讨论】:

    标签: php paypal


    【解决方案1】:

    之前的代码对我不起作用。经过一番头疼后,我终于发现您必须进入 PayPal,并在创建按钮代码的第 2 步确保您单击未托管的按钮,然后复制未加密的按钮代码,这将为您提供类似这样的信息(我将我的安全的商业价值):

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="business" value="XXX">
        <input type="hidden" name="lc" value="CA">
        <input type="hidden" name="item_name" value="Tangled Roots">
        <input type="hidden" name="button_subtype" value="services">
        <input type="hidden" name="no_note" value="0">
        <input type="hidden" name="cn" value="Add special instructions to the seller">
        <input type="hidden" name="no_shipping" value="2">
        <input name="amount" value="16.99">
        <input type="hidden" name="currency_code" value="CAD">
        <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
        <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
        <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>
    

    此代码创建了一个按钮,用户可以在其中输入金额,默认情况下从 16.99 开始,但您可以轻松地将其替换为 PHP 变量。

    【讨论】:

    • 如果将 type="hidden" 添加到数量会更好:)
    • 如果您使用Express Checkout API,效果会更好。
    • 所以这不是来自贝宝 API?并且没有考虑任何安全问题,对吧?
    • 这并不能真正回答提出的问题,而且真的不应该有这么多的赞成票。
    • 这不应被视为 op 对问题的回答。 OP 有一个托管按钮,可避免在客户端篡改任何表单数据。如果您只添加这样的表单并且不执行任何服务器端验证,则用户可以根据需要编辑产品的实际值。即使您将字段隐藏,它仍然不够好,因为您可以使用检查元素编辑值并提交。我很乐意对这篇文章投反对票。
    【解决方案2】:

    我尝试了以上所有方法,但都失败了。我发现这是来自PayPal website 的答案。

    <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="business" value="me@mybusiness.com">
        <input type="hidden" name="currency_code" value="USD">
        <input type="hidden" name="item_name" value="Teddy Bear">
        <input type="hidden" name="amount" value="12.99">
        <input type="image" src="http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"         border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    </form>
    

    测试一下,你就会明白它是如何工作的......将企业更改为你想要付款的人的电子邮件地址等。

    【讨论】:

      【解决方案3】:

      为金额添加一个隐藏字段

      <input type="hidden" name="amount" value="<?php echo $total; ?>">
      

      【讨论】:

      • @x-yurl,您的意思是“PayPal 上的保存按钮”选项不能取消选中?如果是,解决方案是什么,因为现在我也需要一个选项来向 PayPal 添加不同的价格。谢谢!
      • @x-yuri 我尝试了托管和非托管按钮,但似乎都不适合我。我的问题:stackoverflow.com/questions/40210676/…
      • @HarkályGergő 抱歉,我才注意到您的问题。我已经有一段时间没有处理贝宝了,但我相信原因是因为所有托管按钮的数据(如价格)都应该存储在贝宝上。无法从表格中更改它们。如果您想要一个价格不同的按钮,您应该使用非托管按钮。也就是说,paypal 上没有存储任何内容,您可以在表单中指定所有参数。
      • 谢谢 x-yuri,是的,我已经意识到了。我用自己的表单而不是托管按钮解决了我的问题,你是对的!
      • 大家好,您认为这是一种通过价格的安全方式吗?我的意思是用户可以将 input type hidden 更改为 text 并更改 value
      【解决方案4】:

      这里是 2013 版本: 去创建一个按钮,当你到达第 2 步时,取消选中该框,继续第 3 步然后创建按钮。获得代码后,它将如下所示:

      <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
      <input type="hidden" name="cmd" value="_xclick">
      <input type="hidden" name="business" value="XXXXXXXX">
      <input type="hidden" name="lc" value="US">
      <input type="hidden" name="item_name" value="Payments">
      <input type="hidden" name="amount" value="100.00">
      <input type="hidden" name="currency_code" value="USD">
      <input type="hidden" name="button_subtype" value="services">
      <input type="hidden" name="no_note" value="0">
      <input type="hidden" name="cn" value="Add special instructions to the seller:">
      <input type="hidden" name="no_shipping" value="2">
      <input type="hidden" name="rm" value="1">
      <input type="hidden" name="return" value="http://YOURSITE.com/">
      <input type="hidden" name="cancel_return"     value="http://YOURSITE.com/payments.html">
      <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
      <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif"     border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
      <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
      </form>
      

      您的“企业”价值不会是 XXXXXXXX,因此请务必留下 Paypal 为您提供的价值。您还可以设置取消和返回 URL。

      对于更高级的 PHP 用户: 我实际上设置了一个 PHP 字符串,效果很好!例如,见下文:

       https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&businesss=XXXXXXXXX&lc=US&item_name=$mydescription&amount=$myprice&........
      

      等等.....你可以看到 $mydescription 是一个 PHP 变量,而 $myprice 是一个 PHP 变量。我所做的是设置一个 HTML 表单来收集数据并将该表单用作支付处理表单。一旦用户单击提交,我将它转到一个 PHP 页面,用作邮件程序、数据库插入、自动回复,最后是一个标头重定向。重定向的 URL 是带有变量的 Paypal URL!该线程实际上帮助我找到了正确的 Paypal 按钮代码,以便该字符串可以在价格变化时正常工作!仅供参考 - 如果您是 PHP 初学者,则字符串中不使用图像字段。只有 URL,然后是隐藏的名称和值。

      【讨论】:

      • 我认为现在这行不通。 Paypal 似乎可以防止任何客户端更改“立即购买”按钮中的预设付款金额.. 至少我无法让它工作。
      • @Paul Me 也没有。我试图在结帐过程结束时获得“通过贝宝付款”按钮,但我无法手动设置价格。这很烦人。
      【解决方案5】:

      我找到了解决办法:

      <input type="hidden" name="cmd" value="_s-xclick">
      

      需要改成

      <input type="hidden" name="cmd" value="_xclick">
      

      第一个加密表单发送的数据 - 这导致了我的结帐问题。

      【讨论】:

      • 这似乎不是问题的答案。
      • _s-xclick 表示托管(保存)按钮,据我所知。使用_xclick,您必须提供所有数据。
      【解决方案6】:

      这是一个允许用户输入价格和参考的方法。

      注意:您需要将业务从 shop@ekerner.com 更改为您的 PayPal 电子邮件地址,但如果您不这样做,我也可以,因为这只是意味着我会收到您的付款。

      您可能还想更改 currency_code 和 lc 以适合您的语言环境...

                              <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                                  <fieldset>
                                      <input type="hidden" name="cmd" value="_xclick" />
                                      <input type="hidden" name="business" value="shop@ekerner.com" />
                                      <input type="hidden" name="lc" value="AU" />
                                      <input type="hidden" name="item_name" value="Payment" />
                                      <input type="hidden" name="item_number" value="P1" />
                                      <input type="hidden" name="currency_code" value="AUD" />
                                      <input type="hidden" name="button_subtype" value="services" />
                                      <input type="hidden" name="no_note" value="0" />
                                      <input type="hidden" name="cn" value="Comments" />
                                      <input type="hidden" name="no_shipping" value="1" />
                                      <input type="hidden" name="rm" value="1" />
                                      <input type="hidden" name="return" value="http://www.ekerner.com/payments/?payment=success" />
                                      <input type="hidden" name="cancel_return" value="http://www.ekerner.com/payments/?payment=cancelled" />
                                      <input type="hidden" name="bn" value="PP-BuyNowBF:btn_paynowCC_LG.gif:NonHostedGuest" />
                                      <table>
                                              <tr><td style="padding:0 5px 5px 0;">Amount AUD</td><td style="padding:0 5px 5px 0;"><input type="text" name="amount" maxlength="200" /></td></tr>
                                              <tr><td style="padding:0 5px 5px 0;"><input type="hidden" name="on0" value="Reference" />Reference</td><td style="padding:0 5px 5px 0;"> <input type="text" name="os0" maxlength="200" /></td></tr>
                                              <tr><td>&nbsp;</td><td style="padding:0 5px 5px 0;">
                                                      <input style="position:relative; left:-10px; background:#ffffff; border:0;" type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_paynowCC_LG.gif" name="submit" alt="PayPal . The safer, easier way to pay online." />
                                                      <img alt="" style="border:0;" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1" />
                                              </td></tr>
                                      </table>
                                  </fieldset>
                              </form>
      

      【讨论】:

        【解决方案7】:

        不幸的是,在撰写本文时,此问题的所有其他答案都不正确 - 也就是说,如果您尝试更改 托管 按钮的价格;这就是问题所在。

        正确的做法如下:

        重要说明:当您更新按钮详细信息时,它不仅会针对该用户会话进行更新,还会在您的 paypal 帐户中更新 - 因此新名称/价格等将影响所有用户尝试使用它。

        另外,请注意,在更改托管按钮的内容时,您需要将按钮的所有详细信息传递给它,就像创建它时一样;例如,如果您不向它传递项目名称,则项目名称将为空白,Paypal 将允许用户设置它。

        关于这点,我们将继续..

        我个人是从这门课开始的:

        <?php
        
        class Paypal
        {
            /**
             * Last error message(s)
             * @var array
             */
            protected $_errors = array();
        
            /**
             * API Credentials
             * Use the correct credentials for the environment in use (Live / Sandbox)
             * @var array
             */
            protected $_credentials = array(
                'USER' => 'seller_1297608781_biz_api1.lionite.com',
                'PWD' => '1297608792',
                'SIGNATURE' => 'A3g66.FS3NAf4mkHn3BDQdpo6JD.ACcPc4wMrInvUEqO3Uapovity47p',
            );
        
            /**
             * API endpoint
             * Live - https://api-3t.paypal.com/nvp
             * Sandbox - https://api-3t.sandbox.paypal.com/nvp
             * @var string
             */
            protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';
        
            /**
             * API Version
             * @var string
             */
            protected $_version = '74.0';
        
            /**
             * Make API request
             *
             * @param string $method string API method to request
             * @param array $params Additional request parameters
             * @return array / boolean Response array / boolean false on failure
             */
            public function request($method, $params = array())
            {
                $this->_errors = array();
                if (empty($method)) { //Check if API method is not empty
                    $this->_errors = array('API method is missing');
                    return false;
                }
        
                //Our request parameters
                $requestParams = array(
                        'METHOD' => $method,
                        'VERSION' => $this->_version
                    ) + $this->_credentials;
        
                //Building our NVP string
                $request = http_build_query($requestParams + $params);
        
                //cURL settings
                $curlOptions = array(
                    CURLOPT_URL => $this->_endPoint,
                    CURLOPT_VERBOSE => 1,
                    CURLOPT_SSL_VERIFYPEER => true,
                    CURLOPT_SSL_VERIFYHOST => 2,
                    CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_POST => 1,
                    CURLOPT_POSTFIELDS => $request
                );
        
                $ch = curl_init();
                curl_setopt_array($ch, $curlOptions);
        
                //Sending our request - $response will hold the API response
                $response = curl_exec($ch);
        
                //Checking for cURL errors
                if (curl_errno($ch)) {
                    $this->_errors = curl_error($ch);
                    curl_close($ch);
                    return false;
                    //Handle errors
                } else {
                    curl_close($ch);
                    $responseArray = array();
                    parse_str($response, $responseArray); // Break the NVP string to an array
                    return $responseArray;
                }
            }
        }
        
        ?>
        

        信用: https://www.smashingmagazine.com/2011/09/getting-started-with-the-paypal-api/

        然后我做了以下:

        include(dirname(__FILE__) . '/includes/paypal.class.php');
        
        $paypal = new Paypal();
        
        // Set our method
        $method = 'BMUpdateButton';
        
        // Set our params
        $params = array(
            'HOSTEDBUTTONID' => 'your_button_id',
            'BUTTONTYPE' => 'BUYNOW',
            'BUTTONSUBTYPE' => 'SERVICES',
            'L_BUTTONVAR0' => 'item_name=Your Description',
            'L_BUTTONVAR1' => 'amount=999.00',
            'L_BUTTONVAR2' => 'currency_code=AUD',
            'L_BUTTONVAR3' => 'cancel_return=http://www.example.com/cancel.html',
            'L_BUTTONVAR4' => 'return=http://www.example.com/success.html'
        );
        
        // Make request to change button details
        $result = $paypal->request($method, $params);
        

        请注意,虽然 Paypal 说 BUTTONSUBTYPE 是可选的,但如果不包含它,您可能会收到错误消息。

        不幸的是,Paypal 文档不是很清楚,也没有提供最好的示例,所以我希望这可以节省其他人我花费大量时间来寻找如何做到这一点的时间。

        【讨论】:

          【解决方案8】:

          虽然是旧帖子,但在搜索时遇到了我。在第一页上没有任何地方可以回答这个问题!阅读了大约 10 个小时,我设法做了一个工作示例。但请记住,paypal 是不断变化的,所以在某些时候这个解决方案将不再有效。

          首先要做的事情。您永远不可能有一个项目的变量价格值。因此,第一个解决方法是发送请求,就好像商品是您购物车中的总商品一样!是的,聪明的举动:)

          <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
              <input type="hidden" name="cmd" value="_cart">
              <input type="hidden" name="upload" value="1">
              <input type="hidden" name="business" value="XVR95QDG6M53J">
              <input type="hidden" name="item_name_1" value="This is the name of what you are selling">
              <input type="hidden" name="amount_1" value="<?php echo $total_cost; ?>">
              <input type="hidden" name="currency_code" value="the currency code">
              <input type="hidden" name="lc" value="if you dont need delete">
              <input type="hidden" name="shopping_url" value="link to your shop or cart on your website">
              <input type="hidden" name="retun" value="URL the user returns if payment is OK">
              <input type="hidden" name="cancel_return" value="URL the user returns if payment is canceled">
              <input type="submit" class="read-more-btn4" value="Text of [read more] button">
          </form>
          

          您可能希望将business 的值更改为您的值。但我真的不在乎:)

          【讨论】:

            猜你喜欢
            • 2021-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-18
            • 2017-10-11
            • 2014-12-15
            相关资源
            最近更新 更多