Completed
Push — master ( 60539d...725f3a )
by Dmitry
07:23
created

PayController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 4
cp 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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\controllers;
12
13
use hiqdev\yii2\merchant\actions\RequestAction;
14
use hiqdev\yii2\merchant\models\DepositForm;
15
use hiqdev\yii2\merchant\models\DepositRequest;
16
use hiqdev\yii2\merchant\Module;
17
use hiqdev\yii2\merchant\transactions\Transaction;
18
use Yii;
19
use yii\base\InvalidConfigException;
20
use yii\web\BadRequestHttpException;
21
use yii\web\Response;
22
23
class PayController extends \yii\web\Controller
24
{
25
    public function actions()
26
    {
27
        return array_merge(parent::actions(), [
28
            'request' => [
29
                'class' => RequestAction::class
30
            ]
31
        ]);
32
    }
33
34
    /**
35
     * @return Module|\yii\base\Module
36
     */
37
    public function getMerchantModule()
38
    {
39
        return $this->module;
40
    }
41
42
    /**
43
     * Disable CSRF validation for POST requests we receive from outside
44
     * {@inheritdoc}
45
     */
46
    public function beforeAction($action)
47
    {
48
        if (in_array($this->action->id, ['notify', 'return', 'cancel', 'proxy-notification'], true)) {
49
            Yii::$app->controller->enableCsrfValidation = false;
50
        }
51
52
        return parent::beforeAction($action);
53
    }
54
55
    /**
56
     * @return Response
57
     */
58
    public function actionCancel()
59
    {
60
        Yii::$app->session->addFlash('error', Yii::t('merchant', 'Payment failed or cancelled'));
61
62
        return $this->redirect($this->getMerchantModule()->previousUrl() ?: ['deposit']);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function actionReturn()
69
    {
70
        $transaction = $this->checkNotify();
71
        if ($transaction === null) {
72
            return $this->actionCancel();
73
        }
74
75
        return $this->render('return', [
76
            'transactionId' => $transaction->getId(),
77
        ]);
78
    }
79
80
    /**
81
     * @param string $transactionId
82
     * @throws BadRequestHttpException
83
     * @return array
84
     */
85
    public function actionCheckReturn($transactionId)
86
    {
87
        Yii::$app->response->format = Response::FORMAT_JSON;
88
        $transaction = $this->getMerchantModule()->findTransaction($transactionId);
89
90
        if ($transaction->getParameter('username') !== $this->getMerchantModule()->getUsername()) {
91
            throw new BadRequestHttpException('Access denied', 403);
92
        }
93
94
        return [
95
            'status' => $transaction->getSuccess(),
96
            'url'    => $transaction->isConfirmed()
97
                ? $transaction->getParameter('finishUrl')
98
                : $transaction->getParameter('cancelUrl'),
99
        ];
100
    }
101
102
    /**
103
     * Action is designed to get the system notification from payment system,
104
     * process it and report success or error for the payment system.
105
     * @return null|string
106
     */
107
    public function actionNotify()
108
    {
109
        $transaction = $this->checkNotify();
110
        if ($transaction === null) {
111
            return 'Unknown transaction';
112
        }
113
114
        Yii::$app->response->format = Response::FORMAT_RAW;
115
116
        return $transaction->isConfirmed() ? 'OK' : $transaction->getParameter('error');
117
    }
118
119
    /**
120
     * Check notifications.
121
     * TODO: implement actual request check and proper handling.
122
     * @return Transaction
123
     */
124
    public function checkNotify()
125
    {
126
        throw new InvalidConfigException('Method checkNotify must be implemented');
127
    }
128
129
    public function actionProxyNotification()
130
    {
131
        throw new InvalidConfigException('Method actionProxyNotification must be implemented');
132
    }
133
134
    public function actionDeposit()
135
    {
136
        $model   = Yii::createObject($this->getMerchantModule()->depositFromClass);
137
        $request = Yii::$app->request;
138
        if ($model->load($request->isPost ? $request->post() : $request->get()) && $model->validate()) {
139
            return $this->renderDeposit($model);
140
        }
141
142
        return $this->render('deposit-form', [
143
            'model' => $model,
144
            'availableMerchants' => $this->getMerchantModule()->getPurchaseRequestCollection()->getItems(),
145
        ]);
146
    }
147
148
    /**
149
     * Renders depositing buttons for given request data.
150
     *
151
     * @param DepositForm $form request data
152
     * @return \yii\web\Response
153
     */
154
    public function renderDeposit($form)
155
    {
156
        $request = new DepositRequest();
157
        $request->amount = $form->amount;
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type string, but $form->amount is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
158
        $request->finishUrl = $form->finishUrl;
159
160
        $requests = $this->getMerchantModule()->getPurchaseRequestCollection($request)->getItems();
161
162
        return $this->render('deposit', [
163
            'requests' => $requests,
164
            'depositForm' => $form
165
        ]);
166
    }
167
}
168