1- import { TPaymentIntent , TPaymentMethod } from '../../../../types' ;
1+ import { PaymentStatus , TPaymentIntent , TPaymentMethod } from '../../../../types' ;
22import {
33 ChargebeeCard ,
44 ChargebeePaymentSource ,
55 ChargebeeTransaction ,
6+ ChargebeeCardDefaults ,
67} from '../type' ;
78
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-
149export class ChargebeePaymentIntentAdapter {
15- constructor ( ) { }
10+ private readonly cardDefaults : Required < ChargebeeCardDefaults > ;
11+
12+ constructor ( cardDefaults ?: ChargebeeCardDefaults ) {
13+ const currentYear = new Date ( ) . getFullYear ( ) ;
14+ this . cardDefaults = {
15+ defaultExpiryMonth : cardDefaults ?. defaultExpiryMonth ?? 12 ,
16+ defaultExpiryYear : cardDefaults ?. defaultExpiryYear ?? currentYear ,
17+ defaultFundingType : cardDefaults ?. defaultFundingType ?? 'credit' ,
18+ defaultCardBrand : cardDefaults ?. defaultCardBrand ?? 'unknown' ,
19+ } ;
20+ }
1621
1722 /**
1823 * Adapts a ChargeBee transaction to the generic TPaymentIntent format.
@@ -101,24 +106,24 @@ export class ChargebeePaymentIntentAdapter {
101106 */
102107 private mapTransactionStatusToPaymentIntentStatus (
103108 transactionStatus : string ,
104- ) : string {
109+ ) : PaymentStatus {
105110 switch ( transactionStatus ) {
106111 case 'in_progress' :
107- return 'processing' ;
112+ return PaymentStatus . PROCESSING ;
108113 case 'success' :
109- return 'succeeded' ;
114+ return PaymentStatus . SUCCEEDED ;
110115 case 'voided' :
111- return 'canceled' ;
116+ return PaymentStatus . CANCELED ;
112117 case 'failure' :
113- return 'canceled' ;
118+ return PaymentStatus . CANCELED ;
114119 case 'timeout' :
115- return 'canceled' ;
120+ return PaymentStatus . CANCELED ;
116121 case 'needs_attention' :
117- return 'requires_action' ;
122+ return PaymentStatus . REQUIRES_ACTION ;
118123 case 'late_failure' :
119- return 'canceled' ;
124+ return PaymentStatus . CANCELED ;
120125 default :
121- return 'requires_payment_method' ;
126+ return PaymentStatus . REQUIRES_PAYMENT_METHOD ;
122127 }
123128 }
124129
@@ -158,9 +163,9 @@ export class ChargebeePaymentIntentAdapter {
158163 return {
159164 brand : this . getCardBrand ( card ) ,
160165 last4 : card . last4 ?? '****' ,
161- expMonth : card . expiryMonth ?? DEFAULT_EXPIRY_MONTH ,
162- expYear : card . expiryYear ?? DEFAULT_EXPIRY_YEAR ,
163- funding : card . funding ?? DEFAULT_FUNDING_TYPE ,
166+ expMonth : card . expiryMonth ?? this . cardDefaults . defaultExpiryMonth ,
167+ expYear : card . expiryYear ?? this . cardDefaults . defaultExpiryYear ,
168+ funding : card . funding ?? this . cardDefaults . defaultFundingType ,
164169 } ;
165170 }
166171
@@ -174,7 +179,7 @@ export class ChargebeePaymentIntentAdapter {
174179 if ( card . firstSixDigits ) {
175180 return this . detectCardBrand ( card . firstSixDigits ) ;
176181 }
177- return DEFAULT_CARD_BRAND ;
182+ return this . cardDefaults . defaultCardBrand ;
178183 }
179184
180185 /**
0 commit comments