Always validate payment methods before attempting charges:
Code
function validatePaymentMethod(paymentMethod: any) { if (paymentMethod.type === "card") { const currentDate = new Date(); const expiryDate = new Date( paymentMethod.card.exp_year, paymentMethod.card.exp_month - 1 ); if (currentDate > expiryDate) { return { valid: false, reason: "Card is expired" }; } } if (paymentMethod.status !== "active") { return { valid: false, reason: "Payment method is not active" }; } return { valid: true };}
Security Best Practices
PCI Compliance
Never store card numbers, CVV, or other sensitive authentication data
Use only tokens provided by SaligPay
Follow PCI DSS requirements for your system
Data Protection
Encrypt stored payment method tokens
Limit access to payment method data
Implement audit trails for payment method operations
Use secure connections (HTTPS) for all operations
Access Control
Restrict who can view or modify payment methods
Implement role-based access controls
Log all payment method access and changes
Regular security audits of payment method handling
Handling Payment Method Failures
Code
async function handlePaymentFailure(error: any, paymentMethodId: string) { switch (error.code) { case "card_declined": console.log("Card was declined. Customer should try another payment method."); break; case "expired_card": console.log("Card has expired. Customer needs to update card details."); break; case "insufficient_funds": console.log("Insufficient funds. Customer should try another payment method."); break; default: console.log("Payment failed:", error.message); } await updatePaymentMethodStatus(paymentMethodId, "failed");}
Error Recovery
Implement robust error recovery:
Code
async function processPaymentWithFallback(order: any, customer: any) { const paymentMethods = await getCustomerPaymentMethods(customer.id); for (const method of paymentMethods) { try { const result = await saligpay.paymentIntents.confirm(order.intentId, { paymentMethod: method.id, }); if (result.status === "succeeded") { return result; } } catch (error) { console.log(`Payment failed with method ${method.id}:`, error.message); } } throw new Error( "All payment methods failed. Customer must provide new payment method." );}