Refund API for Paypal Adaptive Payments through payKey

Kiran Kashyap March 8, 2019

Refund API for Paypal Adaptive Payments through payKey

Hey techies

Today, I am going to share with you what I learned about “REFUND API for Paypal Adaptive payments through payKey” while working on a WordPress project. Before we go ahead, let’s see what “Paypal Adaptive payment” is and when do we need to use it.

We use the “Paypal adaptive payment” method when we need to split the payment among multiple receivers or accounts. Paypal adaptive payment has 2 modes namely Chain Payments and Parallel Payments.

Chained Payments:

Chained payments mode facilitates kind of transactions wherein a single payment has to be split among multiple receivers. Let’s say you are admin of an e-commerce website and you have multiple vendors at your site. You want to split the payment for one particular deal in a way that you (primary receiver) would keep 15% of the total amount paid, as commission, and pass on the remaining 85% to your vendor (secondary receiver). Chained payments make such transactions happen automatically, without any hassles.

Delayed Chained Payments:

Delayed Chained Payments mode is used when we want to delay the payment to a secondary receiver. The maximum time for which you can delay payment is 90 days.

Let’s understand this payment mode with the help of an example. Once again, suppose you are admin of an e-commerce website and your policy says that your vendors should be paid only after ‘product return or replacement period’ is over. Considering the return/replacement period being 7 days, your vendor should receive his share of money out of that particular deal after 7 days. Delayed Chained Payments enables you to execute this kind of transaction in a smooth manner.

Parallel Payments:

Parallel Payments mode facilitates kind of transactions wherein a single payment has to be split among a number of accounts, in one go. One particular payment can be split into a maximum of 6 accounts using Parallel Payments mode.

I was using Chained payment on my website as my requirement was to pay 10% of the amount to the admin and 90% to the vendor. It depends on your application requirement how much value do you want to get in terms of payment. For refund I used payKey.

Here is the example of a code using curl and PayPal sandbox environment:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://svcs.sandbox.paypal.com/AdaptivePayments/Refund/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "requestEnvelope.errorLanguage=en_US&payKey=$payKey");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "X-Paypal-Security-Userid:your-paypal-id";
$headers[] = "X-Paypal-Security-Password:your-password";
$headers[] = "X-Paypal-Security-Signature:your-signature";
$headers[] = "X-Paypal-Request-Data-Format:NV";
$headers[] = "X-Paypal-Response-Data-Format:JSON";
$headers[] = "X-Paypal-Application-Id:APP-80W284485P519543T";
$headers[] = "Content-Type:text/value";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
	echo 'Error:' . curl_error($ch);
}

curl_close ($ch);

In the above-written code, refundTransactionStatus variable in the result will show if your transaction was successful or refund had already been paid.

You need to pass the user id, password, and signature of your PayPal account in the headers variable. Also, you need to generate an application ID by filling the same details and then submit your application. Paypal takes a while to look into application details and once they approve, they generate application ID for the application. For the sandbox environment, you can use “APP-80W284485P519543T” as an application ID.

Please feel free to seek any further clarification.

Do share this article with your friends/co-workers if you find it helpful!

Happy Coding !! 🙂

Lets’s Talk

About your ideas and concept