|
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 Closure; |
|
14
|
|
|
use hiqdev\php\merchant\AbstractMerchant; |
|
15
|
|
|
use hiqdev\php\merchant\Helper; |
|
16
|
|
|
use hiqdev\yii2\merchant\Collection; |
|
17
|
|
|
use hiqdev\yii2\merchant\models\DepositForm; |
|
18
|
|
|
use hiqdev\yii2\merchant\controllers\PayController; |
|
19
|
|
|
use hiqdev\yii2\merchant\models\DepositRequest; |
|
20
|
|
|
use hiqdev\yii2\merchant\models\PurchaseRequest; |
|
21
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
|
22
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionException; |
|
23
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionRepositoryInterface; |
|
24
|
|
|
use Yii; |
|
25
|
|
|
use yii\base\InvalidConfigException; |
|
26
|
|
|
use yii\helpers\ArrayHelper; |
|
27
|
|
|
use yii\helpers\FileHelper; |
|
28
|
|
|
use yii\helpers\Json; |
|
29
|
|
|
use yii\helpers\Url; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Merchant Module. |
|
33
|
|
|
* |
|
34
|
|
|
* Example application configuration: |
|
35
|
|
|
* |
|
36
|
|
|
* ```php |
|
37
|
|
|
* 'modules' => [ |
|
38
|
|
|
* 'merchant' => [ |
|
39
|
|
|
* 'class' => 'hiqdev\yii2\merchant\Module', |
|
40
|
|
|
* 'notifyPage' => '/my/notify/page', |
|
41
|
|
|
* 'collection' => [ |
|
42
|
|
|
* 'PayPal' => [ |
|
43
|
|
|
* 'purse' => $params['paypal_purse'], |
|
44
|
|
|
* 'secret' => $params['paypal_secret'], /// NEVER keep secret in source control |
|
45
|
|
|
* ], |
|
46
|
|
|
* 'webmoney_usd' => [ |
|
47
|
|
|
* 'gateway' => 'WebMoney', |
|
48
|
|
|
* 'purse' => $params['webmoney_purse'], |
|
49
|
|
|
* 'secret' => $params['webmoney_secret'], /// NEVER keep secret in source control |
|
50
|
|
|
* ], |
|
51
|
|
|
* ], |
|
52
|
|
|
* ], |
|
53
|
|
|
* ], |
|
54
|
|
|
* ``` |
|
55
|
|
|
* |
|
56
|
|
|
* @var string returns username for usage in merchant |
|
57
|
|
|
*/ |
|
58
|
|
|
class Module extends \yii\base\Module |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* The URL prefix that will be used as a key to save current URL in the session. |
|
62
|
|
|
* |
|
63
|
|
|
* @see rememberUrl() |
|
64
|
|
|
* @see previousUrl() |
|
65
|
|
|
* @see \yii\helpers\BaseUrl::remember() |
|
66
|
|
|
* @see \yii\helpers\BaseUrl::previous() |
|
67
|
|
|
*/ |
|
68
|
|
|
const URL_PREFIX = 'merchant_url_'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string merchant collection class name. Defaults to [[Collection]] |
|
72
|
|
|
*/ |
|
73
|
|
|
public $purchaseRequestCollectionClass = Collection::class; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var string Deposit model class name. Defaults to [[DepositForm]] |
|
77
|
|
|
*/ |
|
78
|
|
|
public $depositFromClass = DepositForm::class; |
|
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 PurchaseRequest[] list of merchants |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getPurchaseRequestCollection($depositRequest = null) |
|
101
|
|
|
{ |
|
102
|
|
|
return Yii::createObject([ |
|
103
|
|
|
'class' => $this->purchaseRequestCollectionClass, |
|
104
|
|
|
'module' => $this, |
|
105
|
|
|
'depositRequest' => $depositRequest, |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $merchant_name merchant id |
|
111
|
|
|
* @param DepositRequest $depositRequest |
|
112
|
|
|
* @return PurchaseRequest merchant instance |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getPurchaseRequest($merchant_name, DepositRequest $depositRequest) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getPurchaseRequestCollection($depositRequest)->get($merchant_name); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Checks if merchant exists in the hub. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $id merchant id |
|
123
|
|
|
* @return bool whether merchant exist |
|
124
|
|
|
*/ |
|
125
|
|
|
public function hasPurchaseRequest($id) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getPurchaseRequestCollection()->has($id); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Method builds data for merchant request. |
|
132
|
|
|
* |
|
133
|
|
|
* @param DepositRequest $depositRequest |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
public function prepareRequestData($depositRequest) |
|
137
|
|
|
{ |
|
138
|
|
|
$depositRequest->username = $this->getUsername(); |
|
139
|
|
|
$depositRequest->notifyUrl = $this->buildUrl('notify', $depositRequest); |
|
140
|
|
|
$depositRequest->returnUrl = $this->buildUrl('return', $depositRequest); |
|
141
|
|
|
$depositRequest->cancelUrl = $this->buildUrl('cancel', $depositRequest); |
|
142
|
|
|
$depositRequest->finishUrl = $this->buildUrl('finish', $depositRequest); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @var string client login |
|
147
|
|
|
*/ |
|
148
|
|
|
protected $_username; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Sets [[_username]]. |
|
152
|
|
|
* |
|
153
|
|
|
* @param $username |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setUsername($username) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->_username = $username; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Gets [[_username]] when defined, otherwise - `Yii::$app->user->identity->username`, |
|
162
|
|
|
* otherwise `Yii::$app->user->identity->getId()`. |
|
163
|
|
|
* @throws InvalidConfigException |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getUsername() |
|
167
|
|
|
{ |
|
168
|
|
|
if (isset($this->_username)) { |
|
169
|
|
|
return $this->_username; |
|
170
|
|
|
} elseif (($identity = Yii::$app->user->identity) !== null) { |
|
171
|
|
|
if ($identity->hasProperty('username')) { |
|
172
|
|
|
$this->_username = $identity->username; |
|
173
|
|
|
} else { |
|
174
|
|
|
$this->_username = $identity->getId(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $this->_username; |
|
178
|
|
|
} |
|
179
|
|
|
throw new InvalidConfigException('Unable to determine username'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @var string|array the URL that will be used for payment system notifications. Will be passed through [[Url::to()]] |
|
184
|
|
|
*/ |
|
185
|
|
|
public $notifyPage = 'notify'; |
|
186
|
|
|
/** |
|
187
|
|
|
* @var string|array the URL that will be used to redirect client from the merchant after the success payment. |
|
188
|
|
|
* Will be passed through [[Url::to()]] |
|
189
|
|
|
*/ |
|
190
|
|
|
public $returnPage = 'return'; |
|
191
|
|
|
/** |
|
192
|
|
|
* @var string|array the URL that will be used to redirect client from the merchant after the failed payment. |
|
193
|
|
|
* Will be passed through [[Url::to()]] |
|
194
|
|
|
*/ |
|
195
|
|
|
public $cancelPage = 'cancel'; |
|
196
|
|
|
/** |
|
197
|
|
|
* @var string|array the URL that might be used to redirect used from the success or error page to the finish page. |
|
198
|
|
|
* Will be passed through [[Url::to()]] |
|
199
|
|
|
*/ |
|
200
|
|
|
public $finishPage = 'finish'; |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Builds URLs that will be passed in the request to the merchant. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $destination `notify`, `return`, `cancel` |
|
206
|
|
|
* @param DepositRequest $depositRequest |
|
207
|
|
|
* @return string URL |
|
208
|
|
|
*/ |
|
209
|
|
|
public function buildUrl($destination, DepositRequest $depositRequest) |
|
210
|
|
|
{ |
|
211
|
|
|
$page = [ |
|
212
|
|
|
$this->getPage($destination), |
|
213
|
|
|
'username' => $depositRequest->username, |
|
214
|
|
|
'merchant' => $depositRequest->merchant, |
|
215
|
|
|
'transactionId' => $depositRequest->id, |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
if (is_array($page)) { |
|
219
|
|
|
$page[0] = $this->localizePage($page[0]); |
|
220
|
|
|
} else { |
|
221
|
|
|
$page = $this->localizePage($page); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return Url::to($page, true); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Builds url to `this_module/pay/$page` if page is not /full/page. |
|
229
|
|
|
* @param mixed $page |
|
230
|
|
|
* @return mixed |
|
231
|
|
|
*/ |
|
232
|
|
|
public function localizePage($page) |
|
233
|
|
|
{ |
|
234
|
|
|
return is_string($page) && $page[0] !== '/' ? ('/' . $this->id . '/pay/' . $page) : $page; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function getPage($destination) |
|
238
|
|
|
{ |
|
239
|
|
|
$name = $destination . 'Page'; |
|
240
|
|
|
|
|
241
|
|
|
return $this->hasProperty($name) ? $this->{$name} : $destination; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Saves the $url to session with [[URL_PREFIX]] key, trailed with $name. |
|
246
|
|
|
* |
|
247
|
|
|
* @param array|string $url |
|
248
|
|
|
* @param string $name the trailing part for the URL save key. Defaults to `back` |
|
249
|
|
|
* @void |
|
250
|
|
|
*/ |
|
251
|
|
|
public function rememberUrl($url, $name = 'back') |
|
252
|
|
|
{ |
|
253
|
|
|
Url::remember($url, static::URL_PREFIX . $name); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Extracts the URL from session storage, saved with [[URL_PREFIX]] key, trailed with $name. |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $name the trailing part for the URL save key. Defaults to `back` |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
|
|
public function previousUrl($name = 'back') |
|
263
|
|
|
{ |
|
264
|
|
|
return Url::previous(static::URL_PREFIX . $name); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @var PayController The Payment controller |
|
269
|
|
|
*/ |
|
270
|
|
|
protected $_payController; |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @throws InvalidConfigException |
|
274
|
|
|
* |
|
275
|
|
|
* @return PayController |
|
276
|
|
|
*/ |
|
277
|
|
|
public function getPayController() |
|
278
|
|
|
{ |
|
279
|
|
|
if ($this->_payController === null) { |
|
280
|
|
|
$this->_payController = $this->createControllerById('pay'); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $this->_payController; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Renders page, that contains list of payment systems, that might be choosen by user. |
|
288
|
|
|
* Should be implemented in `PayController`. |
|
289
|
|
|
* |
|
290
|
|
|
* @param array $params |
|
291
|
|
|
* @return \yii\web\Response |
|
292
|
|
|
*/ |
|
293
|
|
|
public function renderDeposit(array $params) |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->getPayController()->renderDeposit($params); |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param Transaction $transaction |
|
300
|
|
|
* @return Transaction |
|
301
|
|
|
*/ |
|
302
|
|
|
public function saveTransaction($transaction) |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->transactionRepository->save($transaction); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
public function insertTransaction($id, $merchant, $data) |
|
308
|
|
|
{ |
|
309
|
|
|
$transaction = $this->transactionRepository->create($id, $merchant, $data); |
|
310
|
|
|
|
|
311
|
|
|
return $this->transactionRepository->insert($transaction); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param string $id transaction ID |
|
316
|
|
|
* @return Transaction|null |
|
317
|
|
|
*/ |
|
318
|
|
|
public function findTransaction($id) |
|
319
|
|
|
{ |
|
320
|
|
|
try { |
|
321
|
|
|
return $this->transactionRepository->findById($id); |
|
322
|
|
|
} catch (TransactionException $e) { |
|
323
|
|
|
return null; |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.