下面的代码对于成功的贝宝交易(没有paypal-mock-response)没有错误。
当我在"PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}"
中添加CURLOPT_HTTPHEADER
来测试不同的senario时,如贝宝在https://developer.paypal.com/docs/api/request-headers/#test-api-error-handling-routines上建议的那样
curl_error()
返回The requested URL returned error: 404 Not Found
问题一定是我在"PayPal-Mock-Response:{\"mock_application_codes\": \"MALFORMED_REQUEST\"}"
中添加CURLOPT_HTTPHEADER
的方式,但是我还没有弄清楚。
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
)
)
)
答案 0 :(得分:1)
根据HTTP404
和官方documentation ...
curl_setopt($curl, CURLOPT_URL, PayPal_BASE_URL.'payments/payment/' . $paymentID. '/execute');
CURLOPT_URL
似乎缺少动词后面的/execute
。
,并涉及到HTTP400
……可能需要先create次付款(并记住paymentId),然后再execute次付款-同时传回相应的PaymentID