GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Click   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 6
dl 0
loc 174
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A run() 0 25 4
A Prepare() 0 48 3
B Complete() 0 44 7
A check_for_required_field() 0 15 4
A getRedirectParams() 0 20 1
1
<?php
2
namespace Goodoneuz\PayUz\Http\Classes\Click;
3
4
use Goodoneuz\PayUz\Models\Transaction;
5
use Goodoneuz\PayUz\Models\PaymentSystem;
6
use Goodoneuz\PayUz\Services\PaymentService;
7
use Goodoneuz\PayUz\Http\Classes\DataFormat;
8
use Goodoneuz\PayUz\Http\Classes\BaseGateway;
9
use Goodoneuz\PayUz\Http\Classes\PaymentException;
10
use Goodoneuz\PayUz\Services\PaymentSystemService;
11
12
class Click extends BaseGateway 
13
{
14
    private $config;
15
    private $merchant;
16
    private $request;
17
    private $response;
18
    const REQUEST_PREPARE = 0;
19
    const REQUEST_COMPLATE = 1;
20
21
    public function __construct()
22
    {
23
        $this->config   = PaymentSystemService::getPaymentSystemParamsCollect(PaymentSystem::CLICK);
24
        $this->request  = request();
25
        $this->response = new Response();
26
        $this->merchant = new Merchant($this->response);
27
    }
28
29
30
    public function run(){
31
32
        $required_fields = [
33
            'click_trans_id', 'service_id', 
34
            'click_paydoc_id', 'merchant_trans_id', 
35
            'amount', 'action', 'error', 'error_note', 
36
            'sign_time', 'sign_string'
37
        ];
38
39
        $res = $this->check_for_required_field($required_fields);
40
        if (!$res){
41
            $this->response->setResult(Response::ERROR_REQUEST_FROM);
42
        }
43
        $this->merchant->validateRequest($this->request->all());
44
        switch ($this->request->all()['action']) {
45
            case self::REQUEST_PREPARE:
46
                $this->Prepare();
47
                break;
48
            case self::REQUEST_COMPLATE:
49
                $this->Complete();
50
                break;
51
            default:
52
                $this->response->setResult(Response::ERROR_ACTION_NOT_FOUND);
53
        }
54
    }
55
    
56
    private function Prepare()
57
    {
58
        
59
        $params = $this->request->all();
60
        
61
        $additional_params = [
62
            'merchant_prepare_id' => null,
63
            'click_trans_id' => null,
64
            'merchant_trans_id' => null
65
        ];
66
67
        $model = PaymentService::convertKeyToModel($this->request['merchant_trans_id']);
68
69
        if(!$model)
70
            $this->response->setResult(Response::ERROR_ORDER_NOT_FOUND);
71
72
        PaymentService::payListener($model,1*($this->request->amount),'before-pay');
73
74
        if (!PaymentService::isProperModelAndAmount($model, $params['amount']))
75
            $this->response->setResult(Response::ERROR_INVALID_AMOUNT);
76
        $additional_params['click_trans_id'] = $params['click_trans_id'];
77
        $additional_params['merchant_trans_id'] = $params['merchant_trans_id'];
78
79
        $create_time = DataFormat::timestamp(true);
80
81
        $detail = json_encode(array(
82
            'create_time'           => $create_time,
83
            'system_time_datetime'  => DataFormat::timestamp2datetime($params['sign_time'])
84
        ));
85
86
        $transaction = Transaction::create([
87
            'payment_system'        => PaymentSystem::CLICK,
88
            'system_transaction_id' => $params['click_trans_id'],
89
            'amount'                => $params['amount'],
90
            'currency_code'         => Transaction::CURRENCY_CODE_UZS,
91
            'state'                 => Transaction::STATE_CREATED,
92
            'updated_time'          => 1*$create_time,
93
            'comment'               => $params['error_note'],
94
            'detail'                => $detail,
95
            'transactionable_type'  => get_class($model),
96
            'transactionable_id'    => $model->id
97
        ]);
98
99
        $additional_params['merchant_prepare_id'] = $transaction->id;
100
        PaymentService::payListener($model,$transaction,'paying');
101
        
102
        $this->response->setResult(Response::SUCCESS,$additional_params);
103
    }
104
    private function Complete()
105
    {
106
        $params = $this->request->all();
107
108
        $additional_params = [
109
            'click_trans_id' => $params['click_trans_id'],
110
            'merchant_trans_id' => $params['merchant_trans_id'],
111
            'merchant_confirm_id' => null
112
        ];
113
114
        $transaction = Transaction::find($params['merchant_prepare_id']);
115
        if (!$transaction)
116
            $this->response->setResult(Response::ERROR_TRANSACTION_NOT_FOUND);
117
        
118
        if ($params['error'] == -1){
119
            $additional_params['error_note'] = $params['error_note'];
120
            $this->response->setResult(Response::ERROR_ALREADY_PAID);
121
        }
122
123
        if ($params['error'] == -5017){
124
            $additional_params['error_note'] = $params['error_note'];
125
            $transaction->state = Transaction::STATE_CANCELLED;
126
            $transaction->update();
127
            $this->response->setResult(Response::ERROR_TRANSACTION_CANCELLED);
128
        }
129
        
130
        if ($transaction->state == Transaction::STATE_CANCELLED)
131
            $this->response->setResult(Response::ERROR_TRANSACTION_CANCELLED);
132
133
        if ($transaction->state != Transaction::STATE_CREATED)
134
            $this->response->setResult(Response::ERROR_ALREADY_PAID);
135
136
        if ($transaction->amount != $params['amount']){
137
            $this->response->setResult(Response::ERROR_INVALID_AMOUNT);
138
        }
139
140
        $transaction->state = Transaction::STATE_COMPLETED;
141
        $transaction->update();
142
        
143
        $additional_params['merchant_confirm_id'] = $transaction->id;
144
        
145
        PaymentService::payListener(null,$transaction,'after-pay');
146
        $this->response->setResult(Response::SUCCESS,$additional_params);
147
    }
148
149
    private function check_for_required_field($fields)
150
    {
151
        $arr = $this->request->all();
152
153
        if ($arr['action'] == self::REQUEST_COMPLATE)
154
            $fields[] = 'merchant_prepare_id';
155
156
        foreach ($fields as $field)
157
            if(!array_key_exists($field, $arr)){
158
                echo $field;
159
                return false;
160
            }
161
162
        return true;
163
    }
164
    
165
    public function getRedirectParams($model, $amount, $currency, $url)
166
    {
167
        $time = date('Y-m-d H:i:s', time());
168
        $sign = MD5($time . $this->config['secret_key'] .
169
        $this->config['service_id'] . $amount);
170
        return [
171
            'MERCHANT_TRANS_AMOUNT' => $amount,
172
            'MERCHANT_ID' => $this->config['merchant_id'],
173
            'MERCHANT_USER_ID' => $this->config['merchant_user_id'],
174
            'MERCHANT_SERVICE_ID' => $this->config['service_id'],
175
            'MERCHANT_TRANS_ID' => PaymentService::convertModelToKey($model),
176
            'MERCHANT_TRANS_NOTE' => '',
177
            'MERCHANT_USER_PHONE' => '',
178
            'MERCHANT_USER_EMAIL' => '',
179
            'SIGN_TIME' => $time,
180
            'SIGN_STRING' => $sign,
181
            'RETURN_URL' => $url,
182
            'url'       => 'https://my.click.uz/pay/'
183
        ];
184
    }
185
}   
186