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

29

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

8个回答

25

之前的代码对我无效。经过长时间的头痛,我最终发现您必须进入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变量替换它。


3
最好在金额中加入 type="hidden" :) - fkoessler
1
所以这不是来自PayPal API?也没有考虑任何安全问题,对吧? - user1900121
这并没有真正回答所提出的问题,而且不应该有这么多赞。 - Brett
5
这不应该被认为是OP问题的答案。OP有一个托管按钮,可以避免客户端的任何表单数据篡改。如果您只是添加了一个这样的表单但没有执行任何服务器端验证,用户可以按照自己的意愿编辑产品的实际价值。即使您将字段隐藏起来,使用检查元素仍然可以编辑该值并提交。我很乐意对此帖子进行投票。 - avizzzy
这条评论很有帮助,谢谢!我的错误在于我创建了托管按钮,而不是非托管按钮,因此我无法即时更改任何值。因此,建议您在创建新按钮时确保取消选中“在PayPal保存按钮”选项。 - Ilja Hämäläinen
显示剩余2条评论

15

我尝试了所有以上方法,都失败了。 我从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="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>

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


14

再添加一个用于金额的隐藏字段

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

1
@x-yurl,你是指无法取消的PayPal“保存按钮”选项吗?如果是的话,有什么解决方案呢?因为现在我需要在PayPal中添加不同的价格选项。谢谢! - Harkály Gergő
2
@x-yuri 我尝试了托管和非托管按钮,但似乎都对我无效。我的问题是:http://stackoverflow.com/questions/40210676/not-able-to-specify-amount-per-payment-request-on-paypal-htmlbutton - cyberjar09
2
@HarkályGergő 抱歉,我现在才注意到你的问题。我已经有一段时间没有处理PayPal了,但我相信原因是所有托管按钮的数据(如价格)都应该存储在PayPal上。无法从表单中更改它们。如果您想要一个带有不同价格的按钮,则应使用非托管按钮。也就是说,没有任何东西存储在PayPal上,您在表单中指定所有参数。 - x-yuri
谢谢x-yuri,是的,我已经意识到了。我用自己的表单解决了问题,而不是使用托管按钮,你是对的! - Harkály Gergő
大家好,你们认为这是一个安全的传递价格的方式吗?我的意思是用户可以将隐藏输入类型更改为文本并更改值。 - Paul Noris

5

以下是2013版本的操作步骤:前往创建按钮,在第二步时取消选择框,继续进行到第三步然后创建按钮。一旦您获得了代码,它将看起来像这样:

<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和$myprice都是PHP变量。我设置了一个HTML表单来收集数据,并将其用作付款处理表单。一旦用户点击提交,它会转到一个PHP页面,用作Mailer、数据库插入、自动响应器和最终的Header重定向。重定向的URL是带有变量字符串的Paypal URL!这个主题实际上帮助我找到了正确的Paypal按钮代码,使价格变化的字符串可以正常工作!FYI-如果您是初学者PHP人员,图像字段不在字符串中使用。只有URL,然后是隐藏的名称和值。


3
我不认为现在这种方式可行。Paypal似乎防止客户端更改立即购买按钮中预设的付款金额……至少我不能让它正常工作。 - P a u l
1
@Paul 我也是。我正在尝试在结账流程的末尾获取一个“通过PayPal支付”的按钮,但我无法手动设置价格。这很烦人。 - Chud37

4

我找到了解决方法:

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

需要更改为

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

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


2
这似乎不是对问题的回答。 - TRiG
从我所了解的情况来看,_s-xclick 表示托管(保存)按钮。而使用 _xclick,您需要提供所有数据。 - x-yuri

1

这里有一个允许用户输入价格和参考的代码。

注意:您需要将商家地址从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>

0

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

以下是正确的方法:

重要提示:当您更新按钮详细信息时,它不仅会更新该用户会话中的信息,而且还会在您的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文档并不是很清晰,也没有提供最好的示例,因此我希望这可以为其他人节省我花费的许多时间来找出如何做到这一点。

-1

虽然这是一篇旧帖子,但当我在搜索时它出现了。而且前几页都没有回答这个问题的地方!经过大约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的值改成你自己的,但我其实不太在意 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接