php - 当我在 CURLOPT_HTTPHEADER 中添加 PayPal-Mock-Response 时出现 404 curl_error

标签 php paypal paypal-sandbox php-curl

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

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

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

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();

}

没有 PayPal-Mock-Response 的交易中的 print_r($result) 以防有帮助:

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
                )

        )

)

最佳答案

根据HTTP404和官方documentation ...

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

CURLOPT_URL 似乎缺少结尾的 /execute,作为动词。

关于 HTTP400 ...可能需要先 create付款(并记住 paymentId)然后到 execute该付款 - 同时传回相应的 paymentID。

关于php - 当我在 CURLOPT_HTTPHEADER 中添加 PayPal-Mock-Response 时出现 404 curl_error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51812242/

相关文章:

php - Apache 缓冲区溢出,虚拟主机过多

php - 使用 PHP + MySQL + Cron 安排电子邮件

paypal - Paypal PayFlow Pro API 是否适用于瑞典?

paypal - 接受拱形卡付款

python - Sandbox Paypal IPN 不发送响应,适用于实时网站 Paypal

php - 在 PHP 和 MySQL 中使用准备好的查询删除行

ios - 如何在 iOS App Swift 4 中集成 Paypal 支付方式?

PayPal HTML 按钮缺少发票参数

c# - PayPal DoReferenceTransaction

php - laravel 在循环中附加 wheres 以执行 AND 过滤