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.

FutubankPayment::content()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace app\components\payment;
4
5
use app\modules\shop\models\OrderTransaction;
6
use yii\helpers\Json;
7
use yii\web\BadRequestHttpException;
8
use yii\web\ServerErrorHttpException;
9
10
class FutubankPayment extends AbstractPayment
11
{
12
    protected $currency;
13
    protected $merchant;
14
    protected $secretKey;
15
    protected $testing;
16
17
    /**
18
     * @param $data
19
     * @return string
20
     */
21
    private function getSignature($data)
22
    {
23
        ksort($data);
24
        $chunks = array();
25
        foreach ($data as $key => $value) {
26
            if ($value && ($key != 'signature')) {
27
                $chunks[] = $key . '=' . base64_encode($value);
28
            }
29
        }
30
        return sha1($this->secretKey . sha1($this->secretKey . implode('&', $chunks)));
31
    }
32
33
    /**
34
     * @param int $length
35
     * @return string
36
     */
37
    private function generateSalt($length = 10)
38
    {
39
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
40
        $result = '';
41
        for ($i = 0; $i < $length; $i++) {
42
            $result .= $characters[rand(0, strlen($characters) - 1)];
43
        }
44
        return $result;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function content()
51
    {
52
        $formData = [
53
            'client_email' => '',
54
            'client_name' => '',
55
            'client_phone' => '',
56
            'merchant' => $this->merchant,
57
            'unix_timestamp' => time(),
58
            'salt' => $this->generateSalt(32),
59
            'amount' => $this->transaction->total_sum,
60
            'currency' => $this->currency,
61
            'description' => 'Order #' . $this->order->id,
62
            'order_id' => $this->transaction->id,
63
            'success_url' => $this->createSuccessUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\db\ActiveRecordInterface as the method generateHash() does only exist in the following implementations of said interface: app\modules\shop\models\OrderTransaction.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
            'fail_url' => $this->createFailUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\db\ActiveRecordInterface as the method generateHash() does only exist in the following implementations of said interface: app\modules\shop\models\OrderTransaction.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
            'cancel_url' => $this->createCancelUrl(['id' => $this->transaction->id, 'hash' => $this->transaction->generateHash()]),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\db\ActiveRecordInterface as the method generateHash() does only exist in the following implementations of said interface: app\modules\shop\models\OrderTransaction.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
66
            'meta' => Json::encode(['transactionId' => $this->transaction->id]),
67
        ];
68
        if ($this->testing == 1) {
69
            $formData['testing'] = 1;
70
        }
71
        $formData['signature'] = $this->getSignature($formData);
72
        return $this->render(
73
            'futubank',
74
            [
75
                'formData' => $formData,
76
                'order' => $this->order,
77
                'transaction' => $this->transaction,
78
            ]
79
        );
80
    }
81
82
    /**
83
     * @param string $hash
84
     * @return string
85
     * @throws BadRequestHttpException
86
     * @throws ServerErrorHttpException
87
     * @throws \yii\web\NotFoundHttpException
88
     */
89
    public function checkResult($hash = '')
90
    {
91
        $transactionId = \Yii::$app->request->post('order_id');
92
        if (null === $transactionId) {
93
            throw new BadRequestHttpException();
94
        }
95
        if (null === $model = $this->loadTransaction($transactionId)) {
96
            throw new BadRequestHttpException();
97
        }
98
99
        $model->result_data = Json::encode(\Yii::$app->request->post());
100
        if ($this->getSignature(\Yii::$app->request->post()) === \Yii::$app->request->post('signature')) {
101
            $model->status = OrderTransaction::TRANSACTION_SUCCESS;
102
            if ($model->save(true, ['status', 'result_data'])) {
103
                return 'OK ' . $model->id;
104
            } else {
105
                throw new ServerErrorHttpException();
106
            }
107
        } else {
108
            $model->status = OrderTransaction::TRANSACTION_ERROR;
109
            $model->save(true, ['status', 'result_data']);
110
            throw new BadRequestHttpException();
111
        }
112
    }
113
}