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.

Helper::apiRequest()   B
last analyzed

Complexity

Conditions 6
Paths 0

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 0
nop 7
dl 0
loc 32
rs 8.9617
1
<?php
2
3
4
namespace bSecure\UniversalCheckout\Helpers;
5
6
use bSecure\UniversalCheckout\Models\Merchant;
7
8
use Exception;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\Exception\RequestException;
11
12
class Helper
13
{
14
    public static function apiRequest($method, $url, $queryParams = [], $body = [], $headers = [], $contentType = 'json', $returnWithStatusCode = false)
15
    {
16
        $response = [];
17
18
        try {
19
            if (is_array($queryParams) && count($queryParams) > 0) {
20
                $url .= '?' . http_build_query($queryParams);
21
            }
22
23
            $payload = [
24
                $contentType => $body,
25
                'headers' => $headers,
26
                'http_errors' => false,
27
                'timeout' => 30,
28
                'connect_timeout' => 30
29
            ];
30
31
            $client = new Client();
32
            $curlResponse = $client->request($method, $url, $payload);
33
34
            if ($returnWithStatusCode) {
35
                $response['code'] = $curlResponse->getStatusCode();
36
                $response['content'] = json_decode($curlResponse->getBody()->getContents(), true);
37
            } else {
38
                $response = json_decode($curlResponse->getBody()->getContents(), true);
39
            }
40
        } catch (RequestException $e) {
41
//            AppException::log($e);
42
        } catch (Exception $e) {
43
//            AppException::log($e);
44
        } finally {
45
            return $response;
46
        }
47
    }
48
49
    /**
50
     * Author: Sara Hasan
51
     * Date: 10-November-2020
52
     */
53
    static function getAccessToken($data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
    {
55
        $accessToken = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $accessToken is dead and can be removed.
Loading history...
56
57
        $http = new Client();
58
        $authUrl = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['oauth'];
59
60
        $response = $http->post($authUrl, [
61
            'form_params' => [
62
                'grant_type' => 'client_credentials',
63
                'client_id' => $data['client_id'],
64
                'client_secret' => $data['client_secret'],
65
                'scope' => "",
66
            ],
67
        ]);
68
69
        $result = json_decode((string)$response->getBody("access_token"), true);
0 ignored issues
show
Unused Code introduced by
The call to Psr\Http\Message\MessageInterface::getBody() has too many arguments starting with 'access_token'. ( Ignorable by Annotation )

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

69
        $result = json_decode((string)$response->/** @scrutinizer ignore-call */ getBody("access_token"), true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
71
        if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
72
73
            $merchantEnvironmentCheck = config('bSecure.environment') ?? 'sandbox';
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
            $merchantEnvironmentCheck = /** @scrutinizer ignore-call */ config('bSecure.environment') ?? 'sandbox';
Loading history...
74
75
            if ($merchantEnvironmentCheck == $result['body']['environment']) {
76
                $accessToken = isset($result['body']['access_token']) ? $result['body']['access_token'] : null;
77
                return ['client_id' => '', 'error' => false, 'accessToken' => $accessToken];
78
            } else {
79
                return ['client_id' => '', 'error' => true, 'message' => trans('bSecure::messages.client.environment.invalid')];
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

79
                return ['client_id' => '', 'error' => true, 'message' => /** @scrutinizer ignore-call */ trans('bSecure::messages.client.environment.invalid')];
Loading history...
80
            }
81
        }
82
    }
83
84
85
    /**
86
     * Author: Sara Hasan
87
     * Date: 10-November-2020
88
     */
89
    static function createOrder($merchantAccessToken, $orderPayload)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    {
91
        $method = 'POST';
92
93
        $url = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['create_order'];
94
95
        $headers = ['Authorization' => 'Bearer ' . $merchantAccessToken];
96
97
        $result = Helper::apiRequest($method, $url, [], $orderPayload, $headers, 'form_params');
98
99
        if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
100
            $response = ['error' => false, 'body' => $result['body']];
101
        } else {
102
            $response = ['error' => true, 'body' => $result];
103
        }
104
        return $response;
105
    }
106
107
108
    /**
109
     * Author: Sara Hasan
110
     * Date: 10-November-2020
111
     */
112
    static function orderStatus($merchantAccessToken, $order_ref)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
113
    {
114
        $method = 'POST';
115
116
        $url = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['order_status'];
117
118
        $headers = ['Authorization' => 'Bearer ' . $merchantAccessToken];
119
120
        $result = Helper::apiRequest($method, $url, [], $order_ref, $headers, 'form_params');
121
122
        if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
123
            $response = ['error' => false, 'body' => $result['body']];
124
        } else {
125
            $response = ['error' => true, 'body' => $result];
126
        }
127
        return $response;
128
    }
