Feat: insufficant funds

This commit is contained in:
Nevo David 2024-12-18 00:23:59 +07:00
parent 3e00e15e35
commit 72c8a4475c
1 changed files with 27 additions and 20 deletions

View File

@ -54,7 +54,10 @@ export class StripeService {
return false; return false;
} }
const getOrgFromCustomer = await this._organizationService.getOrgByCustomerId(event.data.object.customer as string); const getOrgFromCustomer =
await this._organizationService.getOrgByCustomerId(
event.data.object.customer as string
);
if (!getOrgFromCustomer?.allowTrial) { if (!getOrgFromCustomer?.allowTrial) {
return true; return true;
@ -74,28 +77,32 @@ export class StripeService {
return prev; return prev;
}); });
const paymentIntent = await stripe.paymentIntents.create({ try {
amount: 100, const paymentIntent = await stripe.paymentIntents.create({
currency: 'usd', amount: 100,
payment_method: latestMethod.id, currency: 'usd',
customer: event.data.object.customer as string, payment_method: latestMethod.id,
automatic_payment_methods: { customer: event.data.object.customer as string,
allow_redirects: 'never', automatic_payment_methods: {
enabled: true, allow_redirects: 'never',
}, enabled: true,
capture_method: 'manual', // Authorize without capturing },
confirm: true, // Confirm the PaymentIntent capture_method: 'manual', // Authorize without capturing
}); confirm: true, // Confirm the PaymentIntent
});
if (paymentIntent.status !== 'requires_capture') { if (paymentIntent.status !== 'requires_capture') {
console.error('Cant charge'); console.error('Cant charge');
await stripe.paymentMethods.detach(paymentMethods.data[0].id); await stripe.paymentMethods.detach(paymentMethods.data[0].id);
await stripe.subscriptions.cancel(event.data.object.id as string); await stripe.subscriptions.cancel(event.data.object.id as string);
return false;
}
await stripe.paymentIntents.cancel(paymentIntent.id as string);
return true;
} catch (err) {
return false; return false;
} }
await stripe.paymentIntents.cancel(paymentIntent.id as string);
return true;
} }
async createSubscription(event: Stripe.CustomerSubscriptionCreatedEvent) { async createSubscription(event: Stripe.CustomerSubscriptionCreatedEvent) {