# How to Create a Payment

On this page, you will find a walk-through guide on creating a payment and the necessary information to accomplish such a task.

## Prerequisites

Before making any call to LINKs API, you first need a merchant account. Contact our team at Discover@LinksMerchantServices.com to get started.

## Creating a Payment

To create a payment, you will need to follow the steps below:

1. [Create a New Payment](#1-create-a-new-payment)
2. [Authorize the Created Payment](#2-authorize-the-created-payment)
3. [Check Payment Status](#3-check-payment-status-optional) (Optional)
4. [Capture the Payment](#4-capture-the-payment)

<Callout type="info" title="Authentication">
  Depending on the request, LINKs API offers two forms of authentication,
  `Basic` and `client token`. Refer to the [Authentication](/authentication)
  guide to learn more about it.
</Callout>

### 1. Create a New Payment

To create a payment in LINKs, you need to call the [Create a New Payment endpoint](/api/payments/post-api-payments). The following are the parameters needed to start the payment creation:

<table>
  <thead>
    <tr>
      <th><strong>Field</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Required</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>merchantIdentifier</code></td>
      <td>Unique identifier for the merchant account.</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><code>currencyCode</code></td>
      <td>
        Currency code for the transaction in 
        <a href="https://en.wikipedia.org/wiki/ISO_4217">ISO 4217 format</a>.
      </td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><code>amount</code></td>
      <td>The transaction amount; must be a positive number (minimum 0.01).</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><code>customerIdentifier</code></td>
      <td>Identifier for the customer. Used to link transactions to customer profiles.</td>
      <td>No</td>
    </tr>
    <tr>
      <td><code>customerReference</code></td>
      <td>Reference identifier for the customer.</td>
      <td>No</td>
    </tr>
    <tr>
      <td><code>merchantOrderId</code></td>
      <td>A unique identifier for the order, assigned by the merchant.</td>
      <td>Yes</td>
    </tr>
  </tbody>
</table>


You can use the code snippet below to make the endpoint call with the required parameters:

```sh
curl --request POST \
--url https://testapi.linksmerchantservices.com/api/payments \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <your_base64_encoded_credentials>' \
--data '{
  "merchantIdentifier": "<your_merchant_id>",
  "currencyCode": "USD",
  "amount": 10.50,
  "merchantOrderId": "order_789"
}'
```

The response JSON will contain a `paymentIdentifier`, and a `token`, needed in [Step 2](#2-authorize-the-created-payment)

### 2. Authorize the Created Payment

With a payment created, you'll need the `paymentIdentifier` and `token`. The payment identifier will be used as path parameter in the [Authorize a Payment](/api/payments/post-api-payments-identifier-authorize) endpoint. While the token is required to authorize this request.

Add the token to the Authorization header with the `X-Api-Key` prefix as exemplified below:

```sh
curl --request POST \
--url 'https://testapi.linksmerchantservices.com/api/payments/<PAYMENT_IDENTIFIER>/authorize' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: X-Api-Key <TOKEN_FROM_STEP_1>' \
--data '{
  "paymentMethodIdentifier": "string",
  "paymentMethodType": "Unknown",
  "autoCapture": false
}'
```

<Callout type="info" title="Authorize payment details">
  Refer to the [Authorize Payment](/authorize-payment) guide to find details
  about the Authorize method.
</Callout>

### 3. Check Payment Status (Optional)

Normally, at any point after creating a payment, you can use the [Retrieve Payment](/api/payments/get-api-payments-identifier) endpoint to validate the payment's current status.

```sh
curl --request GET \
--url 'https://testapi.linksmerchantservices.com/api/payments/<PAYMENT_IDENTIFIER>' \
--header 'Accept: application/json' \
--header 'Authorization: Basic <your_base64_encoded_credentials>' \
```

This call will return a JSON with details of the payment. You can check the status with the `paymentStatus` parameter.

### 4. Capture the Payment

In this guide example, you still need to capture the payment, as in the authorize endpoint call, the `autoCapture` flag was set to `false`. To perform this step, you need to call the [Capture a Payment](/api/payments/post-api-payments-identifier-capture) endpoint, adding the payment's ID as path parameter of this request:

```sh
curl --request POST \
--url 'https://testapi.linksmerchantservices.com/api/payments/<PAYMENT_IDENTIFIER>/capture' \
--header 'Accept: application/json' \
--header 'Authorization: Basic <your_base64_encoded_credentials>' \
```

<Callout type="warning" title="Capturing payments">
  It's important to note that only authorized payments can be captured.
</Callout>

This will capture the payment that was previously authorized, finalizing the transaction by transfering funds from the customer to the merchant and completing this guide.