我们正在使用PayPal的退款API。在沙盒中,curl请求的返回值为JSON,例如:
{"id":"2MF64726R55120122","create_time":"2018-08-02T16:21:02Z","update_time":"2018-08-02T16:21:02Z","state":"completed","amount":{"total":"223.64","currency":"USD"},"refund_from_transaction_fee":{"currency":"USD","value":"6.49"},"total_refunded_amount":{"currency":"USD","value":"223.64"},"refund_from_received_amount":{"currency":"USD","value":"217.15"},"sale_id":"72D344128A363704P","parent_payment":"PAY-1N951805D8633101ELNRS6FI","links":[{"href":"https://www.sandbox.paypal.com/v1/payments/refund/2MF64726R55120122","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/v1/payments/payment/PAY-1N951805D8633101ELNRS6FI","rel":"parent_payment","method":"GET"},{"href":"https://www.sandbox.paypal.com/v1/payments/sale/72D344128A363704P","rel":"sale","method":"GET"}]}
在生产中,将返回相同的代码:
��Q[k�0�/y�6Ick���lC��7F�)��F�*"���Xa�n��w;_$OID�X�P�c���/��I4(��% �S6R\|��������6�5���.��x�ʺ����F�8�����P%{�Z�r YW�2�u)�VU��ו���a@���u���O�W�T~Y���\XkH ��R�wF#+Ъ�c�q!X@�r����"�QH7�Q��"y�[���G���`cz�4{��x��"�>Z���!~cL�F����TiT�`�g���Gp�}�[|E��̦�A�sL���ƽ������_ƶ]�Z������������}wb�
知道发生了什么吗?请注意,退款交易已成功在PayPal中过帐。只有回应是胡言乱语。这是代码:
// Determine the proper PayPal server to POST to.
if (!empty($rules_config['server']) && $rules_config['server'] == 'sandbox') {
$host = 'https://www.sandbox.paypal.com';
$token_host = "https://api.sandbox.paypal.com/v1/oauth2/token";
}
else {
$host = 'https://www.paypal.com';
$token_host = "https://api.paypal.com/v1/oauth2/token";
}
$body = [
'amount' => [
'total' => $amount,
'currency' => $total[0]['currency_code']
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_host);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USERPWD, $client . ':' . $secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$json = json_decode($output);
if(isset($json->access_token)) {
$token = $json->access_token; // this is our PayPal access token
}
else {
drupal_set_message("Generating token failed. Please check log for details.");
}
// Setup the cURL request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.'/v1/payments/sale/'.$transaction_id.'/refund');
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
// Commerce PayPal requires SSL peer verification, which may prevent out of
// date servers from successfully processing API requests. If you get an error
// related to peer verification, you may need to download the CA certificate
// bundle file from http://curl.haxx.se/docs/caextract.html, place it in a
// safe location on your web server, and update your settings.php to set the
// commerce_paypal_cacert variable to contain the absolute path of the file.
// Alternately, you may be able to update your php.ini to point to the file
// with the curl.cainfo setting.
if (variable_get('commerce_paypal_cacert', FALSE)) {
curl_setopt($ch, CURLOPT_CAINFO, variable_get('commerce_paypal_cacert', ''));
}
$response = curl_exec($ch);
print_r($response);