deny cash payment
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
Param,
|
||||
UseGuards,
|
||||
Query,
|
||||
Delete,
|
||||
} from '@nestjs/common';
|
||||
import { PaymentService } from '../services/payments.service';
|
||||
import {
|
||||
@@ -52,8 +53,16 @@ export class PaymentController {
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Verify cash payment' })
|
||||
verifyPayment(@Param('paymentId') paymentId: string) {
|
||||
return this.paymentsService.verifyCashPayment(paymentId);
|
||||
confirmPayment(@Param('paymentId') paymentId: string) {
|
||||
return this.paymentsService.confirmCashPayment(paymentId);
|
||||
}
|
||||
|
||||
@Delete('admin/payments/:paymentId/verify/cash')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Deny cash payment' })
|
||||
denyPayment(@Param('paymentId') paymentId: string) {
|
||||
return this.paymentsService.denyCashPayment(paymentId);
|
||||
}
|
||||
|
||||
// @UseGuards(AdminAuthGuard)
|
||||
|
||||
@@ -35,12 +35,9 @@ export class PaymentService {
|
||||
|
||||
async payOrder(orderId: string, dto: PayOrderDto): Promise<{ paymentUrl: string | null }> {
|
||||
|
||||
const { method } = dto
|
||||
|
||||
const ctx = await this.loadAndValidateOrder(orderId, dto)
|
||||
|
||||
|
||||
switch (method) {
|
||||
switch (dto.method) {
|
||||
case PaymentMethodEnum.Cash:
|
||||
await this.handleCashPayment(ctx);
|
||||
return { paymentUrl: null };
|
||||
@@ -231,7 +228,7 @@ export class PaymentService {
|
||||
return this.paymentRepository.findAllPaginated({ ...dto, userId })
|
||||
}
|
||||
|
||||
async verifyCashPayment(paymentId: string): Promise<Payment> {
|
||||
async confirmCashPayment(paymentId: string): Promise<Payment> {
|
||||
const payment = await this.em.transactional(async em => {
|
||||
const payment = await em.findOne(Payment, paymentId, { populate: ['order'] });
|
||||
if (!payment) {
|
||||
@@ -246,6 +243,7 @@ export class PaymentService {
|
||||
|
||||
payment.status = PaymentStatusEnum.Paid;
|
||||
payment.paidAt = new Date();
|
||||
payment.failedAt = null;
|
||||
|
||||
// TODO : use decimal js
|
||||
payment.order.paidAmount += payment.amount
|
||||
@@ -259,5 +257,35 @@ export class PaymentService {
|
||||
return payment;
|
||||
}
|
||||
|
||||
async denyCashPayment(paymentId: string): Promise<Payment> {
|
||||
const payment = await this.em.transactional(async em => {
|
||||
|
||||
const payment = await em.findOne(Payment, paymentId, { 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.status == PaymentStatusEnum.Paid) {
|
||||
payment.order.paidAmount -= payment.amount
|
||||
payment.order.balance += payment.amount
|
||||
|
||||
em.persistAndFlush(payment.order)
|
||||
}
|
||||
|
||||
payment.status = PaymentStatusEnum.Failed;
|
||||
payment.failedAt = new Date();
|
||||
payment.paidAt = null
|
||||
|
||||
em.persistAndFlush(payment);
|
||||
|
||||
return payment;
|
||||
});
|
||||
return payment;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user