|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPieces\ANZGateway; |
|
4
|
|
|
|
|
5
|
|
|
use PHPieces\ANZGateway\exceptions\RequiredArgumentException; |
|
6
|
|
|
use PHPieces\ANZGateway\models\ResponseCode; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
class Charge |
|
10
|
|
|
{ |
|
11
|
|
|
const AMOUNT = 'vpc_Amount'; |
|
12
|
|
|
const BATCH_NO = 'vpc_BatchNo'; |
|
13
|
|
|
const CARD = 'vpc_Card'; |
|
14
|
|
|
const LOCALE = 'vpc_Locale'; |
|
15
|
|
|
const MERCH_TXN_REF = 'vpc_MerchTxnRef'; |
|
16
|
|
|
const ORDER_INFO = 'vpc_OrderInfo'; |
|
17
|
|
|
const RECEIPT_NO = 'vpc_ReceiptNo'; |
|
18
|
|
|
const TRANSACTION_NO = 'vpc_TransactionNo'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var ResponseCode |
|
23
|
|
|
*/ |
|
24
|
|
|
public $responseCode; |
|
25
|
|
|
public $amount; |
|
26
|
|
|
public $batchNo; |
|
27
|
|
|
public $card; |
|
28
|
|
|
public $locale; |
|
29
|
|
|
public $merchTxnRef; |
|
30
|
|
|
public $orderInfo; |
|
31
|
|
|
public $receiptNo; |
|
32
|
|
|
public $transactionNo; |
|
33
|
|
|
public $responseContent; |
|
34
|
|
|
private $failedValidation = false; |
|
35
|
|
|
|
|
36
|
15 |
|
public function __construct($params) |
|
37
|
|
|
{ |
|
38
|
15 |
|
$this->formDecode($params); |
|
39
|
15 |
|
$this->validate(); |
|
40
|
15 |
|
} |
|
41
|
|
|
|
|
42
|
15 |
|
private function formDecode(string $params) : void |
|
43
|
|
|
{ |
|
44
|
15 |
|
$output = []; |
|
45
|
15 |
|
parse_str($params, $output); |
|
46
|
15 |
|
$this->responseContent = $params; |
|
47
|
15 |
|
$this->responseCode = ResponseCode::create($output); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
//Required |
|
50
|
15 |
|
$this->transactionNo = @$output[self::TRANSACTION_NO]; |
|
51
|
|
|
|
|
52
|
|
|
//From Input |
|
53
|
15 |
|
$this->amount = @$output[self::AMOUNT]; |
|
54
|
15 |
|
$this->locale = @$output[self::LOCALE]; |
|
55
|
15 |
|
$this->merchTxnRef = @$output[self::MERCH_TXN_REF]; |
|
56
|
15 |
|
$this->orderInfo = @$output[self::ORDER_INFO]; |
|
57
|
|
|
|
|
58
|
|
|
//Optional |
|
59
|
15 |
|
$this->batchNo = @$output[self::BATCH_NO]; |
|
60
|
15 |
|
$this->card = @$output[self::CARD]; |
|
61
|
15 |
|
$this->receiptNo = @$output[self::RECEIPT_NO]; |
|
62
|
|
|
|
|
63
|
15 |
|
$this->validate(); |
|
64
|
15 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* |
|
68
|
|
|
* @throws RequiredArgumentException |
|
69
|
|
|
* @throws Exception |
|
70
|
|
|
*/ |
|
71
|
15 |
|
private function validate() : void |
|
72
|
|
|
{ |
|
73
|
15 |
|
if (empty($this->transactionNo)) { |
|
74
|
3 |
|
$this->failedValidation = true; |
|
75
|
|
|
} |
|
76
|
15 |
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
public function isSuccess() : bool |
|
79
|
|
|
{ |
|
80
|
6 |
|
if ($this->failedValidation) { |
|
81
|
3 |
|
return false; |
|
82
|
|
|
} |
|
83
|
3 |
|
return $this->responseCode->isSuccess(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
public function getMessage() : string |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->responseCode->getErrorMessage(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
public function toArray() : array |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
3 |
|
'responseContent' => $this->responseContent, |
|
95
|
3 |
|
'responseCode' => $this->responseCode->toJson(), |
|
96
|
3 |
|
'amount' => $this->amount, |
|
97
|
3 |
|
'batchNo' => $this->batchNo, |
|
98
|
3 |
|
'card' => $this->card, |
|
99
|
3 |
|
'locale' => $this->locale, |
|
100
|
3 |
|
'merchTxnRef' => $this->merchTxnRef, |
|
101
|
3 |
|
'orderInfo' => $this->orderInfo, |
|
102
|
3 |
|
'receiptNo' => $this->receiptNo, |
|
103
|
3 |
|
'transactionNo' => $this->transactionNo, |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.