AddressGateway.php
8.86 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace Braintree;
use InvalidArgumentException;
/**
* Braintree AddressGateway module
* PHP Version 5
* Creates and manages Braintree Addresses
*
* An Address belongs to a Customer. It can be associated to a
* CreditCard as the billing address. It can also be used
* as the shipping address when creating a Transaction.
*
* @package Braintree
*/
class AddressGateway
{
/**
*
* @var Gateway
*/
private $_gateway;
/**
*
* @var Configuration
*/
private $_config;
/**
*
* @var Http
*/
private $_http;
/**
*
* @param Gateway $gateway
*/
public function __construct($gateway)
{
$this->_gateway = $gateway;
$this->_config = $gateway->config;
$this->_config->assertHasAccessTokenOrKeys();
$this->_http = new Http($gateway->config);
}
/* public class methods */
/**
*
* @access public
* @param array $attribs
* @return Result\Successful|Result\Error
*/
public function create($attribs)
{
Util::verifyKeys(self::createSignature(), $attribs);
$customerId = isset($attribs['customerId']) ?
$attribs['customerId'] :
null;
$this->_validateCustomerId($customerId);
unset($attribs['customerId']);
try {
return $this->_doCreate(
'/customers/' . $customerId . '/addresses',
['address' => $attribs]
);
} catch (Exception\NotFound $e) {
throw new Exception\NotFound(
'Customer ' . $customerId . ' not found.'
);
}
}
/**
* attempts the create operation assuming all data will validate
* returns a Address object instead of a Result
*
* @access public
* @param array $attribs
* @return self
* @throws Exception\ValidationError
*/
public function createNoValidate($attribs)
{
$result = $this->create($attribs);
return Util::returnObjectOrThrowException(__CLASS__, $result);
}
/**
* delete an address by id
*
* @param mixed $customerOrId
* @param string $addressId
*/
public function delete($customerOrId = null, $addressId = null)
{
$this->_validateId($addressId);
$customerId = $this->_determineCustomerId($customerOrId);
$path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId;
$this->_http->delete($path);
return new Result\Successful();
}
/**
* find an address by id
*
* Finds the address with the given <b>addressId</b> that is associated
* to the given <b>customerOrId</b>.
* If the address cannot be found, a NotFound exception will be thrown.
*
*
* @access public
* @param mixed $customerOrId
* @param string $addressId
* @return Address
* @throws Exception\NotFound
*/
public function find($customerOrId, $addressId)
{
$customerId = $this->_determineCustomerId($customerOrId);
$this->_validateId($addressId);
try {
$path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId;
$response = $this->_http->get($path);
return Address::factory($response['address']);
} catch (Exception\NotFound $e) {
throw new Exception\NotFound(
'address for customer ' . $customerId .
' with id ' . $addressId . ' not found.'
);
}
}
/**
* updates the address record
*
* if calling this method in context,
* customerOrId is the 2nd attribute, addressId 3rd.
* customerOrId & addressId are not sent in object context.
*
*
* @access public
* @param array $attributes
* @param mixed $customerOrId (only used in call)
* @param string $addressId (only used in call)
* @return Result\Successful|Result\Error
*/
public function update($customerOrId, $addressId, $attributes)
{
$this->_validateId($addressId);
$customerId = $this->_determineCustomerId($customerOrId);
Util::verifyKeys(self::updateSignature(), $attributes);
$path = $this->_config->merchantPath() . '/customers/' . $customerId . '/addresses/' . $addressId;
$response = $this->_http->put($path, ['address' => $attributes]);
return $this->_verifyGatewayResponse($response);
}
/**
* update an address record, assuming validations will pass
*
* if calling this method in context,
* customerOrId is the 2nd attribute, addressId 3rd.
* customerOrId & addressId are not sent in object context.
*
* @access public
* @param array $transactionAttribs
* @param string $customerId
* @return Transaction
* @throws Exception\ValidationsFailed
* @see Address::update()
*/
public function updateNoValidate($customerOrId, $addressId, $attributes)
{
$result = $this->update($customerOrId, $addressId, $attributes);
return Util::returnObjectOrThrowException(__CLASS__, $result);
}
/**
* creates a full array signature of a valid create request
* @return array gateway create request format
*/
public static function createSignature()
{
return [
'company', 'countryCodeAlpha2', 'countryCodeAlpha3', 'countryCodeNumeric',
'countryName', 'customerId', 'extendedAddress', 'firstName',
'lastName', 'locality', 'postalCode', 'region', 'streetAddress'
];
}
/**
* creates a full array signature of a valid update request
* @return array gateway update request format
*/
public static function updateSignature()
{
// TODO: remove customerId from update signature
return self::createSignature();
}
/**
* verifies that a valid address id is being used
* @ignore
* @param string $id address id
* @throws InvalidArgumentException
*/
private function _validateId($id = null)
{
if (empty($id) || trim($id) == "") {
throw new InvalidArgumentException(
'expected address id to be set'
);
}
if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) {
throw new InvalidArgumentException(
$id . ' is an invalid address id.'
);
}
}
/**
* verifies that a valid customer id is being used
* @ignore
* @param string $id customer id
* @throws InvalidArgumentException
*/
private function _validateCustomerId($id = null)
{
if (empty($id) || trim($id) == "") {
throw new InvalidArgumentException(
'expected customer id to be set'
);
}
if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) {
throw new InvalidArgumentException(
$id . ' is an invalid customer id.'
);
}
}
/**
* determines if a string id or Customer object was passed
* @ignore
* @param mixed $customerOrId
* @return string customerId
*/
private function _determineCustomerId($customerOrId)
{
$customerId = ($customerOrId instanceof Customer) ? $customerOrId->id : $customerOrId;
$this->_validateCustomerId($customerId);
return $customerId;
}
/* private class methods */
/**
* sends the create request to the gateway
* @ignore
* @param string $subPath
* @param array $params
* @return Result\Successful|Result\Error
*/
private function _doCreate($subPath, $params)
{
$fullPath = $this->_config->merchantPath() . $subPath;
$response = $this->_http->post($fullPath, $params);
return $this->_verifyGatewayResponse($response);
}
/**
* generic method for validating incoming gateway responses
*
* creates a new Address object and encapsulates
* it inside a Result\Successful object, or
* encapsulates an Errors object inside a Result\Error
* alternatively, throws an Unexpected exception if the response is invalid
*
* @ignore
* @param array $response gateway response values
* @return Result\Successful|Result\Error
* @throws Exception\Unexpected
*/
private function _verifyGatewayResponse($response)
{
if (isset($response['address'])) {
// return a populated instance of Address
return new Result\Successful(
Address::factory($response['address'])
);
} else if (isset($response['apiErrorResponse'])) {
return new Result\Error($response['apiErrorResponse']);
} else {
throw new Exception\Unexpected(
"Expected address or apiErrorResponse"
);
}
}
}
class_alias('Braintree\AddressGateway', 'Braintree_AddressGateway');