Search code examples
phpcurlpaypalpaypal-nvp

payapl nvp api (refund) working when using via a form but not working with curl


I'm trying to use the refund API of Paypal. I am currently using sandbox accounts. payment is working fine. refund is working fine when I use form fields to post the data to paypal like below

 <form action="https://api-3t.sandbox.paypal.com/nvp" METHOD="GET">
        <input type="text" name="USER" value="**********"/>
        <input type="text" name="PWD" value="**********"/>
        <input type="text" name="SIGNATURE" value="**********"/>
        <input type="text" name="METHOD" value="RefundTransaction"/>
        <input type="text" name="VERSION" value="94"/>
        <input type="text" name="TRANSACTIONID" value="<?php echo $data['transaction_id']; ?>"/>
        <input type="text" name="REFUNDTYPE" value="Partial"/>
        <input type="text" name="AMT" value="<?php echo $amount; ?>"/>
        <input type="text" name="CURRENCYCODE" value="GBP"/>
        <input type="text" name="NOTE"  value="refund"/>
        <input type="submit" value="pay"/>
    </form>

the response is printed on the screen automatically and it shows refund success. but I need to compare the ACK of the response to do other actions after refund. so I tried using curl like below.

$req = array(
    'USER' => urlencode('**************'),
    'PWD' => urlencode('***************'),
    'SIGNATURE' => ************'),
    'METHOD' => 'RefundTransaction',
    'VERSION' => urlencode(119),
    'TRANSACTIONID' => $data[transaction_id],
    'REFUNDTYPE' => 'Partial',
    'AMT' => urlencode($amount),
    'CURRENCYCODE' => 'GBP',
    'NOTE' => urlencode('test'),
    );
$ch = curl_init('https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
if(!($res = curl_exec($ch)) ) {
    error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);
echo "response is : ".urldecode($res);

curl call is made successfully, but the response is always internal error (10001) from paypal. what I am doing wrong. Please help

NOTE : I just replaced the credentials with asterisks.


Solution

  • I don't believe you need to urlencode the AMT value. It should be sent as a properly formatted decimal. Also, the version number doesn't have to be urlencoded either, and as far as I know, the current highest version is 95, not 119