mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-14 06:06:18 +00:00
Compare commits
12 Commits
1.0.0-alph
...
1.0.0-rc.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 437e78770c | |||
| 0dbb27fc94 | |||
|
63e3f0a4a2
|
|||
|
1d2119a32b
|
|||
|
1725ec68de
|
|||
|
dcda4b990e
|
|||
|
937fde603b
|
|||
|
80b1f003b2
|
|||
|
aff901de4e
|
|||
|
92eadcca08
|
|||
|
8b874f540c
|
|||
|
3bcebd9d45
|
79
README.md
79
README.md
@@ -109,17 +109,18 @@ public function boot(): void
|
||||
|
||||
### Implementing Auth
|
||||
|
||||
This method should typically be called in the `boot` method of your `AuthServiceProvider` class:
|
||||
This method should typically be called in the `boot` method of your `AppServiceProvider` class:
|
||||
|
||||
```php
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Anikeen\Id\Providers\AnikeenIdSsoUserProvider;
|
||||
use Anikeen\Id\Providers\AnikeenIdUserProvider;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
Auth::provider('sso-users', function ($app, array $config) {
|
||||
return new AnikeenIdSsoUserProvider(
|
||||
Auth::provider('anikeen', function ($app, array $config) {
|
||||
return new AnikeenIdUserProvider(
|
||||
$app->make(AnikeenId::class),
|
||||
$app->make(Request::class),
|
||||
$config['model'],
|
||||
@@ -140,7 +141,7 @@ reference the guard in the `guards` configuration of your `auth.php` configurati
|
||||
|
||||
'api' => [
|
||||
'driver' => 'anikeen',
|
||||
'provider' => 'sso-users',
|
||||
'provider' => 'anikeen',
|
||||
],
|
||||
],
|
||||
```
|
||||
@@ -154,8 +155,8 @@ reference the provider in the `providers` configuration of your `auth.php` confi
|
||||
'model' => App\Models\User::class,
|
||||
],
|
||||
|
||||
'sso-users' => [
|
||||
'driver' => 'sso-users',
|
||||
'anikeen' => [
|
||||
'driver' => 'anikeen',
|
||||
'model' => App\Models\User::class,
|
||||
'fields' => ['first_name', 'last_name', 'email'],
|
||||
],
|
||||
@@ -294,9 +295,7 @@ public function createOrderPreview(array $attributes = []): Result
|
||||
### ManagesSshKeys
|
||||
|
||||
```php
|
||||
public function sshKeysByUserId(string $sskKeyId): Result
|
||||
public function createSshKey(string $publicKey, ?string $name = null): Result
|
||||
public function deleteSshKey(int $sshKeyId): Result
|
||||
public function sshKeysByUserId(string $sskKeyId): SshKeys
|
||||
```
|
||||
|
||||
### ManagesUsers
|
||||
@@ -305,6 +304,8 @@ public function deleteSshKey(int $sshKeyId): Result
|
||||
public function getAuthedUser(): Result
|
||||
public function createUser(array $attributes): Result
|
||||
public function isEmailExisting(string $email): Result
|
||||
public function setParent(?mixed $parent): self
|
||||
public function getParent()
|
||||
```
|
||||
|
||||
|
||||
@@ -313,11 +314,8 @@ public function isEmailExisting(string $email): Result
|
||||
### ManagesAddresses
|
||||
|
||||
```php
|
||||
public function addresses(): Result
|
||||
public function createAddress(array $attributes = []): Result
|
||||
public function address(string $addressId): Result
|
||||
public function updateAddress(string $addressId, array $attributes = []): Result
|
||||
public function deleteAddress(string $addressId): Result
|
||||
public function addresses(): Addresses
|
||||
public function hasDefaultBillingAddress(): bool
|
||||
```
|
||||
|
||||
### ManagesBalance
|
||||
@@ -325,55 +323,48 @@ public function deleteAddress(string $addressId): Result
|
||||
```php
|
||||
public function balance(): float
|
||||
public function charges(): float
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Result
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction
|
||||
```
|
||||
|
||||
### ManagesCountries
|
||||
|
||||
```php
|
||||
public function countries(): Countries
|
||||
```
|
||||
|
||||
### ManagesInvoices
|
||||
|
||||
```php
|
||||
public function invoices(): Result
|
||||
public function invoice(string $invoiceId): Result
|
||||
public function getInvoiceTemporaryUrl(string $invoiceId): string
|
||||
public function invoices(array $parameters = []): Invoices
|
||||
```
|
||||
|
||||
### ManagesOrders
|
||||
|
||||
```php
|
||||
public function orders(): Result
|
||||
public function createOrder(array $attributes = []): Result
|
||||
public function order(string $orderId): Result
|
||||
public function updateOrder(string $orderId, array $attributes = []): Result
|
||||
public function checkoutOrder(string $orderId): Result
|
||||
public function revokeOrder(string $orderId): Result
|
||||
public function deleteOrder(string $orderId): Result
|
||||
public function orderItems(string $orderId): Result
|
||||
public function createOrderItem(string $orderId, array $attributes = []): Result
|
||||
public function orderItem(string $orderId, string $orderItemId): Result
|
||||
public function updateOrderItem(string $orderId, string $orderItemId, array $attributes = []): Result
|
||||
public function deleteOrderItem(string $orderId, string $orderItemId): Result
|
||||
public function orders(array $parameters = []): Orders
|
||||
```
|
||||
|
||||
### ManagesPaymentMethods
|
||||
|
||||
```php
|
||||
public function hasPaymentMethod(): bool
|
||||
public function paymentMethods(): Result
|
||||
public function paymentMethods(): PaymentMethods
|
||||
public function hasPaymentMethod(): ?PaymentMethod
|
||||
public function defaultPaymentMethod(): ?PaymentMethod
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
public function defaultPaymentMethod(): Result
|
||||
public function billingPortalUrl(string $returnUrl, array $options): string
|
||||
public function billingPortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
public function createSetupIntent(array $options = []): Result
|
||||
```
|
||||
|
||||
### ManagesProfile
|
||||
|
||||
```php
|
||||
public function profilePortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
```
|
||||
|
||||
### ManagesSubscriptions
|
||||
|
||||
```php
|
||||
public function subscriptions(): Result
|
||||
public function subscription(string $subscriptionId): Result
|
||||
public function createSubscription(array $attributes): Result
|
||||
public function checkoutSubscription(string $subscriptionId): Result
|
||||
public function revokeSubscription(string $subscriptionId): Result
|
||||
public function resumeSubscription(string $subscriptionId): Result
|
||||
public function deleteSubscription(string $subscriptionId): Result
|
||||
public function subscriptions(): Subscriptions
|
||||
```
|
||||
|
||||
### ManagesTaxation
|
||||
@@ -385,9 +376,7 @@ public function vat(): float
|
||||
### ManagesTransactions
|
||||
|
||||
```php
|
||||
public function transactions(): Result
|
||||
public function createTransaction(array $attributes = []): Result
|
||||
public function transaction(string $transactionId): Result
|
||||
public function transactions(): Transactions
|
||||
```
|
||||
|
||||
|
||||
|
||||
15
README.stub
15
README.stub
@@ -109,17 +109,18 @@ public function boot(): void
|
||||
|
||||
### Implementing Auth
|
||||
|
||||
This method should typically be called in the `boot` method of your `AuthServiceProvider` class:
|
||||
This method should typically be called in the `boot` method of your `AppServiceProvider` class:
|
||||
|
||||
```php
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Anikeen\Id\Providers\AnikeenIdSsoUserProvider;
|
||||
use Anikeen\Id\Providers\AnikeenIdUserProvider;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
Auth::provider('sso-users', function ($app, array $config) {
|
||||
return new AnikeenIdSsoUserProvider(
|
||||
Auth::provider('anikeen', function ($app, array $config) {
|
||||
return new AnikeenIdUserProvider(
|
||||
$app->make(AnikeenId::class),
|
||||
$app->make(Request::class),
|
||||
$config['model'],
|
||||
@@ -140,7 +141,7 @@ reference the guard in the `guards` configuration of your `auth.php` configurati
|
||||
|
||||
'api' => [
|
||||
'driver' => 'anikeen',
|
||||
'provider' => 'sso-users',
|
||||
'provider' => 'anikeen',
|
||||
],
|
||||
],
|
||||
```
|
||||
@@ -154,8 +155,8 @@ reference the provider in the `providers` configuration of your `auth.php` confi
|
||||
'model' => App\Models\User::class,
|
||||
],
|
||||
|
||||
'sso-users' => [
|
||||
'driver' => 'sso-users',
|
||||
'anikeen' => [
|
||||
'driver' => 'anikeen',
|
||||
'model' => App\Models\User::class,
|
||||
'fields' => ['first_name', 'last_name', 'email'],
|
||||
],
|
||||
|
||||
@@ -13,10 +13,9 @@ use Anikeen\Id\Concerns\ManagesProfile;
|
||||
use Anikeen\Id\Concerns\ManagesSubscriptions;
|
||||
use Anikeen\Id\Concerns\ManagesTaxation;
|
||||
use Anikeen\Id\Concerns\ManagesTransactions;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Helpers\Paginator;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
|
||||
trait Billable
|
||||
{
|
||||
@@ -32,27 +31,23 @@ trait Billable
|
||||
use ManagesTransactions;
|
||||
use Request;
|
||||
|
||||
protected stdClass|null $userData = null;
|
||||
|
||||
/**
|
||||
* Get the currently authenticated user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getUserData(): stdClass
|
||||
{
|
||||
if (!$this->userData) {
|
||||
$this->userData = $this->request('GET', 'v1/user')->data;
|
||||
if (!isset($this->userDataCache)) {
|
||||
$this->userDataCache = $this->request('GET', 'v1/user')->data;
|
||||
}
|
||||
return $this->userData;
|
||||
return $this->userDataCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Anikeen API.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function request(string $method, string $path, null|array $payload = null, array $parameters = [], ?Paginator $paginator = null): Result
|
||||
{
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use stdClass;
|
||||
|
||||
trait MagicProperties
|
||||
{
|
||||
protected function setMagicProperties(stdClass|array $data): void
|
||||
{
|
||||
foreach ((object)$data as $key => $value) {
|
||||
if (!property_exists($this, $key)) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic getter: return null for undefined properties
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic isset: return false for undefined properties
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Addresses;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesAddresses
|
||||
{
|
||||
@@ -14,12 +14,26 @@ trait ManagesAddresses
|
||||
/**
|
||||
* Get addresses from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function addresses(): Addresses
|
||||
{
|
||||
return (new Addresses($this->request('GET', 'v1/addresses')))
|
||||
->setBillable($this);
|
||||
if (!isset($this->addressesCache)) {
|
||||
$this->addressesCache = Addresses::builder(fn() => $this->request('GET', 'v1/addresses'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->addressesCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has a default billing address.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress()
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasDefaultBillingAddress(): bool
|
||||
{
|
||||
return $this->addresses()->hasDefaultBillingAddress();
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,8 @@
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Transaction;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesBalance
|
||||
{
|
||||
@@ -15,8 +13,7 @@ trait ManagesBalance
|
||||
/**
|
||||
* Get balance from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function balance(): float
|
||||
{
|
||||
@@ -26,8 +23,7 @@ trait ManagesBalance
|
||||
/**
|
||||
* Get charges from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function charges(): float
|
||||
{
|
||||
@@ -40,12 +36,11 @@ trait ManagesBalance
|
||||
* @param float $amount Amount to charge in euros.
|
||||
* @param string $paymentMethodId Payment method ID.
|
||||
* @param array $options Additional options for the charge.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction
|
||||
{
|
||||
return new Transaction($this->request('POST', 'billing/charge', [
|
||||
return new Transaction(fn() => $this->request('POST', 'billing/charge', [
|
||||
'amount' => $amount,
|
||||
'payment_method_id' => $paymentMethodId,
|
||||
'options' => $options,
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Countries;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesCountries
|
||||
{
|
||||
@@ -14,12 +14,15 @@ trait ManagesCountries
|
||||
/**
|
||||
* Get available countries for the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function countries(): Countries
|
||||
{
|
||||
return (new Countries($this->request('GET', 'v1/countries')))
|
||||
->setBillable($this);
|
||||
if (!isset($this->countriesCache)) {
|
||||
$this->countriesCache = Countries::builder(fn() => $this->request('GET', 'v1/countries'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->countriesCache;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Invoices;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesInvoices
|
||||
{
|
||||
@@ -14,12 +14,15 @@ trait ManagesInvoices
|
||||
/**
|
||||
* Get invoices from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function invoices(array $parameters = []): Invoices
|
||||
{
|
||||
return (new Invoices($this->request('GET', 'v1/invoices', [], $parameters)))
|
||||
->setBillable($this);
|
||||
if (!isset($this->invoicesCache)) {
|
||||
$this->invoicesCache = Invoices::builder(fn() => $this->request('GET', 'v1/invoices', [], $parameters))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->invoicesCache;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Orders;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesOrders
|
||||
{
|
||||
@@ -15,12 +15,15 @@ trait ManagesOrders
|
||||
/**
|
||||
* Get orders from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function orders(array $parameters = []): Orders
|
||||
{
|
||||
return (new Orders($this->request('GET', 'v1/orders', [], $parameters)))
|
||||
->setBillable($this);
|
||||
if (!isset($this->ordersCache)) {
|
||||
$this->ordersCache = Orders::builder(fn() => $this->request('GET', 'v1/orders', [], $parameters))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->ordersCache;
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,10 @@ 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;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesPaymentMethods
|
||||
{
|
||||
@@ -15,13 +16,50 @@ trait ManagesPaymentMethods
|
||||
/**
|
||||
* Get payment methods from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function paymentMethods(): PaymentMethods
|
||||
{
|
||||
return (new PaymentMethods($this->request('GET', 'v1/payment-methods')))
|
||||
->setBillable($this);
|
||||
if (!isset($this->paymentMethodsCache)) {;
|
||||
$this->paymentMethodsCache = PaymentMethods::builder(
|
||||
fn() => $this->request('GET', 'v1/payment-methods')
|
||||
)->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->paymentMethodsCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user has at least one payment method.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod()
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasPaymentMethod(): ?PaymentMethod
|
||||
{
|
||||
return $this->paymentMethods()->hasPaymentMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function defaultPaymentMethod(): ?PaymentMethod
|
||||
{
|
||||
return $this->paymentMethods()->defaultPaymentMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has a default payment method.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod()
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
{
|
||||
return $this->paymentMethods()->hasDefaultPaymentMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,8 +67,7 @@ trait ManagesPaymentMethods
|
||||
*
|
||||
* @param string|null $returnUrl The URL to redirect to after the user has finished in the billing portal.
|
||||
* @param array $options Additional options for the billing portal.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function billingPortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
{
|
||||
@@ -44,8 +81,7 @@ trait ManagesPaymentMethods
|
||||
* Create a new setup intent.
|
||||
*
|
||||
* @param array $options Additional options for the setup intent.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function createSetupIntent(array $options = []): Result
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Post;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesPricing
|
||||
{
|
||||
@@ -29,8 +29,7 @@ trait ManagesPricing
|
||||
* } $attributes The order data:
|
||||
* - country_iso: ISO 3166-1 alpha-2 country code
|
||||
* - items: Array of order items (each with type, name, price, unit, units, and quantity)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function createOrderPreview(array $attributes = []): Result
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesProfile
|
||||
{
|
||||
@@ -17,8 +17,7 @@ trait ManagesProfile
|
||||
* @param string|null $returnUrl The URL to redirect to after the user has completed their profile.
|
||||
* @param array $options Additional options for the profile URL.
|
||||
* @return string
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function profilePortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Get;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\SshKeys;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesSshKeys
|
||||
{
|
||||
@@ -15,12 +15,15 @@ trait ManagesSshKeys
|
||||
/**
|
||||
* Get currently authed user with Bearer Token.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function sshKeysByUserId(string $sskKeyId): SshKeys
|
||||
{
|
||||
return (new SshKeys($this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId))))
|
||||
->setParent($this);
|
||||
if (!isset($this->sshKeysCache)) {
|
||||
$this->sshKeysCache = SshKeys::builder(fn() => $this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId)))
|
||||
->setParent($this);
|
||||
}
|
||||
|
||||
return $this->sshKeysCache;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Anikeen\Id\Concerns;
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Subscriptions;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesSubscriptions
|
||||
{
|
||||
@@ -14,12 +14,15 @@ trait ManagesSubscriptions
|
||||
/**
|
||||
* Get subscriptions from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function subscriptions(): Subscriptions
|
||||
{
|
||||
return (new Subscriptions($this->request('GET', 'v1/subscriptions')))
|
||||
->setBillable($this);
|
||||
if (!isset($this->subscriptionsCache)) {
|
||||
$this->subscriptionsCache = Subscriptions::builder(fn() => $this->request('GET', 'v1/subscriptions'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->subscriptionsCache;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesTaxation
|
||||
{
|
||||
@@ -13,11 +13,10 @@ trait ManagesTaxation
|
||||
/**
|
||||
* Get VAT for the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function vat(): float
|
||||
public function vatRate(): float
|
||||
{
|
||||
return $this->getUserData()->vat;
|
||||
return $this->getUserData()->vat_rate;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Transactions;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesTransactions
|
||||
{
|
||||
@@ -15,12 +15,15 @@ trait ManagesTransactions
|
||||
/**
|
||||
* Get transactions from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function transactions(): Transactions
|
||||
{
|
||||
return (new Transactions($this->request('GET', 'v1/transactions')))
|
||||
->setBillable($this);;
|
||||
if (!isset($this->transactionsCache)) {
|
||||
$this->transactionsCache = Transactions::builder(fn() => $this->request('GET', 'v1/transactions'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->transactionsCache;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use Anikeen\Id\ApiOperations\Get;
|
||||
use Anikeen\Id\ApiOperations\Post;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesUsers
|
||||
{
|
||||
@@ -17,8 +17,7 @@ trait ManagesUsers
|
||||
/**
|
||||
* Get currently authed user with Bearer Token
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getAuthedUser(): Result
|
||||
{
|
||||
@@ -40,20 +39,32 @@ trait ManagesUsers
|
||||
* - username: The username (optional)
|
||||
* - email: The email (required)
|
||||
* - password: The password (optional, can be reset by the user if not provided)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function createUser(array $attributes): Result
|
||||
{
|
||||
return $this->post('v1/users', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes the access token using the refresh token.
|
||||
*/
|
||||
public function refreshToken(string $storedRefreshToken, string $scope = ''): Result
|
||||
{
|
||||
return $this->post('../oauth/token', [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $storedRefreshToken,
|
||||
'client_id' => $this->clientId,
|
||||
'client_secret' => $this->clientSecret,
|
||||
'scope' => $scope,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given email exists.
|
||||
*
|
||||
* @param string $email The email to check.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function isEmailExisting(string $email): Result
|
||||
{
|
||||
|
||||
11
src/Id/Exceptions/CollectionException.php
Normal file
11
src/Id/Exceptions/CollectionException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Exceptions;
|
||||
|
||||
use Anikeen\Id\Result;
|
||||
use Exception;
|
||||
|
||||
class CollectionException extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
11
src/Id/Exceptions/ResourceException.php
Normal file
11
src/Id/Exceptions/ResourceException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Exceptions;
|
||||
|
||||
use Anikeen\Id\Result;
|
||||
use Exception;
|
||||
|
||||
class ResourceException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Anikeen\Id\Providers;
|
||||
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class AnikeenIdSsoUserProvider implements UserProvider
|
||||
{
|
||||
private ?string $accessTokenField = null;
|
||||
|
||||
public function __construct(
|
||||
private AnikeenId $anikeenId,
|
||||
private Request $request,
|
||||
private string $model,
|
||||
private array $fields
|
||||
)
|
||||
{
|
||||
$this->accessTokenField = AnikeenId::getAccessTokenField();
|
||||
}
|
||||
|
||||
public function retrieveById(mixed $identifier): ?Authenticatable
|
||||
{
|
||||
$model = $this->createModel();
|
||||
$token = $this->request->bearerToken();
|
||||
|
||||
$user = $this->newModelQuery($model)
|
||||
->where($model->getAuthIdentifierName(), $identifier)
|
||||
->first();
|
||||
|
||||
// Return user when found
|
||||
if ($user) {
|
||||
// Update access token when updated
|
||||
if ($this->accessTokenField) {
|
||||
$user[$this->accessTokenField] = $token;
|
||||
|
||||
if ($user->isDirty()) {
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Create new user
|
||||
$this->anikeenId->setToken($token);
|
||||
$result = $this->anikeenId->getAuthedUser();
|
||||
|
||||
if (!$result->success()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributes = Arr::only((array)$result->data(), $this->fields);
|
||||
$attributes[$model->getAuthIdentifierName()] = $result->data->id;
|
||||
|
||||
if ($this->accessTokenField) {
|
||||
$attributes[$this->accessTokenField] = $token;
|
||||
}
|
||||
|
||||
return $this->newModelQuery($model)->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the model.
|
||||
*/
|
||||
public function createModel(): Model
|
||||
{
|
||||
$class = '\\' . ltrim($this->model, '\\');
|
||||
|
||||
return new $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new query builder for the model instance.
|
||||
*/
|
||||
protected function newModelQuery(?Model $model = null): Builder
|
||||
{
|
||||
return is_null($model)
|
||||
? $this->createModel()->newQuery()
|
||||
: $model->newQuery();
|
||||
}
|
||||
|
||||
public function retrieveByToken($identifier, $token)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function updateRememberToken(Authenticatable $user, $token)
|
||||
{
|
||||
// void
|
||||
}
|
||||
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function validateCredentials(Authenticatable $user, array $credentials): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false)
|
||||
{
|
||||
// TODO: Implement rehashPasswordIfRequired() method.
|
||||
}
|
||||
}
|
||||
124
src/Id/Providers/AnikeenIdUserProvider.php
Normal file
124
src/Id/Providers/AnikeenIdUserProvider.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Providers;
|
||||
|
||||
use Anikeen\Id\AnikeenId;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class AnikeenIdUserProvider implements UserProvider
|
||||
{
|
||||
private ?string $accessTokenField;
|
||||
|
||||
public function __construct(
|
||||
private AnikeenId $anikeenId,
|
||||
private Request $request,
|
||||
private string $model,
|
||||
private array $fields = []
|
||||
) {
|
||||
$this->accessTokenField = AnikeenId::getAccessTokenField();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function retrieveByToken($identifier, $token): ?Authenticatable
|
||||
{
|
||||
// Token from request (if not already pass from $token):
|
||||
$token = $token ?: $this->request->bearerToken();
|
||||
if (! $token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Set token in SSO client and request user info
|
||||
$this->anikeenId->setToken($token);
|
||||
$result = $this->anikeenId->getAuthedUser();
|
||||
if (! $result->success()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Only the desired fields
|
||||
$data = Arr::only((array)$result->data(), $this->fields);
|
||||
// Primary key (e.g. $user->id)
|
||||
$pk = $this->createModel()->getAuthIdentifierName();
|
||||
$data[$pk] = $result->data->id;
|
||||
|
||||
// Fill in access token field, if available
|
||||
if ($this->accessTokenField) {
|
||||
$data[$this->accessTokenField] = $token;
|
||||
}
|
||||
|
||||
// Local eloquent model: either find or create a new one
|
||||
/** @var Model $modelInstance */
|
||||
$modelInstance = $this->newModelQuery()
|
||||
->firstOrNew([$pk => $data[$pk]]);
|
||||
|
||||
$modelInstance->fill($data);
|
||||
$modelInstance->save();
|
||||
|
||||
return $modelInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function updateRememberToken(Authenticatable $user, $token): void
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function retrieveByCredentials(array $credentials): ?Authenticatable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validateCredentials(Authenticatable $user, array $credentials): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function retrieveById($identifier): ?Authenticatable
|
||||
{
|
||||
return $this->newModelQuery()
|
||||
->where($this->createModel()->getAuthIdentifierName(), $identifier)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false): void
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model
|
||||
*/
|
||||
protected function createModel(): Model
|
||||
{
|
||||
$class = '\\' . ltrim($this->model, '\\');
|
||||
return new $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder
|
||||
*/
|
||||
protected function newModelQuery(): Builder
|
||||
{
|
||||
return $this->createModel()->newQuery();
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
@@ -63,20 +61,18 @@ class Address extends BaseResource
|
||||
* - email: Email address (optional, e.g. for delivery notifications)
|
||||
* - primary: Whether this address should be the primary address (optional)
|
||||
* - primary_billing_address: Whether this address should be the primary billing address (optional)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function update(array $attributes = []): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/addresses/%s', $this->id), $attributes)))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/addresses/%s', $this->id), $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete given address from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class Addresses extends BaseCollection
|
||||
{
|
||||
@@ -44,12 +42,11 @@ class Addresses extends BaseCollection
|
||||
* - email: Email address (optional, e.g. for delivery notifications)
|
||||
* - primary: Whether this address should be the primary address (optional)
|
||||
* - primary_billing_address: Whether this address should be the primary billing address (optional)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(array $attributes = []): Address
|
||||
{
|
||||
return (new Address($this->billable->request('POST', 'v1/addresses', $attributes)))
|
||||
return (new Address(fn() => $this->billable->request('POST', 'v1/addresses', $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
@@ -58,31 +55,25 @@ class Addresses extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?Address
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/addresses/%s', $id));
|
||||
|
||||
return $result->success() ?
|
||||
(new Address($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new Address(fn() => $this->billable->request('GET', sprintf('v1/addresses/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default address from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function defaultBillingAddress(): Address
|
||||
{
|
||||
return (new Address($this->billable->request('GET', sprintf('v1/addresses/%s', $this->billable->getUserData()->billing_address_id))))
|
||||
return (new Address(fn() => $this->billable->request('GET', sprintf('v1/addresses/%s', $this->billable->getUserData()->billing_address_id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has a default billing address.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasDefaultBillingAddress(): bool
|
||||
{
|
||||
|
||||
@@ -2,17 +2,51 @@
|
||||
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\MagicProperties;
|
||||
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;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use JsonSerializable;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property bool $success
|
||||
* @property mixed $data
|
||||
* @property int $total
|
||||
* @property int $status
|
||||
* @property null|array $links
|
||||
* @property null|array $meta
|
||||
* @property null|Paginator $paginator
|
||||
* @property AnikeenId $anikeenId
|
||||
* @property Response $response
|
||||
* @property null|Throwable $exception
|
||||
*/
|
||||
abstract class BaseCollection implements JsonSerializable
|
||||
{
|
||||
use MagicProperties;
|
||||
private Closure $callable;
|
||||
public ?Result $result = null;
|
||||
|
||||
public function __construct(protected Result $result)
|
||||
/**
|
||||
* @throws CollectionException
|
||||
*/
|
||||
protected function __construct(callable $callable)
|
||||
{
|
||||
//
|
||||
$this->result = $callable();
|
||||
|
||||
if (!$this->result->success()) {
|
||||
throw new CollectionException(sprintf('%s for collection [%s]', rtrim($this->result->data->message, '.'), get_called_class()), $this->result->response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CollectionException
|
||||
*/
|
||||
public static function builder(callable $callable): static
|
||||
{
|
||||
return new static($callable, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,4 +77,14 @@ abstract class BaseCollection implements JsonSerializable
|
||||
* Returns the Resource based on the ID.
|
||||
*/
|
||||
abstract public function find(string $id): ?BaseResource;
|
||||
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->result->{$name} ?? null;
|
||||
}
|
||||
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
return isset($this->result->{$name});
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,38 @@
|
||||
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\MagicProperties;
|
||||
use Anikeen\Id\Exceptions\ResourceException;
|
||||
use Anikeen\Id\Result;
|
||||
use JsonSerializable;
|
||||
|
||||
abstract class BaseResource implements JsonSerializable
|
||||
{
|
||||
use MagicProperties;
|
||||
public Result $result;
|
||||
|
||||
public function __construct(protected Result $result)
|
||||
/**
|
||||
* @throws ResourceException
|
||||
*/
|
||||
public function __construct(callable $callable)
|
||||
{
|
||||
$this->setMagicProperties($this->result->data);
|
||||
$this->result = $callable();
|
||||
|
||||
if (!$this->result->success()) {
|
||||
throw new ResourceException(sprintf('%s for resource [%s]', rtrim($this->result->data->message, '.'), get_called_class()), $this->result->response->getStatusCode());
|
||||
}
|
||||
|
||||
foreach ($this->result->data as $key => $value) {
|
||||
if (!property_exists($this, $key)) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collection of resources as a JSON string.
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,11 +44,13 @@ abstract class BaseResource implements JsonSerializable
|
||||
return (array)$this->result->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collection of resources as a JSON string.
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->toArray();
|
||||
return null;
|
||||
}
|
||||
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
@@ -16,8 +15,7 @@ class Invoice extends BaseResource
|
||||
/**
|
||||
* Get temporary download url from given invoice.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getInvoiceTemporaryUrl(): string
|
||||
{
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class Invoices extends BaseCollection
|
||||
{
|
||||
@@ -16,11 +13,7 @@ class Invoices extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?Invoice
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/invoices/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new Invoice($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new Invoice(fn() => $this->billable->request('GET', sprintf('v1/invoices/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
@@ -55,44 +54,40 @@ class Order extends BaseResource
|
||||
* } $attributes The order data:
|
||||
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code)
|
||||
* - shipping_address: Shipping address (first name, last name, ISO 3166-1 alpha-2 country code)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function update(array $attributes = []): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/orders/%s', $this->id), $attributes)))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s', $this->id), $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given order from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function checkout(string $orderId): self
|
||||
public function checkout(): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/orders/%s/checkout', $this->id))))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/checkout', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke given order from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function revoke(string $orderId): self
|
||||
public function revoke(): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/orders/%s/revoke', $this->id))))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/revoke', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete given order from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
@@ -102,12 +97,11 @@ class Order extends BaseResource
|
||||
/**
|
||||
* Get order items from given order.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function orderItems(array $parameters = []): OrderItems
|
||||
{
|
||||
return (new OrderItems($this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters)))
|
||||
return OrderItems::builder(fn() => $this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@ namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
@@ -31,12 +30,11 @@ class OrderItem extends BaseResource
|
||||
* }>
|
||||
* } $attributes The order data:
|
||||
* - items: Array of order items, each with type, name, description, price, unit, and quantity
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function update(array $attributes = []): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id), $attributes)))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id), $attributes)))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent);
|
||||
}
|
||||
@@ -44,8 +42,7 @@ class OrderItem extends BaseResource
|
||||
/**
|
||||
* Delete given order item from given order.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
|
||||
@@ -4,9 +4,8 @@ namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class OrderItems extends BaseCollection
|
||||
{
|
||||
@@ -30,12 +29,11 @@ class OrderItems extends BaseCollection
|
||||
* }>
|
||||
* } $attributes The order data:
|
||||
* - items: Array of order items, each with type, name, description, price, unit, and quantity
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(string $orderId, array $attributes = []): OrderItem
|
||||
{
|
||||
return (new OrderItem($this->billable->request('POST', sprintf('v1/orders/%s', $orderId), $attributes)))
|
||||
return (new OrderItem(fn() => $this->billable->request('POST', sprintf('v1/orders/%s', $orderId), $attributes)))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent);
|
||||
}
|
||||
@@ -45,13 +43,8 @@ class OrderItems extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?OrderItem
|
||||
{
|
||||
/** @var Result $result */
|
||||
$result = $this->parent->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id));
|
||||
|
||||
return $result->success() ?
|
||||
(new OrderItem($result))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent)
|
||||
: null;
|
||||
return (new OrderItem(fn() => $this->parent->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id))))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,8 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class Orders extends BaseCollection
|
||||
{
|
||||
@@ -62,29 +61,22 @@ class Orders extends BaseCollection
|
||||
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code)
|
||||
* - shipping_address: Shipping address (first name, last name, ISO 3166-1 alpha-2 country code)
|
||||
* - items: Array of order items (each with type, name, price, unit, units, and quantity)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(array $attributes = []): Order
|
||||
{
|
||||
return (new Order($this->billable->request('POST', 'v1/orders', $attributes)))
|
||||
return (new Order(fn() => $this->billable->request('POST', 'v1/orders', $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given order from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function find(string $id): ?Order
|
||||
{
|
||||
/** @var Result $result */
|
||||
$result = $this->billable->request('GET', sprintf('v1/orders/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new Order($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new Order(fn() => $this->billable->request('GET', sprintf('v1/orders/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
@@ -3,65 +3,53 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class PaymentMethods extends BaseCollection
|
||||
{
|
||||
use HasBillable;
|
||||
|
||||
private ?PaymentMethod $cachedDefaultPaymentMethod = null;
|
||||
|
||||
/**
|
||||
* Check if current user has at least one payment method.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasPaymentMethod(): bool
|
||||
{
|
||||
return $this->result->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function defaultPaymentMethod(): PaymentMethod
|
||||
{
|
||||
if ($this->cachedDefaultPaymentMethod === null) {
|
||||
$this->cachedDefaultPaymentMethod = (new PaymentMethod(
|
||||
$this->billable->request('GET', 'v1/payment-methods/default')
|
||||
))->setBillable($this->billable);
|
||||
}
|
||||
|
||||
return $this->cachedDefaultPaymentMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has a default payment method.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
{
|
||||
return $this->defaultPaymentMethod()?->id !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function defaultPaymentMethod(): PaymentMethod
|
||||
{
|
||||
if (!isset($this->defaultPaymentMethodCache)) {
|
||||
$this->defaultPaymentMethodCache = (new PaymentMethod(fn() => $this->billable->request('GET', 'v1/payment-methods/default')))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
return $this->defaultPaymentMethodCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function find(string $id): ?PaymentMethod
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/payment-methods/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new PaymentMethod($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new PaymentMethod(fn() => $this->billable->request('GET', sprintf('v1/payment-methods/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
@@ -16,8 +15,7 @@ class SshKey extends BaseResource
|
||||
/**
|
||||
* Deletes a given ssh key for the currently authed user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class SshKeys extends BaseCollection
|
||||
{
|
||||
@@ -16,12 +15,11 @@ class SshKeys extends BaseCollection
|
||||
*
|
||||
* @param string $publicKey The public key to be added
|
||||
* @param string|null $name The name of the key (optional)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(string $publicKey, ?string $name = null): SshKey
|
||||
{
|
||||
return (new SshKey($this->parent->post('v1/ssh-keys', [
|
||||
return (new SshKey(fn() => $this->parent->post('v1/ssh-keys', [
|
||||
'public_key' => $publicKey,
|
||||
'name' => $name,
|
||||
])))->setParent($this->parent);
|
||||
@@ -32,12 +30,6 @@ class SshKeys extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?SshKey
|
||||
{
|
||||
/** @var Result $result */
|
||||
$result = $this->parent->get(sprintf('v1/ssh-keys/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new SshKey($result))
|
||||
->setParent($this->parent)
|
||||
: null;
|
||||
return (new SshKey(fn() => $this->parent->get(sprintf('v1/ssh-keys/%s', $id))));
|
||||
}
|
||||
}
|
||||
@@ -3,63 +3,105 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $status
|
||||
* @property string $unit
|
||||
* @property float $price
|
||||
* @property float $vat_rate
|
||||
* @property array $payload
|
||||
* @property string $ends_at
|
||||
* @property string $webhook_url
|
||||
* @property string $webhook_secret
|
||||
*/
|
||||
class Subscription extends BaseResource
|
||||
{
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Update a given subscription from the current user.
|
||||
*
|
||||
* @param array{
|
||||
* group: string,
|
||||
* name: string,
|
||||
* description: null|string,
|
||||
* unit: string,
|
||||
* price: float,
|
||||
* vat_rate: float,
|
||||
* payload: null|array,
|
||||
* ends_at: null|string,
|
||||
* webhook_url: null|string,
|
||||
* webhook_secret: null|string
|
||||
* } $attributes The subscription data:
|
||||
* - group: The group (optional)
|
||||
* - name: The name (required when set)
|
||||
* - description: The description (optional)
|
||||
* - unit: The unit (required when set, e.g. "hour", "day", "week", "month", "year")
|
||||
* - price: The price per unit (required when set)
|
||||
* - vat_rate: The VAT rate (required when set)
|
||||
* - payload: The payload (optional)
|
||||
* - ends_at: The end date (optional)
|
||||
* - webhook_url: The webhook URL (optional)
|
||||
* - webhook_secret: The webhook secret (optional)
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function update(array $attributes): self
|
||||
{
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s', $this->id), $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force given subscription to check out (trusted apps only).
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function checkout(): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/subscriptions/%s/checkout', $this->id))))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/checkout', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a given running subscription from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function revoke(): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id))))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause a given running subscription from the current user.
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function pause(): self
|
||||
{
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/pause', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a given running subscription from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function resume(): self
|
||||
{
|
||||
return (new self($this->billable->request('PUT', sprintf('v1/subscriptions/%s/resume', $this->id))))
|
||||
return (new self(fn() => $this->billable->request('PUT', sprintf('v1/subscriptions/%s/resume', $this->id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a given subscription from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class Subscriptions extends BaseCollection
|
||||
{
|
||||
@@ -15,31 +13,32 @@ class Subscriptions extends BaseCollection
|
||||
* Create a new subscription for the current user.
|
||||
*
|
||||
* @param array{
|
||||
* name: null,
|
||||
* description: string,
|
||||
* group: null|string,
|
||||
* name: string,
|
||||
* description: null|string,
|
||||
* unit: string,
|
||||
* price: float,
|
||||
* vat: null|float,
|
||||
* vat_rate: float,
|
||||
* payload: null|array,
|
||||
* ends_at: null|string,
|
||||
* webhook_url: null|string,
|
||||
* webhook_secret: null|string
|
||||
* } $attributes The subscription data:
|
||||
* - group: The group (optional)
|
||||
* - name: The name
|
||||
* - description: The description
|
||||
* - description: The description (optional)
|
||||
* - unit: The unit (e.g. "hour", "day", "week", "month", "year")
|
||||
* - price: The price per unit
|
||||
* - vat: The VAT (optional)
|
||||
* - vat_rate: The VAT rate (required when set)
|
||||
* - payload: The payload (optional)
|
||||
* - ends_at: The end date (optional)
|
||||
* - webhook_url: The webhook URL (optional)
|
||||
* - webhook_secret: The webhook secret (optional)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* - webhook_url: The webhook URL (optional)
|
||||
* - webhook_secret: The webhook secret (optional)
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(array $attributes): Subscription
|
||||
{
|
||||
return (new Subscription($this->billable->request('POST', 'v1/subscriptions', $attributes)))
|
||||
return (new Subscription(fn() => $this->billable->request('POST', 'v1/subscriptions', $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
@@ -48,11 +47,7 @@ class Subscriptions extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?Subscription
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/subscriptions/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new Subscription($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new Subscription(fn() => $this->billable->request('GET', sprintf('v1/subscriptions/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,8 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Anikeen\Id\Exceptions\ResourceException;
|
||||
use Throwable;
|
||||
|
||||
class Transactions extends BaseCollection
|
||||
{
|
||||
@@ -14,14 +13,30 @@ class Transactions extends BaseCollection
|
||||
/**
|
||||
* Create a new transaction for the current user.
|
||||
*
|
||||
* @param array $attributes The attributes for the transaction.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @todo Add type hinting for the attributes array.
|
||||
* @param array{
|
||||
* group: null|string,
|
||||
* invoice_id: null|string,
|
||||
* payment_provider: null|string,
|
||||
* payment_intent: null|string,
|
||||
* status: string,
|
||||
* type: string,
|
||||
* amount: float,
|
||||
* created_at: string
|
||||
* } $attributes The attributes for the transaction.
|
||||
* - group: The group (optional)
|
||||
* - invoice_id: The invoice id (optional)
|
||||
* - payment_provider: The payment provider (optional, e.g. "kofi", "stripe")
|
||||
* - payment_intent: The payment intent (optional)
|
||||
* - status: The status (e.g. "expired", "failed", "pending", "refunded", "succeeded")
|
||||
* - type: The type (e.g. "deposit", "withdrawal")
|
||||
* - amount: The amount
|
||||
* - created_at: The created at datetime string (e.g. "Y-M-D H:i:s")
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(array $attributes = []): Transaction
|
||||
{
|
||||
return new Transaction($this->billable->request('POST', 'v1/transactions', $attributes));
|
||||
return (new Transaction($this->billable->request('POST', 'v1/transactions', $attributes)))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,11 +44,7 @@ class Transactions extends BaseCollection
|
||||
*/
|
||||
public function find(string $id): ?Transaction
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/transactions/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new Transaction($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new Transaction(fn() => $this->billable->request('GET', sprintf('v1/transactions/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user