Selecting a payment method

How to select a payment method for the order

Problem

You have a pending order and you want to give your customer the possibility to select a payment method.

Solution

Within Commerce Layer, an order must have a valid payment method before the order can be placed. To let the customer select a payment method, first you need to get the list of available payment methods for the order and display them to the customer. Then you can associate the selected payment method with the order. To do that:

  1. Send a GET request to the /api/orders/:id endpoint, including the associated available_payment_methods.

  2. Send a PATCH request to the /api/orders/:id endpoint, setting its payment_method relationship.

Example

1. Get the available payment methods

The following request retrieves the available payment methods associated with the order identified by the "NgojhKoyYN" ID:

curl -g -X GET \ 
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN?include=available_payment_methods' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json'

2. Select a payment method

The following request associated the selected payment method (identified by the "zmdjPsavMW" ID) with the order identified by the "NgojhKoyYN" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhKoyYN",
      "relationships": {
        "payment_method": {
          "data": {
            "type": "payment_methods",
            "id": "zmdjPsavMW"
          }
        }
      }
    }
  }'

Mapping

The image below shows how the related action during the checkout process is mapped to a specific relationship of the order object.

Additional notes

Redeeming gift cards

When redeeming a gift card, it is possible that the order amount due goes to zero and no additional payment methods are required. Instead, if the gift card balance doesn't cover the total amount of the order, you need to select an additional payment method before it can be placed.

Payment source types

Each payment method has a payment_source_type attribute which determines the type of payment source that can be used to pay for the order. Many payment source types are strictly related to a specific payment gateway, rather than a payment instrument type.

For example, if you want to collect credit card payments with Stripe, the payment source type is stripe_payment, rather than something like "CreditCard". This lets you leverage the Stripe APIs, official libraries, and documentation to build the integration on the client-side while using Commerce Layer as your server-side endpoint.

For more details, please refer to the payments guide.

Payments

More to read

See our documentation if you need more information about the payment method and gift card objects or on how to update an order and include associations.

Last updated