Skip to content

Commit 5cd374d

Browse files
feat(chore): fix comments
fix comments GH-27
1 parent 800a205 commit 5cd374d

8 files changed

Lines changed: 143 additions & 62 deletions

File tree

src/providers/sdk/chargebee/adapter/payment-intent.adapter.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import {TPaymentIntent, TPaymentMethod} from '../../../../types';
1+
import {PaymentStatus, TPaymentIntent, TPaymentMethod} from '../../../../types';
22
import {
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-
149
export 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
/**

src/providers/sdk/chargebee/charge-bee.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export class ChargeBeeService implements IChargeBeeService {
6565
this.customerAdapter = new CustomerAdapter();
6666
this.paymentSource = new PaymentSourceAdapter();
6767
this.chargebeeSubscriptionAdapter = new ChargebeeSubscriptionAdapter();
68-
this.chargebeePaymentIntentAdapter = new ChargebeePaymentIntentAdapter();
68+
this.chargebeePaymentIntentAdapter = new ChargebeePaymentIntentAdapter(
69+
chargeBeeConfig.cardDefaults,
70+
);
6971
}
7072
async createCustomer(
7173
customerDto: IChargeBeeCustomer,

src/providers/sdk/chargebee/type/chargebee-config.type.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Card default values for when card data is missing from the provider response.
3+
* These should only be used as fallbacks when payment providers return incomplete data.
4+
*/
5+
export interface ChargebeeCardDefaults {
6+
/** Default expiry month (1-12). Defaults to 12. */
7+
defaultExpiryMonth?: number;
8+
/** Default expiry year. Defaults to current year. */
9+
defaultExpiryYear?: number;
10+
/** Default funding type. Defaults to 'credit'. */
11+
defaultFundingType?: string;
12+
/** Default card brand. Defaults to 'unknown'. */
13+
defaultCardBrand?: string;
14+
}
15+
116
/**
217
* Configuration for the Chargebee billing provider.
318
*
@@ -34,6 +49,11 @@ export interface ChargeBeeConfig {
3449
* Must be one of the reason codes configured on your Chargebee site.
3550
*/
3651
defaultCancelReasonCode?: string;
52+
/**
53+
* Card default values for fallback when provider returns incomplete card data.
54+
* These should rarely be needed with valid Chargebee responses.
55+
*/
56+
cardDefaults?: ChargebeeCardDefaults;
3757
}
3858

3959
export type ChargebeePricingModel =

src/providers/sdk/stripe/adapter/payment-intent.adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Stripe from 'stripe';
2-
import {TPaymentIntent, TPaymentMethod} from '../../../../types';
2+
import {PaymentStatus, TPaymentIntent, TPaymentMethod} from '../../../../types';
33

