Issue a refund

DocsCurrentAPI

Last updated: Jan 7th, 8:14am

Refund a captured payment from a seller back to a buyer.

Know before you code

To use this integration you must:

Generate PayPal Auth Assertion header

Pass the PayPal-Auth-Assertion and the PayPal-Partner-Attribution-Id headers with the standard Content-Type, Authorization, and PayPal-Request-ID headers. On the client side, you can generate the value of the PayPal-Auth-Assertion header as follows:

  1. JavaScript
  2. Node.js
  3. Java
1function encodeObjectToBase64(object) {
2 const objectString = JSON.stringify(object);
3 return window.btoa(objectString);
4}
5
6const clientId = "CLIENT-ID";
7const sellerPayerId = "SELLER-PAYER-ID"; // preferred
8// const sellerEmail = "SELLER-ACCOUNT-EMAIL"; // use instead of payer-id if required
9
10const header = {
11 alg: "none"
12};
13const encodedHeader = encodeObjectToBase64(header);
14
15const payload = {
16 iss: clientId,
17 payer_id: sellerPayerId
18 // email: sellerEmail
19};
20const encodedPayload = encodeObjectToBase64(payload);
21
22const jwt = `${encodedHeader}.${encodedPayload}.`; // json web token
23console.log(`Paypal-Auth-Assertion=${jwt}`);

Modify the code

Copy the code and modify the following:

  • Use the client ID of the platform or marketplace from the PayPal Developer dashboard for clientId.
  • In the given example, the sellerPayerId is the payer ID of the receiving seller's PayPal account. You can also use email instead of payer_id and supply the email address of the seller's PayPal account.

Make refund request

To refund an order, use the /v2/payments/captures/{CAPTURE-ID}/refund endpoint. The CAPTURE-ID can be read from the purchase_units/payments/captures/id field of the order you want to refund.

  1. Full refund
  2. Partial refund
1curl -v -X POST https://api-m.sandbox.paypal.com/v2/payments/captures/2GG279541U471931P/refund \
2 -H 'Content-Type: application/json' \
3 -H 'Authorization: Bearer ACCESS-TOKEN' \
4 -H 'PayPal-Partner-Attribution-Id: BN-CODE' \
5 -H 'PayPal-Auth-Assertion: AUTH-ASSERTION-JWT' \
6 -H 'PayPal-Request-Id: REQUEST-ID' \
7 -d '{}'

For a full refund, include an empty payload in the JSON request body.

For a partial refund, include the amount object in the JSON request body. You can also issue multiple partial refunds up to the total captured amount. If you are unsure how much captured amount is remaining to be refunded after one or more partial refunds, make the API call with the total captured amount or leave the amount field blank. The API will automatically calculate and issue the refund for the remaining value.

Modify the code

Copy the code and modify the following:

  • Change ACCESS-TOKEN to your access token.
  • Change BN-CODE to your PayPal Attribution ID to receive revenue attribution. To find your BN code, see Code and Credential Reference.
  • Change AUTH-ASSERTION-JWT to your PayPal-Auth-Assertion token.
  • Change REQUEST-ID to a set of unique alphanumeric characters such as a timestamp to prevent duplicate transactions.

Step result

A successful request returns the HTTP 201 Created status code. If you didn't receive a response, making the same API call without changing the request should result in an HTTP 200 OK with a confirmation of the refund.

Next steps

Refund Resource

For more information about the refunds API, see the Payment API.

Integration Checklist

Go through the integration checklist before you go live.