Passed
Pull Request — master (#85)
by
unknown
09:15 queued 06:17
created

JibitBase::generateNewToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 13
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 19
rs 9.8333
1
<?php
2
namespace Shetabit\Multipay\Drivers\Jibit;
3
4
5
class JibitBase
6
{
7
8
    public $accessToken;
9
    private $apiKey;
10
    private $secretKey;
11
    private $refreshToken;
12
    private $cache;
13
    public $base_url;
14
    
15
    public function __construct($apiKey, $secretKey,$base_url)
16
    {
17
        $this->base_url = $base_url;
18
        $this->apiKey = $apiKey;
19
        $this->secretKey = $secretKey;
20
        $this->cache = new JibitCache('jibit');
21
    }
22
23
    /**
24
     * @param int $amount
25
     * @param string $referenceNumber
26
     * @param string $userIdentifier
27
     * @param string $callbackUrl
28
     * @param string $currency
29
     * @param null $description
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $description is correct as it would always require null to be passed?
Loading history...
30
     * @param $additionalData
31
     * @return bool|mixed|string
32
     * @throws Exception
33
     */
34
    public function paymentRequest($amount, $referenceNumber, $userIdentifier, $callbackUrl, $currency = 'RIALS', $description = null, $additionalData = null)
35
    {
36
        $this->generateToken();
37
        $data = [
38
            'merchantCode' => $this->apiKey,
39
            'password' => $this->secretKey,
40
            'amount' => $amount,
41
            'referenceNumber' => $referenceNumber,
42
            'userIdentifier' => $userIdentifier,
43
            'callbackUrl' => $callbackUrl,
44
            'currency' => $currency,
45
            'description' => $description,
46
            'additionalData' => $additionalData,
47
        ];
48
        return $this->callCurl('/orders', $data, true);
49
    }
50
51
    /**
52
     * @param $id
53
     * @return bool|mixed|string
54
     * @throws Exception
55
     */
56
    public function getOrderById($id)
57
    {
58
        return  $this->callCurl('/orders/' .$id, [], true, 0,'GET');
59
60
    }
61
62
    /**
63
     * @param bool $isForce
64
     * @return string
65
     * @throws Exception
66
     */
67
    private function generateToken($isForce = false)
68
    {
69
        $this->cache->eraseExpired();
70
71
        if ($isForce === false && $this->cache->isCached('accessToken')) {
72
            return $this->setAccessToken($this->cache->retrieve('accessToken'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setAccessToken($t...etrieve('accessToken')) targeting Shetabit\Multipay\Driver...tBase::setAccessToken() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
        } else if ($this->cache->isCached('refreshToken')) {
74
            $refreshToken = $this->refreshTokens();
75
            if ($refreshToken !== 'ok') {
76
                return $this->generateNewToken();
77
            }
78
        } else {
79
            return $this->generateNewToken();
80
        }
81
82
        throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException( 'unExcepted Err in generateToken.');
83
84
    }
85
86
    private function refreshTokens()
87
    {
88
        echo 'refreshing';
89
        $data = [
90
            'accessToken' => str_replace('Bearer ', '', $this->cache->retrieve('accessToken')),
91
            'refreshToken' => $this->cache->retrieve('refreshToken'),
92
        ];
93
        $result = $this->callCurl('/tokens/refresh', $data, false);
94
        if (empty($result['accessToken'])) {
95
            throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException( 'Err in refresh token.');
96
        }
97
        if (!empty($result['accessToken'])) {
98
            $this->cache->store('accessToken', 'Bearer ' . $result['accessToken'], 24 * 60 * 60 - 60);
99
            $this->cache->store('refreshToken', $result['refreshToken'], 48 * 60 * 60 - 60);
100
            $this->setAccessToken('Bearer ' . $result['accessToken']);
101
            $this->setRefreshToken($result['refreshToken']);
102
            return 'ok';
103
        }
104
        throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException( 'unExcepted Err in refreshToken.');
105
106
    }
107
108
    /**
109
     * @param $url
110
     * @param $arrayData
111
     * @param bool $haveAuth
112
     * @param int $try
113
     * @param string $method
114
     * @return bool|mixed|string
115
     * @throws Exception
116
     */
117
    private function callCurl($url, $arrayData, $haveAuth = false, $try = 0, $method = 'POST')
118
    {
119
        $data = $arrayData;
120
        $jsonData = json_encode($data);
121
        $accessToken = '';
122
        if ($haveAuth) {
123
            $accessToken = $this->getAccessToken();
124
        }
125
        $ch = curl_init($this->base_url . $url);
126
        curl_setopt($ch, CURLOPT_USERAGENT, 'Jibit.class Rest Api');
127
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
128
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
129
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
130
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
131
            'Content-Type: application/json',
132
            'Authorization: ' . $accessToken,
133
            'Content-Length: ' . strlen($jsonData)
134
        ));
135
        $result = curl_exec($ch);
136
        $err = curl_error($ch);
137
        $result = json_decode($result, true);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $result = json_decode(/** @scrutinizer ignore-type */ $result, true);
Loading history...
138
        curl_close($ch);
139
140
        if ($err) {
141
            throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException('cURL Error #:' . $err);
142
        }
143
        if (empty($result['errors'])) {
144
            return $result;
145
        }
146
        if ($haveAuth === true && $result['errors'][0]['code'] === 'security.auth_required') {
147
            $this->generateToken(true);
148
            if ($try === 0) {
149
                return $this->callCurl($url, $arrayData, $haveAuth, 1, $method);
150
            }
151
            throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException('Err in auth.');
152
        }
153
154
        return $result;
155
156
    }
157
158
    /**
159
     * @return mixed
160
     */
161
    public function getAccessToken()
162
    {
163
        return $this->accessToken;
164
    }
165
166
    /**
167
     * @param mixed $accessToken
168
     */
169
    public function setAccessToken($accessToken)
170
    {
171
        $this->accessToken = $accessToken;
172
    }
173
174
    /**
175
     * @param mixed $refreshToken
176
     */
177
    public function setRefreshToken($refreshToken)
178
    {
179
        $this->refreshToken = $refreshToken;
180
    }
181
182
    private function generateNewToken()
183
    {
184
        $data = [
185
            'merchantCode' => $this->apiKey,
186
            'password' => $this->secretKey,
187
        ];
188
        $result = $this->callCurl('/tokens/generate', $data);
189
190
        if (empty($result['accessToken'])) {
191
            throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException('Err in generate new token.');
192
        }
193
        if (!empty($result['accessToken'])) {
194
            $this->cache->store('accessToken', 'Bearer ' . $result['accessToken'], 24 * 60 * 60 - 60);
195
            $this->cache->store('refreshToken', $result['refreshToken'], 48 * 60 * 60 - 60);
196
            $this->setAccessToken('Bearer ' . $result['accessToken']);
197
            $this->setRefreshToken($result['refreshToken']);
198
            return 'ok';
199
        }
200
        throw new \Shetabit\Multipay\Exceptions\PurchaseFailedException('unExcepted Err in generateNewToken.');
201
202
    }
203
204
    /**
205
     * @param string $refNum
206
     * @return bool|mixed|string
207
     * @throws Exception
208
     */
209
    public function paymentVerify($refNum)
210
    {
211
        $this->generateToken();
212
        $data = [
213
        ];
214
        return $this->callCurl('/orders/' . $refNum . '/verify', $data, true, 0, 'GET');
215
    }
216
}
217