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 Checkout.com payment source already used to complete a purchase into the customer wallet and eventually reuse it for future payments.

Solution

  1. 1.
    To save the payment source into the customer wallet you need to ensure that the payment_response data are already set, specifically the source part identifying the payment. You must then send a PATCH request to the /api/orders/:id endpoint, setting the _save_payment_source_to_customer_wallet attribute to true.
  2. 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.
To get a list of the customer's available payment sources, include the available_customer_payment_sourcesrelationship in the request (1).

Example

1. Save the payment source into the customer wallet

Request
Response
The following request save the Checkout.com payment source to the customer wallet for the order identified by the "qaMAhZkZvd" ID:
1
curl -g -X PATCH \
2
'http://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd?include=available_customer_payment_sources' \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "orders",
9
"id": "qaMAhZkZvd",
10
"attributes": {
11
"_save_payment_source_to_customer_wallet": true
12
}
13
}
14
}'
Copied!
On success, the API responds with a 200 OK status code, returning the updated order object:
1
{
2
"data": {
3
"id": "qaMAhZkZvd",
4
"type": "orders",
5
"links": {
6
"self": "https://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd"
7
},
8
"attributes": {...},
9
"relationships": {
10
"market": {
11
"links": {...}
12
},
13
"customer": {
14
"links": {...}
15
},
16
"shipping_address": {
17
"links": {...}
18
},
19
"billing_address": {
20
"links": {...}
21
},
22
"available_payment_methods": {
23
"links": {...}
24
},
25
"available_customer_payment_sources": {
26
"links": {...},
27
"data": [
28
{
29
"type": "customer_payment_sources",
30
"id": "QgDXpwsqDx"
31
}
32
]
33
},
34
"payment_method": {
35
"links": {...}
36
},
37
"payment_source": {
38
"links": {...}
39
},
40
"line_items": {
41
"links": {...}
42
},
43
"shipments": {
44
"links": {...}
45
}
46
},
47
"meta": {
48
"mode": "test"
49
}
50
},
51
"included": [
52
{
53
"id": "QgDXpwsqDx",
54
"type": "customer_payment_sources",
55
"links": {
56
"self": "https://yourdomain.commercelayer.io/api/customer_payment_sources/QgDXpwsqDx"
57
},
58
"attributes": {
59
"customer_token": "1",
60
"name": "XXXX-XXXX-XXXX-1111",
61
"payment_source_token": "7RxGd2HUyY1Yc2Tghw5N453piFs",
62
"created_at": "2018-01-01T12:00:00.000Z",
63
"updated_at": "2018-01-01T12:00:00.000Z",
64
"reference": null,
65
"metadata": {}
66
},
67
"relationships": {
68
"customer": {
69
"links": {...}
70
},
71
"payment_source": {
72
"links": {...}
73
}
74
},
75
"meta": {
76
"mode": "test"
77
}
78
}
79
]
80
}
Copied!

2. Reuse the payment source stored in the customer wallet

Request
Response
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:
1
curl -g -X PATCH \
2
'http://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd' \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "orders",
9
"id": "qaMAhZkZvd",
10
"attributes": {
11
"_customer_payment_source_id": "QgDXpwsqDx"
12
}
13
}
14
}'
Copied!
On success, the API responds with a 200 OK status code, returning the updated order object:
1
{
2
"data": {
3
"id": "qaMAhZkZvd",
4
"type": "orders",
5
"links": {
6
"self": "https://yourdomain.commercelayer.io/api/orders/qaMAhZkZvd"
7
},
8
"attributes": {...},
9
"relationships": {
10
"market": {
11
"links": {...}
12
},
13
"customer": {
14
"links": {...}
15
},
16
"shipping_address": {
17
"links": {...}
18
},
19
"billing_address": {
20
"links": {...}
21
},
22
"available_payment_methods": {
23
"links": {...}
24
},
25
"available_customer_payment_sources": {
26
"links": {...}
27
},
28
"payment_method": {
29
"links": {...}
30
},
31
"payment_source": {
32
"links": {...}
33
},
34
"line_items": {
35
"links": {...}
36
},
37
"shipments": {
38
"links": {...}
39
}
40
},
41
"meta": {
42
"mode": "test"
43
}
44
}
45
}
Copied!

More to read

See our documentation if you need more information on how to update an order or deal with customer payment sources.