Multiple refunds on same payment id

I’ve done some tests with RefundPaymentRequest and I’m trying to make multiple refunds on the same order. In the documentation, it says that up to 20 refunds can be make for a payment id.

When I try to do a 2nd refund with a different amount (using same payment id & idem key), I get the following error: [INVALID_REQUEST_ERROR] Refund request idempotency key was previously used for a different amount

If I do it again with the same amount as the first refund, it works but it shows the same refund id. It’s like it only allows a single refund.

$body = new \Square\Models\RefundPaymentRequest($idempotencyKey, $amount_money);
$body->setReason($reason);
$body->setPaymentId($payment_id);
$api_response = $client->getRefundsApi()->refundPayment($body);

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
https://developer.squareup.com/docs/payments-api/refund-payments

Additional Documentation

https://developer.squareup.com/docs/payments-api/collect-fees/payment-with-app-fee-refund
Pay or Refund Invoices
Troubleshoot the Payments API

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The error you’re encountering is due to the use of the same idempotency key for different refund amounts. The idempotency key is used to prevent duplicate requests. If you use the same idempotency key for a different refund amount, the system will throw an error.

When you’re issuing multiple refunds against the same payment, you should use a unique idempotency key for each refund request, even if the payment ID is the same. This way, each refund request is treated as a unique operation.

In your case, when you tried to refund with the same amount as the first refund, it didn’t create a new refund but returned the details of the first refund because of the same idempotency key.

So, to make multiple refunds on the same order, ensure to generate a unique idempotency key for each refund request.

This answer was reviewed by @Bryan-Square.

Ah I get it, I was using the payment’s idempotency key instead of generating new ones for the refunds. It’s all good now.

Many thanks!