1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yii2 extension for payment processing with Omnipay, Payum and more later. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-merchant |
6
|
|
|
* @package yii2-merchant |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\merchant; |
12
|
|
|
|
13
|
|
|
use hiqdev\yii2\merchant\models\DepositForm; |
14
|
|
|
use hiqdev\yii2\merchant\controllers\PayController; |
15
|
|
|
use hiqdev\yii2\merchant\models\DepositRequest; |
16
|
|
|
use hiqdev\yii2\merchant\models\PurchaseRequest; |
17
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
18
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionException; |
19
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionRepositoryInterface; |
20
|
|
|
use Yii; |
21
|
|
|
use yii\base\InvalidConfigException; |
22
|
|
|
use yii\helpers\Url; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Merchant Module. |
26
|
|
|
* |
27
|
|
|
* Example application configuration: |
28
|
|
|
* |
29
|
|
|
* ```php |
30
|
|
|
* 'modules' => [ |
31
|
|
|
* 'merchant' => [ |
32
|
|
|
* 'class' => 'hiqdev\yii2\merchant\Module', |
33
|
|
|
* 'notifyPage' => '/my/notify/page', |
34
|
|
|
* 'collection' => [ |
35
|
|
|
* 'PayPal' => [ |
36
|
|
|
* 'purse' => $params['paypal_purse'], |
37
|
|
|
* 'secret' => $params['paypal_secret'], /// NEVER keep secret in source control |
38
|
|
|
* ], |
39
|
|
|
* 'webmoney_usd' => [ |
40
|
|
|
* 'gateway' => 'WebMoney', |
41
|
|
|
* 'purse' => $params['webmoney_purse'], |
42
|
|
|
* 'secret' => $params['webmoney_secret'], /// NEVER keep secret in source control |
43
|
|
|
* ], |
44
|
|
|
* ], |
45
|
|
|
* ], |
46
|
|
|
* ], |
47
|
|
|
* ``` |
48
|
|
|
* |
49
|
|
|
* @var string returns username for usage in merchant |
50
|
|
|
*/ |
51
|
|
|
class Module extends \yii\base\Module |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* The URL prefix that will be used as a key to save current URL in the session. |
55
|
|
|
* |
56
|
|
|
* @see rememberUrl() |
57
|
|
|
* @see previousUrl() |
58
|
|
|
* @see \yii\helpers\BaseUrl::remember() |
59
|
|
|
* @see \yii\helpers\BaseUrl::previous() |
60
|
|
|
*/ |
61
|
|
|
const URL_PREFIX = 'merchant_url_'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string merchant collection class name. Defaults to [[Collection]] |
65
|
|
|
*/ |
66
|
|
|
public $purchaseRequestCollectionClass = Collection::class; |
67
|
|
|
/** |
68
|
|
|
* @var string currencies collection class name. Defaults to [[Collection]] |
69
|
|
|
*/ |
70
|
|
|
public $currenciesCollectionClass; |
71
|
|
|
/** |
72
|
|
|
* @var string Deposit model class name. Defaults to [[DepositForm]] |
73
|
|
|
*/ |
74
|
|
|
public $depositFromClass = DepositForm::class; |
75
|
|
|
/** |
76
|
|
|
* @var bool Whether to use payment processing only through Cashew |
77
|
|
|
*/ |
78
|
|
|
public bool $cashewOnly = false; |
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var TransactionRepositoryInterface |
81
|
|
|
*/ |
82
|
|
|
protected $transactionRepository; |
83
|
|
|
|
84
|
|
|
public function __construct($id, $parent = null, TransactionRepositoryInterface $transactionRepository, array $config = []) |
85
|
|
|
{ |
86
|
|
|
parent::__construct($id, $parent, $config); |
87
|
|
|
|
88
|
|
|
$this->transactionRepository = $transactionRepository; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setCollection(array $collection) |
92
|
|
|
{ |
93
|
|
|
$this->_collection = $collection; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param DepositRequest $depositRequest |
98
|
|
|
* @return Collection |
99
|
|
|
* @throws InvalidConfigException |
100
|
|
|
*/ |
101
|
|
|
public function getPurchaseRequestCollection($depositRequest = null) |
102
|
|
|
{ |
103
|
|
|
return Yii::createObject([ |
104
|
|
|
'class' => $this->purchaseRequestCollectionClass, |
105
|
|
|
'module' => $this, |
106
|
|
|
'depositRequest' => $depositRequest, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Currencies |
112
|
|
|
* @throws InvalidConfigException |
113
|
|
|
*/ |
114
|
|
|
public function getAvailableCurrenciesCollection(): Currencies |
115
|
|
|
{ |
116
|
|
|
return Yii::createObject([ |
117
|
|
|
'class' => $this->currenciesCollectionClass, |
118
|
|
|
'module' => $this, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $merchant_name merchant id |
124
|
|
|
* @param DepositRequest $depositRequest |
125
|
|
|
* @return PurchaseRequest merchant instance |
126
|
|
|
*/ |
127
|
|
|
public function getPurchaseRequest($merchant_name, DepositRequest $depositRequest) |
128
|
|
|
{ |
129
|
|
|
return $this->getPurchaseRequestCollection($depositRequest)->get($merchant_name); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Checks if merchant exists in the hub. |
134
|
|
|
* |
135
|
|
|
* @param string $id merchant id |
136
|
|
|
* @return bool whether merchant exist |
137
|
|
|
*/ |
138
|
|
|
public function hasPurchaseRequest($id) |
139
|
|
|
{ |
140
|
|
|
return $this->getPurchaseRequestCollection()->has($id); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Method builds data for merchant request. |
145
|
|
|
* |
146
|
|
|
* @param DepositRequest $depositRequest |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function prepareRequestData($depositRequest) |
150
|
|
|
{ |
151
|
|
|
$depositRequest->username = $this->getUsername(); |
152
|
|
|
$depositRequest->notifyUrl = $this->buildUrl('notify', $depositRequest); |
153
|
|
|
$depositRequest->returnUrl = $this->buildUrl('return', $depositRequest); |
154
|
|
|
$depositRequest->cancelUrl = $this->buildUrl('cancel', $depositRequest); |
155
|
|
|
$depositRequest->finishUrl = $this->buildUrl('finish', $depositRequest); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @var string client login |
160
|
|
|
*/ |
161
|
|
|
protected $_username; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets [[_username]]. |
165
|
|
|
* |
166
|
|
|
* @param $username |
167
|
|
|
*/ |
168
|
|
|
public function setUsername($username) |
169
|
|
|
{ |
170
|
|
|
$this->_username = $username; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Gets [[_username]] when defined, otherwise - `Yii::$app->user->identity->username`, |
175
|
|
|
* otherwise `Yii::$app->user->identity->getId()`. |
176
|
|
|
* @throws InvalidConfigException |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getUsername() |
180
|
|
|
{ |
181
|
|
|
if (isset($this->_username)) { |
182
|
|
|
return $this->_username; |
183
|
|
|
} elseif (($identity = Yii::$app->user->identity) !== null) { |
184
|
|
|
if ($identity->hasProperty('username')) { |
185
|
|
|
$this->_username = $identity->username; |
186
|
|
|
} else { |
187
|
|
|
$this->_username = $identity->getId(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->_username; |
191
|
|
|
} |
192
|
|
|
throw new InvalidConfigException('Unable to determine username'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @var string|array the URL that will be used for payment system notifications. Will be passed through [[Url::to()]] |
197
|
|
|
*/ |
198
|
|
|
public $notifyPage = 'notify'; |
199
|
|
|
/** |
200
|
|
|
* @var string|array the URL that will be used to redirect client from the merchant after the success payment. |
201
|
|
|
* Will be passed through [[Url::to()]] |
202
|
|
|
*/ |
203
|
|
|
public $returnPage = 'return'; |
204
|
|
|
/** |
205
|
|
|
* @var string|array the URL that will be used to redirect client from the merchant after the failed payment. |
206
|
|
|
* Will be passed through [[Url::to()]] |
207
|
|
|
*/ |
208
|
|
|
public $cancelPage = 'cancel'; |
209
|
|
|
/** |
210
|
|
|
* @var string|array the URL that might be used to redirect used from the success or error page to the finish page. |
211
|
|
|
* Will be passed through [[Url::to()]] |
212
|
|
|
*/ |
213
|
|
|
public $finishPage = 'finish'; |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Builds URLs that will be passed in the request to the merchant. |
217
|
|
|
* |
218
|
|
|
* @param string $destination `notify`, `return`, `cancel` |
219
|
|
|
* @param DepositRequest $depositRequest |
220
|
|
|
* @return string URL |
221
|
|
|
*/ |
222
|
|
|
public function buildUrl($destination, DepositRequest $depositRequest) |
223
|
|
|
{ |
224
|
|
|
$page = [ |
225
|
|
|
$this->getPage($destination, $depositRequest), |
226
|
|
|
'username' => $depositRequest->username, |
227
|
|
|
'merchant' => $depositRequest->merchant, |
228
|
|
|
'transactionId' => $depositRequest->id, |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
if (is_array($page)) { |
232
|
|
|
$page[0] = $this->localizePage($page[0]); |
233
|
|
|
} else { |
234
|
|
|
$page = $this->localizePage($page); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return Url::to($page, true); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Builds url to `this_module/pay/$page` if page is not /full/page. |
242
|
|
|
* @param mixed $page |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function localizePage($page) |
246
|
|
|
{ |
247
|
|
|
return is_string($page) && $page[0] !== '/' ? ('/' . $this->id . '/pay/' . $page) : $page; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getPage($destination, DepositRequest $depositRequest) |
251
|
|
|
{ |
252
|
|
|
$property = $destination . 'Url'; |
253
|
|
|
if ($depositRequest->$property) { |
254
|
|
|
return $depositRequest->$property; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$name = $destination . 'Page'; |
258
|
|
|
|
259
|
|
|
return $this->hasProperty($name) ? $this->{$name} : $destination; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Saves the $url to session with [[URL_PREFIX]] key, trailed with $name. |
264
|
|
|
* |
265
|
|
|
* @param array|string $url |
266
|
|
|
* @param string $name the trailing part for the URL save key. Defaults to `back` |
267
|
|
|
* @void |
268
|
|
|
*/ |
269
|
|
|
public function rememberUrl($url, $name = 'back') |
270
|
|
|
{ |
271
|
|
|
Url::remember($url, static::URL_PREFIX . $name); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Extracts the URL from session storage, saved with [[URL_PREFIX]] key, trailed with $name. |
276
|
|
|
* |
277
|
|
|
* @param string $name the trailing part for the URL save key. Defaults to `back` |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
public function previousUrl($name = 'back') |
281
|
|
|
{ |
282
|
|
|
return Url::previous(static::URL_PREFIX . $name); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @var PayController The Payment controller |
287
|
|
|
*/ |
288
|
|
|
protected $_payController; |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @throws InvalidConfigException |
292
|
|
|
* |
293
|
|
|
* @return PayController |
294
|
|
|
*/ |
295
|
|
|
public function getPayController() |
296
|
|
|
{ |
297
|
|
|
if ($this->_payController === null) { |
298
|
|
|
$this->_payController = $this->createControllerById('pay'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this->_payController; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Renders page, that contains list of payment systems, that might be choosen by user. |
306
|
|
|
* Should be implemented in `PayController`. |
307
|
|
|
* |
308
|
|
|
* @param DepositForm $form |
309
|
|
|
* @return \yii\web\Response |
310
|
|
|
*/ |
311
|
|
|
public function renderDeposit($form) |
312
|
|
|
{ |
313
|
|
|
return $this->getPayController()->renderDeposit($form); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param Transaction $transaction |
318
|
|
|
* @return Transaction |
319
|
|
|
*/ |
320
|
|
|
public function saveTransaction($transaction) |
321
|
|
|
{ |
322
|
|
|
return $this->transactionRepository->save($transaction); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function insertTransaction($id, $merchant, $data) |
326
|
|
|
{ |
327
|
|
|
$transaction = $this->transactionRepository->create($id, $merchant, $data); |
328
|
|
|
|
329
|
|
|
return $this->transactionRepository->insert($transaction); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param string $id transaction ID |
334
|
|
|
* @return Transaction|null |
335
|
|
|
*/ |
336
|
|
|
public function findTransaction($id) |
337
|
|
|
{ |
338
|
|
|
try { |
339
|
|
|
return $this->transactionRepository->findById($id); |
340
|
|
|
} catch (TransactionException $e) { |
341
|
|
|
return null; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|