How to Send a Refund with Paypal API – PHP Solution

By | September 27, 2013

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.

 
[code type=php]
$value) {
$tmpAr = explode(“=”, $value);
if(sizeof($tmpAr) > 1) {
$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”;

if(isset($memo)) {
$nvpStr .= “&NOTE=$memo”;
}

if(strcasecmp($refundType, ‘Partial’) == 0) {
if(!isset($amount)) {
exit(‘Partial Refund Amount is not specified.’);
} else {
$nvpStr = $nvpStr.”&AMT=$amount”;
}

if(!isset($memo)) {
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));
} else {
exit(‘RefundTransaction failed: ‘ . print_r($httpParsedResponseAr, true));
}

?>
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *