PayPal退款交易问题

时间:2014-05-23 07:37:12

标签: php paypal

我正在使用PayPal退款支付网关。我的主要目标是向用户退还金额,同时我必须向其他用户支付一定金额。我在这里担任管理员,假设我有150美元通过PayPal记入我的账户。我有它的交易ID。现在我需要将假设100 $返回给user1,我需要向user2发送20 $。

                 <?php

/**
 *
 *
 * This PayPal API provides the functionality of Refunding Amount.
 * Credentials are omitted from here for privacy purpose. To use it credentials are compulsory to provide.
 */
class PayPalRefund
{
    private $API_Username, $API_Password, $Signature, $API_Endpoint, $version;
    function __construct($mode = "sandbox")
    {
        if($mode == "live")
        {
            $this->API_Username = "sample@gmail.com";
            $this->API_Password = "137301275897";
            $this->Signature = "AoNBG1CB1212IgLS5QaKlVpODjsaTncPABthzh5N-Nzz511tOodumbgF0TvVDq";
            $this->API_Endpoint = "https://api-3t.paypal.com/nvp";
        }
        else
        {
            $this->API_Username = "sample@gmail.com";
            $this->API_Password = "137301275897";                
            $this->Signature = "AoNBG1CB1212IgLS5QaKlVpODjsaTncPABthzh5N-Nzz5t11OodumbgF0TvVDq";
            $this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
        }
        $this->version = "51.0";
    }

    /**
     * This function actually Sends the CURL Request for Refund
     * @param string - $requestString
     * @return array - returns the response
     */
    function sendRefundRequest($requestString)
    {
        $this->API_UserName  = urlencode($this->API_Username);
        $this->API_Password  = urlencode($this->API_Password);
        $this->API_Signature = urlencode($this->Signature);

        $this->version = urlencode($this->version);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        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.
        $reqStr = "METHOD=RefundTransaction&VERSION={$this->version}&PWD={$this->API_Password}&USER={$this->API_UserName}&SIGNATURE={$this->API_Signature}$requestString";

        // Set the request as a POST FIELD for curl.
        curl_setopt($ch, CURLOPT_POSTFIELDS, $reqStr);

        // Get response from the server.
        $curlResponse = curl_exec($ch);

        if(!$curlResponse)
            return array("ERROR_MESSAGE"=>"RefundTransaction failed".curl_error($ch)."(".curl_errno($ch).")");

        // Extract the response details.
        $httpResponseAr = explode("&", $curlResponse);

        $aryResponse = array();
        foreach ($httpResponseAr as $i => $value)
        {
            $tmpAr = explode("=", $value);
            if(sizeof($tmpAr) > 1)
            {
                $aryResponse[$tmpAr[0]] = urldecode($tmpAr[1]);
            }
        }

        if((0 == sizeof($aryResponse)) || !array_key_exists('ACK', $aryResponse))
            return array("ERROR_MESSAGE"=>"Invalid HTTP Response for POST request ($reqStr) to {$this->API_Endpoint}");

        return $aryResponse;
    }

    /**
     * @param array $aryData
     * @return array
     */
    function refundAmount($aryData)
    {
        if(trim(@$aryData['currencyCode'])=="")
            return array("ERROR_MESSAGE"=>"Currency Code is Missing");
        if(trim(@$aryData['refundType'])=="")
            return array("ERROR_MESSAGE"=>"Refund Type is Missing");
        if(trim(@$aryData['transactionID'])=="")
            return array("ERROR_MESSAGE"=>"Transaction ID is Missing");

        $requestString = "&TRANSACTIONID={$aryData['transactionID']}&REFUNDTYPE={$aryData['refundType']}&CURRENCYCODE={$aryData['currencyCode']}";

        if(trim(@$aryData['invoiceID'])!="")
            $requestString = "&INVOICEID={$aryData['invoiceID']}";

        if(isset($aryData['memo']))
            $requestString .= "&NOTE={$aryData['memo']}";

        if(strcasecmp($aryData['refundType'], 'Partial') == 0)
        {
            if(!isset($aryData['amount']))
            {
                return array("ERROR_MESSAGE"=>"For Partial Refund - It is essential to mention Amount");
            }
            else
            {
                $requestString = $requestString."&AMT={$aryData['amount']}";
            }

            if(!isset($aryData['memo']))
            {
                return array("ERROR_MESSAGE"=>"For Partial Refund - It is essential to enter text for Memo");
            }
        }

        $resCurl = $this->sendRefundRequest($requestString);
        return $resCurl;
    }
}
  ?>

和                

/**
 *
 *
 * This PayPal API provides the functionality of Refunding Amount.
 * Credentials are omitted from here for privacy purpose. To use it credentials are compulsory to provide.
 */

/*
 * Currency Types
 * ('USD', 'GBP', 'EUR', 'JPY', 'CAD', 'AUD')
 *
 * Refund Type
 * ('Partial', 'Full')
 *
 * Transaction ID
 * We can get the Transaction ID from IPN Response
 */

/*
 * Partial Refund
 */    
$aryData['transactionID'] = "7H123X43620BH3959058";
$aryData['refundType'] = "Partial"; //Partial or Full
$aryData['currencyCode'] = "USD";
$aryData['amount'] = 2.00;
$aryData['memo'] = "There Memo Detail entered for Partial Refund";
   // $aryData['invoiceID'] = "xxxxxxxxxx";

 //echo "<pre>";
 //print_r($aryData);die;
$ref = new PayPalRefund("sandbox");
$aryRes = $ref->refundAmount($aryData);

if($aryRes['ACK'] == "Success")
    echo "Amount Refunded Successfully";
else
    echo "Error Refunding Amount";

echo "<pre>";
print_r($aryRes);
echo "</pre>";




            ?>

我收到错误退款金额:

              Error Refunding Amount
            Array
          (
                   [TIMESTAMP] => 2014-05-23T07:13:21Z
                    [CORRELATIONID] => f42e83df6f52b
                   [ACK] => Failure
                   [VERSION] => 51.0
                  [BUILD] => 11110362
                  [L_ERRORCODE0] => 10007
                  [L_SHORTMESSAGE0] => Permission denied
                  [L_LONGMESSAGE0] => You do not have permission to refund this transaction
                  [L_SEVERITYCODE0] => Error
         )

1 个答案:

答案 0 :(得分:0)

您需要获得收到资金的用户的许可,然后代表他发出退款。

阅读this以了解具体方法。

变化

"scope":"INVOICING"

"scope":"REFUND"