Skip to content

Commit bfd455f

Browse files
authored
[Job Launcher] Default card (#3345)
* feat: enhance default payment method handling in confirmCard * feat: improve readability of confirmCard method
1 parent bb9b4ed commit bfd455f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

packages/apps/job-launcher/server/src/modules/payment/payment.service.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,27 @@ export class PaymentService {
131131
this.logger.log(ErrorPayment.SetupNotFound, PaymentService.name);
132132
throw new NotFoundError(ErrorPayment.SetupNotFound);
133133
}
134-
if (data.defaultCard || !user.stripeCustomerId) {
134+
135+
let defaultPaymentMethod: string | null = null;
136+
if (!user.stripeCustomerId) {
137+
// Assign the Stripe customer ID to the user if it does not exist yet.
138+
user.stripeCustomerId = setup.customer as string;
139+
await this.userRepository.updateOne(user);
140+
} else {
141+
// Check if the user already has a default payment method.
142+
defaultPaymentMethod = await this.getDefaultPaymentMethod(
143+
user.stripeCustomerId,
144+
);
145+
}
146+
147+
if (data.defaultCard || !defaultPaymentMethod) {
135148
// Update Stripe customer settings to use this payment method by default.
136-
await this.stripe.customers.update(<string>setup.customer, {
149+
await this.stripe.customers.update(user.stripeCustomerId, {
137150
invoice_settings: {
138151
default_payment_method: <string>setup.payment_method,
139152
},
140153
});
141154
}
142-
if (!user.stripeCustomerId) {
143-
// Assign the Stripe customer ID to the user if it does not exist yet.
144-
user.stripeCustomerId = setup.customer as string;
145-
await this.userRepository.updateOne(user);
146-
}
147155

148156
return true;
149157
}

0 commit comments

Comments
 (0)