backend/services/module-payments/helpers/payments.helpers.ts
2025-05-14 21:45:16 +02:00

94 lines
3.0 KiB
TypeScript

import AccountModel from '@src/accounts/account.model';
import { CurrencyEnum, PaymentIntentInfo, PaymentProvider } from '../components/components.payments';
import StripeService from '../services/stripe/StripeService';
export const createPaymentIntent = async (input: {
amount?: number;
currency?: CurrencyEnum;
account?: AccountModel;
}): Promise<PaymentIntentInfo | undefined> => {
const { amount, currency, account } = input;
const currentPaymentProvider = process.env.PAYMENT_PROVIDER || 'stripe';
let paymentIntent: PaymentIntentInfo | undefined;
switch (currentPaymentProvider) {
case 'stripe':
const inputData: any = {
currency: currency || (process.env.PAYMENT_DEFAULT_CURRENCY as CurrencyEnum) || CurrencyEnum.usd,
};
if (amount) inputData.amount = amount;
if (account) inputData.account = account;
// Create the PaymentIntent
const stripePaymentInfo = await StripeService.getInstance().createPaymentIntent(inputData);
paymentIntent = {
provider: PaymentProvider.stripe,
stripePaymentIntentData: stripePaymentInfo as any,
};
break;
default:
break;
}
return paymentIntent;
};
export const updatePaymentIntent = async (
paymentIntent: PaymentIntentInfo,
updateInfo: {
amount?: number;
currency?: CurrencyEnum;
account?: AccountModel;
},
): Promise<PaymentIntentInfo> => {
const currentPaymentProvider = process.env.PAYMENT_PROVIDER || 'stripe';
switch (currentPaymentProvider) {
case 'stripe':
// Update the payment Intent
if (paymentIntent.stripePaymentIntentData) {
const stripePaymentInfo = await StripeService.getInstance().updatePaymentIntent(paymentIntent.stripePaymentIntentData.id, updateInfo);
paymentIntent = {
provider: PaymentProvider.stripe,
stripePaymentIntentData: stripePaymentInfo as any,
};
}
break;
default:
break;
}
return paymentIntent;
};
export const confirmPaymentIntent = async (paymentIntent: PaymentIntentInfo): Promise<void> => {
const currentPaymentProvider = process.env.PAYMENT_PROVIDER || 'stripe';
switch (currentPaymentProvider) {
case 'stripe':
// Update the payment Intent
if (paymentIntent.stripePaymentIntentData) {
const stripePaymentInfo = await StripeService.getInstance().confirmPaymentIntent(paymentIntent.stripePaymentIntentData.id, {
paymentMethod: 'pm_card_visa',
});
paymentIntent = {
provider: PaymentProvider.stripe,
stripePaymentIntentData: stripePaymentInfo as any,
};
}
break;
default:
break;
}
// return paymentIntent;
};