1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\controllers; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\Merchant; |
14
|
|
|
use hiqdev\hiart\ResponseErrorException; |
15
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
16
|
|
|
use Yii; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class PayController. |
20
|
|
|
* @property \hipanel\modules\finance\Module $module |
21
|
|
|
*/ |
22
|
|
|
class PayController extends \hiqdev\yii2\merchant\controllers\PayController |
23
|
|
|
{ |
24
|
|
|
public function getMerchantModule() |
25
|
|
|
{ |
26
|
|
|
return $this->module->getMerchant(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function render($view, $params = []) |
30
|
|
|
{ |
31
|
|
|
return $this->getMerchantModule()->getPayController()->render($view, $params); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function checkNotify() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$id = Yii::$app->request->get('transactionId') ?: Yii::$app->request->post('transactionId'); |
37
|
|
|
$transaction = $this->getMerchantModule()->findTransaction($id); |
38
|
|
|
if ($transaction === null) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$data = array_merge([ |
43
|
|
|
'transactionId' => $transaction->getId(), |
44
|
|
|
'username' => $transaction->getParameter('username'), |
45
|
|
|
'merchant' => $transaction->getParameter('merchant'), |
46
|
|
|
], $_REQUEST); |
47
|
|
|
Yii::info(http_build_query($data), 'merchant'); |
48
|
|
|
|
49
|
|
|
return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) { |
50
|
|
|
$result = Merchant::perform('pay', $data); |
51
|
|
|
return $this->completeTransaction($transaction, $result); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Transaction $transaction |
57
|
|
|
* @param $response |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
protected function completeTransaction($transaction, $response) |
61
|
|
|
{ |
62
|
|
|
if ($transaction->isCompleted() || isset($data['_error'])) { |
|
|
|
|
63
|
|
|
return $transaction; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$transaction->complete(); |
|
|
|
|
67
|
|
|
$transaction->addParameter('bill_id', $response['id']); |
68
|
|
|
|
69
|
|
|
$this->getMerchantModule()->saveTransaction($transaction); |
70
|
|
|
|
71
|
|
|
return $transaction; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: