diff --git a/composer.json b/composer.json index f47b604..afc9ec8 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "firebase/php-jwt": "^6.0" }, "require-dev": { - "phpunit/phpunit": "^8.0|^9.0" + "phpunit/phpunit": "^8.0|^9.0", + "laravel/framework": "^11.0|^12.0" }, "autoload": { "psr-4": { @@ -49,4 +50,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/Id/AnikeenId.php b/src/Id/AnikeenId.php index 4d76642..8b17e1b 100644 --- a/src/Id/AnikeenId.php +++ b/src/Id/AnikeenId.php @@ -327,7 +327,7 @@ class AnikeenId * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function request(string $method, string $path, null|array $payload = null, array $parameters = [], ?Paginator $paginator = null): Result + public function request(string $method, string $path, null|array $payload = null, array $parameters = [], ?Paginator $paginator = null, bool $useClientSecret = false): Result { if ($paginator !== null) { $parameters[$paginator->action] = $paginator->cursor(); @@ -335,7 +335,7 @@ class AnikeenId try { $response = $this->client->request($method, $path, [ - 'headers' => $this->buildHeaders((bool)$payload), + 'headers' => $this->buildHeaders((bool)$payload, $useClientSecret), 'query' => Query::build($parameters), 'json' => $payload ?: null, ]); @@ -353,14 +353,14 @@ class AnikeenId * * @throws RequestRequiresClientIdException */ - private function buildHeaders(bool $json = false): array + private function buildHeaders(bool $json = false, bool $useClientSecret = false): array { $headers = [ 'Client-ID' => $this->getClientId(), 'Accept' => 'application/json', ]; - if ($this->token) { - $headers['Authorization'] = 'Bearer ' . $this->token; + if ($bearerToken = $useClientSecret ? $this->getClientSecret() : $this->getToken()) { + $headers['Authorization'] = 'Bearer ' . $bearerToken; } if ($json) { $headers['Content-Type'] = 'application/json'; diff --git a/src/Id/Billable.php b/src/Id/Billable.php index 7e5618b..6074035 100644 --- a/src/Id/Billable.php +++ b/src/Id/Billable.php @@ -2,7 +2,6 @@ namespace Anikeen\Id; -use Anikeen\Id\ApiOperations\Request; use Anikeen\Id\Concerns\ManagesAddresses; use Anikeen\Id\Concerns\ManagesBalance; use Anikeen\Id\Concerns\ManagesCountries; @@ -13,8 +12,6 @@ use Anikeen\Id\Concerns\ManagesProfile; use Anikeen\Id\Concerns\ManagesSubscriptions; use Anikeen\Id\Concerns\ManagesTaxation; use Anikeen\Id\Concerns\ManagesTransactions; -use Anikeen\Id\Helpers\Paginator; -use stdClass; use Throwable; trait Billable @@ -29,31 +26,27 @@ trait Billable use ManagesSubscriptions; use ManagesTaxation; use ManagesTransactions; - use Request; /** * Get the currently authenticated user. * * @throws Throwable */ - public function getUserData(): stdClass + public function getUserData(): object { if (!isset($this->userDataCache)) { - $this->userDataCache = $this->request('GET', 'v1/user')->data; + $this->userDataCache = $this->anikeenId()->request('GET', 'v1/user')->data; } return $this->userDataCache; } /** - * Make a request to the Anikeen API. + * Get the AnikeenId class. * * @throws Throwable */ - public function request(string $method, string $path, null|array $payload = null, array $parameters = [], ?Paginator $paginator = null): Result + public function anikeenId(): AnikeenId { - $anikeenId = new AnikeenId(); - $anikeenId->withToken($this->{AnikeenId::getAccessTokenField()}); - - return $anikeenId->request($method, $path, $payload, $parameters, $paginator); + return app(AnikeenId::class)->withToken($this->{AnikeenId::getAccessTokenField()}); } -} \ No newline at end of file +} diff --git a/src/Id/Concerns/HasBillable.php b/src/Id/Concerns/HasBillable.php index a0e1e10..8c8e07b 100644 --- a/src/Id/Concerns/HasBillable.php +++ b/src/Id/Concerns/HasBillable.php @@ -2,18 +2,21 @@ namespace Anikeen\Id\Concerns; +use Anikeen\Id\Contracts\Billable; +use Illuminate\Database\Eloquent\Model; + trait HasBillable { - public mixed $billable; + public Billable|Model $billable; - public function setBillable(mixed $billable): self + public function setBillable(Billable|Model $billable): self { $this->billable = $billable; return $this; } - public function getBillable(): mixed + public function getBillable(): Billable { return $this->billable; } diff --git a/src/Id/Concerns/ManagesAddresses.php b/src/Id/Concerns/ManagesAddresses.php index 63a1f05..f9a64f3 100644 --- a/src/Id/Concerns/ManagesAddresses.php +++ b/src/Id/Concerns/ManagesAddresses.php @@ -2,14 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Addresses; use Throwable; trait ManagesAddresses { - use Request; + use HasBillable; /** * Get addresses from the current user. @@ -19,7 +17,8 @@ trait ManagesAddresses public function addresses(): Addresses { if (!isset($this->addressesCache)) { - $this->addressesCache = Addresses::builder(fn() => $this->request('GET', 'v1/addresses')) + $this->addressesCache = Addresses::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/addresses')) ->setBillable($this); } @@ -29,8 +28,8 @@ trait ManagesAddresses /** * Check if the current user has a default billing address. * - * @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress() * @throws Throwable + * @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress() */ public function hasDefaultBillingAddress(): bool { diff --git a/src/Id/Concerns/ManagesBalance.php b/src/Id/Concerns/ManagesBalance.php index b80028c..7bf856d 100644 --- a/src/Id/Concerns/ManagesBalance.php +++ b/src/Id/Concerns/ManagesBalance.php @@ -2,13 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; use Anikeen\Id\Resources\Transaction; use Throwable; trait ManagesBalance { - use Request; + use HasBillable; /** * Get balance from the current user. @@ -40,10 +39,12 @@ trait ManagesBalance */ public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction { - return new Transaction(fn() => $this->request('POST', 'billing/charge', [ - 'amount' => $amount, - 'payment_method_id' => $paymentMethodId, - 'options' => $options, - ])); + return (new Transaction(fn() => $this->anikeenId() + ->request('POST', 'billing/charge', [ + 'amount' => $amount, + 'payment_method_id' => $paymentMethodId, + 'options' => $options, + ]))) + ->setBillable($this); } } \ No newline at end of file diff --git a/src/Id/Concerns/ManagesCountries.php b/src/Id/Concerns/ManagesCountries.php index 1fe32f8..b20453b 100644 --- a/src/Id/Concerns/ManagesCountries.php +++ b/src/Id/Concerns/ManagesCountries.php @@ -2,14 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Countries; use Throwable; trait ManagesCountries { - use Request; + use HasBillable; /** * Get available countries for the current user. @@ -19,7 +17,8 @@ trait ManagesCountries public function countries(): Countries { if (!isset($this->countriesCache)) { - $this->countriesCache = Countries::builder(fn() => $this->request('GET', 'v1/countries')) + $this->countriesCache = Countries::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/countries')) ->setBillable($this); } diff --git a/src/Id/Concerns/ManagesInvoices.php b/src/Id/Concerns/ManagesInvoices.php index 0664c87..4760fb3 100644 --- a/src/Id/Concerns/ManagesInvoices.php +++ b/src/Id/Concerns/ManagesInvoices.php @@ -2,14 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Invoices; use Throwable; trait ManagesInvoices { - use Request; + use HasBillable; /** * Get invoices from the current user. @@ -19,7 +17,8 @@ trait ManagesInvoices public function invoices(array $parameters = []): Invoices { if (!isset($this->invoicesCache)) { - $this->invoicesCache = Invoices::builder(fn() => $this->request('GET', 'v1/invoices', [], $parameters)) + $this->invoicesCache = Invoices::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/invoices', [], $parameters)) ->setBillable($this); } diff --git a/src/Id/Concerns/ManagesOrders.php b/src/Id/Concerns/ManagesOrders.php index 37fdc6e..233d538 100644 --- a/src/Id/Concerns/ManagesOrders.php +++ b/src/Id/Concerns/ManagesOrders.php @@ -2,15 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Orders; -use Anikeen\Id\Result; use Throwable; trait ManagesOrders { - use Request; + use HasBillable; /** * Get orders from the current user. @@ -20,7 +17,8 @@ trait ManagesOrders public function orders(array $parameters = []): Orders { if (!isset($this->ordersCache)) { - $this->ordersCache = Orders::builder(fn() => $this->request('GET', 'v1/orders', [], $parameters)) + $this->ordersCache = Orders::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/orders', [], $parameters)) ->setBillable($this); } diff --git a/src/Id/Concerns/ManagesPaymentMethods.php b/src/Id/Concerns/ManagesPaymentMethods.php index 847f2cf..a083aa6 100644 --- a/src/Id/Concerns/ManagesPaymentMethods.php +++ b/src/Id/Concerns/ManagesPaymentMethods.php @@ -2,8 +2,6 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\PaymentMethod; use Anikeen\Id\Resources\PaymentMethods; use Anikeen\Id\Result; @@ -11,7 +9,7 @@ use Throwable; trait ManagesPaymentMethods { - use Request; + use HasBillable; /** * Get payment methods from the current user. @@ -20,10 +18,10 @@ trait ManagesPaymentMethods */ public function paymentMethods(): PaymentMethods { - if (!isset($this->paymentMethodsCache)) {; - $this->paymentMethodsCache = PaymentMethods::builder( - fn() => $this->request('GET', 'v1/payment-methods') - )->setBillable($this); + if (!isset($this->paymentMethodsCache)) { + $this->paymentMethodsCache = PaymentMethods::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/payment-methods')) + ->setBillable($this); } return $this->paymentMethodsCache; @@ -32,8 +30,8 @@ trait ManagesPaymentMethods /** * Check if current user has at least one payment method. * - * @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod() * @throws Throwable + * @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod() */ public function hasPaymentMethod(): ?PaymentMethod { @@ -43,8 +41,8 @@ trait ManagesPaymentMethods /** * Get default payment method from the current user. * - * @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod() * @throws Throwable + * @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod() */ public function defaultPaymentMethod(): ?PaymentMethod { @@ -54,8 +52,8 @@ trait ManagesPaymentMethods /** * Check if the current user has a default payment method. * - * @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod() * @throws Throwable + * @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod() */ public function hasDefaultPaymentMethod(): bool { @@ -71,10 +69,13 @@ trait ManagesPaymentMethods */ public function billingPortalUrl(?string $returnUrl = null, array $options = []): string { - return $this->request('POST', 'v1/billing/portal', [ - 'return_url' => $returnUrl, - 'options' => $options, - ])->data->url; + return $this->anikeenId() + ->request('POST', 'v1/billing/portal', [ + 'return_url' => $returnUrl, + 'options' => $options, + ]) + ->data + ->url; } /** @@ -85,8 +86,9 @@ trait ManagesPaymentMethods */ public function createSetupIntent(array $options = []): Result { - return $this->request('POST', 'v1/payment-methods', [ - 'options' => $options, - ]); + return $this->anikeenId() + ->request('POST', 'v1/payment-methods', [ + 'options' => $options, + ]); } } diff --git a/src/Id/Concerns/ManagesPricing.php b/src/Id/Concerns/ManagesPricing.php index 6e92097..43f27d2 100644 --- a/src/Id/Concerns/ManagesPricing.php +++ b/src/Id/Concerns/ManagesPricing.php @@ -3,7 +3,6 @@ namespace Anikeen\Id\Concerns; use Anikeen\Id\ApiOperations\Post; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Result; use Throwable; diff --git a/src/Id/Concerns/ManagesProfile.php b/src/Id/Concerns/ManagesProfile.php index f331ab8..5ab113c 100644 --- a/src/Id/Concerns/ManagesProfile.php +++ b/src/Id/Concerns/ManagesProfile.php @@ -2,14 +2,11 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; -use Anikeen\Id\Result; use Throwable; trait ManagesProfile { - use Request; + use HasBillable; /** * Get the profile url for the current user. @@ -21,7 +18,7 @@ trait ManagesProfile */ public function profilePortalUrl(?string $returnUrl = null, array $options = []): string { - return $this->request('POST', 'v1/user/profile', [ + return $this->anikeenId()->request('POST', 'v1/user/profile', [ 'return_url' => $returnUrl, 'options' => $options, ])->data->url; diff --git a/src/Id/Concerns/ManagesSshKeys.php b/src/Id/Concerns/ManagesSshKeys.php index 17994cb..07f05d5 100644 --- a/src/Id/Concerns/ManagesSshKeys.php +++ b/src/Id/Concerns/ManagesSshKeys.php @@ -4,7 +4,6 @@ namespace Anikeen\Id\Concerns; use Anikeen\Id\ApiOperations\Get; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\SshKeys; use Throwable; diff --git a/src/Id/Concerns/ManagesSubscriptions.php b/src/Id/Concerns/ManagesSubscriptions.php index b6dc837..eb5225a 100644 --- a/src/Id/Concerns/ManagesSubscriptions.php +++ b/src/Id/Concerns/ManagesSubscriptions.php @@ -2,14 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Subscriptions; use Throwable; trait ManagesSubscriptions { - use Request; + use HasBillable; /** * Get subscriptions from the current user. @@ -19,7 +17,8 @@ trait ManagesSubscriptions public function subscriptions(): Subscriptions { if (!isset($this->subscriptionsCache)) { - $this->subscriptionsCache = Subscriptions::builder(fn() => $this->request('GET', 'v1/subscriptions')) + $this->subscriptionsCache = Subscriptions::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/subscriptions')) ->setBillable($this); } diff --git a/src/Id/Concerns/ManagesTaxation.php b/src/Id/Concerns/ManagesTaxation.php index 191fcda..119d14b 100644 --- a/src/Id/Concerns/ManagesTaxation.php +++ b/src/Id/Concerns/ManagesTaxation.php @@ -2,13 +2,11 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable; trait ManagesTaxation { - use Request; + use HasBillable; /** * Get VAT for the current user. diff --git a/src/Id/Concerns/ManagesTransactions.php b/src/Id/Concerns/ManagesTransactions.php index d83a312..bf57f46 100644 --- a/src/Id/Concerns/ManagesTransactions.php +++ b/src/Id/Concerns/ManagesTransactions.php @@ -2,15 +2,12 @@ namespace Anikeen\Id\Concerns; -use Anikeen\Id\ApiOperations\Request; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Resources\Transactions; -use Anikeen\Id\Result; use Throwable; trait ManagesTransactions { - use Request; + use HasBillable; /** * Get transactions from the current user. @@ -20,7 +17,8 @@ trait ManagesTransactions public function transactions(): Transactions { if (!isset($this->transactionsCache)) { - $this->transactionsCache = Transactions::builder(fn() => $this->request('GET', 'v1/transactions')) + $this->transactionsCache = Transactions::builder(fn() => $this->anikeenId() + ->request('GET', 'v1/transactions')) ->setBillable($this); } diff --git a/src/Id/Concerns/ManagesUsers.php b/src/Id/Concerns/ManagesUsers.php index 34fbe99..cdeb20b 100644 --- a/src/Id/Concerns/ManagesUsers.php +++ b/src/Id/Concerns/ManagesUsers.php @@ -5,7 +5,6 @@ namespace Anikeen\Id\Concerns; use Anikeen\Id\ApiOperations\Get; use Anikeen\Id\ApiOperations\Post; -use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Result; use Throwable; @@ -52,11 +51,11 @@ trait ManagesUsers public function refreshToken(string $storedRefreshToken, string $scope = ''): Result { return $this->post('../oauth/token', [ - 'grant_type' => 'refresh_token', + 'grant_type' => 'refresh_token', 'refresh_token' => $storedRefreshToken, - 'client_id' => $this->clientId, + 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, - 'scope' => $scope, + 'scope' => $scope, ]); } diff --git a/src/Id/Contracts/Billable.php b/src/Id/Contracts/Billable.php new file mode 100644 index 0000000..7e52201 --- /dev/null +++ b/src/Id/Contracts/Billable.php @@ -0,0 +1,12 @@ +getResponse(), $exception); + $result = new Result($exception->getResponse(), $exception, $this); } $result->anikeenId = $this; diff --git a/src/Id/Resources/Address.php b/src/Id/Resources/Address.php index b42ed57..37bedbe 100644 --- a/src/Id/Resources/Address.php +++ b/src/Id/Resources/Address.php @@ -65,7 +65,8 @@ class Address extends BaseResource */ public function update(array $attributes = []): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/addresses/%s', $this->id), $attributes))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/addresses/%s', $this->id), $attributes))) ->setBillable($this->billable); } @@ -76,6 +77,7 @@ class Address extends BaseResource */ public function delete(): bool { - return $this->billable->request('DELETE', sprintf('v1/addresses/%s', $this->id))->success(); + return $this->billable->anikeenId() + ->request('DELETE', sprintf('v1/addresses/%s', $this->id))->success(); } } \ No newline at end of file diff --git a/src/Id/Resources/Addresses.php b/src/Id/Resources/Addresses.php index 2232a05..5667a65 100644 --- a/src/Id/Resources/Addresses.php +++ b/src/Id/Resources/Addresses.php @@ -46,7 +46,8 @@ class Addresses extends BaseCollection */ public function create(array $attributes = []): Address { - return (new Address(fn() => $this->billable->request('POST', 'v1/addresses', $attributes))) + return (new Address(fn() => $this->billable->anikeenId() + ->request('POST', 'v1/addresses', $attributes))) ->setBillable($this->billable); } @@ -55,8 +56,9 @@ class Addresses extends BaseCollection */ public function find(string $id): ?Address { - return (new Address(fn() => $this->billable->request('GET', sprintf('v1/addresses/%s', $id)))) - ->setBillable($this->billable); + return (new Address(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/addresses/%s', $id)))) + ->setBillable($this->billable); } /** @@ -66,7 +68,8 @@ class Addresses extends BaseCollection */ public function defaultBillingAddress(): Address { - return (new Address(fn() => $this->billable->request('GET', sprintf('v1/addresses/%s', $this->billable->getUserData()->billing_address_id)))) + return (new Address(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/addresses/%s', $this->billable->getUserData()->billing_address_id)))) ->setBillable($this->billable); } diff --git a/src/Id/Resources/BaseCollection.php b/src/Id/Resources/BaseCollection.php index 6095d37..9d50f83 100644 --- a/src/Id/Resources/BaseCollection.php +++ b/src/Id/Resources/BaseCollection.php @@ -4,7 +4,6 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\AnikeenId; use Anikeen\Id\Exceptions\CollectionException; -use Anikeen\Id\Exceptions\ResourceException; use Anikeen\Id\Helpers\Paginator; use Anikeen\Id\Result; use Closure; @@ -24,8 +23,8 @@ use Throwable; * @property Response $response * @property null|Throwable $exception */ -#[\AllowDynamicProperties] -abstract class BaseCollection implements JsonSerializable +#[\AllowDynamicProperties] +abstract class BaseCollection implements JsonSerializable { private Closure $callable; public ?Result $result = null; @@ -88,4 +87,4 @@ abstract class BaseCollection implements JsonSerializable { return isset($this->result->{$name}); } -} +} diff --git a/src/Id/Resources/Invoice.php b/src/Id/Resources/Invoice.php index 6b82baf..055cd71 100644 --- a/src/Id/Resources/Invoice.php +++ b/src/Id/Resources/Invoice.php @@ -19,6 +19,9 @@ class Invoice extends BaseResource */ public function getInvoiceTemporaryUrl(): string { - return $this->billable->request('PUT', sprintf('v1/invoices/%s', $this->id))->data->temporary_url; + return $this->billable->anikeenId() + ->request('PUT', sprintf('v1/invoices/%s', $this->id)) + ->data + ->temporary_url; } } \ No newline at end of file diff --git a/src/Id/Resources/Invoices.php b/src/Id/Resources/Invoices.php index 3d9dc8f..3667eea 100644 --- a/src/Id/Resources/Invoices.php +++ b/src/Id/Resources/Invoices.php @@ -13,7 +13,8 @@ class Invoices extends BaseCollection */ public function find(string $id): ?Invoice { - return (new Invoice(fn() => $this->billable->request('GET', sprintf('v1/invoices/%s', $id)))) - ->setBillable($this->billable); + return (new Invoice(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/invoices/%s', $id)))) + ->setBillable($this->billable); } } \ No newline at end of file diff --git a/src/Id/Resources/Order.php b/src/Id/Resources/Order.php index 9a799a0..b71f975 100644 --- a/src/Id/Resources/Order.php +++ b/src/Id/Resources/Order.php @@ -58,7 +58,8 @@ class Order extends BaseResource */ public function update(array $attributes = []): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s', $this->id), $attributes))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/orders/%s', $this->id), $attributes))) ->setBillable($this->billable); } @@ -69,7 +70,8 @@ class Order extends BaseResource */ public function checkout(): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/checkout', $this->id)))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/orders/%s/checkout', $this->id)))) ->setBillable($this->billable); } @@ -80,7 +82,8 @@ class Order extends BaseResource */ public function revoke(): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/revoke', $this->id)))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/orders/%s/revoke', $this->id)))) ->setBillable($this->billable); } @@ -91,7 +94,8 @@ class Order extends BaseResource */ public function delete(): bool { - return $this->billable->request('DELETE', sprintf('v1/orders/%s', $this->id))->success(); + return $this->billable->anikeenId() + ->request('DELETE', sprintf('v1/orders/%s', $this->id))->success(); } /** @@ -101,7 +105,8 @@ class Order extends BaseResource */ public function orderItems(array $parameters = []): OrderItems { - return OrderItems::builder(fn() => $this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters)) + return OrderItems::builder(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters)) ->setBillable($this->billable) ->setParent($this); } diff --git a/src/Id/Resources/OrderItem.php b/src/Id/Resources/OrderItem.php index 36308d5..6cae99b 100644 --- a/src/Id/Resources/OrderItem.php +++ b/src/Id/Resources/OrderItem.php @@ -34,7 +34,8 @@ class OrderItem extends BaseResource */ public function update(array $attributes = []): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id), $attributes))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id), $attributes))) ->setBillable($this->billable) ->setParent($this->parent); } @@ -46,6 +47,7 @@ class OrderItem extends BaseResource */ public function delete(): bool { - return $this->billable->request('DELETE', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id))->success(); + return $this->billable->anikeenId() + ->request('DELETE', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id))->success(); } } \ No newline at end of file diff --git a/src/Id/Resources/OrderItems.php b/src/Id/Resources/OrderItems.php index 481aa0d..b0dd390 100644 --- a/src/Id/Resources/OrderItems.php +++ b/src/Id/Resources/OrderItems.php @@ -4,7 +4,6 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasParent; -use Anikeen\Id\Result; use Throwable; class OrderItems extends BaseCollection @@ -33,7 +32,8 @@ class OrderItems extends BaseCollection */ public function create(string $orderId, array $attributes = []): OrderItem { - return (new OrderItem(fn() => $this->billable->request('POST', sprintf('v1/orders/%s', $orderId), $attributes))) + return (new OrderItem(fn() => $this->billable->anikeenId() + ->request('POST', sprintf('v1/orders/%s', $orderId), $attributes))) ->setBillable($this->billable) ->setParent($this->parent); } @@ -43,7 +43,8 @@ class OrderItems extends BaseCollection */ public function find(string $id): ?OrderItem { - return (new OrderItem(fn() => $this->parent->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id)))) + return (new OrderItem(fn() => $this->parent->anikeenId() + ->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id)))) ->setBillable($this->billable) ->setParent($this->parent); } diff --git a/src/Id/Resources/Orders.php b/src/Id/Resources/Orders.php index 49e3302..194503e 100644 --- a/src/Id/Resources/Orders.php +++ b/src/Id/Resources/Orders.php @@ -3,7 +3,6 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; -use Anikeen\Id\Result; use Throwable; class Orders extends BaseCollection @@ -65,7 +64,8 @@ class Orders extends BaseCollection */ public function create(array $attributes = []): Order { - return (new Order(fn() => $this->billable->request('POST', 'v1/orders', $attributes))) + return (new Order(fn() => $this->billable->anikeenId() + ->request('POST', 'v1/orders', $attributes))) ->setBillable($this->billable); } @@ -76,7 +76,8 @@ class Orders extends BaseCollection */ public function find(string $id): ?Order { - return (new Order(fn() => $this->billable->request('GET', sprintf('v1/orders/%s', $id)))) + return (new Order(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/orders/%s', $id)))) ->setBillable($this->billable); } } \ No newline at end of file diff --git a/src/Id/Resources/PaymentMethods.php b/src/Id/Resources/PaymentMethods.php index d894456..34d0ba8 100644 --- a/src/Id/Resources/PaymentMethods.php +++ b/src/Id/Resources/PaymentMethods.php @@ -37,7 +37,8 @@ class PaymentMethods extends BaseCollection public function defaultPaymentMethod(): PaymentMethod { if (!isset($this->defaultPaymentMethodCache)) { - $this->defaultPaymentMethodCache = (new PaymentMethod(fn() => $this->billable->request('GET', 'v1/payment-methods/default'))) + $this->defaultPaymentMethodCache = (new PaymentMethod(fn() => $this->billable->anikeenId() + ->request('GET', 'v1/payment-methods/default'))) ->setBillable($this->billable); } @@ -46,10 +47,13 @@ class PaymentMethods extends BaseCollection /** * {@inheritDoc} + * + * @throws Throwable */ public function find(string $id): ?PaymentMethod { - return (new PaymentMethod(fn() => $this->billable->request('GET', sprintf('v1/payment-methods/%s', $id)))) + return (new PaymentMethod(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/payment-methods/%s', $id)))) ->setBillable($this->billable); } } \ No newline at end of file diff --git a/src/Id/Resources/SshKeys.php b/src/Id/Resources/SshKeys.php index 1934231..3ad451d 100644 --- a/src/Id/Resources/SshKeys.php +++ b/src/Id/Resources/SshKeys.php @@ -3,7 +3,6 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasParent; -use Anikeen\Id\Result; use Throwable; class SshKeys extends BaseCollection diff --git a/src/Id/Resources/Subscription.php b/src/Id/Resources/Subscription.php index 7fb9445..06e4c75 100644 --- a/src/Id/Resources/Subscription.php +++ b/src/Id/Resources/Subscription.php @@ -3,6 +3,7 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; +use Anikeen\Id\Contracts\AppTokenRepository; use Throwable; /** @@ -50,7 +51,8 @@ class Subscription extends BaseResource */ public function update(array $attributes): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s', $this->id), $attributes))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/subscriptions/%s', $this->id), $attributes))) ->setBillable($this->billable); } @@ -61,7 +63,8 @@ class Subscription extends BaseResource */ public function checkout(): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/checkout', $this->id)))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/subscriptions/%s/checkout', $this->id)))) ->setBillable($this->billable); } @@ -76,7 +79,8 @@ class Subscription extends BaseResource 'refund' => $refund, ]; - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id), $attributes))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id), $attributes))) ->setBillable($this->billable); } @@ -87,7 +91,8 @@ class Subscription extends BaseResource */ public function pause(): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/pause', $this->id)))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/subscriptions/%s/pause', $this->id)))) ->setBillable($this->billable); } @@ -98,17 +103,8 @@ class Subscription extends BaseResource */ public function resume(): self { - return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/resume', $this->id)))) + return (new self(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/subscriptions/%s/resume', $this->id)))) ->setBillable($this->billable); } - - /** - * Delete a given subscription from the current user. - * - * @throws Throwable - */ - public function delete(): bool - { - return $this->billable->request('DELETE', sprintf('v1/subscriptions/%s', $this->id))->success(); - } } diff --git a/src/Id/Resources/Subscriptions.php b/src/Id/Resources/Subscriptions.php index f3ba0fc..214affd 100644 --- a/src/Id/Resources/Subscriptions.php +++ b/src/Id/Resources/Subscriptions.php @@ -3,6 +3,8 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; +use Anikeen\Id\Contracts\AppTokenRepository; +use Anikeen\Id\Exceptions\ResourceException; use Throwable; class Subscriptions extends BaseCollection @@ -38,16 +40,20 @@ class Subscriptions extends BaseCollection */ public function create(array $attributes): Subscription { - return (new Subscription(fn() => $this->billable->request('POST', 'v1/subscriptions', $attributes))) + return (new Subscription(fn() => $this->billable->anikeenId() + ->request('POST', 'v1/subscriptions', $attributes))) ->setBillable($this->billable); } /** * {@inheritDoc} + * + * @throws ResourceException */ public function find(string $id): ?Subscription { - return (new Subscription(fn() => $this->billable->request('GET', sprintf('v1/subscriptions/%s', $id)))) + return (new Subscription(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/subscriptions/%s', $id)))) ->setBillable($this->billable); } -} \ No newline at end of file +} diff --git a/src/Id/Resources/Transactions.php b/src/Id/Resources/Transactions.php index e512475..c1f68bf 100644 --- a/src/Id/Resources/Transactions.php +++ b/src/Id/Resources/Transactions.php @@ -3,8 +3,6 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; -use Anikeen\Id\Exceptions\ResourceException; -use Throwable; class Transactions extends BaseCollection { @@ -15,7 +13,8 @@ class Transactions extends BaseCollection */ public function find(string $id): ?Transaction { - return (new Transaction(fn() => $this->billable->request('GET', sprintf('v1/transactions/%s', $id)))) + return (new Transaction(fn() => $this->billable->anikeenId() + ->request('GET', sprintf('v1/transactions/%s', $id)))) ->setBillable($this->billable); } } \ No newline at end of file