Adding the payment source
How to add a PayPal payment source to an order

Problem

You have a pending order with a selected payment method that is associated with a PayPal payment integration. You want to give your customer the possibility to select Paypal to process the payment.

Solution

To add a PayPal payment source to an order, you have to create a PayPal payment source object and associate it with the order, as described in the Checkout guide.

Example

1. Get the payment source type

Request
Response
The following request retrieves the attributes of the payment method associated with the order identified by the "jNbQLhMeqo" ID:
1
curl -g -X GET \
2
'http://yourdomain.commercelayer.io/api/orders/jNbQLhMeqo?include=payment_method' \
3
-H 'Authorization: Bearer your-access-token' \
4
-H 'Accept: application/vnd.api+json'
Copied!
On success, the API responds with a 200 OK status code, returning the requested order object and the associated payment method:
1
{
2
"data": {
3
"id": "jNbQLhMeqo",
4
"type": "orders",
5
"links": {
6
"self": "https://yourdomain.commercelayer.io/api/orders/jNbQLhMeqo"
7
},
8
"attributes": {...},
9
"relationships": {
10
"market": {
11
"links": {...
12
}
13
},
14
"customer": {
15
"links": {...
16
}
17
},
18
"shipping_address": {
19
"links": {...
20
}
21
},
22
"billing_address": {
23
"links": {...
24
}
25
},
26
"payment_method": {
27
"links": {
28
"self": "https://yourdomain.commercelayer.io/api/orders/jNbQLhMeqo/relationships/payment_method",
29
"related": "https://yourdomain.commercelayer.io/api/orders/jNbQLhMeqo/payment_method"
30
},
31
"data": {
32
"type": "payment_methods",
33
"id": "PNMGGzsQMd"
34
}
35
},
36
"payment_source": {
37
"links": {...
38
}
39
},
40
"line_items": {
41
"links": {...
42
}
43
},
44
"shipments": {
45
"links": {...
46
}
47
},
48
},
49
"meta": {
50
"mode": "test"
51
}
52
},
53
"included": [
54
{
55
"id": "PNMGGzsQMd",
56
"type": "payment_methods",
57
"links": {
58
"self": "https://yourdomain.commercelayer.io/api/payment_methods/PNMGGzsQMd"
59
},
60
"attributes": {
61
"payment_source_type": "paypal_payments",
62
"name": "Paypal Payment",
63
"disabled_at": null,
64
"price_amount_cents": 0,
65
"price_amount_float": 0.0,
66
"formatted_price_amount": "€0,00",
67
"created_at": "2018-01-01T12:00:00.000Z",
68
"updated_at": "2018-01-01T12:00:00.000Z",
69
"reference": "",
70
"reference_origin": null,
71
"metadata": {}
72
},
73
"relationships": {
74
"market": {
75
"links": {...
76
}
77
},
78
"payment_gateway": {
79
"links": {...
80
}
81
}
82
},
83
"meta": {
84
"mode": "test"
85
}
86
}
87
]
88
}
Copied!

2. Create the payment source and associate it with the order

Request
Response
The following request creates a PayPal payment object and associates it with the order identified by the "jNbQLhMeqo" ID:
1
curl -g -X POST \
2
'http://yourdomain.commercelayer.io/api/paypal_payments' \
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": "paypal_payments",
9
"attributes": {
10
"return_url": "https://checkout.yourdomain.com/jNbQLhMeqo/paypal",
11
"cancel_url": "https://checkout.yourdomain.com/jNbQLhMeqo"
12
},
13
"relationships": {
14
"order": {
15
"data": {
16
"type": "orders",
17
"id": "jNbQLhMeqo"
18
}
19
}
20
}
21
}
22
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created PayPal payment object:
1
{
2
"data": {
3
"id": "ZaDVpIqzeq",
4
"type": "paypal_payments",
5
"links": {
6
"self": "https://yourdomain.commercelayer.io/api/paypal_payments/ZaDVpIqzeq"
7
},
8
"attributes": {
9
"return_url": "https://checkout.yourdomain.com/jNbQLhMeqo/paypal",
10
"cancel_url": "https://checkout.yourdomain.com/jNbQLhMeqo",
11
"note_to_payer": null,
12
"paypal_payer_id": null,
13
"name": "paypal_payment #123",
14
"paypal_id": "PAYID-xxxxyyyyzzzz",
15
"status": "created",
16
"approval_url": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-123abc456",
17
"created_at": "2018-01-01T12:00:00.000Z",
18
"updated_at": "2018-01-01T12:00:00.000Z",
19
"reference": null,
20
"reference_origin": null,
21
"metadata": {}
22
},
23
"relationships": {
24
"order": {
25
"links": {
26
"self": "https://yourdomain.commercelayer.io/api/paypal_payments/ZaDVpIqzeq/relationships/order",
27
"related": "https://yourdomain.commercelayer.io/api/paypal_payments/ZaDVpIqzeq/order"
28
}
29
}
30
},
31
"meta": {
32
"mode": "test"
33
}
34
}
35
}
Copied!

Additional notes

Client-side payment approval

The successful PayPal payment creation returns all the pieces of information you need to get the payment approval in the PayPal client-side integration flow, such as the paypal_id and the approval_url — see PayPal documentation for any reference.

Payment source nullification

PayPal payments are synchronous. When a Commerce Layer's paypal_payment is created and associated with an order, PayPal immediately creates a payment intent using the current order's total_amount as the amount to be authorized. If the order is modified before placement its amount and the original intent amount may differ, resulting in an error. To prevent that scenario previously created PayPal's payment sources are nullified in case of order edit/refresh (or if the payment method associated with the order is changed) and must be recreated.
In view of this, make sure to set the payment source as the last step of your checkout implementation.

More to read

See our documentation if you need more information on how to retrieve an order, include associations, or create a PayPal payment.