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
|
|||||
54 | { |
||||
55 | $accessToken = null; |
||||
0 ignored issues
–
show
|
|||||
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
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
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. ![]() |
|||||
70 | |||||
71 | if (isset($result['status']) && $result['status'] == Constant::HTTP_RESPONSE_STATUSES['success']) { |
||||
72 | |||||
73 | $merchantEnvironmentCheck = config('bSecure.environment') ?? 'sandbox'; |
||||
0 ignored issues
–
show
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
![]() |
|||||
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
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
![]() |
|||||
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
|
|||||
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
|
|||||
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
|
|||||
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
|
|||||
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
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
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. ![]() |
|||||
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
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
![]() |
|||||
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 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.