Just wanted to paste a PHP solution for a Paypal refund. It was found on Paypal official site, but I spent some time to get it.
/** RefundTransaction NVP example; last modified 08MAY23. |
* Issue a refund for a prior transaction. |
$environment = 'sandbox'; // or 'beta-sandbox' or 'live' |
* @param string The API method name |
* @param string The POST Message fields in &name=value pair format |
* @return array Parsed HTTP Response body |
function PPHttpPost($methodName_, $nvpStr_) { |
// Set up your API credentials, PayPal end point, and API version. |
$API_UserName = urlencode('my_api_username'); |
$API_Password = urlencode('my_api_password'); |
$API_Signature = urlencode('my_api_signature'); |
if("sandbox" === $environment || "beta-sandbox" === $environment) { |
$version = urlencode('51.0'); |
// Set the curl parameters. |
curl_setopt($ch, CURLOPT_URL, $API_Endpoint); |
curl_setopt($ch, CURLOPT_VERBOSE, 1); |
// Turn off the server and peer verification (TrustManager Concept). |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
curl_setopt($ch, CURLOPT_POST, 1); |
// Set the API operation, version, and API signature in the request. |
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_"; |
// Set the request as a POST FIELD for curl. |
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); |
// Get response from the server. |
$httpResponse = curl_exec($ch); |
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')'); |
// Extract the response details. |
$httpResponseAr = explode("&", $httpResponse); |
$httpParsedResponseAr = array(); |
foreach ($httpResponseAr as $i => $value) { |
$tmpAr = explode("=", $value); |
$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1]; |
if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) { |
exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint."); |
return $httpParsedResponseAr; |
// Set request-specific fields. |
$transactionID = urlencode('example_transaction_id'); |
$refundType = urlencode('Full'); // or 'Partial' |
$amount; // required if Partial. |
$memo; // required if Partial. |
$currencyID = urlencode('USD'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD') |
// Add request-specific fields to the request string. |
$nvpStr = "&TRANSACTIONID=$transactionID&REFUNDTYPE=$refundType&CURRENCYCODE=$currencyID"; |
$nvpStr .= "&NOTE=$memo"; |
if(strcasecmp($refundType, 'Partial') == 0) { |
exit('Partial Refund Amount is not specified.'); |
$nvpStr = $nvpStr."&AMT=$amount"; |
exit('Partial Refund Memo is not specified.'); |
// Execute the API operation; see the PPHttpPost function above. |
$httpParsedResponseAr = PPHttpPost('RefundTransaction', $nvpStr); |
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) { |
exit('Refund Completed Successfully: '.print_r($httpParsedResponseAr, true)); |
exit('RefundTransaction failed: ' . print_r($httpParsedResponseAr, true)); |