|
2 | 2 |
|
3 | 3 | namespace App\Services\Anystack; |
4 | 4 |
|
5 | | -use Illuminate\Http\Client\PendingRequest; |
6 | | -use Illuminate\Http\Client\Response; |
7 | | -use Illuminate\Support\Facades\Http; |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Http\Client\HttpClientException; |
8 | 7 |
|
9 | | -class Anystack |
| 8 | +final class Anystack |
10 | 9 | { |
11 | 10 | /** |
12 | 11 | * Create a new Anystack API client. |
13 | 12 | */ |
14 | | - public function client(): PendingRequest |
| 13 | + public static function api(?string $apiKey = null): AnystackClient |
15 | 14 | { |
16 | | - return Http::withToken(config('services.anystack.key')) |
17 | | - ->acceptJson() |
18 | | - ->asJson(); |
19 | | - } |
| 15 | + if (! ($apiKey ??= config('services.anystack.key'))) { |
| 16 | + throw new HttpClientException('Anystack API key is not configured.'); |
| 17 | + } |
20 | 18 |
|
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 |
31 | | - { |
32 | | - return $this->client() |
33 | | - ->patch("https://api.anystack.sh/v1/products/{$productId}/licenses/{$licenseId}", [ |
34 | | - 'suspended' => true, |
35 | | - ]) |
36 | | - ->throw(); |
| 19 | + return app(AnystackClient::class, ['apiKey' => $apiKey]); |
37 | 20 | } |
38 | 21 |
|
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 |
| 22 | + public static function findContact(string $contactUuid): ?User |
49 | 23 | { |
50 | | - return $this->client() |
51 | | - ->delete("https://api.anystack.sh/v1/products/{$productId}/licenses/{$licenseId}") |
52 | | - ->throw(); |
| 24 | + return User::query() |
| 25 | + ->where('anystack_contact_id', $contactUuid) |
| 26 | + ->first(); |
53 | 27 | } |
54 | 28 |
|
55 | | - /** |
56 | | - * Retrieve a license from AnyStack. |
57 | | - * |
58 | | - * @param string $productId The AnyStack product ID |
59 | | - * @param string $licenseId The AnyStack license ID |
60 | | - * @return Response The API response |
61 | | - * |
62 | | - * @throws \Illuminate\Http\Client\RequestException If the request fails |
63 | | - */ |
64 | | - public function getLicense(string $productId, string $licenseId): Response |
| 29 | + public static function findContactOrFail(string $contactUuid): User |
65 | 30 | { |
66 | | - return $this->client() |
67 | | - ->get("https://api.anystack.sh/v1/products/{$productId}/licenses/{$licenseId}") |
68 | | - ->throw(); |
| 31 | + return User::query() |
| 32 | + ->where('anystack_contact_id', $contactUuid) |
| 33 | + ->firstOrFail(); |
69 | 34 | } |
70 | 35 | } |
0 commit comments