๐Ÿงช 20 Minute Guide - Trending

Testing Best Practices

Learn how to thoroughly test your integration using our sandbox environment and automated testing tools.

1

Sandbox Environment

Use our sandbox for safe testing without real transactions.

// Configure SDK for testing
const gopaynow = new GoPayNow({
  secretKey: 'sk_test_your_test_key',
  environment: 'sandbox'
});

// Test card numbers
const testCards = {
  success: '4242424242424242',
  declined: '4000000000000002',
  insufficient_funds: '4000000000009995'
};
2

Unit Testing

Test individual API functions with mocked responses.

// Jest test example
describe('Payment Link Creation', () => {
  it('should create a payment link successfully', async () => {
    const mockResponse = {
      id: 'pl_test_123',
      url: 'https://checkout.gopay.now/pl_test_123',
      amount: 5000
    };
    
    gopaynow.paymentLinks.create = jest.fn().mockResolvedValue(mockResponse);
    
    const result = await createPaymentLink(5000, 'USD');
    
    expect(result.url).toBeDefined();
    expect(result.amount).toBe(5000);
  });
});
3

Integration Testing

Test the complete payment flow end-to-end.

// Integration test with real sandbox API
describe('Payment Flow Integration', () => {
  it('should process payment successfully', async () => {
    // Create payment link
    const paymentLink = await gopaynow.paymentLinks.create({
      amount: 2500,
      currency: 'USD'
    });
    
    expect(paymentLink.id).toMatch(/^pl_test_/);
    
    // Simulate payment completion
    const payment = await simulatePayment(paymentLink.id);
    
    expect(payment.status).toBe('completed');
  }, 10000); // 10 second timeout
});
4

Webhook Testing

Test webhook endpoints with simulated events.

// Test webhook endpoint
describe('Webhook Handler', () => {
  it('should handle payment.completed event', async () => {
    const mockEvent = {
      type: 'payment.completed',
      data: {
        id: 'pay_test_123',
        amount: 5000,
        status: 'completed'
      }
    };
    
    const signature = generateTestSignature(mockEvent);
    
    const response = await request(app)
      .post('/webhook')
      .set('x-gopaynow-signature', signature)
      .send(mockEvent)
      .expect(200);
  });
});

๐Ÿงช Testing Checklist

โœ… Unit Tests

Test individual functions in isolation

๐Ÿ”— Integration Tests

Test complete payment flows

๐Ÿ”” Webhook Tests

Verify event handling works correctly

๐Ÿšซ Error Scenarios

Test failure cases and edge conditions