当我在 CURLOPT_HTTPHEADER 中添加 PayPal-Mock-Response 时出现 404 curl_error [英] 404 curl_error when i add PayPal-Mock-Response in CURLOPT_HTTPHEADER

查看:47
本文介绍了当我在 CURLOPT_HTTPHEADER 中添加 PayPal-Mock-Response 时出现 404 curl_error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于成功的 paypal 交易(没有 paypal-mock-response),以下代码没有错误.

当我在 CURLOPT_HTTPHEADER 中添加 "PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}" 以测试不同的 senarios 时,就像 paypal 建议的那样在 https://developer.paypal.com/docs/api/request-headers/#test-api-error-handling-routinescurl_error() 返回 请求的 URL 返回错误:404 Not Found

问题必须是我在 CURLOPT_HTTPHEADER 中添加 "PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}" 的方式,但是我还没弄明白.

paypal_button.php

<div id="paypal-button-container"></div><script src="https://www.paypalobjects.com/api/checkout.js"></script><脚本>paypal.Button.render({环境:'沙箱',客户: {沙箱:'<?php echo PayPal_CLIENT_ID;?>'},提交:真实,风格: {布局:'水平',资金图标:真实"},//当按钮被点击时调用payment()支付:功能(数据,动作){//调用 REST api 以创建付款返回 actions.payment.create({支付: {交易:[{数量: {总计:'200',货币:'欧元'}]]},经验: {输入字段:{no_shipping: 1}}});},//当买家批准付款时调用 onAuthorize()onAuthorize:函数(数据,动作){//调用 REST api 来执行支付返回 actions.payment.execute().then(function(){console.log('支付完成!');window.location = "<?php echo BASE_URL ?>process.php?paymentID="+data.paymentID+"&payerID="+data.payerID+"&token="+data.paymentToken});}}, '#paypal-button-container');

process.php

';echo "访问令牌接收期间的卷曲错误 = ".卷曲错误($卷曲);echo '</pre>';curl_close($curl);}else//如果 curl 成功从结果中获取访问令牌{$json = json_decode($result);$accessToken = $json->access_token;echo "访问令牌已成功收到,它是 = ".$accessToken;$curl = curl_init();curl_setopt($curl, CURLOPT_URL, PayPal_BASE_URL.'payments/payment/' .$paymentID);curl_setopt($curl, CURLOPT_FAILONERROR, true);curl_setopt($curl, CURLOPT_HEADER, false);curl_setopt($curl, CURLOPT_POST, false);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_HTTPHEADER, 数组('授权:承载'.$访问令牌,'接受:应用程序/json','内容类型:应用程序/json','PayPal-Mock-Response: {\"mock_application_codes\":\"MALFORMED_REQUEST\"}'//如果我们删除 PayPa-Mock-Response 行,它会起作用));if( !($response = curl_exec($curl)) )//如果 curl 没有成功{//请求的 URL 返回错误:404 Not Foundecho '

';回声卷曲错误=".卷曲错误($卷曲);echo '</pre>';//当然是空的$result = json_decode($response);echo '

';echo "result with curl error = ".$结果;echo '</pre>';curl_close($curl);}else//如果卷曲成功{$result = json_decode($response);echo '

';print_r($result);echo '</pre>';$state = $result->state;curl_close($curl);if($state == 'approved') 返回真;否则返回假}}返回假;}if(!empty($_GET['paymentID']) && !empty($_GET['payerID']) && !empty($_GET['token']) ){$paymentID = $_GET['paymentID'];$payerID = $_GET['payerID'];$token = $_GET['token'];$paypalCheck=paypalCheck($paymentID, $payerID, $token);if($paypalCheck) echo "成功";else echo "失败";}别的{echo "某些获取参数丢失或为空";}}捕获(异常 $e){echo $e->getMessage();}

来自没有 PayPal-Mock-Response 的交易的 print_r($result) 以防万一:

stdClass 对象([id] =>PAY-8JU54317DF169510YLNYKGKI[意图] =>销售[状态] =>得到正式认可的[购物车] =>9E037228C8147091B[付款人] =>标准类对象([付款方式] =>贝宝[状态] =>已验证[付款人信息] =>标准类对象([电子邮件] =>shashasha@gmail.com[名字] =>帕诺斯[姓氏] =>辣椒粉[payer_id] =>PCVE6KXLWZWV2[送货地址] =>标准类对象([收件人姓名] =>帕诺斯帕皮斯)[电话] =>3063691955[国家代码] =>GR))[交易] =>大批([0] =>标准类对象([金额] =>标准类对象([总计] =>200.00[货币] =>欧元[详情] =>标准类对象([小计] =>200.00))[收款人] =>标准类对象([merchant_id] =>WLYWH4ZE3BUTW)[soft_descriptor] =>PAYPAL *测试工具[item_list] =>标准类对象([项目] =>大批()[送货地址] =>标准类对象([收件人姓名] =>帕诺斯帕皮斯))[related_resources] =>大批([0] =>标准类对象([销售] =>标准类对象([id] =>9MM47166KB525480R[状态] =>完全的[金额] =>标准类对象([总计] =>200.00[货币] =>欧元[详情] =>标准类对象([小计] =>200.00))[支付模式] =>INSTANT_TRANSFER[保护资格] =>有资格的[protection_eligibility_type] =>ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE[交易费用] =>标准类对象([值] =>7.15[货币] =>欧元)[parent_payment] =>PAY-8JU54317DF169510YLNYKGKI[创建时间] =>2018-08-12T21:14:55Z[更新时间] =>2018-08-12T21:14:55Z[链接] =>大批([0] =>标准类对象([参考] =>https://api.sandbox.paypal.com/v1/payments/sale/9MM47166KB525480R[rel] =>自己[方法] =>得到)[1] =>标准类对象([参考] =>https://api.sandbox.paypal.com/v1/payments/sale/9MM47166KB525480R/refund[rel] =>退款[方法] =>邮政)[2] =>标准类对象([参考] =>https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JU54317DF169510YLNYKGKI[rel] =>parent_payment[方法] =>得到))[soft_descriptor] =>PAYPAL *测试工具)))))[创建时间] =>2018-08-12T21:14:17Z[链接] =>大批([0] =>标准类对象([参考] =>https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JU54317DF169510YLNYKGKI[rel] =>自己[方法] =>得到)))

解决方案

根据HTTP404和官方文档 ...

curl_setopt($curl, CURLOPT_URL, PayPal_BASE_URL.'payments/payment/' . $paymentID. '/execute');

CURLOPT_URL 似乎缺少作为动词的尾随 /execute.

关于 HTTP400 ... 可能需要首先 创建一笔付款(并记住paymentId)然后执行该付款 - 同时传回相应的付款 ID.

The following code works with no errors for a successful paypal transaction (without paypal-mock-response ).

When i added "PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}" in CURLOPT_HTTPHEADER to test different senarios as paypal suggests at https://developer.paypal.com/docs/api/request-headers/#test-api-error-handling-routines curl_error() returns The requested URL returned error: 404 Not Found

The problem has to be the way i add "PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}" in CURLOPT_HTTPHEADER but i haven't figure it out yet.

paypal_button.php

<?php
    define("BASE_URL", "http://my_website.com/paypal_test/");
    define("PayPal_CLIENT_ID", "my_paypal_client_id");
    define("PayPal_BASE_URL", "https://api.sandbox.paypal.com/v1/");
?>
<div id="paypal-button-container"></div>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
    paypal.Button.render(
    {
        env: 'sandbox',
        client: {
            sandbox:    '<?php echo PayPal_CLIENT_ID; ?>'
        },
        commit: true,
        style: {
            layout: 'horizontal',
            fundingicons: 'true'
        },
        // payment() is called when the button is clicked
        payment: function(data, actions) 
        {
            // Make a call to the REST api to create the payment
            return actions.payment.create({
                payment: {
                    transactions: [
                        {
                            amount: {
                                total: '200',
                                currency: 'EUR'
                            }
                        } ]
                },
                experience: {
                    input_fields: {
                        no_shipping: 1
                    }
                }
            });
        },
        // onAuthorize() is called when the buyer approves the payment
        onAuthorize: function(data, actions) 
        {
            // Make a call to the REST api to execute the payment
            return actions.payment.execute().then(function() 
            {
                console.log('Payment Complete!');
                window.location = "<?php echo BASE_URL ?>process.php?paymentID="+data.paymentID+"&payerID="+data.payerID+"&token="+data.paymentToken
            });
        }
    }, '#paypal-button-container');
</script>

process.php

<?php
define("BASE_URL", "http://my_website.com/paypal_test/");
define("PayPal_CLIENT_ID", "my_paypal_client_id");
define("PayPal_SECRET", "my_paypal_secret");
define("PayPal_BASE_URL", "https://api.sandbox.paypal.com/v1/");
try
{
    function paypalCheck($paymentID, $payerID, $paymentToken)
    {
        // Request Access Token
        $ch = curl_init();
        $clientId = PayPal_CLIENT_ID;
        $secret = PayPal_SECRET;
        curl_setopt($ch, CURLOPT_URL, PayPal_BASE_URL.'oauth2/token');
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        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");
        if( !($result = curl_exec($ch)) ) // If curl didn't succeed
        {
            echo '<pre>';
            echo "curl error during access token recieval = " . curl_error($curl);
            echo '</pre>';
            curl_close($curl);
        }
        else // If curl succeed get access token from result
        {
            $json = json_decode($result);
            $accessToken = $json->access_token;
            echo "access token was recieved succesfully and it is = " . $accessToken;

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, PayPal_BASE_URL.'payments/payment/' . $paymentID);
            curl_setopt($curl, CURLOPT_FAILONERROR, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_POST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer ' . $accessToken,
                'Accept: application/json',
                'Content-Type: application/json',
                'PayPal-Mock-Response: {\"mock_application_codes\":\"MALFORMED_REQUEST\"}'
               // If we remove PayPa-Mock-Response line it works
            ));


            if( !($response = curl_exec($curl)) ) // If curl didn't succeed
            {
                // The requested URL returned error: 404 Not Found
                echo '<pre>';
                echo "curl error = " . curl_error($curl);
                echo '</pre>';

                //it's empty of course
                $result = json_decode($response);
                echo '<pre>';
                echo "result with curl error = " . $result;
                echo '</pre>';

                curl_close($curl);
            }
            else // If curl succeeded
            {        
                $result = json_decode($response);
                echo '<pre>';
                print_r($result);
                echo '</pre>';

                $state = $result->state;
                curl_close($curl);
                if($state == 'approved') return true;
                else return false
            }
        }
        return false;
    }


    if(!empty($_GET['paymentID']) && !empty($_GET['payerID']) && !empty($_GET['token']) )
    {
        $paymentID = $_GET['paymentID'];
        $payerID = $_GET['payerID'];
        $token = $_GET['token'];
        $paypalCheck=paypalCheck($paymentID, $payerID, $token);

        if($paypalCheck) echo "Success";
        else echo "Failure";
    }
    else
    {
        echo "Some of the get parameters are missing or are empty";
    }

}
catch(Exception $e)
{
    echo $e->getMessage();

}

The print_r($result) from a transaction without PayPal-Mock-Response in case it helps:

stdClass Object
(
    [id] => PAY-8JU54317DF169510YLNYKGKI
    [intent] => sale
    [state] => approved
    [cart] => 9E037228C8147091B
    [payer] => stdClass Object
        (
            [payment_method] => paypal
            [status] => VERIFIED
            [payer_info] => stdClass Object
                (
                    [email] => shashasha@gmail.com
                    [first_name] => panos
                    [last_name] => papepis
                    [payer_id] => PCVE6KXLWZWV2
                    [shipping_address] => stdClass Object
                        (
                            [recipient_name] => panos papepis
                        )

                    [phone] => 3063691955
                    [country_code] => GR
                )

        )

    [transactions] => Array
        (
            [0] => stdClass Object
                (
                    [amount] => stdClass Object
                        (
                            [total] => 200.00
                            [currency] => EUR
                            [details] => stdClass Object
                                (
                                    [subtotal] => 200.00
                                )

                        )

                    [payee] => stdClass Object
                        (
                            [merchant_id] => WLYWH4ZE3BUTW
                        )

                    [soft_descriptor] => PAYPAL *TESTFACILIT
                    [item_list] => stdClass Object
                        (
                            [items] => Array
                                (
                                )

                            [shipping_address] => stdClass Object
                                (
                                    [recipient_name] => panos papepis
                                )

                        )

                    [related_resources] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [sale] => stdClass Object
                                        (
                                            [id] => 9MM47166KB525480R
                                            [state] => completed
                                            [amount] => stdClass Object
                                                (
                                                    [total] => 200.00
                                                    [currency] => EUR
                                                    [details] => stdClass Object
                                                        (
                                                            [subtotal] => 200.00
                                                        )

                                                )

                                            [payment_mode] => INSTANT_TRANSFER
                                            [protection_eligibility] => ELIGIBLE
                                            [protection_eligibility_type] => ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE
                                            [transaction_fee] => stdClass Object
                                                (
                                                    [value] => 7.15
                                                    [currency] => EUR
                                                )

                                            [parent_payment] => PAY-8JU54317DF169510YLNYKGKI
                                            [create_time] => 2018-08-12T21:14:55Z
                                            [update_time] => 2018-08-12T21:14:55Z
                                            [links] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [href] => https://api.sandbox.paypal.com/v1/payments/sale/9MM47166KB525480R
                                                            [rel] => self
                                                            [method] => GET
                                                        )

                                                    [1] => stdClass Object
                                                        (
                                                            [href] => https://api.sandbox.paypal.com/v1/payments/sale/9MM47166KB525480R/refund
                                                            [rel] => refund
                                                            [method] => POST
                                                        )

                                                    [2] => stdClass Object
                                                        (
                                                            [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JU54317DF169510YLNYKGKI
                                                            [rel] => parent_payment
                                                            [method] => GET
                                                        )

                                                )

                                            [soft_descriptor] => PAYPAL *TESTFACILIT
                                        )

                                )

                        )

                )

        )

    [create_time] => 2018-08-12T21:14:17Z
    [links] => Array
        (
            [0] => stdClass Object
                (
                    [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JU54317DF169510YLNYKGKI
                    [rel] => self
                    [method] => GET
                )

        )

)

解决方案

according to the HTTP404 and the official documentation ...

curl_setopt($curl, CURLOPT_URL, PayPal_BASE_URL.'payments/payment/' . $paymentID. '/execute');

the CURLOPT_URL seems to be missing the trailing /execute, as the verb.

and concerning that HTTP400 ... it might be required to first create a payment (and remembering the paymentId) and then to execute that payment - while passing back the corresponding paymentID.

这篇关于当我在 CURLOPT_HTTPHEADER 中添加 PayPal-Mock-Response 时出现 404 curl_error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