verify cash paymnt

This commit is contained in:
2025-12-19 20:58:19 +03:30
parent 2ce7dda168
commit 465810efc2
8 changed files with 70 additions and 40 deletions
@@ -16,6 +16,7 @@ import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto';
import { VerifyPaymentDto } from '../dto/verify-payment.dto';
import { RestId, UserId } from 'src/common/decorators';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { API_HEADER_SLUG } from 'src/common/constants';
@ApiTags('payments')
@Controller()
@@ -23,20 +24,13 @@ export class PaymentsController {
constructor(
private readonly paymentsService: PaymentsService,
private readonly paymentMethodService: PaymentMethodService,
) { }
) {}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/payments/methods/restaurant')
@ApiOperation({ summary: 'Get the restaurant payment methods' })
@ApiHeader({
name: 'X-Slug',
required: true,
schema: {
type: 'string',
default: 'zhivan',
},
})
@ApiHeader(API_HEADER_SLUG)
@ApiNotFoundResponse({ description: 'Restaurant payment methods not found' })
getTheRestaurantPaymentMethods(@RestId() restId: string) {
return this.paymentMethodService.findByRestaurant(restId);
@@ -46,14 +40,7 @@ export class PaymentsController {
@ApiBearerAuth()
@Get('public/payments')
@ApiOperation({ summary: 'Get all the restaurant payments for user' })
@ApiHeader({
name: 'X-Slug',
required: true,
schema: {
type: 'string',
default: 'zhivan',
},
})
@ApiHeader(API_HEADER_SLUG)
findAllByRestaurant(@UserId() userId: string, @RestId() restId: string) {
return this.paymentsService.findAllByRestaurantId(restId, userId);
}
@@ -63,13 +50,19 @@ export class PaymentsController {
@Get('public/payments/pay-order/:orderId')
@ApiOperation({ summary: 'Pay for an order' })
@ApiParam({ name: 'orderId', type: 'string', description: 'Order ID' })
@ApiHeader({
name: 'X-Slug',
required: true,
})
@ApiHeader(API_HEADER_SLUG)
payAnOrder(@Param('orderId') orderId: string) {
return this.paymentsService.payOrder(orderId);
}
@Post('public/payments/verify')
@ApiOperation({ summary: 'Verify a payment' })
@ApiHeader(API_HEADER_SLUG)
@ApiBody({ type: VerifyPaymentDto })
@ApiNotFoundResponse({ description: 'Payment not found' })
verifyPayment(@Body() verifyPaymentDto: VerifyPaymentDto) {
return this.paymentsService.verifyPayment(verifyPaymentDto.authority, verifyPaymentDto.orderId);
}
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Get('admin/payments/:id')
@@ -152,19 +145,12 @@ export class PaymentsController {
return this.paymentMethodService.remove(paymentMethodId);
}
@Post('public/payments/verify')
@ApiOperation({ summary: 'Verify a payment' })
@ApiHeader({
name: 'X-Slug',
required: true,
schema: {
type: 'string',
default: 'zhivan',
},
})
@ApiBody({ type: VerifyPaymentDto })
@ApiNotFoundResponse({ description: 'Payment not found' })
verifyPayment(@Body() verifyPaymentDto: VerifyPaymentDto) {
return this.paymentsService.verifyPayment(verifyPaymentDto.authority, verifyPaymentDto.orderId);
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Post('admin/payments/verify-cash-method/:paymentId')
@ApiOperation({ summary: 'Verify cash payment ' })
@ApiParam({ name: 'paymentId', type: 'string', description: 'Payment ID' })
verifyCashPayment(@Param('paymentId') paymentId: string) {
return this.paymentMethodService.remove(paymentId);
}
}
@@ -57,4 +57,6 @@ export class PaymentMethodService {
await this.em.removeAndFlush(paymentMethod);
return paymentMethod;
}
}
@@ -9,6 +9,7 @@ import { UserWallet } from 'src/modules/users/entities/user-wallet.entity';
import { OrderPaymentContext } from '../interface/payment';
import { InventoryService } from 'src/modules/inventory/inventory.service';
import { OrderStatus } from 'src/modules/orders/interface/order.interface';
import { PaymentMethod } from '../entities/payment-method.entity';
@Injectable()
export class PaymentsService {
@@ -212,6 +213,29 @@ export class PaymentsService {
);
}
async verifyCashPayment(id: string): Promise<Payment> {
const payment = await this.em.transactional(async em => {
const payment = await em.findOne(Payment, id, { populate: ['order'] });
if (!payment) {
throw new NotFoundException('Payment not found');
}
if (payment.method !== PaymentMethodEnum.Cash) {
throw new BadRequestException('Payment method is not cash');
}
if (payment.status === PaymentStatusEnum.Paid) {
throw new BadRequestException('Payment already paid');
}
payment.order.status = OrderStatus.PAID;
payment.status = PaymentStatusEnum.Paid;
payment.paidAt = new Date();
em.persist(payment);
em.persist(payment.order);
await em.flush();
return payment;
});
return payment;
}
private markPaid(payment: Payment, referenceId: string) {
payment.status = PaymentStatusEnum.Paid;
payment.paidAt = new Date();