1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiqdev\yii2\merchant\transactions; |
4
|
|
|
|
5
|
|
|
class Transaction |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $parameters; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var boolean |
19
|
|
|
*/ |
20
|
|
|
protected $status; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $merchant; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Transaction constructor. |
29
|
|
|
* @param string $id |
30
|
|
|
* @param $merchant |
31
|
|
|
*/ |
32
|
|
|
public function __construct($id, $merchant) |
33
|
|
|
{ |
34
|
|
|
$this->id = $id; |
35
|
|
|
$this->merchant = $merchant; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getMerchant() |
39
|
|
|
{ |
40
|
|
|
return $this->merchant; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function isCompleted() |
47
|
|
|
{ |
48
|
|
|
return $this->status !== null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function confirm() |
52
|
|
|
{ |
53
|
|
|
$this->status = true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function cancel($error = null) |
57
|
|
|
{ |
58
|
|
|
$this->status = false; |
59
|
|
|
$this->addParameter('error', $error); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isConfirmed() |
63
|
|
|
{ |
64
|
|
|
return $this->status === true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function getStatus() |
71
|
|
|
{ |
72
|
|
|
return $this->status; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getId() |
79
|
|
|
{ |
80
|
|
|
return $this->id; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getParameters() |
87
|
|
|
{ |
88
|
|
|
return $this->parameters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $parameters |
93
|
|
|
*/ |
94
|
|
|
public function setParameters($parameters) |
95
|
|
|
{ |
96
|
|
|
$this->parameters = $parameters; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Adds parameter $parameter with value $value to the parameters |
101
|
|
|
* |
102
|
|
|
* @param string $parameter |
103
|
|
|
* @param mixed $value |
104
|
|
|
*/ |
105
|
|
|
public function addParameter($parameter, $value) |
106
|
|
|
{ |
107
|
|
|
if (isset($this->parameters[$parameter])) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->parameters[$parameter] = $value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function toArray() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
'id' => $this->getId(), |
121
|
|
|
'merchant' => $this->getMerchant(), |
122
|
|
|
'parameters' => $this->getParameters(), |
123
|
|
|
'status' => $this->getStatus() |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getParameter($parameter) |
128
|
|
|
{ |
129
|
|
|
return isset($this->parameters[$parameter]) ? $this->parameters[$parameter] : null; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|