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 Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException; |
14
|
|
|
use hipanel\modules\finance\models\Merchant; |
15
|
|
|
use hiqdev\hiart\ResponseErrorException; |
16
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
17
|
|
|
use Yii; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class PayController. |
21
|
|
|
* @property \hipanel\modules\finance\Module $module |
22
|
|
|
*/ |
23
|
|
|
class PayController extends \hiqdev\yii2\merchant\controllers\PayController |
24
|
|
|
{ |
25
|
|
|
public function getMerchantModule() |
26
|
|
|
{ |
27
|
|
|
return $this->module->getMerchant(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function render($view, $params = []) |
31
|
|
|
{ |
32
|
|
|
return $this->getMerchantModule()->getPayController()->render($view, $params); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function checkNotify() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$id = Yii::$app->request->get('transactionId') ?: Yii::$app->request->post('transactionId'); |
38
|
|
|
$transaction = $this->getMerchantModule()->findTransaction($id); |
39
|
|
|
if ($transaction === null) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$data = array_merge([ |
44
|
|
|
'transactionId' => $transaction->getId(), |
45
|
|
|
'username' => $transaction->getParameter('username'), |
46
|
|
|
'merchant' => $transaction->getParameter('merchant'), |
47
|
|
|
], $_REQUEST); |
48
|
|
|
Yii::info(http_build_query($data), 'merchant'); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
return Yii::$app->get('hiart')->callWithDisabledAuth(function () use ($transaction, $data) { |
52
|
|
|
$result = Merchant::perform('pay', $data); |
53
|
|
|
|
54
|
|
|
return $this->completeTransaction($transaction, $result); |
55
|
|
|
}); |
56
|
|
|
} catch (ResponseErrorException $e) { |
57
|
|
|
// Does not matter |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Transaction $transaction |
64
|
|
|
* @param $response |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
protected function completeTransaction($transaction, $response) |
68
|
|
|
{ |
69
|
|
|
if ($transaction->isCompleted() || isset($data['_error'])) { |
|
|
|
|
70
|
|
|
return $transaction; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$transaction->complete(); |
|
|
|
|
74
|
|
|
$transaction->addParameter('bill_id', $response['id']); |
75
|
|
|
|
76
|
|
|
$this->getMerchantModule()->saveTransaction($transaction); |
77
|
|
|
|
78
|
|
|
return $transaction; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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: