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

Bre*_*ett 5 php paypal paypal-ipn

好吧,我之前没有使用托管按钮,但它更有意义,因为它们更安全.

我环顾四周,一直在阅读文档(这些文档并不是那么有用),到目前为止我找到的最好的帮助就在这里 ; 虽然我仍然对确切放置代码的地方感到困惑?

另外,我在技术上不想要一个"按钮",但它背后的想法似乎是我想要的.

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

另外,我本来不想要一个按钮,我更倾向于用适当的数据将用户重定向到贝宝,但不确定如何在设置动态价格时这样做呢?

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

Bre*_*ett 2

好吧,我终于发现BMUpdateButton API的响应不仅返回HTML创建表单,还在返回的数组中返回其他数据。

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

这些都是:

  • 网站代码

    网页的 HTML 代码

    • 电子邮件链接

这就是我一直在寻找的;您可以将用户重定向到的简单链接

  • 托管按钮ID

按钮的 ID。

请注意,更改托管按钮的内容时,您需要像创建按钮时一样将按钮的所有详细信息传递给它;例如,如果您省略向其传递项目名称,则项目名称将为空,并且 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;
        }
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

信用: 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);
Run Code Online (Sandbox Code Playgroud)

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