1
|
|
|
<?php |
2
|
|
|
namespace CultureKings\Afterpay\Service\InStore; |
3
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
5
|
|
|
use CultureKings\Afterpay\Model; |
6
|
|
|
use DateTime; |
7
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
use GuzzleHttp\HandlerStack; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Order |
13
|
|
|
* @package CultureKings\Afterpay\Service\InStore |
14
|
|
|
*/ |
15
|
|
|
class Order extends AbstractService |
16
|
|
|
{ |
17
|
|
|
const ERROR_DECLINED = 402; |
18
|
|
|
const ERROR_MINIMUM_NOT_MET = 402; |
19
|
|
|
const ERROR_EXCEED_PREAPPROVAL = 402; |
20
|
|
|
const ERROR_CONFLICT = 409; |
21
|
|
|
const ERROR_INVALID_CODE = 412; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Model\InStore\Order $order |
25
|
|
|
* @param HandlerStack|null $stack |
26
|
|
|
* |
27
|
|
|
* @return array|\JMS\Serializer\scalar|object |
28
|
|
|
*/ |
29
|
|
|
public function create(Model\InStore\Order $order, HandlerStack $stack = null) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
$params = $this->generateParams($order, $stack); |
33
|
|
|
|
34
|
|
|
$result = $this->getClient()->post('orders', $params); |
35
|
|
|
|
36
|
|
|
return $this->getSerializer()->deserialize( |
37
|
|
|
(string) $result->getBody(), |
38
|
|
|
Model\InStore\Order::class, |
39
|
|
|
'json' |
40
|
|
|
); |
41
|
|
|
} catch (BadResponseException $e) { |
42
|
|
|
throw new ApiException( |
43
|
|
|
$this->getSerializer()->deserialize( |
44
|
|
|
(string) $e->getResponse()->getBody(), |
45
|
|
|
Model\ErrorResponse::class, |
46
|
|
|
'json' |
47
|
|
|
), |
48
|
|
|
$e |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Model\InStore\Reversal $orderReversal |
55
|
|
|
* @param HandlerStack|null $stack |
56
|
|
|
* @throws ApiException |
57
|
|
|
* |
58
|
|
|
* @return Model\InStore\Reversal |
59
|
|
|
*/ |
60
|
|
|
public function reverse(Model\InStore\Reversal $orderReversal, HandlerStack $stack = null) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$params = $this->generateParams($orderReversal, $stack); |
64
|
|
|
|
65
|
|
|
$result = $this->getClient()->post('orders/reverse', $params); |
66
|
|
|
|
67
|
|
|
/** @var Model\InStore\Reversal $reversal */ |
68
|
|
|
$reversal = $this->getSerializer()->deserialize( |
69
|
|
|
(string) $result->getBody(), |
70
|
|
|
Model\InStore\Reversal::class, |
71
|
|
|
'json' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $reversal; |
75
|
|
|
} catch (BadResponseException $e) { |
76
|
|
|
throw new ApiException( |
77
|
|
|
$this->getSerializer()->deserialize( |
78
|
|
|
(string) $e->getResponse()->getBody(), |
79
|
|
|
Model\ErrorResponse::class, |
80
|
|
|
'json' |
81
|
|
|
), |
82
|
|
|
$e |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Helper method to automatically attempt to reverse an order if an error occurs. |
89
|
|
|
* |
90
|
|
|
* Order reversal model does not have to be passed in and will be automatically generated if not. |
91
|
|
|
* |
92
|
|
|
* @param Model\InStore\Order $order |
93
|
|
|
* @param Model\InStore\Reversal|null $orderReversal |
94
|
|
|
* @param HandlerStack|null $stack |
95
|
|
|
* |
96
|
|
|
* @return Model\InStore\Order|Model\InStore\Reversal|array|\JMS\Serializer\scalar|object |
97
|
|
|
*/ |
98
|
|
|
public function createOrReverse( |
99
|
|
|
Model\InStore\Order $order, |
100
|
|
|
Model\InStore\Reversal $orderReversal = null, |
101
|
|
|
HandlerStack $stack = null |
102
|
|
|
) { |
103
|
|
|
$errorResponse = null; |
|
|
|
|
104
|
|
|
try { |
105
|
|
|
return $this->create($order, $stack); |
106
|
|
|
} catch (ApiException $e) { |
107
|
|
|
// http://docs.afterpay.com.au/instore-api-v1.html#create-order |
108
|
|
|
// Should a success or error response (with exception to 409 conflict) not be received, |
109
|
|
|
// the POS should queue the request ID for reversal |
110
|
|
|
if ($e->getErrorResponse()->getErrorCode() == self::ERROR_CONFLICT) { |
111
|
|
|
throw $e; |
112
|
|
|
} |
113
|
|
|
$errorResponse = $e->getErrorResponse(); |
114
|
|
|
} catch (RequestException $e) { |
115
|
|
|
// a timeout or other exception has occurred. attempt a reversal |
116
|
|
|
$errorResponse = new Model\ErrorResponse(); |
117
|
|
|
$errorResponse->setHttpStatusCode($e->getResponse()->getStatusCode()); |
118
|
|
|
$errorResponse->setMessage($e->getMessage()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($orderReversal === null) { |
122
|
|
|
$orderReversal = new Model\InStore\Reversal(); |
|
|
|
|
123
|
|
|
$orderReversal->setReversingRequestId($order->getRequestId()); |
124
|
|
|
$orderReversal->setRequestedAt(new DateTime()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$reversal = $this->reverse($orderReversal, $stack); |
128
|
|
|
$reversal->setErrorReason($errorResponse); |
129
|
|
|
|
130
|
|
|
return $reversal; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.