2013-11-27 151 views
0

目前我正在使用paypal NVP API,paypal API退款

我們的客戶有一個paypal賬戶。每個客戶都已經允許我退還這些命令。爲此,他們添加了我的API paypal帳戶的名稱,他們檢查選項:對特定交易發出退款。

他們向我提供每個訂單的交易ID,我的申請應該退款。

這裏是我的方案的摘要:

url = 'https://api-3t.paypal.com/nvp' 
params = { 
    'USER':'name of my api', 
    'PWD': 'pass word of my api', 
    'SIGNATURE':'my signature' 
    } 
params['METHOD'] = 'RefundTransaction' 
params['VERSION'] = 94 
params['TRANSACTIONID'] = transaction_id 
params['currencyCode'] = currency 
params['REFUNDTYPE'] = 'Full' 
http.post(url,params) 

,但在執行結束返回我:

{'res': 'TIMESTAMP=2013%2d11%2d26T15%3a43%3a16Z&CORRELATIONID=848a8035cc65&ACK=Failure& VERSION=51%2e0&BUILD=8620107&L_ERRORCODE0=10007&L_SHORTMESSAGE0=Permission%20denied&L_LONGMESSAGE0=You%20do%20not%20have%20permission%20to%20refund%20this%20transaction&L_SEVERITYCODE0=Error', 'code': 200} 

這意味着我沒有permession此交易,儘管我們的客戶已將我加入他們的PayPal賬戶。

我該如何解決這個問題?

回答

0

如果這意味着要退款Live,請確保您使用的是正確的API endpoint

如果這是對Sandbox的退款交易,請確保使用的憑證來自您的測試賣家帳戶沙盒。

0

當某人授予您第三方API權限時,他們授予您以他們的名義進行API調用的權限。
由於您代表某人調用了RefundTransaction API,因此您需要指定SUBJECT參數並使用該人員帳戶的PayPal電子郵件地址填充該參數。

I.e.

params = { 
    'USER':'name of my api', 
    'PWD': 'pass word of my api', 
    'SIGNATURE':'my signature' 
    'SUBJECT':'email of PP account who granted you 3rd party permissions' 
    } 

因爲你現在不指定這個權利,你基本上試圖退還不屬於自己的PayPal帳戶TRANSACTIONID。所以它正確地否認你這樣做。

1

這裏是最好的工作示例與NVP到退款

class PayPalRefund 
{ 
private $API_Username, $API_Password, $Signature, $API_Endpoint, $version; 
function __construct($intializeData) 
{ 

    if($intializeData['mode'] == "live") 
    { 
     $this->API_Endpoint = "https://api-3t.paypal.com/nvp"; 
    }else{ 
     $this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp"; 
    } 
    $this->API_Username = $intializeData['username']; 
    $this->API_Password = $intializeData['password']; 
    $this->Signature = $intializeData['signature']; 
    $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; 
} 

} 

一旦類是準備好了,你可以調用函數在您的業務邏輯

 require_once('PaypalRefund.php'); 
     /* Refund Type ('Partial', 'Full')*/ 
     $intializeData = array('email'=>$this->credentials->email, 
           'username'=>$this->credentials->username, 
           'password'=>$this->credentials->password, 
           'signature'=>$this->credentials->signature, 
           'mode'=>'sandbox', //'live' 
          ); 
     $aryData['transactionID'] = $data['transaction_id']; 
     $aryData['refundType'] = "Full"; //Partial or Full 
     $aryData['currencyCode'] = $data['currency_code']; 
     $aryData['amount'] = $data['amount']; //$data['amount']; 
     $aryData['memo'] = $data['notes']; 


     // Paypal Refund API Call 
     $ref = new PaypalRefund($intializeData); 
     $aryRes = $ref->refundAmount($aryData); 
     echo "<pre>"; print_r($aryRes);echo "</pre>";die; 

你會得到響應的前期以及上IPN URL(如果設置)。

謝謝