try {
const response = await fetch('https://api.cwmpay.com/v1/payments', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.CWM_SECRET_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ amount: 1000, currency: 'usd' })
});
if (!response.ok) {
const { error } = await response.json();
console.error(error.code, error.message);
switch (error.type) {
case 'card_error':
// Surface error.message to the customer
break;
case 'authentication_error':
// Check your API key configuration
break;
case 'invalid_request_error':
// Fix your request parameters — check error.param
break;
case 'rate_limit_error':
case 'api_error':
// Retry with exponential backoff
break;
default:
// Log and investigate
}
}
} catch (err) {
// Network error — retry with backoff
console.error('Network error:', err.message);
}