-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBazaarApiRefreshTokenCommand.php
163 lines (130 loc) · 4.78 KB
/
BazaarApiRefreshTokenCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php namespace Nikapps\BazaarApiLaravel\Console;
use Nikapps\BazaarApiPhp\BazaarApi;
use Nikapps\BazaarApiPhp\Configs\AccountConfig;
use Nikapps\BazaarApiPhp\Configs\ApiConfig;
use Nikapps\BazaarApiPhp\Exceptions\NetworkErrorException;
use Nikapps\BazaarApiPhp\Models\Requests\AuthorizationRequest;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Console\Command;
use Illuminate\Config\Repository as ConfigRepository;
class BazaarApiRefreshTokenCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'bazaar:refresh-token';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This will fetch refresh_token from CafeBazaar';
/**
* @var ConfigRepository
*/
protected $config;
private $generateCodeUri = 'https://pardakht.cafebazaar.ir/auth/authorize/?response_type=code&access_type=offline&redirect_uri=%s&client_id=%s';
/**
* Create a new command instance.
*
* @param ConfigRepository $config
* @return \Nikapps\BazaarApiLaravel\Console\BazaarApiRefreshTokenCommand
*/
public function __construct(ConfigRepository $config)
{
$this->config = $config;
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$code = $this->option('code');
$redirectUri = $this->option('redirect-uri');
$apiConfig = $this->getApiConfig();
$accountConfig = $this->getAccountConfig($code, $redirectUri);
$bazaarApi = new BazaarApi();
$bazaarApi->setApiConfig($apiConfig);
$bazaarApi->setAccountConfig($accountConfig);
$this->line(sprintf(
$this->generateCodeUri,
$accountConfig->getRedirectUri(),
$accountConfig->getClientId()
));
try {
$fetchedRefreshToken = $bazaarApi->fetchRefreshToken(new AuthorizationRequest());
$json = $fetchedRefreshToken->getResponseJson();
$headers = array_keys($json);
$rows = [$json];
$this->info("Refresh Token: " . $fetchedRefreshToken->getRefreshToken());
$this->comment('Save refresh token in your config file');
$this->table($headers, $rows);
} catch (NetworkErrorException $e) {
$this->error('Error: ' . $e->getClientException()->getRequest()->getUrl());
$this->error('Response Status: ' . $e->getClientException()->getResponse()->getStatusCode());
if ($e->getClientException()->getResponse()->getStatusCode() == 401) {
$this->comment('Your credentials is invalid or you should generate new code from this url: ');
$this->line(sprintf(
$this->generateCodeUri,
$accountConfig->getRedirectUri(),
$accountConfig->getClientId()
));
}
} catch (\Exception $e) {
$this->error("Unknown Error: " . $e->getMessage());
}
}
/**
* command options
*
* @return array
*/
protected function getOptions()
{
return [
["code", "c", InputOption::VALUE_REQUIRED, "returned code from cafebazaar", null],
[
"redirect-uri",
"r",
InputOption::VALUE_OPTIONAL,
"redirect-uri is defined in cafebazaar panel ",
$this->config->get('bazaar-api-laravel::credentials.redirect_uri')
]
];
}
/**
* fill api config & return
*
* @return ApiConfig
*/
protected function getApiConfig()
{
$apiConfig = new ApiConfig();
$apiConfig->setAuthorizationGrantType($this->config->get('bazaar-api-laravel::api.authorization.grant_type'));
$apiConfig->setAuthorizationPath($this->config->get('bazaar-api-laravel::api.authorization.path'));
$apiConfig->setBaseUrl($this->config->get('bazaar-api-laravel::api.base_url'));
$apiConfig->setVerifySsl($this->config->get('bazaar-api-laravel::api.verify_ssl'));
return $apiConfig;
}
/**
* fill account config & return
*
* @param $code
* @param $redirectUrl
* @return \Nikapps\BazaarApiPhp\Configs\AccountConfig
*/
protected function getAccountConfig($code, $redirectUrl)
{
$accountConfig = new AccountConfig();
$accountConfig->setClientId($this->config->get('bazaar-api-laravel::credentials.client_id'));
$accountConfig->setClientSecret($this->config->get('bazaar-api-laravel::credentials.client_secret'));
$accountConfig->setRedirectUri($redirectUrl);
$accountConfig->setCode($code);
return $accountConfig;
}
}