-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
306 lines (266 loc) · 7.39 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* This file is part of the EkiPayum package.
*
* (c) EkiPower <http://ekipower.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Eki\Payum\Nganluong;
use Eki\Payum\Nganluong\Api\ApiInterface;
use Eki\Payum\Nganluong\Api\AbstractApi;
use Eki\Payum\Nganluong\Api\Errors;
use Eki\Payum\Nganluong\Api\ApiException;
use Buzz\Client\ClientInterface;
use Buzz\Message\Form\FormRequest;
use Eki\Payum\Nganluong\Bridge\Buzz\Response;
use Payum\Core\Bridge\Buzz\ClientFactory;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Exception\RuntimeException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
class Api extends AbstractApi implements LoggerAwareInterface
{
const VERSION = '3.1';
/**
*
* @var Buzz\Client\ClientInterface
*
*/
protected $client;
/**
*
* @var Psr\Log\LoggerInterface
*
*/
private $logger;
/**
*
* @var array
*
*/
protected $options = array(
'merchant_id' => null,
'merchant_password' => null,
'receiver_email' => null,
'sandbox' => null,
'sandbox_url' => null,
'return_url' => null,
'cancel_url' => null,
);
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param array $options
* @param ClientInterface|null $client
*/
public function __construct(array $options, ClientInterface $client = null)
{
$this->options = array_replace($this->options, $options);
if (true == empty($this->options['merchant_id'])) {
throw new \InvalidArgumentException('The merchant id option must be set.');
}
if (true == empty($this->options['merchant_password'])) {
throw new \InvalidArgumentException('The merchant password option must be set.');
}
if (true == empty($this->options['receiver_email'])) {
throw new \InvalidArgumentException('The receiver email as NL account option must be set.');
}
if (false == is_bool($this->options['sandbox'])) {
throw new \InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->client = $client ?: ClientFactory::createCurl();
}
/**
* @inheritdoc
*/
public function getVersion()
{
return self::VERSION;
}
/**
* @inheritdoc
*/
public function checkFields(array $fields)
{
$this->checkVersion($fields);
$this->checkAuthorize($fields);
}
/**
* Check version
* @internal: called by checkFields function
*
* @param array $fields
* @throw ApiException
*/
private function checkVersion(array $fields)
{
if ( false === $fields['version'] || $fields['version'] !== $this->getVersion() )
{
throw new ApiException(Errors::ERRCODE_VERSION_WRONG);
}
}
/**
* Check ahthorize
* @internal: called by checkFields function
*
* @param array $fields
* @throw ApiException
*/
private function checkAuthorize(array $fields)
{
if ( false === $fields['merchant_id'] )
{
throw new ApiException(Errors::ERRCODE_MERCHANT_ID_INVALID);
}
if ( false === $fields['merchant_password'] )
{
throw new ApiException(Errors::ERRCODE_MERCHANT_PASSWORD_INVALID);
}
if ( false === $fields['receiver_email'] )
{
throw new ApiException(Errors::ERRCODE_MERCHANT_EMAIL_INVALID);
}
}
/**
* @inheritdoc
*/
protected function getSupportedFunctions()
{
return array(
'SetExpressCheckout' => 'setExpressCheckout',
'GetTransactionDetail' => 'getTransactionDetails'
);
}
/**
* Wrapper of setExpressCheckout function
*
* @param array $fields
*
*/
public function setExpressCheckout(array $fields)
{
return $this->doFunction($fields + array('function'=>'SetExpressCheckout'));
}
/**
* Require:
*
* @param array $fields
*
* @return array
*/
protected function doFunction_setExpressCheckout(array $fields)
{
$request = new FormRequest;
$request->setFields($fields);
if (!isset($fields['return_url'])) {
if (false == $this->options['return_url']) {
throw new RuntimeException('The return_url must be set either to FormRequest or to options.');
}
$request->setField('return_url', $this->options['return_url']);
}
if (!isset($fields['cancel_url'])) {
if (false == $this->options['cancel_url']) {
throw new RuntimeException('The cancel_url must be set either to FormRequest or to options.');
}
$request->setField('cancel_url', $this->options['cancel_url']);
}
$request->setField('function', 'SetExpressCheckout');
$this->addVersionField($request);
$this->addAuthorizeFields($request);
return $this->doRequest($request);
}
/**
* Wrapper of getTransactionDetails function
*
* @param array $fields
*
*/
public function getTransactionDetails(array $fields)
{
return $this->doFunction($fields + array('function'=>'GetTransactionDetail'));
}
/**
* Require: token
*
* @param array $fields
*
* @return array
*/
protected function doFunction_getTransactionDetails(array $fields)
{
if (!isset($fields['token'])) {
throw new RuntimeException('The token must be set.');
}
$request = new FormRequest;
$request->setFields($fields);
$request->setField('function', 'GetTransactionDetail');
$this->addVersionField($request);
$this->addAuthorizeFields($request);
return $this->doRequest($request);
}
/**
* @param FormRequest $request
*
* @throws HttpException
*
* @return array
*/
protected function doRequest(FormRequest $request)
{
$request->setMethod('POST');
$request->fromUrl($this->getApiEndpoint());
$this->client->send($request, $response = new Response);
if (false == $response->isSuccessful())
{
throw HttpException::factory($request, $response);
}
else
{
return $response->toArray();
}
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return
$this->options['sandbox'] === true ?
(
!isset($this->options['sandbox_url']) ?
'https://www.nganluong.vn/checkout.api.nganluong.post.php' :
$this->options['sandbox_url']
) :
'https://www.nganluong.vn/checkout.api.nganluong.post.php' ;
}
/**
* @param FormRequest $request
*/
protected function addAuthorizeFields(FormRequest $request)
{
$request->setField('merchant_id', $this->options['merchant_id']);
$request->setField('merchant_password', md5($this->options['merchant_password']));
$request->setField('receiver_email', $this->options['receiver_email']);
}
/**
* @param FormRequest $request
*/
protected function addVersionField(FormRequest $request)
{
$request->setField('version', self::VERSION);
}
public function getErrorMessages()
{
return Errors::ErrorMessages();
}
public function getErrorMessage($code)
{
$messages = Errors::ErrorMessages();
return $messages[$code];
}
}