-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRefreshAppToken.php
115 lines (102 loc) · 4.41 KB
/
RefreshAppToken.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
<?php
declare(strict_types=1);
/**
*
* @author xiaoguo0426
* @contact [email protected]
* @license MIT
*/
namespace App\Command\Crontab\Amazon;
use AmazonPHP\SellingPartner\Exception\ApiException;
use App\Model\AmazonAppModel;
use App\Model\AmazonAppRegionModel;
use App\Util\AmazonApp;
use App\Util\AmazonSDK;
use App\Util\Constants;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Client\ClientExceptionInterface;
use function Hyperf\Support\make;
#[Command]
class RefreshAppToken extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('crontab:amazon:refresh-app-token');
}
public function configure(): void
{
parent::configure();
// 指令配置
$this->setDescription('Crontab Amazon Refresh App Token Command');
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): void
{
AmazonApp::single(static function (AmazonAppModel $amazonAppCollection) {
$console = ApplicationContext::getContainer()->get(StdoutLoggerInterface::class);
$merchant_id = $amazonAppCollection->merchant_id;
$merchant_store_id = $amazonAppCollection->merchant_store_id;
$amazonAppRegionCollections = AmazonAppRegionModel::query()
->where('merchant_id', $merchant_id)
->where('merchant_store_id', $merchant_store_id)
->where('status', Constants::STATUS_ACTIVE)
->get();
if ($amazonAppRegionCollections->isEmpty()) {
return false;
}
foreach ($amazonAppRegionCollections as $amazonAppRegionCollection) {
$amazonAppCollection->setAttribute('region', $amazonAppRegionCollection->region);
$amazonAppCollection->setAttribute('country_ids', $amazonAppRegionCollection->country_codes);
$amazonAppCollection->setAttribute('refresh_token', $amazonAppRegionCollection->refresh_token);
// $amazonAppCollection->region = $amazonAppRegionCollection->region;
// $amazonAppCollection->country_ids = $amazonAppRegionCollection->country_codes;
// $amazonAppCollection->refresh_token = $amazonAppRegionCollection->refresh_token;
/**
* @var AmazonSDK $amazonSDK
*/
$amazonSDK = make(AmazonSDK::class, [$amazonAppCollection]);
$region = $amazonSDK->getRegion();
$retry_sdk = 3;
while ($retry_sdk) {
try {
$sdk = $amazonSDK->getSdk($region, true);
break;
} catch (ApiException|ClientExceptionInterface|\Exception $exception) {
--$retry_sdk;
if ($retry_sdk === 0) {
$log = sprintf('AmazonAppRefreshToken Amazon App SDK构建失败,请检查. %s merchant_id:%s merchant_store_id:%s ', $exception->getMessage(), $merchant_id, $merchant_store_id);
$console->error($log);
return false;
}
continue;
}
}
$retry_token = 3;
while ($retry_token) {
try {
$accessToken = $amazonSDK->getToken($region, true);
break;
} catch (ApiException|ClientExceptionInterface|\Exception $exception) {
--$retry_token;
if ($retry_token === 0) {
$log = sprintf('AmazonAppRefreshToken Amazon App Token获取失败,请检查. %s merchant_id:%s merchant_store_id:%s ', $exception->getMessage(), $merchant_id, $merchant_store_id);
$console->error($log);
return false;
}
continue;
}
}
}
return true;
});
}
}