try {
await square.payments.create({ sourceId: cardId, customerId, amount, idempotencyKey });
} catch (error) {
const decline = declineReason(error);
if (decline?.retryable) {
await scheduleRetry(subscriptionId, { inDays: 3, reason: decline.code });
} else if (decline) {
await askForNewCard(customerId, decline.code);
} else {
throw error;
}
}
Classify a failed charge: Square's decline code and whether charging the same card again later can succeed.
Returns
undefinedwhenerroris not a card decline (not a SquarePaymentError, and noPAYMENT_METHOD_ERRORentry on a SquareApiError), and for Square'sTEMPORARY_ERROR, which is a transient failure rather than a decline. Unknown codes are not retryable.retryableINSUFFICIENT_FUNDStrue, after a few daysTRANSACTION_LIMITtrue, laterGENERIC_DECLINE,CVV_FAILURE,CARD_EXPIRED,INVALID_ACCOUNT, ...falsefalseThis is about the card. Network failures, 5xx responses and Square's
TEMPORARY_ERRORare a separate question: see isRetryableSquareError, and retry those with the same idempotency key.