Skip to content

Commit 0917189

Browse files
authored
Update Auth.php
1 parent 70c339c commit 0917189

File tree

1 file changed

+27
-62
lines changed

1 file changed

+27
-62
lines changed

src/Auth.php

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,36 @@
22

33
namespace RahulGodiyal\PhpUpsApiWrapper;
44

5+
use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient;
6+
57
class Auth
68
{
7-
private $_mode;
8-
private $_dev_api_base_url;
9-
private $_prod_api_base_url;
9+
private const DEV_API_BASE_URL = 'https://wwwcie.ups.com';
10+
private const PROD_API_BASE_URL = 'https://onlinetools.ups.com';
11+
12+
private string $mode = 'DEV';
1013

11-
public function __construct()
14+
public function setMode(string $mode): self
1215
{
13-
$this->_mode = 'DEV';
14-
$this->_dev_api_base_url = 'https://wwwcie.ups.com';
15-
$this->_prod_api_base_url = 'https://onlinetools.ups.com';
16+
if ($mode === 'PROD') {
17+
$this->mode = $mode;
18+
}
19+
20+
return $this;
1621
}
1722

18-
/**
19-
* Authenticate User
20-
* @param string $client_id
21-
* @param string $client_secret
22-
* @return string $access_token
23-
*/
24-
public function authenticate(String $client_id, String $client_secret)
23+
public function authenticate(string $client_id, string $client_secret): array
2524
{
26-
$curl = curl_init();
27-
28-
curl_setopt_array($curl, [
29-
CURLOPT_HTTPHEADER => [
30-
"Content-Type: application/x-www-form-urlencoded",
31-
"x-merchant-id: string",
32-
"Authorization: Basic " . base64_encode($client_id . ":" . $client_secret)
33-
],
34-
CURLOPT_POSTFIELDS => $this->_getPayload(),
35-
CURLOPT_URL => $this->_getAPIBaseURL() . "/security/v1/oauth/token",
36-
CURLOPT_RETURNTRANSFER => true,
37-
CURLOPT_CUSTOMREQUEST => "POST",
25+
$httpClient = new HttpClient();
26+
$httpClient->setHeader([
27+
"Content-Type: application/x-www-form-urlencoded",
28+
"x-merchant-id: string",
29+
"Authorization: Basic " . base64_encode($client_id . ":" . $client_secret)
3830
]);
39-
40-
$response = curl_exec($curl);
41-
curl_close($curl);
42-
$res = json_decode($response);
31+
$httpClient->setPayload("grant_type=client_credentials");
32+
$httpClient->setUrl($this->_getAPIBaseURL() . "/security/v1/oauth/token");
33+
$httpClient->setMethod("POST");
34+
$res = $httpClient->fetch();
4335

4436
if (!isset($res->access_token)) {
4537
if (isset($res->response)) {
@@ -51,43 +43,16 @@ public function authenticate(String $client_id, String $client_secret)
5143
}
5244

5345
$access_token = $res->access_token;
54-
return ['status' => 'success', 'access_token' => $access_token];
55-
}
5646

57-
/**
58-
* Get Payload
59-
* @return string
60-
*/
61-
private function _getPayload()
62-
{
63-
return "grant_type=client_credentials";
64-
}
65-
66-
/**
67-
* Set Mode
68-
* @param string DEV|PROD
69-
*/
70-
public function setMode(String $mode)
71-
{
72-
if ($mode === 'PROD') {
73-
$this->_mode = $mode;
74-
return $mode;
75-
}
76-
77-
$this->_mode = 'DEV';
78-
return $mode;
47+
return ['status' => 'success', 'access_token' => $access_token];
7948
}
8049

81-
/**
82-
* Get Api Base URL
83-
* @return string url
84-
*/
85-
protected function _getAPIBaseURL()
50+
protected function _getAPIBaseURL(): string
8651
{
87-
if ($this->_mode === 'PROD') {
88-
return $this->_prod_api_base_url;
52+
if ($this->mode === 'PROD') {
53+
return self::PROD_API_BASE_URL;
8954
}
9055

91-
return $this->_dev_api_base_url;
56+
return self::DEV_API_BASE_URL;
9257
}
9358
}

0 commit comments

Comments
 (0)