How to do partial refund in adyen payment api

Adyen Payment API is a robust platform that allows developers to process payments, refunds, and other transactions using a variety of payment methods and currencies. In this guide, we will focus on how to do a partial refund using Adyen Payment API in PHP. We will cover the steps required to install the Adyen PHP SDK, create the \Adyen\Client object, and make a refund request using the Adyen API. By the end of this guide, you will have a solid understanding of how to do a partial refund using Adyen Payment API in PHP, and how to customize the refund request to meet your specific business requirements.

here’s a full guide on how to do a partial refund using Adyen Payment API in PHP, including installing the Adyen PHP SDK and creating the \Adyen\Client object:

1. Install the Adyen PHP SDK using Composer. You can do this by running the following command in your terminal:

composer require adyen/php-api-library

This will install the Adyen PHP SDK in your project and create a vendor directory with the required files.

2. After installing the Adyen PHP SDK, you can create a new instance of the \Adyen\Client object:

require_once __DIR__ . '/vendor/autoload.php'; // Include the Composer autoload file$client = new \Adyen\Client();

3. Set up the \Adyen\Client object with your API key, environment, and merchant account:

$client->setXApiKey("YOUR_API_KEY"); // Set your API key$client->setEnvironment(\Adyen\Environment::TEST); // Set the environment (test or live)$client->setMerchantAccount("YOUR_MERCHANT_ACCOUNT"); // Set your merchant account

4. To do a partial refund, you will need to create a refund request object that includes the original pspReference and the amount to be refunded. Here’s an example of a refund request object:

$refundRequest = array(    "merchantAccount" => "YOUR_MERCHANT_ACCOUNT",    "reference" => "YOUR_ORDER_REFERENCE",    "amount" => array(        "currency" => "EUR",        "value" => 1000 // refund amount in minor units (e.g. cents)    ),    "originalReference" => "ORIGINAL_PSP_REFERENCE",    "modificationAmount" => array(        "currency" => "EUR",        "value" => 500 // partial refund amount in minor units (e.g. cents)    ));

The merchantAccount and reference fields should contain your merchant account name and an order reference that you can use to identify the refund. The amount field should contain the total amount of the original payment to be refunded, and the originalReference field should contain the pspReference of the original payment. Finally, the modificationAmount field should contain the amount to be refunded in the currency and value specified.

Note that you may need to modify the refund request object to include additional fields depending on your specific use case.

5. Once you have created the refund request object, you can make the refund request using the Adyen API client object:

$service = new \Adyen\Service\Modification($client);$result = $service->refund($refundRequest);

6. Finally, you can check the result of the refund request to ensure it was successful:

if ($result['response'] == '[refund-received]') {    // Partial refund successful} else {    // Partial refund failed}

That’s it! This is a basic example of how to do a partial refund using Adyen Payment API in PHP. Note that this guide assumes you have already set up your Adyen account and have obtained your API credentials (including your API key and merchant account name). If you have not done so, you will need to follow the Adyen documentation to obtain these credentials before you can use the API.

Comments