|
| 1 | +import {TPaymentIntent, TPaymentMethod} from '../../../../types'; |
| 2 | +import { |
| 3 | + ChargebeeCard, |
| 4 | + ChargebeePaymentSource, |
| 5 | + ChargebeeTransaction, |
| 6 | +} from '../type'; |
| 7 | + |
| 8 | +// Payment method default values |
| 9 | +const DEFAULT_EXPIRY_MONTH = 12; |
| 10 | +const DEFAULT_EXPIRY_YEAR = 2025; |
| 11 | +const DEFAULT_FUNDING_TYPE = 'credit'; |
| 12 | +const DEFAULT_CARD_BRAND = 'unknown'; |
| 13 | + |
| 14 | +export class ChargebeePaymentIntentAdapter { |
| 15 | + constructor() {} |
| 16 | + |
| 17 | + /** |
| 18 | + * Adapts a ChargeBee transaction to the generic TPaymentIntent format. |
| 19 | + * |
| 20 | + * @param transaction - ChargeBee transaction object |
| 21 | + * @param paymentMethod - Optional payment method details |
| 22 | + * @returns TPaymentIntent - Normalized payment intent format |
| 23 | + */ |
| 24 | + adaptToModel( |
| 25 | + transaction: ChargebeeTransaction, |
| 26 | + paymentMethod?: TPaymentMethod, |
| 27 | + ): TPaymentIntent { |
| 28 | + const currencyCode = |
| 29 | + ((transaction as Record<string, unknown>)['currency_code'] as |
| 30 | + | string |
| 31 | + | undefined) ?? |
| 32 | + transaction.currencyCode ?? |
| 33 | + 'usd'; |
| 34 | + const customerId = |
| 35 | + ((transaction as Record<string, unknown>)['customer_id'] as |
| 36 | + | string |
| 37 | + | undefined) ?? transaction.customerId; |
| 38 | + const amountCapturable = (transaction as Record<string, unknown>)[ |
| 39 | + 'amount_capturable' |
| 40 | + ] as number | undefined; |
| 41 | + const transactionType = |
| 42 | + ((transaction as Record<string, unknown>)['type'] as |
| 43 | + | string |
| 44 | + | undefined) ?? 'transaction'; |
| 45 | + |
| 46 | + return { |
| 47 | + id: transaction.id ?? '', |
| 48 | + amount: transaction.amount ?? 0, |
| 49 | + currency: String(currencyCode).toLowerCase(), |
| 50 | + status: this.mapTransactionStatusToPaymentIntentStatus( |
| 51 | + transaction.status ?? 'in_progress', |
| 52 | + ), |
| 53 | + created: |
| 54 | + typeof transaction.date === 'string' |
| 55 | + ? Math.floor(new Date(transaction.date).getTime() / 1000) |
| 56 | + : (transaction.date ?? 0), |
| 57 | + customer: customerId, |
| 58 | + paymentMethod: paymentMethod, |
| 59 | + description: `ChargeBee ${transactionType} transaction`, |
| 60 | + latestCharge: transaction.id ?? '', // In ChargeBee, transaction is the charge |
| 61 | + clientSecret: undefined, // ChargeBee doesn't have client secret concept |
| 62 | + amountCapturable: amountCapturable, |
| 63 | + captureMethod: 'automatic', // ChargeBee default behavior |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Maps ChargeBee transaction status to PaymentIntent status. |
| 69 | + * |
| 70 | + * ChargeBee transaction statuses: in_progress, success, voided, failure, timeout, needs_attention, late_failure |
| 71 | + * PaymentIntent statuses: requires_payment_method, requires_confirmation, requires_action, processing, requires_capture, canceled, succeeded |
| 72 | + * |
| 73 | + * @param transactionStatus - ChargeBee transaction status |
| 74 | + * @returns Corresponding PaymentIntent status |
| 75 | + */ |
| 76 | + private mapTransactionStatusToPaymentIntentStatus( |
| 77 | + transactionStatus: string, |
| 78 | + ): string { |
| 79 | + switch (transactionStatus) { |
| 80 | + case 'in_progress': |
| 81 | + return 'processing'; |
| 82 | + case 'success': |
| 83 | + return 'succeeded'; |
| 84 | + case 'voided': |
| 85 | + return 'canceled'; |
| 86 | + case 'failure': |
| 87 | + return 'canceled'; |
| 88 | + case 'timeout': |
| 89 | + return 'canceled'; |
| 90 | + case 'needs_attention': |
| 91 | + return 'requires_action'; |
| 92 | + case 'late_failure': |
| 93 | + return 'canceled'; |
| 94 | + default: |
| 95 | + return 'requires_payment_method'; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Adapts a ChargeBee payment source to the generic TPaymentMethod format. |
| 101 | + */ |
| 102 | + adaptPaymentSource(source: ChargebeePaymentSource): TPaymentMethod { |
| 103 | + if (source.type === 'card' && source.card) { |
| 104 | + return { |
| 105 | + type: 'card', |
| 106 | + id: source.id ?? '', |
| 107 | + customer: source.customerId, |
| 108 | + card: this.buildCardDetails(source.card), |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + type: source.type ?? 'unknown', |
| 114 | + id: source.id ?? '', |
| 115 | + customer: source.customerId, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Builds card details object from ChargeBee card data. |
| 121 | + * |
| 122 | + * @param card - ChargeBee card information |
| 123 | + * @returns Formatted card details |
| 124 | + */ |
| 125 | + private buildCardDetails(card: ChargebeeCard): { |
| 126 | + brand: string; |
| 127 | + last4: string; |
| 128 | + expMonth: number; |
| 129 | + expYear: number; |
| 130 | + funding: string; |
| 131 | + } { |
| 132 | + return { |
| 133 | + brand: this.getCardBrand(card), |
| 134 | + last4: card.last4 ?? '****', |
| 135 | + expMonth: card.expiryMonth ?? DEFAULT_EXPIRY_MONTH, |
| 136 | + expYear: card.expiryYear ?? DEFAULT_EXPIRY_YEAR, |
| 137 | + funding: card.funding ?? DEFAULT_FUNDING_TYPE, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Gets card brand from first six digits. |
| 143 | + * |
| 144 | + * @param card - ChargeBee card information |
| 145 | + * @returns Card brand |
| 146 | + */ |
| 147 | + private getCardBrand(card: ChargebeeCard): string { |
| 148 | + if (card.firstSixDigits) { |
| 149 | + return this.detectCardBrand(card.firstSixDigits); |
| 150 | + } |
| 151 | + return DEFAULT_CARD_BRAND; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Detects card brand from first six digits. |
| 156 | + */ |
| 157 | + private detectCardBrand(firstSix: string): string { |
| 158 | + if (firstSix.startsWith('4')) return 'visa'; |
| 159 | + if (firstSix.startsWith('5') || firstSix.startsWith('2')) |
| 160 | + return 'mastercard'; |
| 161 | + if (firstSix.startsWith('3')) return 'amex'; |
| 162 | + return 'unknown'; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Creates a pending payment method object. |
| 167 | + * |
| 168 | + * @returns Payment method with pending status |
| 169 | + */ |
| 170 | + createPendingPaymentMethod(): TPaymentMethod { |
| 171 | + return { |
| 172 | + type: 'pending', |
| 173 | + description: 'Payment not yet processed', |
| 174 | + } as TPaymentMethod; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Creates an unknown payment method object. |
| 179 | + * |
| 180 | + * @param description - Description for the unknown payment method |
| 181 | + * @returns Payment method with unknown status |
| 182 | + */ |
| 183 | + createUnknownPaymentMethod(description: string): TPaymentMethod { |
| 184 | + return { |
| 185 | + type: 'unknown', |
| 186 | + description, |
| 187 | + } as TPaymentMethod; |
| 188 | + } |
| 189 | +} |
0 commit comments