S
ShadhinPay Docs

Error Codes

Understanding and handling ShadhinPay API errors

Error Codes

This guide explains the error codes returned by the ShadhinPay API and how to handle them.

Error Response Format

All errors follow this structure:

Error Response
{
  "success": false,
  "error": {
    "code": "INVALID_AMOUNT",
    "message": "Amount must be greater than 10 BDT",
    "details": {
      "field": "amount",
      "min_value": 10
    }
  }
}

HTTP Status Codes

CodeDescription
200Success
201Created
400Bad Request - Invalid parameters
401Unauthorized - Invalid credentials
403Forbidden - Insufficient permissions
404Not Found - Resource doesn't exist
409Conflict - Resource already exists
422Unprocessable - Validation failed
429Too Many Requests - Rate limited
500Internal Server Error

Authentication Errors

CodeHTTPDescription
MISSING_CLIENT_ID401Client-Id header is required
INVALID_CLIENT_ID401Client-Id is invalid
MISSING_BUSINESS_ID401Business-Id header is required
INVALID_BUSINESS_ID401Business-Id is invalid
MISSING_SECRET_KEY401Secret-Key header is required
INVALID_SECRET_KEY401Secret-Key is invalid
BUSINESS_NOT_AUTHORIZED403Business doesn't have access

Payment Errors

CodeHTTPDescription
INVALID_AMOUNT400Amount is invalid or below minimum
INVALID_CURRENCY400Currency not supported
INVALID_PHONE400Phone number format is invalid
PAYMENT_NOT_FOUND404Payment doesn't exist
PAYMENT_ALREADY_COMPLETED409Payment was already processed
PAYMENT_EXPIRED410Payment link has expired
REFUND_EXCEEDS_AMOUNT400Refund amount exceeds payment
REFUND_NOT_ALLOWED400Payment cannot be refunded

Invoice Errors

CodeHTTPDescription
INVOICE_NOT_FOUND404Invoice doesn't exist
INVOICE_ALREADY_PAID409Invoice was already paid
INVALID_LINE_ITEMS400Line items are invalid
INVALID_DUE_DATE400Due date is in the past

Rate Limit Errors

CodeHTTPDescription
RATE_LIMIT_EXCEEDED429Too many requests

When rate limited, check the Retry-After header:

Rate Limit Response
{
  "success": false,
  "error": {
    "code": "RATE_LIMIT_EXCEEDED",
    "message": "Too many requests. Please retry after 60 seconds",
    "details": {
      "retry_after": 60
    }
  }
}

Handling Errors

JavaScript

Error Handling
async function createPayment(paymentData) {
  try {
    const response = await fetch('/api/v1/payments', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Client-Id': CLIENT_ID,
        'Business-Id': BUSINESS_ID,
        'Secret-Key': SECRET_KEY,
      },
      body: JSON.stringify(paymentData),
    });

    const data = await response.json();

    if (!data.success) {
      // Handle specific error codes
      switch (data.error.code) {
        case 'INVALID_AMOUNT':
          console.error('Amount too low:', data.error.details.min_value);
          break;
        case 'RATE_LIMIT_EXCEEDED':
          const retryAfter = data.error.details.retry_after;
          console.log(`Rate limited. Retry in ${retryAfter}s`);
          break;
        default:
          console.error('Error:', data.error.message);
      }
      return null;
    }

    return data.data;
  } catch (error) {
    console.error('Network error:', error);
    return null;
  }
}

Python

Error Handling
import requests
import time

def create_payment(payment_data):
    try:
        response = requests.post(
            'https://api.shadhinpay.com/api/v1/payments',
            headers={
                'Content-Type': 'application/json',
                'Client-Id': CLIENT_ID,
                'Business-Id': BUSINESS_ID,
                'Secret-Key': SECRET_KEY,
            },
            json=payment_data
        )

        data = response.json()

        if not data['success']:
            error = data['error']

            if error['code'] == 'RATE_LIMIT_EXCEEDED':
                retry_after = error['details']['retry_after']
                time.sleep(retry_after)
                return create_payment(payment_data)  # Retry

            raise Exception(f"{error['code']}: {error['message']}")

        return data['data']

    except requests.exceptions.RequestException as e:
        print(f"Network error: {e}")
        return None

Best Practices

  1. Always check success - Don't assume requests succeed
  2. Handle specific codes - Different errors need different handling
  3. Implement retries - For rate limits and temporary failures
  4. Log errors - Store error details for debugging
  5. Show user-friendly messages - Don't expose raw error codes to users

On this page