44
export class StripePaymentIntentAdapter {
55
constructor() {}
@@ -19,7 +19,7 @@ export class StripePaymentIntentAdapter {
1919
id: paymentIntent.id,
2020
amount: paymentIntent.amount,
2121
currency: paymentIntent.currency,
22-
status: paymentIntent.status,
22+
status: paymentIntent.status as PaymentStatus,
2323
created: paymentIntent.created,
2424
customer: (paymentIntent.customer as string) ?? undefined,
2525
paymentMethod: paymentMethod,

src/providers/sdk/stripe/adapter/payment-source.adapter.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import Stripe from 'stripe';
22
import {AnyObject} from '@loopback/repository';
33
import {TPaymentMethod, IAdapter} from '../../../../types';
4-
import {IStripePaymentSource, StripeLegacySource} from '../type';
5-
6-
// Payment method default values
7-
const DEFAULT_EXPIRY_MONTH = 12;
8-
const DEFAULT_EXPIRY_YEAR = 2025;
9-
const DEFAULT_FUNDING_TYPE = 'credit';
10-
const DEFAULT_CARD_BRAND = 'unknown';
4+
import {
5+
IStripePaymentSource,
6+
StripeLegacySource,
7+
StripeCardDefaults,
8+
} from '../type';
119

1210
export class StripePaymentAdapter implements IAdapter<IStripePaymentSource> {
13-
constructor() {}
11+
private readonly cardDefaults: Required<StripeCardDefaults>;
12+
13+
constructor(cardDefaults?: StripeCardDefaults) {
14+
const currentYear = new Date().getFullYear();
15+
this.cardDefaults = {
16+
defaultExpiryMonth: cardDefaults?.defaultExpiryMonth ?? 12,
17+
defaultExpiryYear: cardDefaults?.defaultExpiryYear ?? currentYear,
18+
defaultFundingType: cardDefaults?.defaultFundingType ?? 'credit',
19+
defaultCardBrand: cardDefaults?.defaultCardBrand ?? 'unknown',
20+
};
21+
}
1422

1523
adaptToModel(resp: AnyObject): IStripePaymentSource {
1624
return {
@@ -78,11 +86,11 @@ export class StripePaymentAdapter implements IAdapter<IStripePaymentSource> {
7886
type: 'card',
7987
id: source.id,
8088
card: {
81-
brand: card.brand || DEFAULT_CARD_BRAND,
89+
brand: card.brand || this.cardDefaults.defaultCardBrand,
8290
last4: card.last4 || '****',
83-
expMonth: card.expMonth || DEFAULT_EXPIRY_MONTH,
84-
expYear: card.expYear || DEFAULT_EXPIRY_YEAR,
85-
funding: card.funding || DEFAULT_FUNDING_TYPE,
91+
expMonth: card.expMonth || this.cardDefaults.defaultExpiryMonth,
92+
expYear: card.expYear || this.cardDefaults.defaultExpiryYear,
93+
funding: card.funding || this.cardDefaults.defaultFundingType,
8694
},
8795
};
8896
}

src/providers/sdk/stripe/stripe.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export class StripeService implements IStripeService {
5555
});
5656
this.stripeCustomerAdapter = new StripeCustomerAdapter();
5757
this.stripeInvoiceAdapter = new StripeInvoiceAdapter();
58-
this.stripePaymentAdapter = new StripePaymentAdapter();
58+
this.stripePaymentAdapter = new StripePaymentAdapter(
59+
stripeConfig.cardDefaults,
60+
);
5961
this.stripeSubscriptionAdapter = new StripeSubscriptionAdapter();
6062
this.stripePaymentIntentAdapter = new StripePaymentIntentAdapter();
6163
}

src/providers/sdk/stripe/type/stripe-config.type.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
import Stripe from 'stripe';
2+
3+
/**
4+
* Card default values for when card data is missing from the provider response.
5+
* These should only be used as fallbacks when payment providers return incomplete data.
6+
*/
7+
export interface StripeCardDefaults {
8+
/** Default expiry month (1-12). Defaults to 12. */
9+
defaultExpiryMonth?: number;
10+
/** Default expiry year. Defaults to current year. */
11+
defaultExpiryYear?: number;
12+
/** Default funding type. Defaults to 'credit'. */
13+
defaultFundingType?: string;
14+
/** Default card brand. Defaults to 'unknown'. */
15+
defaultCardBrand?: string;
16+
}
17+
218
export interface StripeConfig {
319
secretKey: string;
420
/**
@@ -13,6 +29,11 @@ export interface StripeConfig {
1329
* @see https://stripe.com/docs/api/subscriptions/create#create_subscription-payment_behavior
1430
*/
1531
defaultPaymentBehavior?: Stripe.SubscriptionCreateParams.PaymentBehavior;
32+
/**
33+
* Card default values for fallback when provider returns incomplete card data.
34+
* These should rarely be needed with valid Stripe responses.
35+
*/
36+
cardDefaults?: StripeCardDefaults;
1637
}
1738

1839
/**

src/types.ts

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ export enum ProrationBehavior {
167167
ALWAYS_INVOICE = 'always_invoice',
168168
}
169169

170+
/**
171+
* Payment intent status values
172+
*/
173+
export enum PaymentStatus {
174+
REQUIRES_PAYMENT_METHOD = 'requires_payment_method',
175+
REQUIRES_CONFIRMATION = 'requires_confirmation',
176+
REQUIRES_ACTION = 'requires_action',
177+
PROCESSING = 'processing',
178+
REQUIRES_CAPTURE = 'requires_capture',
179+
CANCELED = 'canceled',
180+
SUCCEEDED = 'succeeded',
181+
}
182+
170183
/**
171184
* Parameters required to create a product in the billing provider.
172185
*/
@@ -269,6 +282,38 @@ export interface TInvoicePdf {
269282
generatedAt: number;
270283
}
271284

285+
/**
286+
* Card payment method details
287+
*/
288+
export interface TCard {
289+
/** Card brand: visa, mastercard, amex, etc. */
290+
brand: string;
291+
/** Last 4 digits */
292+
last4: string;
293+
/** Expiration month */
294+
expMonth: number;
295+
/** Expiration year */
296+
expYear: number;
297+
/** Funding type: credit, debit, prepaid, unknown */
298+
funding: string;
299+
/** Country code */
300+
country?: string;
301+
}
302+
303+
/**
304+
* Bank account payment method details
305+
*/
306+
export interface TBankAccount {
307+
/** Bank name */
308+
bankName: string;
309+
/** Last 4 digits */
310+
last4: string;
311+
/** Routing number */
312+
routingNumber?: string;
313+
/** Account type: checking, savings */
314+
accountType?: string;
315+
}
316+
272317
/**
273318
* Represents payment method details (card, bank account, etc.)
274319
*/
@@ -277,32 +322,10 @@ export interface TPaymentMethod {
277322
type: string;
278323

279324
/** Card details (if type is card) */
280-
card?: {
281-
/** Card brand: visa, mastercard, amex, etc. */
282-
brand: string;
283-
/** Last 4 digits */
284-
last4: string;
285-
/** Expiration month */
286-
expMonth: number;
287-
/** Expiration year */
288-
expYear: number;
289-
/** Funding type: credit, debit, prepaid, unknown */
290-
funding: string;
291-
/** Country code */
292-
country?: string;
293-
};
325+
card?: TCard;
294326

295327
/** Bank account details (if type is bank_account) */
296-
bankAccount?: {
297-
/** Bank name */
298-
bankName: string;
299-
/** Last 4 digits */
300-
last4: string;
301-
/** Routing number */
302-
routingNumber?: string;
303-
/** Account type: checking, savings */
304-
accountType?: string;
305-
};
328+
bankAccount?: TBankAccount;
306329

307330
/** Customer ID */
308331
customer?: string;
@@ -363,7 +386,7 @@ export interface TPaymentIntent {
363386
* - canceled
364387
* - succeeded
365388
*/
366-
status: string;
389+
status: PaymentStatus;
367390

368391
/** Creation timestamp (seconds) */
369392
created: number;

0 commit comments

Comments
 (0)