1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpMob package. |
5
|
|
|
* |
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace PhpMob\Omise\Domain; |
15
|
|
|
|
16
|
|
|
use PhpMob\Omise\Country; |
17
|
|
|
use PhpMob\Omise\Currency; |
18
|
|
|
use PhpMob\Omise\Exception\InvalidRequestArgumentException; |
19
|
|
|
use PhpMob\Omise\Model; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Ishmael Doss <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @property string id |
25
|
|
|
* @property string object |
26
|
|
|
* @property bool livemode |
27
|
|
|
* @property string location |
28
|
|
|
* @property string status |
29
|
|
|
* @property int amount |
30
|
|
|
* @property string currency |
31
|
|
|
* @property string description |
32
|
|
|
* @property string metadata |
33
|
|
|
* @property bool capture |
34
|
|
|
* @property bool authorized |
35
|
|
|
* @property bool reversed |
36
|
|
|
* @property bool paid |
37
|
|
|
* @property string transaction |
38
|
|
|
* @property Card card |
39
|
|
|
* @property int refunded |
40
|
|
|
* @property Pagination refunds |
41
|
|
|
* @property string failureCode |
42
|
|
|
* @property string failureMessage |
43
|
|
|
* @property Customer customer |
44
|
|
|
* @property string ip |
45
|
|
|
* @property Dispute dispute |
46
|
|
|
* @property string created |
47
|
|
|
* @property string returnUri |
48
|
|
|
* @property string authorizeUri |
49
|
|
|
* @property string $cardToken |
50
|
|
|
* @property Source $source |
51
|
|
|
*/ |
52
|
|
|
class Charge extends Model |
53
|
|
|
{ |
54
|
|
|
const STATUS_FAILED = 'failed'; |
55
|
|
|
const STATUS_EXPIRED = 'expired'; |
56
|
|
|
const STATUS_PENDING = 'pending'; |
57
|
|
|
const STATUS_REVERSED = 'reversed'; |
58
|
|
|
const STATUS_SUCCESSFUL = 'successful'; |
59
|
|
|
|
60
|
|
|
const EVENT_CREATE = 'charge.create'; |
61
|
|
|
const EVENT_UPDATE = 'charge.update'; |
62
|
|
|
const EVENT_CAPTURE = 'charge.capture'; |
63
|
|
|
const EVENT_REVERSE = 'charge.reverse'; |
64
|
|
|
const EVENT_COMPLETE = 'charge.complete'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
6 |
|
*/ |
69
|
|
|
public function __set($name, $value) |
70
|
6 |
|
{ |
71
|
4 |
|
if ('currency' === $name) { |
72
|
|
|
$value = strtolower($value); |
73
|
|
|
} |
74
|
6 |
|
|
75
|
6 |
|
parent::__set($name, $value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $countryCode |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
4 |
|
*/ |
83
|
|
|
public function getCreateData($countryCode = Country::TH) |
84
|
4 |
|
{ |
85
|
|
View Code Duplication |
if (!in_array($this->currency, Currency::getSupporteds($countryCode))) { |
|
|
|
|
86
|
|
|
throw new InvalidRequestArgumentException( |
87
|
|
|
sprintf('The currency `%s` is not supported in your country `%s`.', $this->currency, $countryCode) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
return [ |
92
|
4 |
|
'customer' => (string)($this->customer), |
93
|
4 |
|
'card' => $this->card, |
94
|
4 |
|
'amount' => $this->amount * Currency::getDivisionOffset($this->currency), |
95
|
4 |
|
'currency' => $this->currency, |
96
|
4 |
|
'description' => $this->description, |
97
|
4 |
|
'metadata' => $this->metadata, |
98
|
4 |
|
'capture' => $this->capture, |
99
|
|
|
'return_uri' => $this->returnUri, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $countryCode |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function getCreateUsingTokenData($countryCode = Country::TH) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$data = $this->getCreateData($countryCode); |
111
|
|
|
$data['card'] = $this->cardToken; |
112
|
|
|
|
113
|
|
|
unset($data['customer']); |
114
|
|
|
|
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $countryCode |
120
|
1 |
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
1 |
View Code Duplication |
public function getCreateUsingSourceData($countryCode = Country::TH) |
|
|
|
|
124
|
1 |
|
{ |
125
|
|
|
$data = $this->getCreateData($countryCode); |
126
|
|
|
$data['source'] = $this->source->id; |
127
|
|
|
|
128
|
|
|
unset($data['card'], $data['customer']); |
129
|
|
|
|
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getUpdateData() |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
'description' => $this->description, |
140
|
|
|
'metadata' => $this->metadata, |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array|Refund[] |
146
|
|
|
*/ |
147
|
|
|
public function getRefunds() |
148
|
|
|
{ |
149
|
|
|
if (count($this->refunds)) { |
150
|
|
|
return $this->refunds->data; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return []; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $id |
158
|
|
|
* |
159
|
|
|
* @return Refund|null |
160
|
|
|
*/ |
161
|
|
|
public function findRefunds($id) |
162
|
|
|
{ |
163
|
|
|
$charge = array_filter( |
164
|
|
|
$this->getRefunds(), |
165
|
|
|
function (Refund $charge) use ($id) { |
166
|
|
|
return $charge->id === $id; |
167
|
|
|
} |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
if (empty($charge)) { |
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $charge[0]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.