Error Handling Best Practices
How to manage errors effectively in PayOS integrations
Overview
When integrating with PayOS, it’s important to implement robust error handling to ensure a seamless payment experience. PayOS offers automatic retries for specific types of failures, provides detailed error codes, and uses webhooks to notify merchants of transaction statuses. This document outlines best practices for managing errors effectively.
Key Error Types
-
Client Errors (4xx):
- These errors occur due to invalid requests from the merchant (e.g., missing parameters, invalid data).
- Examples:
400 Bad Request
: Incorrect or missing request parameters.401 Unauthorized
: Invalid or missing API key.404 Not Found
: Resource not available (e.g., transaction ID does not exist).
-
Server Errors (5xx):
- These errors are caused by issues on PayOS’s side.
- Examples:
500 Internal Server Error
: Temporary system failure.503 Service Unavailable
: PayOS service is temporarily down or under maintenance.
-
Payment-Specific Errors:
- These errors occur during payment processing and could result from card issues, insufficient funds, or expired payment methods.
- Examples:
402 Payment Declined
: Card issuer declined the payment.409 Duplicate Transaction
: A transaction with the same ID has already been processed.
Automatic Retry Mechanism
PayOS employs an automatic retry system for certain transient errors, such as timeouts or temporary PSP unavailability.
-
How it Works:
- If the first payment attempt fails due to a recoverable issue (e.g., PSP downtime), PayOS will retry the payment automatically up to three times within a set period.
- If the retry is successful, the merchant is notified via webhook.
- If retries fail, PayOS sends a final failure notification.
-
No Merchant Intervention Needed:
- Merchants do not need to manually trigger retries. PayOS will manage the retry logic internally.
Example Error Response
Here is an example of a typical error response from the PayOS API:
Handling Errors in Your Integration
-
Monitor Webhook Notifications:
- Use webhooks to stay informed about payment statuses and errors. Always implement retry logic for webhook failures on your side.
-
Implement Graceful Error Messages:
- Display user-friendly error messages when a payment fails. Avoid showing raw error codes to end-users.
-
Log All Errors for Debugging:
- Log errors with detailed context for troubleshooting. Include request IDs, timestamps, and transaction data.
-
Use Exponential Backoff for Retries:
- When retrying failed API requests, use exponential backoff to avoid overwhelming the system.
Error Codes and Suggested Actions
Webhook-Triggered Error Responses
When a payment attempt fails, PayOS sends a failure notification via webhook. Example webhook payload for a failed payment:
Conclusion
Effective error handling is essential for maintaining a smooth payment experience. By leveraging PayOS’s automatic retries, monitoring webhook notifications, and implementing clear error messages, merchants can minimize disruptions and ensure better customer satisfaction.