php 使用自定义按钮将价格变量传递给 PayPal

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5255571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:45:57  来源:igfitidea点击:

Passing price variable to PayPal with custom button

phppaypal

提问by Denoteone

I have a form and a custom PayPal button, but how do I pass the value/price variable to PayPal?

我有一个表单和一个自定义 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>

I have a variable $total = "238.00";

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

采纳答案by Shakti Singh

Add one more hidden field for amount

为金额再添加一个隐藏字段

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

回答by Julian

The previous code did not work for me. After much headache I finally figured out you have to go into PayPal and on step 2 of creating the button code make sure you click the unhosted button, and then copy the unencrypted button code, which will give you something like this (I blanked out my business value for security):

以前的代码对我不起作用。经过一番头痛,我终于发现您必须进入 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>

This code creates a button where the user can input the amount, which by default starts at 16.99, but you could just as easily replace that with a PHP variable.

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

回答by IanOSullivan

I tried and failed with all of the above. I found this to be the answer from the PayPal website.

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

<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="[email protected]">
    <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>

Test this and you'll understand how it works... change the business to the email address of person you want to pay etc.

测试一下,您就会了解它是如何工作的……将业务更改为您要付款的人的电子邮件地址等。

回答by Phil Mulkins

Here is the 2013 version: Go to create a button, when you get to step 2, uncheck the box, proceed to step 3 then create the button. Once you have the code, it will look like this:

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

Your "business" value will not be XXXXXXXX, so make sure you leave the one Paypal gives you. You can also set your cancel and return URL's.

您的“业务”价值不会是 XXXXXXXX,因此请确保保留 Paypal 给您的价值。您还可以设置取消和返回 URL。

For more advanced PHP users: I actually setup a PHP string and it works great! For example, see below:

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

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

And so on.....As you can see $mydescription is a PHP variable and $myprice is a PHP variable. What I did was setup a HTML form to collect data and used that form as a payment processing form. Once the user clicks submit, I have it going to a PHP page to use as a Mailer, Database Insertion, Autoresponder, and finally a Header redirect. The URL for the redirect is the Paypal URL with the Variables in the string! This thread actually helped me find the correct Paypal button code so that string will work properly with price variations! FYI - If you are a beginner PHP person, the image field is not used in the string. Only the URL and then the Hidden Names and Values.

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

回答by mavame

I found the solution:

我找到了解决方案:

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

needs to be changed to

需要改为

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

The first encrypts the data sent by the form - which caused the issue with my checkout.

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

回答by ekerner

Here is one that allows the user to enter a price and reference.

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

NOTE:You'll need to change business from [email protected] to your PayPal email address, but its fine by me if you don't because it just means that I will receive your payments.

注意:您需要将业务从 [email protected] 更改为您的 PayPal 电子邮件地址,但如果您不这样做,我也可以这样做,因为这意味着我会收到您的付款。

You may also like to change currency_code and lc to suit your locale ...

您可能还想更改 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="[email protected]" />
                                <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>

回答by Brett

Unfortunately at the time of writing, all of the other answers to this question are incorrect - that is if you're trying to change the price of a hostedbutton; which is what the question was about.

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

The correct way to do this is below:

正确的方法如下:

Important Notes:When you update the button details, it isn't just updated for that users session, it updates it within your paypal account - so the new name/price etc will affect all users that attempt to use it.

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

Also, be advised when altering the contents of a hosted button you need to pass allthe details of the button to it as when you created it; so as an example, if you leave out passing it an item name the item name will be blank and Paypal will allow the user to set it.

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

On that note, we shall continue..

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

I personally started with this class:

我个人从这门课开始:

<?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;
        }
    }
}

?>

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

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

Then I did the below:

然后我做了以下事情:

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

Note that while Paypal say that BUTTONSUBTYPEis optional, you will likely get an error if you don't include it.

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

Unfortunately the Paypal docs aren't very clear and don't provide the greatest examples, so I hope this saves someone else the many hours I spent finding out how to do this.

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

回答by K. Balta

Although an old post, it came across me when searching. And there is nowhere on the first pages that answers the question! Reading for about 10 hours, I managed to make a working example. But remember that paypal is constantly changing, so at some point this solution will not work anymore.

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

First things first. You cannot ever have a variableprice value for an item. So, the first workaround is to send a request as if the item is a total in your cart! Yep, smart move :)

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

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

You may want to change the value of businessto yours. But I don't really care :)

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