贝宝 API 退款

Posted

技术标签:

【中文标题】贝宝 API 退款【英文标题】:paypal API refund 【发布时间】:2013-12-12 20:26:38 【问题描述】:

目前我正在研究 paypal NVP API,

我们的客户有一个贝宝帐户。每个客户都允许我退还这些命令。为此,他们添加了我的 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

这意味着我没有参与此交易,而我们的客户已将我添加到他们的贝宝帐户。

我该如何解决这个问题?

【问题讨论】:

【参考方案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;



一旦 Class 准备就绪,您就可以在业务逻辑中调用函数

        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(如果已设置)上预先获得响应。

谢谢

【讨论】:

【参考方案2】:

如果这是实时退款,请确保您使用的是正确的 API endpoint

如果这是对沙盒测试环境的退款交易,请确保使用的凭据来自您的测试卖家帐户沙盒。

【讨论】:

【参考方案3】:

当有人授予您第三方 API 权限时,他们授予您代表他们进行 API 调用的权限。 由于您代表某人调用RefundTransaction API,因此您需要指定SUBJECT 参数并使用该人帐户的PayPal 电子邮件地址填充它。

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。所以它正确地拒绝你这样做。

【讨论】:

以上是关于贝宝 API 退款的主要内容,如果未能解决你的问题,请参考以下文章

退款状态 NO_API_ACCESS_TO_RECEIVER,贝宝响应

如何使用自适应贝宝 API 退还部分金额

如何使用贝宝以编程方式退款?

贝宝并行付款和退款

无法在沙盒中测试付款撤销 webhook 事件。贝宝 REST API

从贝宝转账到信用卡