1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
4
|
|
|
* |
5
|
|
|
* NOTICE OF LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace eBayEnterprise\RetailOrderManagement\Payload\Payment; |
17
|
|
|
|
18
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\IPayload; |
19
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\IPayloadMap; |
20
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\ISchemaValidator; |
21
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\IValidatorIterator; |
22
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\TTopLevelPayload; |
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
use Psr\Log\NullLogger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class StoredValueRedeemReply |
28
|
|
|
* @package eBayEnterprise\RetailOrderManagement\Payload\Payment |
29
|
|
|
*/ |
30
|
|
|
class StoredValueRedeemReply implements IStoredValueRedeemReply |
31
|
|
|
{ |
32
|
|
|
use TTopLevelPayload, TPaymentContext; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $pin; |
36
|
|
|
/** @var float */ |
37
|
|
|
protected $amountRedeemed; |
38
|
|
|
/** @var string */ |
39
|
|
|
protected $amountRedeemedCurrencyCode; |
40
|
|
|
/** @var float */ |
41
|
|
|
protected $balanceAmount; |
42
|
|
|
/** @var string */ |
43
|
|
|
protected $balanceAmountCurrencyCode; |
44
|
|
|
/** @var string */ |
45
|
|
|
protected $responseCode; |
46
|
|
|
/** @var array response codes that are considered a success */ |
47
|
|
|
protected $successResponseCodes = ['Success']; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param IValidatorIterator |
51
|
|
|
* @param ISchemaValidator |
52
|
|
|
* @param IPayloadMap |
53
|
|
|
* @param LoggerInterface |
54
|
|
|
* @param IPayload |
55
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
58
|
|
|
IValidatorIterator $validators, |
59
|
|
|
ISchemaValidator $schemaValidator, |
60
|
|
|
IPayloadMap $payloadMap, |
|
|
|
|
61
|
|
|
LoggerInterface $logger, |
62
|
|
|
IPayload $parentPayload = null |
63
|
|
|
) { |
64
|
|
|
$this->logger = $logger; |
|
|
|
|
65
|
|
|
$this->validators = $validators; |
66
|
|
|
$this->schemaValidator = $schemaValidator; |
67
|
|
|
$this->parentPayload = $parentPayload; |
68
|
|
|
|
69
|
|
|
$this->extractionPaths = [ |
70
|
|
|
'orderId' => 'string(x:PaymentContext/x:OrderId)', |
71
|
|
|
'cardNumber' => 'string(x:PaymentContext/x:PaymentAccountUniqueId)', |
72
|
|
|
'responseCode' => 'string(x:ResponseCode)', |
73
|
|
|
'amountRedeemed' => 'number(x:AmountRedeemed)', |
74
|
|
|
'amountRedeemedCurrencyCode' => 'string(x:AmountRedeemed/@currencyCode)', |
75
|
|
|
'balanceAmount' => 'number(x:BalanceAmount)', |
76
|
|
|
'balanceAmountCurrencyCode' => 'string(x:BalanceAmount/@currencyCode)', |
77
|
|
|
]; |
78
|
|
|
$this->booleanExtractionPaths = [ |
79
|
|
|
'panIsToken' => 'string(x:PaymentContext/x:PaymentAccountUniqueId/@isToken)', |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getAmountRedeemed() |
84
|
|
|
{ |
85
|
|
|
return $this->amountRedeemed; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function setAmountRedeemed($amount) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if (is_float($amount)) { |
91
|
|
|
$this->amountRedeemed = round($amount, 2, PHP_ROUND_HALF_UP); |
92
|
|
|
} else { |
93
|
|
|
$this->amountRedeemed = null; |
94
|
|
|
} |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getAmountRedeemedCurrencyCode() |
99
|
|
|
{ |
100
|
|
|
return $this->amountRedeemedCurrencyCode; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function setAmountRedeemedCurrencyCode($code) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$value = null; |
106
|
|
|
|
107
|
|
|
$cleaned = $this->cleanString($code, 3); |
108
|
|
|
if ($cleaned !== null) { |
109
|
|
|
if (!strlen($cleaned) < 3) { |
110
|
|
|
$value = $cleaned; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$this->amountRedeemedCurrencyCode = $value; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getBalanceAmount() |
119
|
|
|
{ |
120
|
|
|
return $this->balanceAmount; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function setBalanceAmount($amount) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
if (is_float($amount)) { |
126
|
|
|
$this->balanceAmount = round($amount, 2, PHP_ROUND_HALF_UP); |
127
|
|
|
} else { |
128
|
|
|
$this->balanceAmount = null; |
129
|
|
|
} |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getBalanceAmountCurrencyCode() |
134
|
|
|
{ |
135
|
|
|
return $this->balanceAmountCurrencyCode; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function setBalanceAmountCurrencyCode($code) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$value = null; |
141
|
|
|
|
142
|
|
|
$cleaned = $this->cleanString($code, 3); |
143
|
|
|
if ($cleaned !== null) { |
144
|
|
|
if (!strlen($cleaned) < 3) { |
145
|
|
|
$value = $cleaned; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
$this->balanceAmountCurrencyCode = $value; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The 3-character ISO 4217 code that represents |
155
|
|
|
* the type of currency being used for a transaction. |
156
|
|
|
* |
157
|
|
|
* @link http://www.iso.org/iso/home/standards/currency_codes.htm |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getCurrencyCodeRedeemed() |
161
|
|
|
{ |
162
|
|
|
return $this->amountRedeemedCurrencyCode; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string |
167
|
|
|
* @return self |
168
|
|
|
*/ |
169
|
|
|
public function setCurrencyCodeRedeemed($code) |
170
|
|
|
{ |
171
|
|
|
$this->amountRedeemedCurrencyCode = $code; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* The 3-character ISO 4217 code that represents |
177
|
|
|
* the type of currency being used for a transaction. |
178
|
|
|
* |
179
|
|
|
* @link http://www.iso.org/iso/home/standards/currency_codes.htm |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getBalanceCurrencyCode() |
183
|
|
|
{ |
184
|
|
|
return $this->balanceAmountCurrencyCode; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
|
|
public function setBalanceCurrencyCode($code) |
192
|
|
|
{ |
193
|
|
|
$this->balanceAmountCurrencyCode = $code; |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Whether the gift card was successfully redeemed. |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function wasRedeemed() |
202
|
|
|
{ |
203
|
|
|
return in_array($this->getResponseCode(), $this->successResponseCodes, true); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Serialize the various parts of the payload into XML strings and |
208
|
|
|
* simply concatenate them together. |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
protected function serializeContents() |
212
|
|
|
{ |
213
|
|
|
return $this->serializePaymentContext() |
214
|
|
|
. $this->serializeResponseCode() |
215
|
|
|
. $this->serializeAmounts('AmountRedeemed', $this->getAmountRedeemed(), $this->getAmountRedeemedCurrencyCode()) |
216
|
|
|
. $this->serializeAmounts('BalanceAmount', $this->getBalanceAmount(), $this->getBalanceAmountCurrencyCode()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Build the response code node |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
protected function serializeResponseCode() |
224
|
|
|
{ |
225
|
|
|
return "<ResponseCode>{$this->xmlEncode($this->getResponseCode())}</ResponseCode>"; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* The result of the request transaction. |
230
|
|
|
* |
231
|
|
|
* xsd note: possible values: Fail, Success, Timeout |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function getResponseCode() |
235
|
|
|
{ |
236
|
|
|
return $this->responseCode; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string |
241
|
|
|
* @return self |
242
|
|
|
*/ |
243
|
|
|
public function setResponseCode($code) |
244
|
|
|
{ |
245
|
|
|
$this->responseCode = $code; |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Build an Amount node |
251
|
|
|
* @param string |
252
|
|
|
* @param float |
253
|
|
|
* @param string |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
protected function serializeAmounts($amountType, $amount, $currencyCode) |
257
|
|
|
{ |
258
|
|
|
return sprintf( |
259
|
|
|
'<%s currencyCode="%s">%1.02F</%1$s>', |
260
|
|
|
$amountType, |
261
|
|
|
$this->xmlEncode($currencyCode), |
262
|
|
|
$amount |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Build the Pin node |
268
|
|
|
* |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
protected function serializePin() |
272
|
|
|
{ |
273
|
|
|
return "<Pin>{$this->xmlEncode($this->getPin())}</Pin>"; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function getPin() |
277
|
|
|
{ |
278
|
|
|
return $this->pin; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function setPin($pin) |
282
|
|
|
{ |
283
|
|
|
$this->pin = $this->cleanString($pin, 8); |
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
protected function getSchemaFile() |
288
|
|
|
{ |
289
|
|
|
return $this->getSchemaDir() . self::XSD; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Return the name of the xml root node. |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
protected function getRootNodeName() |
298
|
|
|
{ |
299
|
|
|
return static::ROOT_NODE; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* The XML namespace for the payload. |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
protected function getXmlNamespace() |
308
|
|
|
{ |
309
|
|
|
return static::XML_NS; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.