129
130
131
    /**
132
     * Author: Sara Hasan
133
     * Date: 10-November-2020
134
     */
135
    static function manualOrderStatusUpdate($merchantAccessToken, $payload)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136
    {
137
        $method = 'POST';
138
139
        $url = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['manual_order_status_update'];
140
141
        $headers = ['Authorization' => 'Bearer ' . $merchantAccessToken];
142
143
        $result = Helper::apiRequest($method, $url, [], $payload, $headers, 'form_params');
144
145
        if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
146
            $response = ['error' => false, 'body' => $result['body']];
147
        } else {
148
            $response = ['error' => true, 'body' => $result];
149
        }
150
        return $response;
151
    }
152
153
    /**
154
     * Author: Sara Hasan
155
     * Date: 26-November-2020
156
     */
157
    public static function verifyClient($ssoPayload)
158
    {
159
        try {
160
            $client_response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $client_response is dead and can be removed.
Loading history...
161
162
            $http = new Client();
163
            $authUrl = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['verify_client'];
164
165
            $response = $http->post($authUrl, [
166
                'form_params' => $ssoPayload
167
            ]);
168
169
            $result = json_decode((string)$response->getBody("access_token"), true);
0 ignored issues
show
Unused Code introduced by
The call to Psr\Http\Message\MessageInterface::getBody() has too many arguments starting with 'access_token'. ( Ignorable by Annotation )

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

169
            $result = json_decode((string)$response->/** @scrutinizer ignore-call */ getBody("access_token"), true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
170
171
            if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
172
                $response = ['error' => false, 'body' => $result['body']];
173
            } else {
174
                $response = ['error' => true, 'body' => $result];
175
            }
176
            return $response;
177
        } catch (Exception $e) {
178
            return ['error' => true, 'message' => trans('bSecure::messages.sso_sco.failure'), 'exception' => $e->getTraceAsString()];
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

178
            return ['error' => true, 'message' => /** @scrutinizer ignore-call */ trans('bSecure::messages.sso_sco.failure'), 'exception' => $e->getTraceAsString()];
Loading history...
179
        }
180
    }
181
182
183
    /**
184
     * Author: Sara Hasan
185
     * Date: 26-November-2020
186
     */
187
    public static function customerProfile($ssoCustomerProfile)
188
    {
189
        $merchantToken = Merchant::getMerchantAccessToken();
190
191
        if ($merchantToken['error']) {
192
            return ['error' => true, 'message' => $merchantToken['message']];
193
        } else {
194
            $merchantAccessToken = $merchantToken['body'];
195
            // Call Create Order API
196
            $response = Helper::getCustomerProfile($merchantAccessToken, $ssoCustomerProfile);
197
198
            if ($response['error']) {
199
                return ['error' => true, 'message' => $response['body']['message']];
200
            } else {
201
                return $response;
202
            }
203
        }
204
205
    }
206
207
208
    /**
209
     * Author: Sara Hasan
210
     * Date: 26-November-2020
211
     */
212
    public static function getCustomerProfile($merchantAccessToken, $ssoCustomerProfile)
213
    {
214
        $method = 'POST';
215
216
        $url = Constant::AUTH_SERVER_URL . Constant::API_ENDPOINTS['customer_profile'];
217
218
        $headers = ['Authorization' => 'Bearer ' . $merchantAccessToken];
219
220
        $result = Helper::apiRequest($method, $url, [], $ssoCustomerProfile, $headers, 'form_params');
221
222
        if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) {
223
            $response = ['error' => false, 'body' => $result['body']];
224
        } else {
225
            $response = ['error' => true, 'body' => $result];
226
        }
227
        return $response;
228
229
    }
230
231
}
232
233