| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*******************************************************************************
|
|
|
3 |
* Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
*
|
|
|
6 |
* You may not use this file except in compliance with the License.
|
|
|
7 |
* You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
|
|
|
8 |
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
9 |
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
10 |
* specific language governing permissions and limitations under the License.
|
|
|
11 |
* *****************************************************************************
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class CheckoutByAmazon_Service_Client
|
|
|
16 |
{
|
|
|
17 |
|
|
|
18 |
/** @var string */
|
|
|
19 |
private $_awsAccessKeyId = null;
|
|
|
20 |
|
|
|
21 |
/** @var string */
|
|
|
22 |
private $_awsSecretAccessKey = null;
|
|
|
23 |
|
|
|
24 |
/** @var array */
|
|
|
25 |
private $_config = array ('ServiceURL' => null,
|
|
|
26 |
'UserAgent' => 'CheckoutByAmazon Service Library',
|
|
|
27 |
'SignatureVersion' => 2,
|
|
|
28 |
'SignatureMethod' => 'HmacSHA256',
|
|
|
29 |
'ProxyHost' => null,
|
|
|
30 |
'ProxyPort' => -1,
|
|
|
31 |
'MaxErrorRetry' => 3
|
|
|
32 |
);
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Construct new Client
|
|
|
36 |
*
|
|
|
37 |
* @param string $awsAccessKeyId AWS Access Key ID
|
|
|
38 |
* @param string $awsSecretAccessKey AWS Secret Access Key
|
|
|
39 |
* @param array $config configuration options.
|
|
|
40 |
* Valid configuration options are:
|
|
|
41 |
* <ul>
|
|
|
42 |
* <li>ServiceURL</li>
|
|
|
43 |
* <li>UserAgent</li>
|
|
|
44 |
* <li>SignatureVersion</li>
|
|
|
45 |
* <li>TimesRetryOnError</li>
|
|
|
46 |
* <li>ProxyHost</li>
|
|
|
47 |
* <li>ProxyPort</li>
|
|
|
48 |
* <li>MaxErrorRetry</li>
|
|
|
49 |
* </ul>
|
|
|
50 |
*/
|
|
|
51 |
public function __construct($awsAccessKeyId, $awsSecretAccessKey)
|
|
|
52 |
{
|
|
|
53 |
iconv_set_encoding('output_encoding', 'UTF-8');
|
|
|
54 |
iconv_set_encoding('input_encoding', 'UTF-8');
|
|
|
55 |
iconv_set_encoding('internal_encoding', 'UTF-8');
|
|
|
56 |
|
|
|
57 |
$this->_awsAccessKeyId = $awsAccessKeyId;
|
|
|
58 |
$this->_awsSecretAccessKey = $awsSecretAccessKey;
|
|
|
59 |
$config = array ('ServiceURL' =>CheckoutByAmazon_Service_MerchantValues::getInstance()->getCbaServiceURL());
|
|
|
60 |
$this->_config = array_merge($this->_config, $config);
|
|
|
61 |
define( SERVICE_VERSION , CheckoutByAmazon_Service_MerchantValues::getInstance()->getVersion());
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Public API ------------------------------------------------------------//
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Create Purchase Contract
|
|
|
70 |
* @param mixed $request array of parameters for CheckoutByAmazon_Service_Model_CreatePurchaseContractRequest request
|
|
|
71 |
* or CheckoutByAmazon_Service_Model_CreatePurchaseContractRequest object itself
|
|
|
72 |
* @see CheckoutByAmazon_Service_Model_CreatePurchaseContract
|
|
|
73 |
* @return CheckoutByAmazon_Service_Model_CreatePurchaseContractResponse CheckoutByAmazon_Service_Model_CreatePurchaseContractResponse
|
|
|
74 |
*
|
|
|
75 |
* @throws CheckoutByAmazon_Service_Exception
|
|
|
76 |
*/
|
|
|
77 |
public function createPurchaseContract($request)
|
|
|
78 |
{
|
|
|
79 |
if (!$request instanceof CheckoutByAmazon_Service_Model_CreatePurchaseContractRequest) {
|
|
|
80 |
require_once ('CheckoutByAmazon/Service/Model/CreatePurchaseContractRequest.php');
|
|
|
81 |
$request = new CheckoutByAmazon_Service_Model_CreatePurchaseContractRequest($request);
|
|
|
82 |
}
|
|
|
83 |
require_once ('CheckoutByAmazon/Service/Model/CreatePurchaseContractResponse.php');
|
|
|
84 |
return CheckoutByAmazon_Service_Model_CreatePurchaseContractResponse::fromXML($this->invoke($this->convertCreatePurchaseContract($request)));
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get Purchase Contract
|
|
|
91 |
* @param mixed $request array of parameters for CheckoutByAmazon_Service_Model_GetPurchaseContractRequest request
|
|
|
92 |
* or CheckoutByAmazon_Service_Model_GetPurchaseContractRequest object itself
|
|
|
93 |
* @see CheckoutByAmazon_Service_Model_GetPurchaseContract
|
|
|
94 |
* @return CheckoutByAmazon_Service_Model_GetPurchaseContractResponse CheckoutByAmazon_Service_Model_GetPurchaseContractResponse
|
|
|
95 |
*
|
|
|
96 |
* @throws CheckoutByAmazon_Service_Exception
|
|
|
97 |
*/
|
|
|
98 |
public function getPurchaseContract($request)
|
|
|
99 |
{
|
|
|
100 |
if (!$request instanceof CheckoutByAmazon_Service_Model_GetPurchaseContractRequest) {
|
|
|
101 |
require_once ('CheckoutByAmazon/Service/Model/GetPurchaseContractRequest.php');
|
|
|
102 |
$request = new CheckoutByAmazon_Service_Model_GetPurchaseContractRequest($request);
|
|
|
103 |
}
|
|
|
104 |
require_once ('CheckoutByAmazon/Service/Model/GetPurchaseContractResponse.php');
|
|
|
105 |
return CheckoutByAmazon_Service_Model_GetPurchaseContractResponse::fromXML($this->invoke($this->convertGetPurchaseContract($request)));
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Set Purchase Items
|
|
|
112 |
* @param mixed $request array of parameters for CheckoutByAmazon_Service_Model_SetPurchaseItemsRequest request
|
|
|
113 |
* or CheckoutByAmazon_Service_Model_SetPurchaseItemsRequest object itself
|
|
|
114 |
* @see CheckoutByAmazon_Service_Model_SetPurchaseItems
|
|
|
115 |
* @return CheckoutByAmazon_Service_Model_SetPurchaseItemsResponse CheckoutByAmazon_Service_Model_SetPurchaseItemsResponse
|
|
|
116 |
*
|
|
|
117 |
* @throws CheckoutByAmazon_Service_Exception
|
|
|
118 |
*/
|
|
|
119 |
public function setPurchaseItems($request)
|
|
|
120 |
{
|
|
|
121 |
if (!$request instanceof CheckoutByAmazon_Service_Model_SetPurchaseItemsRequest) {
|
|
|
122 |
require_once ('CheckoutByAmazon/Service/Model/SetPurchaseItemsRequest.php');
|
|
|
123 |
$request = new CheckoutByAmazon_Service_Model_SetPurchaseItemsRequest($request);
|
|
|
124 |
}
|
|
|
125 |
require_once ('CheckoutByAmazon/Service/Model/SetPurchaseItemsResponse.php');
|
|
|
126 |
return CheckoutByAmazon_Service_Model_SetPurchaseItemsResponse::fromXML($this->invoke($this->convertSetPurchaseItems($request)));
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Complete Purchase Contract
|
|
|
133 |
* @param mixed $request array of parameters for CheckoutByAmazon_Service_Model_CompletePurchaseContractRequest request
|
|
|
134 |
* or CheckoutByAmazon_Service_Model_CompletePurchaseContractRequest object itself
|
|
|
135 |
* @see CheckoutByAmazon_Service_Model_CompletePurchaseContract
|
|
|
136 |
* @return CheckoutByAmazon_Service_Model_CompletePurchaseContractResponse CheckoutByAmazon_Service_Model_CompletePurchaseContractResponse
|
|
|
137 |
*
|
|
|
138 |
* @throws CheckoutByAmazon_Service_Exception
|
|
|
139 |
*/
|
|
|
140 |
public function completePurchaseContract($request)
|
|
|
141 |
{
|
|
|
142 |
if (!$request instanceof CheckoutByAmazon_Service_Model_CompletePurchaseContractRequest) {
|
|
|
143 |
require_once ('CheckoutByAmazon/Service/Model/CompletePurchaseContractRequest.php');
|
|
|
144 |
$request = new CheckoutByAmazon_Service_Model_CompletePurchaseContractRequest($request);
|
|
|
145 |
}
|
|
|
146 |
require_once ('CheckoutByAmazon/Service/Model/CompletePurchaseContractResponse.php');
|
|
|
147 |
return CheckoutByAmazon_Service_Model_CompletePurchaseContractResponse::fromXML($this->invoke($this->convertCompletePurchaseContract($request)));
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Set Contract Charges
|
|
|
154 |
* @param mixed $request array of parameters for CheckoutByAmazon_Service_Model_SetContractChargesRequest request
|
|
|
155 |
* or CheckoutByAmazon_Service_Model_SetContractChargesRequest object itself
|
|
|
156 |
* @see CheckoutByAmazon_Service_Model_SetContractCharges
|
|
|
157 |
* @return CheckoutByAmazon_Service_Model_SetContractChargesResponse CheckoutByAmazon_Service_Model_SetContractChargesResponse
|
|
|
158 |
*
|
|
|
159 |
* @throws CheckoutByAmazon_Service_Exception
|
|
|
160 |
*/
|
|
|
161 |
public function setContractCharges($request)
|
|
|
162 |
{
|
|
|
163 |
if (!$request instanceof CheckoutByAmazon_Service_Model_SetContractChargesRequest) {
|
|
|
164 |
require_once ('CheckoutByAmazon/Service/Model/SetContractChargesRequest.php');
|
|
|
165 |
$request = new CheckoutByAmazon_Service_Model_SetContractChargesRequest($request);
|
|
|
166 |
}
|
|
|
167 |
require_once ('CheckoutByAmazon/Service/Model/SetContractChargesResponse.php');
|
|
|
168 |
return CheckoutByAmazon_Service_Model_SetContractChargesResponse::fromXML($this->invoke($this->convertSetContractCharges($request)));
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
// Private API ------------------------------------------------------------//
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Invoke request and return response
|
|
|
175 |
*/
|
|
|
176 |
protected function invoke(array $parameters)
|
|
|
177 |
{
|
|
|
178 |
$actionName = $parameters["Action"];
|
|
|
179 |
$response = array();
|
|
|
180 |
$responseBody = null;
|
|
|
181 |
$statusCode = 200;
|
|
|
182 |
|
|
|
183 |
/* Submit the request and read response body */
|
|
|
184 |
try {
|
|
|
185 |
|
|
|
186 |
/* Add required request parameters */
|
|
|
187 |
$parameters = $this->addRequiredParameters($parameters);
|
|
|
188 |
|
|
|
189 |
$shouldRetry = true;
|
|
|
190 |
$retries = 0;
|
|
|
191 |
do {
|
|
|
192 |
try {
|
|
|
193 |
$response = $this->httpPost($parameters);
|
|
|
194 |
if ($response['Status'] === 200) {
|
|
|
195 |
$shouldRetry = false;
|
|
|
196 |
} else {
|
|
|
197 |
if ($response['Status'] === 500 || $response['Status'] === 503) {
|
|
|
198 |
$shouldRetry = true;
|
|
|
199 |
$this->pauseOnRetry(++$retries, $response['Status']);
|
|
|
200 |
} else {
|
|
|
201 |
throw $this->reportAnyErrors($response['ResponseBody'], $response['Status']);
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
/* Rethrow on deserializer error */
|
|
|
205 |
} catch (Exception $e) {
|
|
|
206 |
require_once ('CheckoutByAmazon/Service/Exception.php');
|
|
|
207 |
if (($e instanceof CheckoutByAmazon_Service_Exception)) {
|
|
|
208 |
throw $e;
|
|
|
209 |
} else {
|
|
|
210 |
require_once ('CheckoutByAmazon/Service/Exception.php');
|
|
|
211 |
throw new CheckoutByAmazon_Service_Exception(array('Exception' => $e, 'Message' => $e->getMessage()));
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
} while ($shouldRetry);
|
|
|
216 |
|
|
|
217 |
} catch (CheckoutByAmazon_Service_Exception $se) {
|
|
|
218 |
throw $se;
|
|
|
219 |
} catch (Exception $t) {
|
|
|
220 |
throw new CheckoutByAmazon_Service_Exception(array('Exception' => $t, 'Message' => $t->getMessage()));
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
return $response['ResponseBody'];
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Look for additional error strings in the response and return formatted exception
|
|
|
228 |
*/
|
|
|
229 |
protected function reportAnyErrors($responseBody, $status, Exception $e = null)
|
|
|
230 |
{
|
|
|
231 |
$ex = null;
|
|
|
232 |
$xml = new SimpleXMLElement($responseBody);
|
|
|
233 |
$doc = simplexml_load_string($responseBody);
|
|
|
234 |
$message = $doc->Error->Message;
|
|
|
235 |
$requestId = $doc->RequestId;
|
|
|
236 |
$code = $doc->Error->Code;
|
|
|
237 |
$type = $doc->Error->Type;
|
|
|
238 |
if(is_null($type))
|
|
|
239 |
{
|
|
|
240 |
$type = 'Unknown';
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
if(!is_null($message))
|
|
|
244 |
{
|
|
|
245 |
require_once ('CheckoutByAmazon/Service/Exception.php');
|
|
|
246 |
$ex = new CheckoutByAmazon_Service_Exception(array ('Message' => $message, 'StatusCode' => $status, 'ErrorCode' => $code,
|
|
|
247 |
'ErrorType' => $type, 'RequestId' => $requestId, 'XML' => $responseBody));
|
|
|
248 |
}
|
|
|
249 |
else
|
|
|
250 |
{
|
|
|
251 |
require_once ('CheckoutByAmazon/Service/Exception.php');
|
|
|
252 |
$ex = new CheckoutByAmazon_Service_Exception(array('Message' => 'Internal Error', 'StatusCode' => $status));
|
|
|
253 |
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
return $ex;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Perform HTTP post with exponential retries on error 500 and 503
|
|
|
264 |
*
|
|
|
265 |
*/
|
|
|
266 |
protected function httpPost(array $parameters)
|
|
|
267 |
{
|
|
|
268 |
|
|
|
269 |
$query = $this->getParametersAsString($parameters);
|
|
|
270 |
$url = parse_url ($this->_config['ServiceURL']);
|
|
|
271 |
$path = array_key_exists('path', $url) ? $url['path'] : "/";
|
|
|
272 |
$post = "POST " . $path . " HTTP/1.0\r\n";
|
|
|
273 |
$post .= "Host: " . $url['host'] . "\r\n";
|
|
|
274 |
$post .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
|
|
|
275 |
$post .= "Content-Length: " . strlen($query) . "\r\n";
|
|
|
276 |
$post .= "User-Agent: " . $this->_config['UserAgent'] . "\r\n";
|
|
|
277 |
$post .= "\r\n";
|
|
|
278 |
$post .= $query;
|
|
|
279 |
$port = array_key_exists('port',$url) ? $url['port'] : null;
|
|
|
280 |
$scheme = '';
|
|
|
281 |
|
|
|
282 |
switch ($url['scheme']) {
|
|
|
283 |
case 'https':
|
|
|
284 |
$scheme = 'ssl://';
|
|
|
285 |
$port = $port === null ? 443 : $port;
|
|
|
286 |
break;
|
|
|
287 |
default:
|
|
|
288 |
$scheme = '';
|
|
|
289 |
$port = $port === null ? 80 : $port;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
$response = '';
|
|
|
293 |
if ($socket = @fsockopen($scheme . $url['host'], $port, $errno, $errstr, 10)) {
|
|
|
294 |
|
|
|
295 |
fwrite($socket, $post);
|
|
|
296 |
|
|
|
297 |
while (!feof($socket)) {
|
|
|
298 |
$response .= fgets($socket, 1160);
|
|
|
299 |
}
|
|
|
300 |
fclose($socket);
|
|
|
301 |
|
|
|
302 |
list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
|
|
|
303 |
$other = preg_split("/\r\n|\n|\r/", $other);
|
|
|
304 |
list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);
|
|
|
305 |
} else {
|
|
|
306 |
throw new Exception ("Unable to establish connection to host " . $url['host'] . " $errstr");
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
return array ('Status' => (int)$code, 'ResponseBody' => $responseBody);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Exponential sleep on failed request
|
|
|
315 |
* @param retries current retry
|
|
|
316 |
* @throws CheckoutByAmazon_Service_Exception if maximum number of retries has been reached
|
|
|
317 |
*/
|
|
|
318 |
protected function pauseOnRetry($retries, $status)
|
|
|
319 |
{
|
|
|
320 |
if ($retries <= $this->_config['MaxErrorRetry']) {
|
|
|
321 |
$delay = (int) (pow(4, $retries) * 100000) ;
|
|
|
322 |
usleep($delay);
|
|
|
323 |
} else {
|
|
|
324 |
require_once ('CheckoutByAmazon/Service/Exception.php');
|
|
|
325 |
throw new CheckoutByAmazon_Service_Exception (array ('Message' => "Maximum number of retry attempts reached : $retries", 'StatusCode' => $status));
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
/**
|
|
|
330 |
* Add authentication related and version parameters
|
|
|
331 |
*/
|
|
|
332 |
protected function addRequiredParameters(array $parameters)
|
|
|
333 |
{
|
|
|
334 |
$parameters['AWSAccessKeyId'] = $this->_awsAccessKeyId;
|
|
|
335 |
$parameters['Timestamp'] = $this->getFormattedTimestamp();
|
|
|
336 |
$parameters['Version'] = SERVICE_VERSION;
|
|
|
337 |
$parameters['SignatureVersion'] = $this->_config['SignatureVersion'];
|
|
|
338 |
if ($parameters['SignatureVersion'] > 1) {
|
|
|
339 |
$parameters['SignatureMethod'] = $this->_config['SignatureMethod'];
|
|
|
340 |
}
|
|
|
341 |
$parameters['Signature'] = $this->signParameters($parameters, $this->_awsSecretAccessKey);
|
|
|
342 |
|
|
|
343 |
return $parameters;
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Convert paremeters to Url encoded query string
|
|
|
348 |
*/
|
|
|
349 |
protected function getParametersAsString(array $parameters)
|
|
|
350 |
{
|
|
|
351 |
$queryParameters = array();
|
|
|
352 |
foreach ($parameters as $key => $value) {
|
|
|
353 |
$queryParameters[] = $key . '=' . $this->_urlencode($value);
|
|
|
354 |
}
|
|
|
355 |
return implode('&', $queryParameters);
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
/**
|
|
|
360 |
* Computes RFC 2104-compliant HMAC signature for request parameters
|
|
|
361 |
* Implements AWS Signature, as per following spec:
|
|
|
362 |
*
|
|
|
363 |
* If Signature Version is 0, it signs concatenated Action and Timestamp
|
|
|
364 |
*
|
|
|
365 |
* If Signature Version is 1, it performs the following:
|
|
|
366 |
*
|
|
|
367 |
* Sorts all parameters (including SignatureVersion and excluding Signature,
|
|
|
368 |
* the value of which is being created), ignoring case.
|
|
|
369 |
*
|
|
|
370 |
* Iterate over the sorted list and append the parameter name (in original case)
|
|
|
371 |
* and then its value. It will not URL-encode the parameter values before
|
|
|
372 |
* constructing this string. There are no separators.
|
|
|
373 |
*
|
|
|
374 |
* If Signature Version is 2, string to sign is based on following:
|
|
|
375 |
*
|
|
|
376 |
* 1. The HTTP Request Method followed by an ASCII newline (%0A)
|
|
|
377 |
* 2. The HTTP Host header in the form of lowercase host, followed by an ASCII newline.
|
|
|
378 |
* 3. The URL encoded HTTP absolute path component of the URI
|
|
|
379 |
* (up to but not including the query string parameters);
|
|
|
380 |
* if this is empty use a forward '/'. This parameter is followed by an ASCII newline.
|
|
|
381 |
* 4. The concatenation of all query string components (names and values)
|
|
|
382 |
* as UTF-8 characters which are URL encoded as per RFC 3986
|
|
|
383 |
* (hex characters MUST be uppercase), sorted using lexicographic byte ordering.
|
|
|
384 |
* Parameter names are separated from their values by the '=' character
|
|
|
385 |
* (ASCII character 61), even if the value is empty.
|
|
|
386 |
* Pairs of parameter and values are separated by the '&' character (ASCII code 38).
|
|
|
387 |
*
|
|
|
388 |
*/
|
|
|
389 |
protected function signParameters(array $parameters, $key) {
|
|
|
390 |
$signatureVersion = $parameters['SignatureVersion'];
|
|
|
391 |
$algorithm = "HmacSHA1";
|
|
|
392 |
$stringToSign = null;
|
|
|
393 |
$algorithm = $this->_config['SignatureMethod'];
|
|
|
394 |
$parameters['SignatureMethod'] = $algorithm;
|
|
|
395 |
$stringToSign = $this->calculateStringToSignV2($parameters);
|
|
|
396 |
|
|
|
397 |
return $this->sign($stringToSign, $key, $algorithm);
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
* Calculate String to Sign for SignatureVersion 2
|
|
|
403 |
* @param array $parameters request parameters
|
|
|
404 |
* @return String to Sign
|
|
|
405 |
*/
|
|
|
406 |
protected function calculateStringToSignV2(array $parameters) {
|
|
|
407 |
$data = 'POST';
|
|
|
408 |
$data .= "\n";
|
|
|
409 |
$endpoint = parse_url ($this->_config['ServiceURL']);
|
|
|
410 |
$data .= $endpoint['host'];
|
|
|
411 |
$data .= "\n";
|
|
|
412 |
$uri = array_key_exists('path', $endpoint) ? $endpoint['path'] : null;
|
|
|
413 |
if (!isset ($uri)) {
|
|
|
414 |
$uri = "/";
|
|
|
415 |
}
|
|
|
416 |
$uriencoded = implode("/", array_map(array($this, "_urlencode"), explode("/", $uri)));
|
|
|
417 |
$data .= $uriencoded;
|
|
|
418 |
$data .= "\n";
|
|
|
419 |
uksort($parameters, 'strcmp');
|
|
|
420 |
$data .= $this->getParametersAsString($parameters);
|
|
|
421 |
return $data;
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
protected function _urlencode($value) {
|
|
|
425 |
return str_replace('%7E', '~', rawurlencode($value));
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
|
|
|
429 |
/**
|
|
|
430 |
* Computes RFC 2104-compliant HMAC signature.
|
|
|
431 |
*/
|
|
|
432 |
protected function sign($data, $key, $algorithm)
|
|
|
433 |
{
|
|
|
434 |
switch($algorithm)
|
|
|
435 |
{
|
|
|
436 |
case 'HmacSHA1' :
|
|
|
437 |
$hash = 'sha1';
|
|
|
438 |
break;
|
|
|
439 |
case 'HmacSHA256' :
|
|
|
440 |
$hash = 'sha256';
|
|
|
441 |
break;
|
|
|
442 |
default :
|
|
|
443 |
throw new Exception ("Non-supported signing method specified");
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
return base64_encode(
|
|
|
447 |
hash_hmac($hash, $data, $key, true));
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* Formats date as ISO 8601 timestamp
|
|
|
453 |
*/
|
|
|
454 |
protected function getFormattedTimestamp()
|
|
|
455 |
{
|
|
|
456 |
return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
|
|
|
460 |
|
|
|
461 |
/**
|
|
|
462 |
* Convert CreatePurchaseContractRequest to name value pairs
|
|
|
463 |
*/
|
|
|
464 |
protected function convertCreatePurchaseContract($request) {
|
|
|
465 |
|
|
|
466 |
$parameters = array();
|
|
|
467 |
$parameters['Action'] = 'CreatePurchaseContract';
|
|
|
468 |
if ($request->isSetPurchaseContractMetadata()) {
|
|
|
469 |
$parameters['PurchaseContractMetadata'] = $request->getPurchaseContractMetadata();
|
|
|
470 |
}
|
|
|
471 |
return $parameters;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
|
|
|
475 |
/**
|
|
|
476 |
* Convert GetPurchaseContractRequest to name value pairs
|
|
|
477 |
*/
|
|
|
478 |
protected function convertGetPurchaseContract($request) {
|
|
|
479 |
|
|
|
480 |
$parameters = array();
|
|
|
481 |
$parameters['Action'] = 'GetPurchaseContract';
|
|
|
482 |
if ($request->isSetPurchaseContractId()) {
|
|
|
483 |
$parameters['PurchaseContractId'] = $request->getPurchaseContractId();
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
return $parameters;
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
/**
|
|
|
491 |
* Convert SetPurchaseItemsRequest to name value pairs
|
|
|
492 |
*/
|
|
|
493 |
protected function convertSetPurchaseItems($request) {
|
|
|
494 |
|
|
|
495 |
$parameters = array();
|
|
|
496 |
$parameters['Action'] = 'SetPurchaseItems';
|
|
|
497 |
if ($request->isSetPurchaseContractId()) {
|
|
|
498 |
$parameters['PurchaseContractId'] = $request->getPurchaseContractId();
|
|
|
499 |
}
|
|
|
500 |
if ($request->isSetPurchaseItems()) {
|
|
|
501 |
$purchaseItemssetPurchaseItemsRequest = $request->getPurchaseItems();
|
|
|
502 |
$purchaseItempurchaseItemsIndex = 1;
|
|
|
503 |
foreach ($purchaseItemssetPurchaseItemsRequest->getPurchaseItem() as $purchaseItempurchaseItemsIndex1 => $purchaseItempurchaseItems) {
|
|
|
504 |
if ($purchaseItempurchaseItems->isSetMerchantItemId()) {
|
|
|
505 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.MerchantItemId'] = $purchaseItempurchaseItems->getMerchantItemId();
|
|
|
506 |
}
|
|
|
507 |
if ($purchaseItempurchaseItems->isSetSKU()) {
|
|
|
508 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.SKU'] = $purchaseItempurchaseItems->getSKU();
|
|
|
509 |
}
|
|
|
510 |
if ($purchaseItempurchaseItems->isSetMerchantId()) {
|
|
|
511 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.MerchantId'] = $purchaseItempurchaseItems->getMerchantId();
|
|
|
512 |
}
|
|
|
513 |
if ($purchaseItempurchaseItems->isSetTitle()) {
|
|
|
514 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.Title'] = $purchaseItempurchaseItems->getTitle();
|
|
|
515 |
}
|
|
|
516 |
if ($purchaseItempurchaseItems->isSetDescription()) {
|
|
|
517 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.Description'] = $purchaseItempurchaseItems->getDescription();
|
|
|
518 |
}
|
|
|
519 |
if ($purchaseItempurchaseItems->isSetUnitPrice()) {
|
|
|
520 |
$UnitPricepurchaseItem = $purchaseItempurchaseItems->getUnitPrice();
|
|
|
521 |
if ($UnitPricepurchaseItem->isSetAmount()) {
|
|
|
522 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.UnitPrice.Amount'] = $UnitPricepurchaseItem->getAmount();
|
|
|
523 |
}
|
|
|
524 |
if ($UnitPricepurchaseItem->isSetCurrencyCode()) {
|
|
|
525 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.UnitPrice.CurrencyCode'] = $UnitPricepurchaseItem->getCurrencyCode();
|
|
|
526 |
}
|
|
|
527 |
}
|
|
|
528 |
if ($purchaseItempurchaseItems->isSetQuantity()) {
|
|
|
529 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.Quantity'] = $purchaseItempurchaseItems->getQuantity();
|
|
|
530 |
}
|
|
|
531 |
if ($purchaseItempurchaseItems->isSetURL()) {
|
|
|
532 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.URL'] = $purchaseItempurchaseItems->getURL();
|
|
|
533 |
}
|
|
|
534 |
if ($purchaseItempurchaseItems->isSetCategory()) {
|
|
|
535 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.Category'] = $purchaseItempurchaseItems->getCategory();
|
|
|
536 |
}
|
|
|
537 |
if ($purchaseItempurchaseItems->isSetFulfillmentNetwork()) {
|
|
|
538 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.FulfillmentNetwork'] = $purchaseItempurchaseItems->getFulfillmentNetwork();
|
|
|
539 |
}
|
|
|
540 |
if ($purchaseItempurchaseItems->isSetItemCustomData()) {
|
|
|
541 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.ItemCustomData'] = $purchaseItempurchaseItems->getItemCustomData();
|
|
|
542 |
}
|
|
|
543 |
if ($purchaseItempurchaseItems->isSetProductType()) {
|
|
|
544 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.ProductType'] = $purchaseItempurchaseItems->getProductType();
|
|
|
545 |
}
|
|
|
546 |
if ($purchaseItempurchaseItems->isSetPhysicalProductAttributes()) {
|
|
|
547 |
$physicalProductAttributespurchaseItem = $purchaseItempurchaseItems->getPhysicalProductAttributes();
|
|
|
548 |
if ($physicalProductAttributespurchaseItem->isSetWeight()) {
|
|
|
549 |
$weightphysicalProductAttributes = $physicalProductAttributespurchaseItem->getWeight();
|
|
|
550 |
if ($weightphysicalProductAttributes->isSetValue()) {
|
|
|
551 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.Weight.Value'] = $weightphysicalProductAttributes->getValue();
|
|
|
552 |
}
|
|
|
553 |
if ($weightphysicalProductAttributes->isSetUnit()) {
|
|
|
554 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.Weight.Unit'] = $weightphysicalProductAttributes->getUnit();
|
|
|
555 |
}
|
|
|
556 |
}
|
|
|
557 |
if ($physicalProductAttributespurchaseItem->isSetCondition()) {
|
|
|
558 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.Condition'] = $physicalProductAttributespurchaseItem->getCondition();
|
|
|
559 |
}
|
|
|
560 |
if ($physicalProductAttributespurchaseItem->isSetDeliveryMethod()) {
|
|
|
561 |
$deliveryMethodphysicalProductAttributes = $physicalProductAttributespurchaseItem->getDeliveryMethod();
|
|
|
562 |
if ($deliveryMethodphysicalProductAttributes->isSetServiceLevel()) {
|
|
|
563 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.DeliveryMethod.ServiceLevel'] = $deliveryMethodphysicalProductAttributes->getServiceLevel();
|
|
|
564 |
}
|
|
|
565 |
if ($deliveryMethodphysicalProductAttributes->isSetDisplayableShippingLabel()) {
|
|
|
566 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.DeliveryMethod.DisplayableShippingLabel'] = $deliveryMethodphysicalProductAttributes->getDisplayableShippingLabel();
|
|
|
567 |
}
|
|
|
568 |
if ($deliveryMethodphysicalProductAttributes->isSetDestinationName()) {
|
|
|
569 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.DeliveryMethod.DestinationName'] = $deliveryMethodphysicalProductAttributes->getDestinationName();
|
|
|
570 |
}
|
|
|
571 |
if ($deliveryMethodphysicalProductAttributes->isSetShippingCustomData()) {
|
|
|
572 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.DeliveryMethod.ShippingCustomData'] = $deliveryMethodphysicalProductAttributes->getShippingCustomData();
|
|
|
573 |
}
|
|
|
574 |
}
|
|
|
575 |
if ($physicalProductAttributespurchaseItem->isSetItemCharges()) {
|
|
|
576 |
$itemChargesPhysicalProductAttributes = $physicalProductAttributespurchaseItem->getItemCharges();
|
|
|
577 |
if ($itemChargesPhysicalProductAttributes->isSetTax()) {
|
|
|
578 |
$taxItemCharges = $itemChargesPhysicalProductAttributes->getTax();
|
|
|
579 |
if ($taxItemCharges->isSetAmount()) {
|
|
|
580 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Tax.Amount'] = $taxItemCharges->getAmount();
|
|
|
581 |
}
|
|
|
582 |
if ($taxItemCharges->isSetCurrencyCode()) {
|
|
|
583 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Tax.CurrencyCode'] = $taxItemCharges->getCurrencyCode();
|
|
|
584 |
}
|
|
|
585 |
}
|
|
|
586 |
if ($itemChargesPhysicalProductAttributes->isSetShipping()) {
|
|
|
587 |
$shippingItemCharges = $itemChargesPhysicalProductAttributes->getShipping();
|
|
|
588 |
if ($shippingItemCharges->isSetAmount()) {
|
|
|
589 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Shipping.Amount'] = $shippingItemCharges->getAmount();
|
|
|
590 |
}
|
|
|
591 |
if ($shippingItemCharges->isSetCurrencyCode()) {
|
|
|
592 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Shipping.CurrencyCode'] = $shippingItemCharges->getCurrencyCode();
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
if ($itemChargesPhysicalProductAttributes->isSetPromotions()) {
|
|
|
596 |
$promotionsItemCharges = $itemChargesPhysicalProductAttributes->getPromotions();
|
|
|
597 |
foreach ($promotionsItemCharges->getPromotion() as $promotionPromotionsIndex1 => $promotionPromotions ) {
|
|
|
598 |
$promotionPromotionsIndex = $promotionPromotionsIndex1 + 1;
|
|
|
599 |
if ($promotionPromotions->isSetPromotionId()) {
|
|
|
600 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.PromotionId'] = $promotionPromotions->getPromotionId();
|
|
|
601 |
}
|
|
|
602 |
if ($promotionPromotions->isSetDescription()) {
|
|
|
603 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.Description'] = $promotionPromotions->getDescription();
|
|
|
604 |
}
|
|
|
605 |
if ($promotionPromotions->isSetDiscount()) {
|
|
|
606 |
$discountPromotion = $promotionPromotions->getDiscount();
|
|
|
607 |
if ($discountPromotion->isSetAmount()) {
|
|
|
608 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Promotions.Promotion.'. ($promotionPromotionsIndex) . '.Discount.Amount'] = $discountPromotion->getAmount();
|
|
|
609 |
}
|
|
|
610 |
if ($discountPromotion->isSetCurrencyCode()) {
|
|
|
611 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.PhysicalProductAttributes.ItemCharges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.Discount.CurrencyCode'] = $discountPromotion->getCurrencyCode();
|
|
|
612 |
}
|
|
|
613 |
}
|
|
|
614 |
|
|
|
615 |
}
|
|
|
616 |
}
|
|
|
617 |
}
|
|
|
618 |
}
|
|
|
619 |
if ($purchaseItempurchaseItems->isSetDigitalProductAttributes()) {
|
|
|
620 |
$digitalProductAttributespurchaseItem = $purchaseItempurchaseItems->getDigitalProductAttributes();
|
|
|
621 |
if ($digitalProductAttributespurchaseItem->isSetdummyDigitalProperty()) {
|
|
|
622 |
$parameters['PurchaseItems.PurchaseItem.' . ($purchaseItempurchaseItemsIndex) . '.DigitalProductAttributes.dummyDigitalProperty'] = $digitalProductAttributespurchaseItem->getdummyDigitalProperty();
|
|
|
623 |
}
|
|
|
624 |
}
|
|
|
625 |
$purchaseItempurchaseItemsIndex++;
|
|
|
626 |
}
|
|
|
627 |
}
|
|
|
628 |
|
|
|
629 |
return $parameters;
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Convert CompletePurchaseContractRequest to name value pairs
|
|
|
635 |
*/
|
|
|
636 |
protected function convertCompletePurchaseContract($request) {
|
|
|
637 |
|
|
|
638 |
$parameters = array();
|
|
|
639 |
$parameters['Action'] = 'CompletePurchaseContract';
|
|
|
640 |
if ($request->isSetPurchaseContractId()) {
|
|
|
641 |
$parameters['PurchaseContractId'] = $request->getPurchaseContractId();
|
|
|
642 |
}
|
|
|
643 |
if ($request->isSetIntegratorId()) {
|
|
|
644 |
$parameters['IntegratorId'] = $request->getIntegratorId();
|
|
|
645 |
}
|
|
|
646 |
if ($request->isSetIntegratorName()) {
|
|
|
647 |
$parameters['IntegratorName'] = $request->getIntegratorName();
|
|
|
648 |
}
|
|
|
649 |
if ($request->isSetInstantOrderProcessingNotificationURLs()) {
|
|
|
650 |
$instantOrderProcessingNotificationURLscompletePurchaseContractRequest = $request->getInstantOrderProcessingNotificationURLs();
|
|
|
651 |
if ($instantOrderProcessingNotificationURLscompletePurchaseContractRequest->isSetIntegratorURL()) {
|
|
|
652 |
$parameters['InstantOrderProcessingNotificationURLs.IntegratorURL'] = $instantOrderProcessingNotificationURLscompletePurchaseContractRequest->getIntegratorURL();
|
|
|
653 |
}
|
|
|
654 |
if ($instantOrderProcessingNotificationURLscompletePurchaseContractRequest->isSetMerchantURL()) {
|
|
|
655 |
$parameters['InstantOrderProcessingNotificationURLs.MerchantURL'] = $instantOrderProcessingNotificationURLscompletePurchaseContractRequest->getMerchantURL();
|
|
|
656 |
}
|
|
|
657 |
}
|
|
|
658 |
|
|
|
659 |
return $parameters;
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
|
|
|
663 |
/**
|
|
|
664 |
* Convert SetContractChargesRequest to name value pairs
|
|
|
665 |
*/
|
|
|
666 |
protected function convertSetContractCharges($request) {
|
|
|
667 |
|
|
|
668 |
$parameters = array();
|
|
|
669 |
$parameters['Action'] = 'SetContractCharges';
|
|
|
670 |
if ($request->isSetPurchaseContractId()) {
|
|
|
671 |
$parameters['PurchaseContractId'] = $request->getPurchaseContractId();
|
|
|
672 |
}
|
|
|
673 |
if ($request->isSetCharges()) {
|
|
|
674 |
$chargesSetContractChargesRequest = $request->getCharges();
|
|
|
675 |
if ($chargesSetContractChargesRequest->isSetTax()) {
|
|
|
676 |
$taxCharges = $chargesSetContractChargesRequest->getTax();
|
|
|
677 |
if ($taxCharges->isSetAmount()) {
|
|
|
678 |
$parameters['Charges.Tax.Amount'] = $taxCharges->getAmount();
|
|
|
679 |
}
|
|
|
680 |
if ($taxCharges->isSetCurrencyCode()) {
|
|
|
681 |
$parameters['Charges.Tax.CurrencyCode'] = $taxCharges->getCurrencyCode();
|
|
|
682 |
}
|
|
|
683 |
}
|
|
|
684 |
if ($chargesSetContractChargesRequest->isSetShipping()) {
|
|
|
685 |
$shippingCharges = $chargesSetContractChargesRequest->getShipping();
|
|
|
686 |
if ($shippingCharges->isSetAmount()) {
|
|
|
687 |
$parameters['Charges.Shipping.Amount'] = $shippingCharges->getAmount();
|
|
|
688 |
}
|
|
|
689 |
if ($shippingCharges->isSetCurrencyCode()) {
|
|
|
690 |
$parameters['Charges.Shipping.CurrencyCode'] = $shippingCharges->getCurrencyCode();
|
|
|
691 |
}
|
|
|
692 |
}
|
|
|
693 |
if ($chargesSetContractChargesRequest->isSetPromotions()) {
|
|
|
694 |
$promotionsCharges = $chargesSetContractChargesRequest->getPromotions();
|
|
|
695 |
$promotionPromotionsIndex = 1;
|
|
|
696 |
foreach ($promotionsCharges->getPromotion() as $promotionPromotionsIndex1 => $promotionPromotions) {
|
|
|
697 |
if ($promotionPromotions->isSetPromotionId()) {
|
|
|
698 |
|
|
|
699 |
$parameters['Charges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.PromotionId'] = $promotionPromotions->getPromotionId();
|
|
|
700 |
}
|
|
|
701 |
if ($promotionPromotions->isSetDescription()) {
|
|
|
702 |
$parameters['Charges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.Description'] = $promotionPromotions->getDescription();
|
|
|
703 |
}
|
|
|
704 |
if ($promotionPromotions->isSetDiscount()) {
|
|
|
705 |
$discountPromotion = $promotionPromotions->getDiscount();
|
|
|
706 |
if ($discountPromotion->isSetAmount()) {
|
|
|
707 |
$parameters['Charges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.Discount.Amount'] = $discountPromotion->getAmount();
|
|
|
708 |
}
|
|
|
709 |
if ($discountPromotion->isSetCurrencyCode()) {
|
|
|
710 |
$parameters['Charges.Promotions.Promotion.' . ($promotionPromotionsIndex) . '.Discount.CurrencyCode'] = $discountPromotion->getCurrencyCode();
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
$promotionPromotionsIndex++;
|
|
|
714 |
}
|
|
|
715 |
}
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
return $parameters;
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
?>
|