# Payment

Now for the payment logic. In the **controller**, let's add a route to pay an order.

```typescript
@Post(':id')
payOrder(@Param() { id }: IdDto) {
  return this.paymentsService.payOrder(id);
}
```

Register the order inside the <mark style="color:blue;">`TypeOrmModule`</mark> in the <mark style="color:blue;">`PaymentsModule`</mark>, 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 <mark style="color:blue;">`order`</mark> will be searched for, and brought with its <mark style="color:blue;">`payment`</mark>. If the <mark style="color:blue;">`order`</mark> is not found, or a <mark style="color:blue;">`payment`</mark> is already present, an exception is thrown. Then, a <mark style="color:blue;">`payment`</mark> will be created. After that, the <mark style="color:blue;">`order`</mark> will have the <mark style="color:blue;">`payment`</mark> associated with it, and change its <mark style="color:blue;">`status`</mark> to  <mark style="color:blue;">`AWAITING_SHIPMENT`</mark>. Finally, the <mark style="color:blue;">`order`</mark> will be saved together with the <mark style="color:blue;">`payment`</mark>.

```typescript
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);
}
```

{% hint style="info" %}
As the payment logic is being made in a single operation, we don't need to use a **Transaction**, which we'll learn about in the next section.
{% endhint %}

{% hint style="info" %}
In the **Extra module 1 - Authentication/Authorization**, we'll learn how to check if the payer is indeed the order's owner, among other security measures.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Implementing payment logic
