Testing
Learn how to test your ZenPays integration.
Test Environment
Use test API keys for development:
const zenpays = new ZenPays({
apiKey: process.env.ZENPAYS_TEST_API_KEY!, // zp_test_xxx
baseUrl: 'https://sandbox.zenpays.com',
})
Test Cards
Use these card numbers in the sandbox:
| Card Number | Scenario |
|---|---|
4111111111111111 | Successful payment |
4000000000000002 | Card declined |
4000000000009995 | Insufficient funds |
4000000000003220 | 3D Secure required |
4000000000000069 | Card expired |
Mocking the SDK
Using Vitest
import { describe, expect, it, vi } from 'vitest'
import { ZenPays } from 'zenpays'
vi.mock('zenpays', () => ({
ZenPays: vi.fn().mockImplementation(() => ({
payments: {
createPaymentIntent: vi.fn().mockResolvedValue({
intentId: 'pi_test_xxx',
status: 'pending',
}),
},
})),
}))
describe('payment flow', () => {
it('should create a payment intent', async () => {
const zenpays = new ZenPays({ apiKey: 'test' })
const intent = await zenpays.payments.createPaymentIntent({
amount: 1000,
currency: 'USD',
})
expect(intent.intentId).toBe('pi_test_xxx')
})
})
Using Jest
import { ZenPays } from 'zenpays'
jest.mock('zenpays')
const mockZenPays = ZenPays as jest.MockedClass<typeof ZenPays>
describe('payment flow', () => {
beforeEach(() => {
mockZenPays.mockImplementation(() => ({
payments: {
createPaymentIntent: jest.fn().mockResolvedValue({
intentId: 'pi_test_xxx',
status: 'pending',
}),
},
} as any))
})
it('should create a payment intent', async () => {
const zenpays = new ZenPays({ apiKey: 'test' })
const intent = await zenpays.payments.createPaymentIntent({
amount: 1000,
currency: 'USD',
})
expect(intent.intentId).toBe('pi_test_xxx')
})
})
Integration Tests
Test against the sandbox API:
import { ZenPays } from 'zenpays'
describe('integration tests', () => {
const zenpays = new ZenPays({
apiKey: process.env.ZENPAYS_TEST_API_KEY!,
baseUrl: 'https://sandbox.zenpays.com',
})
it('should create and confirm a payment', async () => {
// Create intent
const intent = await zenpays.payments.createPaymentIntent({
amount: 1000,
currency: 'USD',
})
expect(intent.intentId).toBeDefined()
// Confirm payment
const result = await zenpays.payments.confirmPayment(intent.intentId, {
customerDetails: {
name: 'Test User',
email: 'test@example.com',
address: { country: 'US' },
},
paymentMethodDetails: {
type: 'credit_card',
cardNumber: '4111111111111111',
expiryMonth: '12',
expiryYear: '2030',
cvv: '123',
},
})
expect(result.status).toBe('succeeded')
})
})
Custom Fetch for Testing
Provide a custom fetch implementation:
import { ZenPays } from 'zenpays'
const mockFetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({
success: true,
data: { intentId: 'pi_xxx', status: 'pending' },
}),
})
const zenpays = new ZenPays({
apiKey: 'test',
fetch: mockFetch,
})
// Now all requests go through mockFetch
await zenpays.payments.createPaymentIntent({ amount: 1000, currency: 'USD' })
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('/payment-intent'),
expect.objectContaining({ method: 'POST' })
)
Testing Webhooks
Use a tool like ngrok for local webhook testing:
ngrok http 3000
Then register the ngrok URL as your webhook endpoint:
await zenpays.merchants.createWebhook({
url: 'https://your-ngrok-url.ngrok.io/webhooks',
events: ['payment.intent.succeeded'],
})