【问题标题】:PayPal express checkout server side, authentication failure贝宝快递结账服务器端,认证失败
【发布时间】:2018-08-01 00:59:57
【问题描述】:

我到处搜索了很多,但我没有找到我的问题的答案。

我无法让它工作。我的 PayPal 按钮正在使用快速结帐服务器端 Rest,但我测试了客户端,它很简单,而且很有效。

我正在使用 CodeIgniter,所以这是我的代码。

这是我的视图文件夹中的 javascript 代码。 CREATE_URLEXECUTE_URL 转到控制器。

paypal.Button.render({

        env: 'sandbox', // sandbox | production

        // Show the buyer a 'Pay Now' button in the checkout flow
        commit: true,

        // payment() is called when the button is clicked
        payment: function() {

            // Set up a url on your server to create the payment
            var CREATE_URL = '/projects/createurl';

            // Make a call to your server to set up the payment
            return paypal.request.post(CREATE_URL)
                .then(function(res) {
                    return res.paymentID;
                });
        },

        // onAuthorize() is called when the buyer approves the payment
        onAuthorize: function(data, actions) {

            // Set up a url on your server to execute the payment
            var EXECUTE_URL = '/projects/executeurl';

            // Set up the data you need to pass to your server
            var data = {
                paymentID: data.paymentID,
                payerID: data.payerID
            };

            // Make a call to your server to execute the payment
            return paypal.request.post(EXECUTE_URL, data)
                .then(function (res) {
                    window.alert('Payment Complete!');
                });
        }

    }, '#paypal-button-container');

这里是CREATE_URL控制器

public function createurl() {

            $ch = curl_init();
    $clientId = "something";
    $secret = "something";

    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSLVERSION , 6); 
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

    $result = curl_exec($ch);

    if(empty($result))die("Error: No response.");
    else
    {
        $json = json_decode($result, TRUE);
    }



    curl_close($ch); 


    $ch2 = curl_init();
    $token = $json['access_token'];
    $data = '{
            "transactions": [{
            "amount": {
                "currency":"EUR",
                "total":"12"
            },
            "description":"creating a payment"
            }],
            "payer": {
                "payment_method":"paypal"
            },
            "intent":"sale",
            "redirect_urls": {
                "cancel_url":"https://mysite.gr/projects/project/10",
                "return_url":"https://mysite.gr/projects/project/10"
            }
       }'; 


    curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
    curl_setopt($ch2, CURLOPT_VERBOSE, 1);
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch2, CURLOPT_HTTPHEADER , "Content-Type: application/json");
    curl_setopt($ch2, CURLOPT_HTTPHEADER , "Authorization: Bearer ".$token);
    curl_setopt($ch2, CURLOPT_POST, 1);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);

    $result = curl_exec($ch2);
    $json = json_decode($result, TRUE); 
    print_r($json);
    curl_close($ch2);
} 

所以当我点击 PayPal 按钮时 - 它失败了。来自控制台的日志:

ppxo_no_token_passed_to_payment

ppxo_unhandled_error {堆栈:“错误:没有值传递给支付↵在 https://w…://www.paypalobjects.com/api/checkout.js:3076:13)",错误类型: “[对象错误]”

未捕获的错误:没有传递给付款的值

当我将控制器输入到 url 以查看数组时,它会说:

数组([名称] => AUTHENTICATION_FAILURE [消息] => 身份验证 由于身份验证凭据无效或丢失而失败 授权标头。 [链接] => 数组([0] => 数组([href] => https://developer.paypal.com/docs/api/overview/#error [rel] => 信息链接 ) ) )

【问题讨论】:

    标签: codeigniter authentication curl paypal express-checkout


    【解决方案1】:

    好的,经过 1 周的工作,我解决了我的问题,我终于找到了解决方案,现在效果很好!

    AUTHENTICATION_FAILURE [消息] => 身份验证失败,原因是 身份验证凭据无效或缺少授权标头。

    这是因为我发送了两个单独的 HTTPHEADERS 而不是一个,结果是相互覆盖。所以正确的做法是在 HTTPHEADER 的第三个参数中传递一个数组,如下所示:

    curl_setopt($ch2, CURLOPT_HTTPHEADER , array("Content-Type: application/json", "Authorization: Bearer myaccesstokenishere"));
    

    在那之后我仍然有问题:

    ppxo_no_token_passed_to_payment

    ppxo_unhandled_error {stack: "错误:没有值传递给支付↵

    未捕获的错误:没有传递给付款的值

    为了解决这个问题,我只是在没有 json_decode 的情况下回显结果,如下所示:

    $result = curl_exec($ch2);
    
    echo $result;
    
    curl_close($ch2);
    

    现在一切正常。

    executeurl 控制器很简单,如果有人要我发布它告诉我。

    我希望我能帮助遇到类似问题的人。

    【讨论】:

    • 参考developer.paypal.com/docs/checkout/how-to/server-integration/…你能解释一下吗: curl_setopt($ch2, CURLOPT_HTTPHEADER , array("Content-Type: application/json", "Authorization: Bearer myaccesstokenishere"));
    • 一开始的问题是我发送了两个不同的 CURLOPT_HTTPHEADER。这是错误的。为了在 curl 调用中发送两个或更多属性,您需要将其与数组一起发送。所以第三个参数是一个需要发送的属性数组 1) Content-Type: application/json 和 2) Authorization: Bearer $mytoken。我回答你的问题了吗?
    猜你喜欢
    • 2015-01-16
    • 2012-10-13
    • 2018-08-18
    • 1970-01-01
    • 2021-06-14
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多