> For the complete documentation index, see [llms.txt](https://docs.commercelayer.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.commercelayer.io/how-tos/placing-orders/payments/braintree/reusing-the-payment-source.md).

# Reusing the payment source

How to save the payment source into the customer wallet so that it can be reused

## Problem

You want to save a Braintree payment source already used to complete a purchase into the customer wallet and eventually reuse it for future payments.

## Solution

1. To save the payment source into the customer wallet you need to ensure that the `payment_method_nonce` is already set, then send a `PATCH` request to the `/api/orders/:id` endpoint, setting the `_save_payment_source_to_customer_wallet` attribute to `true`.
2. To reuse the stored payment source you need to associate it with the related order. To do that send a `PATCH` request to the `/api/orders/:id` endpoint, using the ID of the selected customer payment source to properly set the `_customer_payment_source_id` attribute.

{% hint style="warning" %}
Remember that Braintree `payment_method_nonce` is one-time-use: you need to store it in the customer wallet **before** using it to place the order, otherwise Braintree will respond with an error — *"cannot use a `payment_method_nonce` more than once"*.
{% endhint %}

{% hint style="info" %}
To get a list of the customer's available payment sources, include the `available_customer_payment_sources`relationship in the [request (1)](#1.-save-the-payment-source-into-the-customer-wallet).
{% endhint %}

### Example

#### 1. Save the payment source into the customer wallet

{% tabs %}
{% tab title="Request" %}
The following request save the Braintree payment source to the customer wallet for the order identified by the "qaMAhZkZvd" ID:

```javascript
curl -g -X PATCH \
  'http://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd?include=available_customer_payment_sources' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
  "data": {
    "type": "orders",
    "id": "qaMAhZkZvd",
    "attributes": {
      "_save_payment_source_to_customer_wallet": true
    }
  }
}'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the updated order object:

```javascript
{
  "data": {
    "id": "qaMAhZkZvd",
    "type": "orders",
    "links": {
      "self": "https://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd"
    },
    "attributes": {...},
    "relationships": {
      "market": {
        "links": {...}
      },
      "customer": {
        "links": {...}
      },
      "shipping_address": {
        "links": {...}
      },
      "billing_address": {
        "links": {...}
      },
      "available_payment_methods": {
        "links": {...}
      },
      "available_customer_payment_sources": {
        "links": {...},
        "data": [
          {
            "type": "customer_payment_sources",
            "id": "QgDXpwsqDx"
          }
        ]
      },
      "payment_method": {
        "links": {...}
      },
      "payment_source": {
        "links": {...}
      },
      "line_items": {
        "links": {...}
      },
      "shipments": {
        "links": {...}
      }
    },
    "meta": {
      "mode": "test",
      "organization_id": "Mn6oOpQrSt",
      "trace_id": "8a1b4c7d0e3f6a9b2c5d8e1f4a7b0c3d6e9f2a5b8c1d4e7f0a3b6c9d2e5f8a1b4"
    }
  },
  "included": [
    {
      "id": "QgDXpwsqDx",
      "type": "customer_payment_sources",
      "links": {
          "self": "https://yourdomain.commercelayer.io/api/customer_payment_sources/QgDXpwsqDx"
      },
      "attributes": {
        "customer_token": "1",
        "name": "XXXX-XXXX-XXXX-1111",
        "payment_source_token": "7RxGd2HUyY1Yc2Tghw5N453piFs",
        "created_at": "2018-01-01T12:00:00.000Z",
        "updated_at": "2018-01-01T12:00:00.000Z",
        "reference": null,
        "metadata": {}
      },
      "relationships": {
        "customer": {
          "links": {...}
        },
        "payment_source": {
          "links": {...}
        }
      },
      "meta": {
        "mode": "test",
        "organization_id": "Mn6oOpQrSt",
        "trace_id": "8a1b4c7d0e3f6a9b2c5d8e1f4a7b0c3d6e9f2a5b8c1d4e7f0a3b6c9d2e5f8a1b4"
      }
    }
  ],
  "meta": {
    "mode": "test",
    "organization_id": "Mn6oOpQrSt",
    "trace_id": "8a1b4c7d0e3f6a9b2c5d8e1f4a7b0c3d6e9f2a5b8c1d4e7f0a3b6c9d2e5f8a1b4"
  }
}
```

{% endtab %}
{% endtabs %}

#### 2. Reuse the payment source stored in the customer wallet

{% tabs %}
{% tab title="Request" %}
The following request associates the payment source saved into the customer wallet and identified by the ID "QgDXpwsqDx" with the order identified by the "qaMAhZkZvd" ID:

```javascript
curl -g -X PATCH \
  'http://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
  "data": {
    "type": "orders",
    "id": "qaMAhZkZvd",
    "attributes": {
      "_customer_payment_source_id": "QgDXpwsqDx"
    }
  }
}'
```

{% endtab %}

{% tab title="Response" %}
On success, the API responds with a `200 OK` status code, returning the updated order object:

```json
{
  "data": {
    "id": "qaMAhZkZvd",
    "type": "orders",
    "links": {
      "self": "https://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd"
    },
    "attributes": {...},
    "relationships": {
      "market": {
        "links": {...}
      },
      "customer": {
        "links": {...}
      },
      "shipping_address": {
        "links": {...}
      },
      "billing_address": {
        "links": {...}
      },
      "available_payment_methods": {
        "links": {...}
      },
      "available_customer_payment_sources": {
        "links": {...}
      },
      "payment_method": {
        "links": {...}
      },
      "payment_source": {
        "links": {...}
      },
      "line_items": {
        "links": {...}
      },
      "shipments": {
        "links": {...}
      }
    },
    "meta": {
      "mode": "test",
      "organization_id": "Mn6oOpQrSt",
      "trace_id": "8a1b4c7d0e3f6a9b2c5d8e1f4a7b0c3d6e9f2a5b8c1d4e7f0a3b6c9d2e5f8a1b4"
    }
  },
  "meta": {
    "mode": "test",
    "organization_id": "Mn6oOpQrSt",
    "trace_id": "8a1b4c7d0e3f6a9b2c5d8e1f4a7b0c3d6e9f2a5b8c1d4e7f0a3b6c9d2e5f8a1b4"
  }
}
```

{% endtab %}
{% endtabs %}

## More to read

See our documentation if you need more information on how to [update an order](https://docs.commercelayer.io/developers/v/api-reference/orders/update) or deal with [customer payment sources](https://docs.commercelayer.io/developers/v/api-reference/customer_payment_sources).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.commercelayer.io/how-tos/placing-orders/payments/braintree/reusing-the-payment-source.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
