This commit is contained in:
@@ -44,6 +44,10 @@ export class PaymentsService {
|
||||
case PaymentMethodEnum.Online:
|
||||
return this.handleOnlinePayment(ctx);
|
||||
|
||||
case PaymentMethodEnum.CreditCard:
|
||||
await this.handleCreditCardPayment(ctx);
|
||||
return { paymentUrl: null };
|
||||
|
||||
default:
|
||||
throw new BadRequestException(PaymentMessage.UNSUPPORTED_PAYMENT_METHOD);
|
||||
}
|
||||
@@ -75,6 +79,10 @@ export class PaymentsService {
|
||||
if (!pm.restaurant?.domain) throw new BadRequestException(PaymentMessage.RESTAURANT_DOMAIN_REQUIRED);
|
||||
}
|
||||
|
||||
if (pm.method === PaymentMethodEnum.CreditCard && !pm.cardNumber) {
|
||||
throw new BadRequestException(PaymentMessage.CARD_NUMBER_REQUIRED);
|
||||
}
|
||||
|
||||
return {
|
||||
order,
|
||||
amount: order.total,
|
||||
@@ -93,6 +101,14 @@ export class PaymentsService {
|
||||
});
|
||||
}
|
||||
|
||||
private async handleCreditCardPayment(ctx: OrderPaymentContext): Promise<void> {
|
||||
await this.getOrCreateLatestPendingPayment(ctx.order.id, {
|
||||
amount: ctx.amount,
|
||||
method: PaymentMethodEnum.CreditCard,
|
||||
gateway: null,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO clean this code and refactor it
|
||||
private async handleWalletPayment(ctx: OrderPaymentContext): Promise<void> {
|
||||
await this.em.transactional(async em => {
|
||||
@@ -242,13 +258,25 @@ export class PaymentsService {
|
||||
}
|
||||
|
||||
async verifyCashPayment(id: string): Promise<Payment> {
|
||||
return this.verifyManualPayment(id, PaymentMethodEnum.Cash, PaymentMessage.PAYMENT_METHOD_NOT_CASH);
|
||||
}
|
||||
|
||||
async verifyCreditCardPayment(id: string): Promise<Payment> {
|
||||
return this.verifyManualPayment(id, PaymentMethodEnum.CreditCard, PaymentMessage.PAYMENT_METHOD_NOT_CREDIT_CARD);
|
||||
}
|
||||
|
||||
private async verifyManualPayment(
|
||||
id: string,
|
||||
expectedMethod: PaymentMethodEnum,
|
||||
wrongMethodMessage: PaymentMessage,
|
||||
): Promise<Payment> {
|
||||
const payment = await this.em.transactional(async em => {
|
||||
const payment = await em.findOne(Payment, id, { populate: ['order'] });
|
||||
if (!payment) {
|
||||
throw new NotFoundException(PaymentMessage.PAYMENT_NOT_FOUND);
|
||||
}
|
||||
if (payment.method !== PaymentMethodEnum.Cash) {
|
||||
throw new BadRequestException(PaymentMessage.PAYMENT_METHOD_NOT_CASH);
|
||||
if (payment.method !== expectedMethod) {
|
||||
throw new BadRequestException(wrongMethodMessage);
|
||||
}
|
||||
if (payment.status === PaymentStatusEnum.Paid) {
|
||||
throw new BadRequestException(PaymentMessage.PAYMENT_ALREADY_PAID);
|
||||
|
||||
Reference in New Issue
Block a user