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

问题描述 投票:1回答:1

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

当我在qazxsw poi中添加qazxsw poi来测试不同的场景,因为paypal建议在"PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}" CURLOPT_HTTPHEADER返回https://developer.paypal.com/docs/api/request-headers/#test-api-error-handling-routines

问题必须是我在curl_error()添加The requested URL returned error: 404 Not Found的方式,但我还没弄明白。

paypal_button.php

"PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}"

process.php

CURLOPT_HTTPHEADER

没有PayPal-Mock-Response的事务中的print_r($ result),如果它有帮助:

<?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>
php paypal paypal-sandbox php-curl
1个回答
1
投票

根据<?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(); } 和官方stdClass Object ( [id] => PAY-8JU54317DF169510YLNYKGKI [intent] => sale [state] => approved [cart] => 9E037228C8147091B [payer] => stdClass Object ( [payment_method] => paypal [status] => VERIFIED [payer_info] => stdClass Object ( [email] => [email protected] [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支付(并记住paymentId)然后HTTP400支付 - 同时传回相应的paymentID。

© www.soinside.com 2019 - 2024. All rights reserved.