What is the Recommended Practice on PayPal NVP API

2019-09-14 22:40发布

问题:

So I've started this little class for doing PayPal voids, refunds, and subscription cancels. My question is, what's the recommended practice? I mean, should I attempt to void the transaction, then ignore error, then refund it, and then cancel the subscription?

Note in my case, the transaction was started from a click on a PayPal Subscription button, and then it got renewed once after the 6 month period ended, and then the customer wanted a refund and to stop further subscriptions.

<?php

class Paypp {

public static function voidCard($r) {
  $asData = array(
    'METHOD' => 'DoVoid',
    'AUTHORIZATIONID' =>  @ $r['paypal_txn_id'] // tnx_id from subscr_payment IPN
  );
  return self::_sendData($asData);
}

public static function cancelSubscription($r) {
  $asData = array(
    'METHOD' => 'ManageRecurringPaymentsProfileStatus',
    'PROFILEID' =>  @ $r['paypal_subscr_profile_id'], // subscr_id from subscr_payment IPN
    'ACTION' => 'cancel'
  );
  return self::_sendData($asData);
}

public static function refundCard($r) {
  $asData = array(
    'METHOD' => 'RefundTransaction',
    'TRANSACTIONID' =>  @ $r['paypal_txn_id'],
    'REFUNDTYPE' => 'full',
    'CURRENCYCODE' => @ $r['currency'],
    'AMT' => @ $r['gross']
  );
  return self::_sendData($asData);

}

private static function _sendData($asData) {
global $config;

  $sActive = $config->PAYMENT_GATEWAY_DATA->pp->ACTIVE;
  $sURL = $config->PAYMENT_GATEWAY_DATA->pp->$sActive->NVP_URL;

  $sVersion = $config->PAYMENT_GATEWAY_DATA->pp->$sActive->NVP_VERSION;
  $sUser = $config->PAYMENT_GATEWAY_DATA->pp->$sActive->NVP_USER;
  $sPass = $config->PAYMENT_GATEWAY_DATA->pp->$sActive->NVP_PASS;
  $sSig = $config->PAYMENT_GATEWAY_DATA->pp->$sActive->NVP_SIG;

  $asExtra = array(
    'VERSION' => $sVersion,
    'USER' => $sUser,
    'PWD' => $sPass,
    'SIGNATURE' => $sSig
  );
  $asData = array_merge($asData,$asExtra);

  $asOpt = array(
    CURLOPT_HEADER => FALSE,
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS => http_build_query($asData),
    CURLOPT_VERBOSE => TRUE,
  );

  $hCurl = curl_init($sURL);

  curl_setopt_array($hCurl, $asOpt);
  $bVerifySSL = @ $config->VERIFY_SSL;
  curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, $bVerifySSL);

  $sResponse = curl_exec($hCurl);
  curl_close($hCurl);
  return $sResponse;

}


} // end class

回答1:

Skip the void step. Voids are only valid for authorizations and orders, and you can't create either through a subscription. But yes, you'll need to cancel the subscription and refund the individual payments.



回答2:

Through trial and error in the sandbox, I found what the workflow should be:

  1. Skip void. According to a PayPal rep: "Voids are only valid for authorizations and orders, and you can't create either through a subscription."

  2. Yes, refund. It runs agains the txn_id, which is unique for every subscr_payment that you receive in the subscription process. So, you'll only refund the most recent subscr_payment you received on your IPN.

  3. Yes, cancel subscription -- the refund alone won't do that step for you.