使用重定向而不是按钮通过 Paypal 付款安全地设置动态金额?

Setting dynamic amounts securely with Paypal payments with a redirection and not button?

好吧,我以前没有使用过托管按钮,但这是有道理的,因为它们更安全。

我环顾四周并阅读了文档(这些文档并不是很有帮助),到目前为止我找到的最好的帮助是 here;尽管我仍然对将代码放在哪里感到困惑?

此外,从技术上讲,我不想要 "button",但它们背后的想法似乎是我想要的。

我只想每次都使用相同的查询变量,但只想更改价格 - 价格是动态的,具体取决于用户在表单中选择的内容。

此外,我本身不需要按钮,我更愿意使用适当的数据将用户重定向到 paypal,但不确定如何在设置动态价格时做到这一点?

如果我不必设置动态价格,我知道我可以将托管按钮的查询变量附加到 URL,然后重定向到那个 URL,但我需要更改价格,因此我的问题...

如果您要使用托管按钮,则需要使用他们的按钮设计器。

https://www.paypal.com/us/cgi-bin/webscr?cmd=_button-designer

安全来自交易后的IPN验证。

参见:

https://www.paypal.com/us/cgi-bin/webscr?cmd=p/acc/ipn-info-outside

Validate that IPN call is from PayPal?

这个 post 的答案是一个更好的方法:

Dynamic PayPal button generation - isn't it very insecure?

按钮代码是 HTML 基于表单标签的,因此您可以轻松地将其转换为字符串查询和 assemble 您的动态负载,格式如下:

https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=yourAccount@email.com&item_name=itemNameTest&item_number=123&amount=1&currency_code=USD&no_shipping=1

对其进行自定义并根据列出的变量追加变量 HERE,并将其与您的重定向方法一起使用。

按钮管理器 API 更全面,除了它不会 return 您像这样查询字符串 URL,尽管背后的想法与HTML 表单代码片段

好的,我终于发现 BMUpdateButton API return 和 HTML 的响应不仅创建了一个表单,它还 returns 其他数据以及在 returned 数组中。

一旦您发出请求,它将 return 一个包含三个键的数组,根据 API 页面上的 BMUpdateButton Response 部分 link 以上。

这些是:

  • 网站代码

    HTML 网页代码

    • 电子邮件链接

这就是我要找的;一个普通的 link 你可以将用户重定向到

  • HOSTEDBUTTONID

按钮的id。

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

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

如果您仍然想要更新按钮的详细信息,您可以使用以下方法进行更新:

我个人是从这个开始的 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;
        }
    }
}

?>

来源: 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 是可选的,但如果您不包含它,您可能会收到错误消息。