Payment Processing: Error Handling

Robust error handling is crucial for providing a good user experience with payment processing. This guide covers the error types, handling strategies, and best practices for the ConnexPay SDK v2.

Overview

The SDK provides structured error information with categories, messages, and field-specific details to help you handle different error scenarios appropriately.

Error Structure

Error Interface

interface UserError {
    category: 'VALIDATION_ERROR' | 'PAYMENT_ERROR';
    message: string;
    fields?: FieldError[];
    originalError?: any;
}

interface FieldError {
    field: string;  // Credit: 'cardNumber' | 'expirationDate' | 'securityCode'
                    // ACH: 'nameOnAccount' | 'accountType' | 'routingNumber' | 'accountNumber' | 'confirmAccountNumber'
                    // General: 'form'
    message: string;
}

Errors surface in two ways:

  • The error event receives structured UserError objects.
  • Rejected confirmPayment() / verifyPayment() / authOnly() promises reject with the error message string, or with an array of FieldError objects for form validation failures. authOnly() also rejects with an Error when the selected payment method isn't a keyed card.

Error Categories

CategoryDescriptionWhen It Occurs
VALIDATION_ERRORForm field validation failuresInvalid card number, expired card, missing CVV
PAYMENT_ERRORPayment processing failuresNetwork issues, declined cards, API errors

Error Event Handling

Basic Error Listening

// Listen for all error events
connexpay.on('error', (error) => {
    console.error('ConnexPay Error:', error);
    handleError(error);
});

function handleError(error) {
    switch (error.category) {
        case 'VALIDATION_ERROR':
            handleValidationError(error);
            break;
            
        case 'PAYMENT_ERROR':
            handlePaymentError(error);
            break;
            
        default:
            handleGenericError(error);
    }
}

Payment Errors

Payment Processing Errors

Handle errors that occur during payment processing:

function handlePaymentError(error) {
    console.log('Payment error:', error.message);
    
    // Common payment error handling
    switch (error.message) {
        case 'No payment method has been selected':
            showPaymentMethodPrompt();
            break;
            
        case 'Apple Pay: User canceled.':
            // Don't show error for user cancellation
            console.log('User canceled Apple Pay');
            break;
            
        case 'Payment already performed':
            showErrorAndRedirect('This payment has already been completed', '/payment-status');
            break;
        
        default:
            showGenericPaymentError(error.message);
    }
}

Did this page help you?