Payment
Now for the payment logic. In the controller, let's add a route to pay an order.
@Post(':id')
payOrder(@Param() { id }: IdDto) {
return this.paymentsService.payOrder(id);
}
Register the order inside the TypeOrmModule
in the PaymentsModule
, as we'll use a repository for it in the next step. Now, in the service, we'll create the method that makes a payment.
First, an order
will be searched for, and brought with its payment
. If the order
is not found, or a payment
is already present, an exception is thrown. Then, a payment
will be created. After that, the order
will have the payment
associated with it, and change its status
to AWAITING_SHIPMENT
. Finally, the order
will be saved together with the payment
.
async payOrder(id: number) {
const order = await this.ordersRepository.findOne({
where: { id },
relations: {
payment: true,
},
});
if (!order) {
throw new NotFoundException('Order not found');
}
if (order.payment) {
throw new ConflictException('Order already paid');
}
const payment = this.paymentsRepository.create();
order.payment = payment;
order.status = OrderStatus.AWAITING_SHIPMENT;
return this.ordersRepository.save(order);
}
Commit - Implementing payment logic
Last updated