- Customer returns a product or requests a refund from a service
- Customer cancels an order after payment
- Customer requests a partial refund
Prerequisites
- Complete the quick start PayPal integration.
- You have a capture ID from the original payment transaction.
- You have a database or system to track payment and refund history.
Integrate server side
Add the following to your existing server file from the quick start integration.Test endpoints
Best practices
Use the following best practices to ensure refunds are processed safely, accurately, and in compliance with operational and regulatory requirements.- Store capture IDs: Always save capture IDs from successful payments in your database for future refunds.
- Use idempotency keys: Use idempotency keys when processing refunds to avoid duplicate refunds in case of network issues.
- Validate refund amounts: Check the refund amount doesn’t exceed original payment or remaining refundable balance.
- Log all refunds: Keep an audit trail of who initiated refunds, when, and why. This is critical for compliance.
- Provide refund notes: Include clear explanation in the
note_to_payerfield for customer clarity. - Handle partial refunds: Track cumulative refunded amounts to prevent over-refunding.
- Process refunds within 180 days: Process refunds within 180 days of the original capture date. Your customer’s bank may have shorter windows. After extended periods, manual intervention through PayPal support may be required.
- Implement approval workflows: Processed refunds cannot be cancelled. Build approval workflows for refunds over a certain threshold.
- Have backup manual refund process: Have a manual refund process as backup. Log failed attempts and escalate to PayPal support with the error details.
Important details
- Find capture IDs: The capture ID is returned as
capture.result.idwhen you capture a payment. Store this value in your database immediately. You’ll need it for any future refunds on that transaction. - Refund processing time: In sandbox mode, refunds complete instantly. In production, customers see refunds in their account within 3-5 business days, though timing varies by payment method.
- Webhook notifications: Set up webhooks to receive
PAYMENT.CAPTURE.REFUNDEDevents for real-time status updates. Always verify webhook signatures for security. - No refunds to different payment methods: Refunds always go back to the customer’s original payment method.
- No currency conversion: Always refund in the same currency as the original payment. PayPal handles any exchange rate adjustments automatically.
Test your integration
Make sure you have sandbox account credentials for both buyer and seller roles. Complete a test payment to get a valid capture ID.Standard testing
| Test scenario | Setup | Expected result |
|---|---|---|
| Full refund success | Default settings | Entire payment amount refunded. |
| Partial refund success | Default settings | Specified amount refunded. |
| Multiple partial refund success | Default settings | Each refunds succeeds until limit. |
| Invalid capture ID | Fake IDL XXX123 | 404 error: capture not found. |
| Refund after 3 days | Wait 3 days | Success if within 180 days |
Negative testing
For negative testing:- Make sure to enable negative testing in your sandbox business account as described in the quick start prerequisites.
- In the
.envfile, setENABLE_NEGATIVE_TESTING=trueand setNEGATIVE_TEST_TYPEto one of the error codes in the table. - Restart the server after changing the
.envfile:node server.js.
| Test scenario | Error code | Expected result |
|---|---|---|
| Exceed original amount | REFUND_AMOUNT_EXCEEDED | Error: refund amount exceeds capture. |
| Already fully refunded | CAPTURE_FULLY_REFUNDED | Error: already fully refunded. |
| Refund after 180 days | REFUND_NOT_ALLOWED_AFTER_180_DAYS | Error: refund period expired. |
| Permission denied | PERMISSION_DENIED | 403 error: no refund permission. |
| Internal server error | INTERNAL_SERVER_ERROR | Error: 500 error occurred at refund. |
Go-live checklist
- Test full and partial refunds in sandbox.
- Implement refund approval workflow.
- Set up refund logging and audit trail.
- Add authentication to refund endpoints.
- Configure refund permission roles.
- Set up webhook listeners for refund events.
- Test with real $1 payment and refund it.
Post-launch monitoring
These values are suggested monitoring thresholds for your integration, not performance guarantees from PayPal.| Metric | Target | Action if below target |
|---|---|---|
| Refund success rate | 98% | Check API errors and validate capture IDs. |
| Refund processing time | <5 seconds | Optimize database queries. |
| Failed refund rate | <2% | Review error logs, check amounts. |
| Refund-to-payment ratio | <5% | Analyze if high - may indicate quality issues. |
| API response time | <2 seconds | Check PayPal API status. |