Migrate HickLab3D dashboard and add production auto deployment
Deploy HickLab3D Dashboard / Deploy to production (push) Failing after 19s
Deploy HickLab3D Dashboard / Deploy to production (push) Failing after 19s
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\EtsyConnection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use RuntimeException;
|
||||
|
||||
class Etsy
|
||||
{
|
||||
private const API_URL = 'https://api.etsy.com/v3';
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return filled(config('services.etsy.keystring')) && filled(config('services.etsy.shared_secret'));
|
||||
}
|
||||
|
||||
public function authorizationUrl(string $state, string $verifier): string
|
||||
{
|
||||
if (! $this->configured()) {
|
||||
throw new RuntimeException('Etsy application credentials are not configured.');
|
||||
}
|
||||
$challenge = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=');
|
||||
|
||||
return 'https://www.etsy.com/oauth/connect?'.http_build_query([
|
||||
'response_type' => 'code', 'redirect_uri' => $this->redirectUri(),
|
||||
'scope' => 'transactions_r listings_r', 'client_id' => config('services.etsy.keystring'),
|
||||
'state' => $state, 'code_challenge' => $challenge, 'code_challenge_method' => 'S256',
|
||||
], '', '&', PHP_QUERY_RFC3986);
|
||||
}
|
||||
|
||||
public function connect(string $code, string $verifier): EtsyConnection
|
||||
{
|
||||
$tokens = Http::asForm()->acceptJson()->connectTimeout(3)->timeout(10)
|
||||
->post(self::API_URL.'/public/oauth/token', [
|
||||
'grant_type' => 'authorization_code', 'client_id' => config('services.etsy.keystring'),
|
||||
'redirect_uri' => $this->redirectUri(), 'code' => $code, 'code_verifier' => $verifier,
|
||||
])->throw()->json();
|
||||
$userId = explode('.', $tokens['access_token'] ?? '')[0];
|
||||
if (! ctype_digit($userId) || empty($tokens['refresh_token'])) {
|
||||
throw new RuntimeException('Etsy returned an invalid OAuth response.');
|
||||
}
|
||||
|
||||
$shop = $this->get('application/users/'.$userId.'/shops', $tokens['access_token']);
|
||||
if (empty($shop['shop_id'])) {
|
||||
throw new RuntimeException('This Etsy account does not own a shop.');
|
||||
}
|
||||
if (strcasecmp($shop['shop_name'] ?? '', config('services.etsy.shop_name', 'HickLab3D')) !== 0) {
|
||||
throw new RuntimeException('The Etsy account does not own the configured HickLab3D shop.');
|
||||
}
|
||||
|
||||
return EtsyConnection::updateOrCreate(['shop_id' => $shop['shop_id']], [
|
||||
'shop_name' => $shop['shop_name'] ?? 'Etsy shop',
|
||||
'access_token' => $tokens['access_token'], 'refresh_token' => $tokens['refresh_token'],
|
||||
'expires_at' => now()->addSeconds(max(60, (int) ($tokens['expires_in'] ?? 3600) - 60)),
|
||||
]);
|
||||
}
|
||||
|
||||
public function receipts(EtsyConnection $connection): array
|
||||
{
|
||||
return $this->get('application/shops/'.$connection->shop_id.'/receipts', $this->accessToken($connection), [
|
||||
'was_paid' => 'true', 'was_shipped' => 'false', 'limit' => 20,
|
||||
]);
|
||||
}
|
||||
|
||||
public function listings(EtsyConnection $connection): array
|
||||
{
|
||||
return $this->get('application/shops/'.$connection->shop_id.'/listings', $this->accessToken($connection), [
|
||||
'state' => 'active', 'limit' => 20,
|
||||
]);
|
||||
}
|
||||
|
||||
private function accessToken(EtsyConnection $connection): string
|
||||
{
|
||||
if ($connection->expires_at->isFuture()) {
|
||||
return $connection->access_token;
|
||||
}
|
||||
$tokens = Http::asForm()->acceptJson()->connectTimeout(3)->timeout(10)
|
||||
->post(self::API_URL.'/public/oauth/token', [
|
||||
'grant_type' => 'refresh_token', 'client_id' => config('services.etsy.keystring'),
|
||||
'refresh_token' => $connection->refresh_token,
|
||||
])->throw()->json();
|
||||
if (empty($tokens['access_token']) || empty($tokens['refresh_token'])) {
|
||||
throw new RuntimeException('Etsy did not refresh the shop connection.');
|
||||
}
|
||||
$connection->update([
|
||||
'access_token' => $tokens['access_token'], 'refresh_token' => $tokens['refresh_token'],
|
||||
'expires_at' => now()->addSeconds(max(60, (int) ($tokens['expires_in'] ?? 3600) - 60)),
|
||||
]);
|
||||
|
||||
return $tokens['access_token'];
|
||||
}
|
||||
|
||||
private function get(string $path, string $accessToken, array $query = []): array
|
||||
{
|
||||
return Http::withHeaders(['x-api-key' => config('services.etsy.keystring').':'.config('services.etsy.shared_secret')])
|
||||
->withToken($accessToken)->acceptJson()->connectTimeout(3)->timeout(10)
|
||||
->get(self::API_URL.'/'.$path, $query)->throw()->json() ?? [];
|
||||
}
|
||||
|
||||
private function redirectUri(): string
|
||||
{
|
||||
return config('services.etsy.redirect_uri') ?: route('etsy.callback');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user