-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHTTPRequestCreateCheckoutWithValidation.php
More file actions
238 lines (211 loc) · 7.7 KB
/
HTTPRequestCreateCheckoutWithValidation.php
File metadata and controls
238 lines (211 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* @copyright Copyright (c) 2020-2021 Afterpay Corporate Services Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$composer_autoload = __DIR__ . '/../vendor/autoload.php';
if (file_exists($composer_autoload)) {
require_once $composer_autoload;
} else {
require_once __DIR__ . '/../test/autoload.php';
}
use Afterpay\SDK\Exception\InvalidModelException as AfterpayInvalidModelException;
use Afterpay\SDK\HTTP\Request\CreateCheckout as AfterpayCreateCheckoutRequest;
use Afterpay\SDK\Model\Consumer as AfterpayConsumer;
use Afterpay\SDK\Model\Money as AfterpayMoney;
if (! headers_sent()) {
header('Content-Type: text/plain');
}
/**
* The CreateCheckout Request class allows you to validate the entire payload in the constructor, or use
* setters for each field of the request body. Either way, the provided arguments are automatically validated
* against the application data models. You can also instantiate the models manually and build the request
* payload yourself.
*
* Note that all classes that extend from the Model base class have two different validation modes. When
* "Automatic Validation" is enabled, Exceptions will be thrown whenever an attempt is made to assign an
* invalid value to a property of the object. Otherwise, the validation still takes place, but you must
* manually check the validity using the "isValid" method when you are ready to perform validation logic.
*
* Method A:
*
* Validating the entire request payload in the constructor. Classes are instantiated for each field of the
* request, according to the data models described in the API specification. If one or more fields are
* invalid, and Automatic Validation is enabled, an InvalidModelException will be thrown at the first
* encounter of an invalid model.
*/
\Afterpay\SDK\Model::setAutomaticValidationEnabled(true);
try {
$createCheckoutRequest = new AfterpayCreateCheckoutRequest([
'amount' => [ '10.00', 'AUD' ],
'consumer' => [
'phoneNumber' => '0400 000 000',
'givenNames' => 'Test',
'surname' => 'Test',
'email' => 'test@example.com'
],
'billing' => [
'name' => 'Joe Consumer',
'line1' => 'Level 5',
'line2' => '406 Collins Street',
'area1' => 'Melbourne',
'region' => 'VIC',
'postcode' => '3000',
'countryCode' => 'AU',
'phoneNumber' => '0400 000 000'
],
'shipping' => [
'name' => 'Joe Consumer',
'line1' => 'Level 5',
'line2' => '406 Collins Street',
'area1' => 'Melbourne',
'region' => 'VIC',
'postcode' => '3000',
'countryCode' => 'AU',
'phoneNumber' => '0400 000 000'
],
'courier' => [
'shippedAt' => '2019-01-01T00:00:00+10:00',
'name' => 'Australia Post',
'tracking' => 'AA0000000000000',
'priority' => 'STANDARD'
],
'items' => [
[
'name' => 'T-Shirt - Blue - Size M',
'sku' => 'TSH0001B1MED',
'quantity' => 10,
'pageUrl' => 'https://www.example.com/page.html',
'imageUrl' => 'https://www.example.com/image.jpg',
'price' => [ '10.00', 'AUD' ],
'categories' => [
[ 'Clothing', 'T-Shirts', 'Under $25' ],
[ 'Sale', 'Clothing' ]
]
]
],
'discounts' => [
[
'displayName' => '20% off SALE',
'amount' => [ '24.00', 'AUD' ]
]
],
'merchant' => [
'redirectConfirmUrl' => 'http://localhost',
'redirectCancelUrl' => 'http://localhost'
],
'taxAmount' => [ '0.00', 'AUD' ],
'shippingAmount' => [ '0.00', 'AUD' ]
]);
$createCheckoutRequest->send();
echo $createCheckoutRequest->getRawLog();
} catch (AfterpayInvalidModelException $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
/**
* Method B:
*
* Instantiating an empty Request class, then setting the values of each field using the individual
* setter methods. If Automatic Validation is disabled, you can load in all of the data and then iterate over
* the list of errors, rather than only catching the first.
*/
\Afterpay\SDK\Model::setAutomaticValidationEnabled(false);
$createCheckoutRequest = new AfterpayCreateCheckoutRequest();
$createCheckoutRequest
->setAmount('10.00', 'AUD')
->setConsumer([
'phoneNumber' => '0400 000 000',
'givenNames' => 'Test',
'surname' => 'Test',
'email' => 'test@example.com'
])
->setBilling([
'name' => 'Joe Consumer',
'line1' => 'Level 5',
'line2' => '406 Collins Street',
'area1' => 'Melbourne',
'region' => 'VIC',
'postcode' => '3000',
'countryCode' => 'AU',
'phoneNumber' => '0400 000 000'
])
->setShipping([
'name' => 'Joe Consumer',
'line1' => 'Level 5',
'line2' => '406 Collins Street',
'area1' => 'Melbourne',
'region' => 'VIC',
'postcode' => '3000',
'countryCode' => 'AU',
'phoneNumber' => '0400 000 000'
])
->setCourier([
'shippedAt' => '2019-01-01T00:00:00+10:00',
'name' => 'Australia Post',
'tracking' => 'AA0000000000000',
'priority' => 'STANDARD'
])
->setItems([
[
'name' => 'T-Shirt - Blue - Size M',
'sku' => 'TSH0001B1MED',
'quantity' => 10,
'pageUrl' => 'https://www.example.com/page.html',
'imageUrl' => 'https://www.example.com/image.jpg',
'price' => [ '10.00', 'AUD' ],
'categories' => [
[ 'Clothing', 'T-Shirts', 'Under $25' ],
[ 'Sale', 'Clothing' ]
]
]
])
->setDiscounts([
[
'displayName' => '20% off SALE',
'amount' => [ '24.00', 'AUD' ]
]
])
->setMerchant([
'redirectConfirmUrl' => 'http://localhost',
'redirectCancelUrl' => 'http://localhost'
])
->setTaxAmount('0.00', 'AUD')
->setShippingAmount('0.00', 'AUD')
;
if ($createCheckoutRequest->isValid()) {
$createCheckoutRequest->send();
echo $createCheckoutRequest->getRawLog();
} else {
echo $createCheckoutRequest->getValidationErrorsAsHtml();
}
/**
* Method C:
*
* Using some of the Model classes, but ultimately constructing the JSON payload manually. This sample
* demonstrates that since the Model class implements JsonSerializable, any Model can be easily JSON-encoded.
*/
$createCheckoutRequest = new AfterpayCreateCheckoutRequest();
$amount = new AfterpayMoney('10.00', 'AUD');
$consumer = new AfterpayConsumer();
$consumer
->setPhoneNumber('0400 000 000')
->setGivenNames('Test')
->setSurname('Test')
->setEmail('test@example.com')
;
$createCheckoutRequest
->setRequestBody('{"amount":' . json_encode($amount) . ',"consumer":' . json_encode($consumer) . ',"merchant":{"redirectConfirmUrl":"http://localhost","redirectCancelUrl":"http://localhost"}}')
->send()
;
echo $createCheckoutRequest->getRawLog();