通过PayPal RefundTransaction NVP API发出部分退款后,IPN监听器仅显示付款状态为"退款,"不是" Partially_Refunded"

时间:2015-09-08 02:39:20

标签: paypal paypal-ipn paypal-nvp

当我使用RefundTransaction API操作在我的网站上发出部分退款时,退款会成功处理。但是,我的IPN侦听器不断收到此事务的付款状态Refunded。我不确定为什么它不是Partially_Refunded

我已经使用PayPal的IPN模拟器测试了部分退款,我的IPN监听器每次都会在这些测试中返回Partially_Refunded

这是我的IPN侦听器文件的开头:

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}


// STEP 2: POST IPN data back to PayPal to validate

$ch = curl_init($paypal_url);
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'));

// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set 
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    //mscampMail($my_email, 'MSCamp curl error', "Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // The IPN is verified, process it:
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process the notification

    require_once ('includes/mysql_connect.php');

    // Get payment status & parent_txn_id if refund
    $payment_status = escape_data($_POST['payment_status']);

    // Cart Items
    $num_cart_items = isset($_POST['num_cart_items']) ? $_POST['num_cart_items'] : '';

    $txn_id = escape_data($_POST['txn_id']);
    $user_id = escape_data($_POST['custom']);
    $order_total = escape_data($_POST['mc_gross']);
    $shipping_fee = escape_data($_POST['mc_handling']);
    $first_name = escape_data($_POST['first_name']);
    $last_name = escape_data($_POST['last_name']);

    // For guest orders, need name & address info for shipping
    $guest = $user_id == 0 ? 'guest' : '';
    $address_street = escape_data($_POST['address_street']);
    $address_city = escape_data($_POST['address_city']);
    $address_state = escape_data($_POST['address_state']);
    $address_zip = escape_data($_POST['address_zip']);

将部分退款数据发布到PayPal的功能:

function PPHttpPost($methodName_, $nvpStr_, $env) {
 global $live;
 // Set up your API credentials, PayPal end point, and API version.


 if("sandbox" === $env)
    $API_Endpoint = "https://api-3t.$env.paypal.com/nvp";
 else
    $API_Endpoint = "https://api-3t.paypal.com/nvp";

 $version = urlencode('122');

 // Set the curl parameters.
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);

 // Turn off the server and peer verification (TrustManager Concept).
 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.
 $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";

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

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

 if(!$httpResponse) {
    exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
 }

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

 $httpParsedResponseAr = array();
 foreach ($httpResponseAr as $i => $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;
}

最后,当用户启动部分退款时,来自调用上述功能的页面中的代码:

if ($live === false) $env = "sandbox";

// Set request-specific fields.
$item = urlencode($refund_detail['item']);
$amount = urlencode($refund_detail['cost']);
$inventory_num = urlencode($order_detail_id_for_refund);
$transactionID = urlencode($refund_detail['paypal_txn_id']);
$refundType = urlencode('Partial');     // or 'Partial'
$memo = urlencode("Refund of ".$refund_detail['item']);     // required if Partial.
$currencyID = urlencode('USD');         // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

// Add request-specific fields to the request string.
$nvpStr = "&L_INVOICEITEMNAME0=$item&L_SKU0=$inventory_num&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.');
    }
}

由于我的客户需要能够在网站上处理部分退款,因此我们非常感谢任何正确方向的建议或推动。我已经搜索了Stackoverflow和谷歌获取更多信息无济于事。感谢。

1 个答案:

答案 0 :(得分:1)

麦克,

payment_status = Refunded txn将有一个parent_txn_id字段,其中包含“部分”退款的交易值。

假设您已将原始付款存储在数据库中,则IPN侦听器可以查找原始事务,并将该记录中的mc_gross与当前IPN txn中的相同字段进行比较。如果不同,则为“部分”退款。