|
2 | 2 |
|
3 | 3 | namespace App\Services\Anystack;
|
4 | 4 |
|
5 |
| -use Illuminate\Http\Client\PendingRequest; |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Http\Client\HttpClientException; |
6 | 7 | use Illuminate\Http\Client\Response;
|
7 |
| -use Illuminate\Support\Facades\Http; |
8 | 8 |
|
9 |
| -class Anystack |
| 9 | +final class Anystack |
10 | 10 | {
|
11 | 11 | /**
|
12 | 12 | * Create a new Anystack API client.
|
13 | 13 | */
|
14 |
| - public function client(): PendingRequest |
| 14 | + public static function api(?string $apiKey = null): AnystackClient |
15 | 15 | {
|
16 |
| - return Http::withToken(config('services.anystack.key')) |
17 |
| - ->acceptJson() |
18 |
| - ->asJson(); |
| 16 | + if (! ($apiKey ??= config('services.anystack.key'))) { |
| 17 | + throw new HttpClientException('Anystack API key is not configured.'); |
| 18 | + } |
| 19 | + |
| 20 | + return app(AnystackClient::class, ['apiKey' => $apiKey]); |
19 | 21 | }
|
20 | 22 |
|
21 |
| - /** |
22 |
| - * Suspend a license on AnyStack. |
23 |
| - * |
24 |
| - * @param string $productId The AnyStack product ID |
25 |
| - * @param string $licenseId The AnyStack license ID |
26 |
| - * @return Response The API response |
27 |
| - * |
28 |
| - * @throws \Illuminate\Http\Client\RequestException If the request fails |
29 |
| - */ |
30 |
| - public function suspendLicense(string $productId, string $licenseId): Response |
| 23 | + public static function findContact(string $contactUuid): ?User |
31 | 24 | {
|
32 |
| - return $this->client() |
33 |
| - ->patch("https://api.anystack.sh/v1/products/{$productId}/licenses/{$licenseId}", [ |
34 |
| - 'suspended' => true, |
35 |
| - ]) |
36 |
| - ->throw(); |
| 25 | + return User::query() |
| 26 | + ->where('anystack_contact_id', $contactUuid) |
| 27 | + ->first(); |
37 | 28 | }
|
38 | 29 |
|
39 |
| - /** |
40 |
| - * Delete a license on AnyStack. |
41 |
| - * |
42 |
| - * @param string $productId The AnyStack product ID |
43 |
| - * @param string $licenseId The AnyStack license ID |
44 |
| - * @return Response The API response |
45 |
| - * |
46 |
| - * @throws \Illuminate\Http\Client\RequestException If the request fails |
47 |
| - */ |
48 |
| - public function deleteLicense(string $productId, string $licenseId): Response |
| 30 | + public static function findContactOrFail(string $contactUuid): ?User |
49 | 31 | {
|
50 |
| - return $this->client() |
51 |
| - ->delete("https://api.anystack.sh/v1/products/{$productId}/licenses/{$licenseId}") |
52 |
| - ->throw(); |
| 32 | + return User::query() |
| 33 | + ->where('anystack_contact_id', $contactUuid) |
| 34 | + ->firstOrFail(); |
53 | 35 | }
|
54 | 36 |
|
55 | 37 | /**
|
|
0 commit comments