17 Commits

Author SHA1 Message Date
bc9c202c3b update composer.json 2025-09-21 17:15:45 +00:00
cecf8560ff Merge pull request #1 from anikeen-com/laravel-10
Update composer.json
2025-09-21 19:13:39 +02:00
25248e7822 refactor code 2025-09-21 17:12:19 +00:00
c641ec725d add refund to revoke endpoint 2025-09-21 12:13:56 +00:00
8eb0c25582 fix api path 2025-09-19 17:21:23 +00:00
30ac4ae4f9 update provider 2025-09-19 16:37:22 +00:00
e1a6af11a3 introduce new scopes 2025-09-19 15:36:28 +00:00
bb5df7f115 fix mode
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-18 19:28:08 +00:00
297404b05d add AllowDynamicProperties attribute
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-18 19:22:11 +00:00
5ab57dcdfe add staging key
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-18 19:17:50 +00:00
0f14fa1b4c remove endpoint
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-06 13:32:06 +00:00
437e78770c fix typo
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-06 13:23:00 +00:00
0dbb27fc94 update transaction description
Signed-off-by: Maurice Preuß <hello@envoyr.com>
2025-09-06 13:22:23 +00:00
René Preuß
8232de4003 Update OauthTrait.php 2025-07-30 23:08:13 +02:00
René Preuß
ac3e28f67f Update composer.json 2025-07-30 21:52:09 +02:00
63e3f0a4a2 add refresh token method
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-05-02 07:29:43 +02:00
1d2119a32b update resources
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-05-02 06:27:53 +02:00
47 changed files with 538 additions and 462 deletions

View File

@@ -15,14 +15,15 @@
"require": { "require": {
"php": "^8.1", "php": "^8.1",
"ext-json": "*", "ext-json": "*",
"illuminate/support": "^11.0|^12.0", "illuminate/support": "^10.0|^11.0|^12.0",
"illuminate/console": "^11.0|^12.0", "illuminate/console": "^10.0|^11.0|^12.0",
"guzzlehttp/guzzle": "^6.3|^7.0", "guzzlehttp/guzzle": "^6.3|^7.0",
"socialiteproviders/manager": "^3.4|^4.0.1", "socialiteproviders/manager": "^3.4|^4.0.1",
"firebase/php-jwt": "^6.0" "firebase/php-jwt": "^6.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^8.0|^9.0" "phpunit/phpunit": "^8.0|^9.0",
"laravel/framework": "^10.0|^11.0|^12.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

14
oauth-public.staging.key Normal file
View File

@@ -0,0 +1,14 @@
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAi4Ta8r01zKaGSnGi1EiD
uMFWRXBlK4y/ZIfWBpElmS2ygv4mGeP3hT4Flm696Z2UMy56KC+c7CC/PQCiutLk
5NUphyX/t+0QS5Dqpw6FB33fLTNNY7GqSmGIUE4os8XYZRSyDQRgtOgq3R3vJkoV
7zoavTJmSCQlG5Qf0T//iMmzQ+b+6VZm1CJSz5nGx94u1DuXNyP5Epkk0wuHrtwy
kADR2lmydNodJzqpSD+8yQqnAhOZNtNF4qwQ3g13fRvHycBp3G2nlCfOn2g5PmYD
KYBKqvTq4PQH4E+K3pbbMz6zf/T6Dw7zTfksqHR4hqMgN6byRRxmwuBczIumcu9b
y7xbgoIGIVZXgJliALPFi+zTPTN7c8MedFs/xCBHCmzWYTCZfHgr8RPRewD19tCG
NSny5R0vlArpuZCTTgedPESDeGU4eNEddg4yXFzKlpE2nNuvzZ1Ohruc5ETOSU19
RTCBUBkjeL6ESZRd/yKGjbVx4dEYxZdIz4yBl+hZ2ZOIyG7L3zPrccAWrPpG56xr
E5IDBXxLFhaJ5LlyEAGQehB0ShEuCdkr88Xz7ba9PHpGqY83l4//ULrqPIZPAa4Z
E3AWHT1ZtXNPeA4SzZ9Y9Oij4M3chyHxqM0lL3kYP+dstZehTujStfElDIx2Ni10
73tILu4edYS0FxsL19m8gbsCAwEAAQ==
-----END PUBLIC KEY-----

View File

@@ -23,7 +23,7 @@
<php> <php>
<env name="ANIKEEN_ID_KEY" value="38"/> <env name="ANIKEEN_ID_KEY" value="38"/>
<env name="ANIKEEN_ID_SECRET" value="2goNRF8x37HPVZVaa28ySZGVXJuksvxnxM7TcGzM"/> <env name="ANIKEEN_ID_SECRET" value="2goNRF8x37HPVZVaa28ySZGVXJuksvxnxM7TcGzM"/>
<env name="ANIKEEN_ID_BASE_URL" value="https://id.anikeen.com/api/"/> <env name="ANIKEEN_ID_BASE_URL" value="https://id.anikeen.com"/>
<env name="CLIENT_ACCESS_TOKEN" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxNSIsImp0aSI6IjAxN2QxZDg0Y2MxNjAyZTYxOGZkNjYwZGViZWVjMDY4MTk2YmYzMDk1OGMzY2RiYzBjZmJkZWFjZjFhOTUxODQzZDU1YTk3OGY2YWIxY2YzIiwiaWF0IjoxNjY0NjIzMzA4LjQyODI4MSwibmJmIjoxNjY0NjIzMzA4LjQyODI4NCwiZXhwIjoxNjgwMzQ4MTA4LjQxNjc4MSwic3ViIjoiMzkiLCJzY29wZXMiOlsiYXBpIiwicmVhZF91c2VyIl0sImNsaWVudCI6eyJ0cnVzdGVkIjpmYWxzZX19.vxnzCaU4PpOrNVHa5AnGSS6gX_RCvxIERAnHFhjTrUzRafV9mr2Cvwd-BDVYoUr10wHvxa_TJSYfnAdDuhE-MEyDv13O3dL2XGTtJNa_Rg6L6CQ0JvC3wL-lWPvGPFax9pu-_lqbA3jm5B08hc3-7tq3f2nXcxjhtkqT6TTJv1-RCAppb2HCXiUDAqANzbhyInDjOH2WvFj1OGC_AI03J3W2KRWyeFLtUne8XKPFyr9XGcPuTrqogcuuXLeUt2kcf2bXbuIV1OlgIECrDiP1Ee0F2AzPs27ZVJ2z0R0JbT6AubKhGl5_Qi27cwjOH7hT2dmjcF1mLjzpN1uChLIdSnGSoStH8VzYHnHE2I8G-owW_aadG2UmGdnRY143q6g_28f3WIZNSucBSXkwFeS_t4fylsvpxhpjYJusf5qiEU_X3YbeawYMUCFUkSD2XTIypAqMJLNZQAeJ52eyL-9fln-Bv7n9v7K9G6ieR6Tm0tsJ1PRnaQi7rA1NTFwHoQmIOW9tfMycLzT7bgSoz3ra6Ez2J7ZNuWBZNKS0O-0YfSrAWyWK5U8YRfQuSVzP2VrIU63K6RGU2c284PfQGy11kgKUNQPykirb8p7MDQ8PwrxKaylBnD6hhDgjqEh2bfsr_43DfJA0R58L1HK3BmQnxgap0C77wK1e0yNlABpN28Q"/> <env name="CLIENT_ACCESS_TOKEN" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxNSIsImp0aSI6IjAxN2QxZDg0Y2MxNjAyZTYxOGZkNjYwZGViZWVjMDY4MTk2YmYzMDk1OGMzY2RiYzBjZmJkZWFjZjFhOTUxODQzZDU1YTk3OGY2YWIxY2YzIiwiaWF0IjoxNjY0NjIzMzA4LjQyODI4MSwibmJmIjoxNjY0NjIzMzA4LjQyODI4NCwiZXhwIjoxNjgwMzQ4MTA4LjQxNjc4MSwic3ViIjoiMzkiLCJzY29wZXMiOlsiYXBpIiwicmVhZF91c2VyIl0sImNsaWVudCI6eyJ0cnVzdGVkIjpmYWxzZX19.vxnzCaU4PpOrNVHa5AnGSS6gX_RCvxIERAnHFhjTrUzRafV9mr2Cvwd-BDVYoUr10wHvxa_TJSYfnAdDuhE-MEyDv13O3dL2XGTtJNa_Rg6L6CQ0JvC3wL-lWPvGPFax9pu-_lqbA3jm5B08hc3-7tq3f2nXcxjhtkqT6TTJv1-RCAppb2HCXiUDAqANzbhyInDjOH2WvFj1OGC_AI03J3W2KRWyeFLtUne8XKPFyr9XGcPuTrqogcuuXLeUt2kcf2bXbuIV1OlgIECrDiP1Ee0F2AzPs27ZVJ2z0R0JbT6AubKhGl5_Qi27cwjOH7hT2dmjcF1mLjzpN1uChLIdSnGSoStH8VzYHnHE2I8G-owW_aadG2UmGdnRY143q6g_28f3WIZNSucBSXkwFeS_t4fylsvpxhpjYJusf5qiEU_X3YbeawYMUCFUkSD2XTIypAqMJLNZQAeJ52eyL-9fln-Bv7n9v7K9G6ieR6Tm0tsJ1PRnaQi7rA1NTFwHoQmIOW9tfMycLzT7bgSoz3ra6Ez2J7ZNuWBZNKS0O-0YfSrAWyWK5U8YRfQuSVzP2VrIU63K6RGU2c284PfQGy11kgKUNQPykirb8p7MDQ8PwrxKaylBnD6hhDgjqEh2bfsr_43DfJA0R58L1HK3BmQnxgap0C77wK1e0yNlABpN28Q"/>
</php> </php>
</phpunit> </phpunit>

View File

@@ -46,16 +46,6 @@ class AnikeenId
*/ */
public static bool $unserializesCookies = false; public static bool $unserializesCookies = false;
/**
* The base URL for Anikeen ID API.
*/
private static string $baseUrl = 'https://id.anikeen.com/api/';
/**
* The staging base URL for Anikeen ID API.
*/
private static string $stagingBaseUrl = 'https://staging.id.anikeen.com/api/';
/** /**
* The key for the access token. * The key for the access token.
*/ */
@@ -96,6 +86,16 @@ class AnikeenId
*/ */
protected ?string $redirectUri = null; protected ?string $redirectUri = null;
/**
* The base URL for Anikeen ID.
*/
protected string $baseUrl = 'https://id.anikeen.com';
/**
* The staging base URL for Anikeen ID.
*/
protected string $stagingBaseUrl = 'https://staging.id.anikeen.com';
/** /**
* Constructor. * Constructor.
*/ */
@@ -110,25 +110,25 @@ class AnikeenId
if ($redirectUri = config('services.anikeen.redirect')) { if ($redirectUri = config('services.anikeen.redirect')) {
$this->setRedirectUri($redirectUri); $this->setRedirectUri($redirectUri);
} }
if (config('services.anikeen.mode') === 'staging') { if (self::getMode() === 'staging' && !config('services.anikeen.base_url')) {
self::setBaseUrl(self::$stagingBaseUrl); self::setBaseUrl($this->stagingBaseUrl);
} }
if ($baseUrl = config('services.anikeen.base_url')) { if ($baseUrl = config('services.anikeen.base_url')) {
self::setBaseUrl($baseUrl); self::setBaseUrl($baseUrl);
} }
$this->client = new Client([ $this->client = new Client([
'base_uri' => self::$baseUrl, 'base_uri' => $this->baseUrl . '/api/',
]); ]);
} }
/** protected function setBaseUrl(string $baseUrl): void
* @param string $baseUrl
*
* @internal only for internal and debug purposes.
*/
public static function setBaseUrl(string $baseUrl): void
{ {
self::$baseUrl = $baseUrl; $this->baseUrl = $baseUrl;
}
public function getBaseUrl(): string
{
return rtrim($this->baseUrl, '/');
} }
public static function useAccessTokenField(string $accessTokenField): void public static function useAccessTokenField(string $accessTokenField): void
@@ -141,6 +141,11 @@ class AnikeenId
return self::$accessTokenField; return self::$accessTokenField;
} }
public static function getMode(): string
{
return config('services.anikeen.mode') ?: 'production';
}
public static function useRefreshTokenField(string $refreshTokenField): void public static function useRefreshTokenField(string $refreshTokenField): void
{ {
self::$refreshTokenField = $refreshTokenField; self::$refreshTokenField = $refreshTokenField;
@@ -322,7 +327,7 @@ class AnikeenId
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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) { if ($paginator !== null) {
$parameters[$paginator->action] = $paginator->cursor(); $parameters[$paginator->action] = $paginator->cursor();
@@ -330,7 +335,7 @@ class AnikeenId
try { try {
$response = $this->client->request($method, $path, [ $response = $this->client->request($method, $path, [
'headers' => $this->buildHeaders((bool)$payload), 'headers' => $this->buildHeaders((bool)$payload, $useClientSecret),
'query' => Query::build($parameters), 'query' => Query::build($parameters),
'json' => $payload ?: null, 'json' => $payload ?: null,
]); ]);
@@ -348,14 +353,14 @@ class AnikeenId
* *
* @throws RequestRequiresClientIdException * @throws RequestRequiresClientIdException
*/ */
private function buildHeaders(bool $json = false): array private function buildHeaders(bool $json = false, bool $useClientSecret = false): array
{ {
$headers = [ $headers = [
'Client-ID' => $this->getClientId(), 'Client-ID' => $this->getClientId(),
'Accept' => 'application/json', 'Accept' => 'application/json',
]; ];
if ($this->token) { if ($bearerToken = $useClientSecret ? $this->getClientSecret() : $this->getToken()) {
$headers['Authorization'] = 'Bearer ' . $this->token; $headers['Authorization'] = 'Bearer ' . $bearerToken;
} }
if ($json) { if ($json) {
$headers['Content-Type'] = 'application/json'; $headers['Content-Type'] = 'application/json';

View File

@@ -2,7 +2,6 @@
namespace Anikeen\Id; namespace Anikeen\Id;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Concerns\ManagesAddresses; use Anikeen\Id\Concerns\ManagesAddresses;
use Anikeen\Id\Concerns\ManagesBalance; use Anikeen\Id\Concerns\ManagesBalance;
use Anikeen\Id\Concerns\ManagesCountries; use Anikeen\Id\Concerns\ManagesCountries;
@@ -13,10 +12,7 @@ use Anikeen\Id\Concerns\ManagesProfile;
use Anikeen\Id\Concerns\ManagesSubscriptions; use Anikeen\Id\Concerns\ManagesSubscriptions;
use Anikeen\Id\Concerns\ManagesTaxation; use Anikeen\Id\Concerns\ManagesTaxation;
use Anikeen\Id\Concerns\ManagesTransactions; use Anikeen\Id\Concerns\ManagesTransactions;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Helpers\Paginator;
use GuzzleHttp\Exception\GuzzleException;
use stdClass;
trait Billable trait Billable
{ {
@@ -30,35 +26,27 @@ trait Billable
use ManagesSubscriptions; use ManagesSubscriptions;
use ManagesTaxation; use ManagesTaxation;
use ManagesTransactions; use ManagesTransactions;
use Request;
protected stdClass|null $userData = null;
/** /**
* Get the currently authenticated user. * Get the currently authenticated user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function getUserData(): stdClass public function getUserData(): object
{ {
if (!$this->userData) { if (!isset($this->userDataCache)) {
$this->userData = $this->request('GET', 'v1/user')->data; $this->userDataCache = $this->anikeenId()->request('GET', 'v1/user')->data;
} }
return $this->userData; return $this->userDataCache;
} }
/** /**
* Make a request to the Anikeen API. * Get the AnikeenId class.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function request(string $method, string $path, null|array $payload = null, array $parameters = [], ?Paginator $paginator = null): Result public function anikeenId(): AnikeenId
{ {
$anikeenId = new AnikeenId(); return app(AnikeenId::class)->withToken($this->{AnikeenId::getAccessTokenField()});
$anikeenId->withToken($this->{AnikeenId::getAccessTokenField()});
return $anikeenId->request($method, $path, $payload, $parameters, $paginator);
} }
} }

View File

@@ -2,18 +2,21 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\Contracts\Billable;
use Illuminate\Database\Eloquent\Model;
trait HasBillable 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; $this->billable = $billable;
return $this; return $this;
} }
public function getBillable(): mixed public function getBillable(): Billable
{ {
return $this->billable; return $this->billable;
} }

View File

@@ -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;
}
}

View File

@@ -2,33 +2,34 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Addresses; use Anikeen\Id\Resources\Addresses;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesAddresses trait ManagesAddresses
{ {
use Request; use HasBillable;
/** /**
* Get addresses from the current user. * Get addresses from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function addresses(): Addresses public function addresses(): Addresses
{ {
return (new Addresses($this->request('GET', 'v1/addresses'))) if (!isset($this->addressesCache)) {
->setBillable($this); $this->addressesCache = Addresses::builder(fn() => $this->anikeenId()
->request('GET', 'v1/addresses'))
->setBillable($this);
}
return $this->addressesCache;
} }
/** /**
* Check if the current user has a default billing address. * Check if the current user has a default billing address.
* *
* @throws Throwable
* @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress() * @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/ */
public function hasDefaultBillingAddress(): bool public function hasDefaultBillingAddress(): bool
{ {

View File

@@ -2,21 +2,17 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Transaction; use Anikeen\Id\Resources\Transaction;
use Anikeen\Id\Result; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
trait ManagesBalance trait ManagesBalance
{ {
use Request; use HasBillable;
/** /**
* Get balance from the current user. * Get balance from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function balance(): float public function balance(): float
{ {
@@ -26,8 +22,7 @@ trait ManagesBalance
/** /**
* Get charges from the current user. * Get charges from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function charges(): float public function charges(): float
{ {
@@ -40,15 +35,16 @@ trait ManagesBalance
* @param float $amount Amount to charge in euros. * @param float $amount Amount to charge in euros.
* @param string $paymentMethodId Payment method ID. * @param string $paymentMethodId Payment method ID.
* @param array $options Additional options for the charge. * @param array $options Additional options for the charge.
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction
{ {
return new Transaction($this->request('POST', 'billing/charge', [ return (new Transaction(fn() => $this->anikeenId()
'amount' => $amount, ->request('POST', 'billing/charge', [
'payment_method_id' => $paymentMethodId, 'amount' => $amount,
'options' => $options, 'payment_method_id' => $paymentMethodId,
])); 'options' => $options,
])))
->setBillable($this);
} }
} }

View File

@@ -2,24 +2,26 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Countries; use Anikeen\Id\Resources\Countries;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesCountries trait ManagesCountries
{ {
use Request; use HasBillable;
/** /**
* Get available countries for the current user. * Get available countries for the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function countries(): Countries public function countries(): Countries
{ {
return (new Countries($this->request('GET', 'v1/countries'))) if (!isset($this->countriesCache)) {
->setBillable($this); $this->countriesCache = Countries::builder(fn() => $this->anikeenId()
->request('GET', 'v1/countries'))
->setBillable($this);
}
return $this->countriesCache;
} }
} }

View File

@@ -2,24 +2,26 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Invoices; use Anikeen\Id\Resources\Invoices;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesInvoices trait ManagesInvoices
{ {
use Request; use HasBillable;
/** /**
* Get invoices from the current user. * Get invoices from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function invoices(array $parameters = []): Invoices public function invoices(array $parameters = []): Invoices
{ {
return (new Invoices($this->request('GET', 'v1/invoices', [], $parameters))) if (!isset($this->invoicesCache)) {
->setBillable($this); $this->invoicesCache = Invoices::builder(fn() => $this->anikeenId()
->request('GET', 'v1/invoices', [], $parameters))
->setBillable($this);
}
return $this->invoicesCache;
} }
} }

View File

@@ -2,25 +2,26 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Orders; use Anikeen\Id\Resources\Orders;
use Anikeen\Id\Result; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
trait ManagesOrders trait ManagesOrders
{ {
use Request; use HasBillable;
/** /**
* Get orders from the current user. * Get orders from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function orders(array $parameters = []): Orders public function orders(array $parameters = []): Orders
{ {
return (new Orders($this->request('GET', 'v1/orders', [], $parameters))) if (!isset($this->ordersCache)) {
->setBillable($this); $this->ordersCache = Orders::builder(fn() => $this->anikeenId()
->request('GET', 'v1/orders', [], $parameters))
->setBillable($this);
}
return $this->ordersCache;
} }
} }

View File

@@ -2,35 +2,36 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\PaymentMethod; use Anikeen\Id\Resources\PaymentMethod;
use Anikeen\Id\Resources\PaymentMethods; use Anikeen\Id\Resources\PaymentMethods;
use Anikeen\Id\Result; use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesPaymentMethods trait ManagesPaymentMethods
{ {
use Request; use HasBillable;
/** /**
* Get payment methods from the current user. * Get payment methods from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function paymentMethods(): PaymentMethods public function paymentMethods(): PaymentMethods
{ {
return (new PaymentMethods($this->request('GET', 'v1/payment-methods'))) if (!isset($this->paymentMethodsCache)) {
->setBillable($this); $this->paymentMethodsCache = PaymentMethods::builder(fn() => $this->anikeenId()
->request('GET', 'v1/payment-methods'))
->setBillable($this);
}
return $this->paymentMethodsCache;
} }
/** /**
* Check if current user has at least one payment method. * Check if current user has at least one payment method.
* *
* @throws Throwable
* @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod() * @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/ */
public function hasPaymentMethod(): ?PaymentMethod public function hasPaymentMethod(): ?PaymentMethod
{ {
@@ -40,9 +41,8 @@ trait ManagesPaymentMethods
/** /**
* Get default payment method from the current user. * Get default payment method from the current user.
* *
* @throws Throwable
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod() * @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/ */
public function defaultPaymentMethod(): ?PaymentMethod public function defaultPaymentMethod(): ?PaymentMethod
{ {
@@ -52,9 +52,8 @@ trait ManagesPaymentMethods
/** /**
* Check if the current user has a default payment method. * Check if the current user has a default payment method.
* *
* @throws Throwable
* @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod() * @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/ */
public function hasDefaultPaymentMethod(): bool public function hasDefaultPaymentMethod(): bool
{ {
@@ -66,28 +65,30 @@ trait ManagesPaymentMethods
* *
* @param string|null $returnUrl The URL to redirect to after the user has finished in the billing portal. * @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. * @param array $options Additional options for the billing portal.
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function billingPortalUrl(?string $returnUrl = null, array $options = []): string public function billingPortalUrl(?string $returnUrl = null, array $options = []): string
{ {
return $this->request('POST', 'v1/billing/portal', [ return $this->anikeenId()
'return_url' => $returnUrl, ->request('POST', 'v1/billing/portal', [
'options' => $options, 'return_url' => $returnUrl,
])->data->url; 'options' => $options,
])
->data
->url;
} }
/** /**
* Create a new setup intent. * Create a new setup intent.
* *
* @param array $options Additional options for the setup intent. * @param array $options Additional options for the setup intent.
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function createSetupIntent(array $options = []): Result public function createSetupIntent(array $options = []): Result
{ {
return $this->request('POST', 'v1/payment-methods', [ return $this->anikeenId()
'options' => $options, ->request('POST', 'v1/payment-methods', [
]); 'options' => $options,
]);
} }
} }

View File

@@ -3,9 +3,8 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Post; use Anikeen\Id\ApiOperations\Post;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result; use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesPricing trait ManagesPricing
{ {
@@ -29,8 +28,7 @@ trait ManagesPricing
* } $attributes The order data: * } $attributes The order data:
* - country_iso: ISO 3166-1 alpha-2 country code * - country_iso: ISO 3166-1 alpha-2 country code
* - items: Array of order items (each with type, name, price, unit, units, and quantity) * - items: Array of order items (each with type, name, price, unit, units, and quantity)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function createOrderPreview(array $attributes = []): Result public function createOrderPreview(array $attributes = []): Result
{ {

View File

@@ -2,14 +2,11 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request; use Throwable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
trait ManagesProfile trait ManagesProfile
{ {
use Request; use HasBillable;
/** /**
* Get the profile url for the current user. * Get the profile url for the current user.
@@ -17,12 +14,11 @@ trait ManagesProfile
* @param string|null $returnUrl The URL to redirect to after the user has completed their profile. * @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. * @param array $options Additional options for the profile URL.
* @return string * @return string
* @throws GuzzleException * @throws Throwable
* @throws RequestRequiresClientIdException
*/ */
public function profilePortalUrl(?string $returnUrl = null, array $options = []): string 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, 'return_url' => $returnUrl,
'options' => $options, 'options' => $options,
])->data->url; ])->data->url;

View File

@@ -4,9 +4,8 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Get; use Anikeen\Id\ApiOperations\Get;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\SshKeys; use Anikeen\Id\Resources\SshKeys;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesSshKeys trait ManagesSshKeys
{ {
@@ -15,12 +14,15 @@ trait ManagesSshKeys
/** /**
* Get currently authed user with Bearer Token. * Get currently authed user with Bearer Token.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function sshKeysByUserId(string $sskKeyId): SshKeys public function sshKeysByUserId(string $sskKeyId): SshKeys
{ {
return (new SshKeys($this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId)))) if (!isset($this->sshKeysCache)) {
->setParent($this); $this->sshKeysCache = SshKeys::builder(fn() => $this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId)))
->setParent($this);
}
return $this->sshKeysCache;
} }
} }

View File

@@ -2,24 +2,26 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Subscriptions; use Anikeen\Id\Resources\Subscriptions;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesSubscriptions trait ManagesSubscriptions
{ {
use Request; use HasBillable;
/** /**
* Get subscriptions from the current user. * Get subscriptions from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function subscriptions(): Subscriptions public function subscriptions(): Subscriptions
{ {
return (new Subscriptions($this->request('GET', 'v1/subscriptions'))) if (!isset($this->subscriptionsCache)) {
->setBillable($this); $this->subscriptionsCache = Subscriptions::builder(fn() => $this->anikeenId()
->request('GET', 'v1/subscriptions'))
->setBillable($this);
}
return $this->subscriptionsCache;
} }
} }

View File

@@ -2,22 +2,19 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request; use Throwable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use GuzzleHttp\Exception\GuzzleException;
trait ManagesTaxation trait ManagesTaxation
{ {
use Request; use HasBillable;
/** /**
* Get VAT for the current user. * Get VAT for the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function vat(): float public function vatRate(): float
{ {
return $this->getUserData()->vat; return $this->getUserData()->vat_rate;
} }
} }

View File

@@ -2,25 +2,26 @@
namespace Anikeen\Id\Concerns; namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Request;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Resources\Transactions; use Anikeen\Id\Resources\Transactions;
use Anikeen\Id\Result; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
trait ManagesTransactions trait ManagesTransactions
{ {
use Request; use HasBillable;
/** /**
* Get transactions from the current user. * Get transactions from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function transactions(): Transactions public function transactions(): Transactions
{ {
return (new Transactions($this->request('GET', 'v1/transactions'))) if (!isset($this->transactionsCache)) {
->setBillable($this);; $this->transactionsCache = Transactions::builder(fn() => $this->anikeenId()
->request('GET', 'v1/transactions'))
->setBillable($this);
}
return $this->transactionsCache;
} }
} }

View File

@@ -5,9 +5,8 @@ namespace Anikeen\Id\Concerns;
use Anikeen\Id\ApiOperations\Get; use Anikeen\Id\ApiOperations\Get;
use Anikeen\Id\ApiOperations\Post; use Anikeen\Id\ApiOperations\Post;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result; use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
trait ManagesUsers trait ManagesUsers
{ {
@@ -17,8 +16,7 @@ trait ManagesUsers
/** /**
* Get currently authed user with Bearer Token * Get currently authed user with Bearer Token
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function getAuthedUser(): Result public function getAuthedUser(): Result
{ {
@@ -40,20 +38,32 @@ trait ManagesUsers
* - username: The username (optional) * - username: The username (optional)
* - email: The email (required) * - email: The email (required)
* - password: The password (optional, can be reset by the user if not provided) * - password: The password (optional, can be reset by the user if not provided)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function createUser(array $attributes): Result public function createUser(array $attributes): Result
{ {
return $this->post('v1/users', $attributes); 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. * Checks if the given email exists.
* *
* @param string $email The email to check. * @param string $email The email to check.
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function isEmailExisting(string $email): Result public function isEmailExisting(string $email): Result
{ {

View File

@@ -0,0 +1,12 @@
<?php
namespace Anikeen\Id\Contracts;
use Anikeen\Id\AnikeenId;
interface Billable
{
public function getUserData(): object;
public function anikeenId(): AnikeenId;
}

View File

@@ -6,11 +6,35 @@ class Scope
{ {
const USER = 'user'; const USER = 'user';
const USER_READ = 'user:read'; const USER_READ = 'user:read';
const ORDERS = 'orders';
const ORDERS_READ = 'orders:read'; const ADDRESSES = 'addresses';
const PRODUCTS = 'products'; const ADDRESSES_READ = 'addresses:read';
const PRODUCTS_READ = 'products:read';
const BILLING = 'billing'; const BILLING = 'billing';
const BILLING_READ = 'billing:read'; const BILLING_READ = 'billing:read';
const BILLING_CLIENT = 'billing:client';
const INVOICES = 'invoices';
const INVOICES_READ = 'invoices:read';
const INVOICES_CLIENT = 'invoices:client';
const ORDERS = 'orders';
const ORDERS_READ = 'orders:read';
const ORDERS_CLIENT = 'orders:client';
const PAYMENT_METHODS = 'payment-methods';
const PAYMENT_METHODS_READ = 'payment-methods:read';
const SUBSCRIPTIONS = 'subscriptions';
const SUBSCRIPTIONS_READ = 'subscriptions:read';
const SUBSCRIPTIONS_CLIENT = 'subscriptions:client';
const TRANSACTIONS = 'transactions';
const TRANSACTIONS_READ = 'transactions:read';
const TRANSACTIONS_CLIENT = 'transactions:client';
const SSH_KEYS = 'ssh-keys';
const SSH_KEYS_READ = 'ssh-keys:read';
const ADMIN = 'admin'; const ADMIN = 'admin';
} }

View File

@@ -0,0 +1,11 @@
<?php
namespace Anikeen\Id\Exceptions;
use Anikeen\Id\Result;
use Exception;
class CollectionException extends Exception
{
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Anikeen\Id\Exceptions;
use Anikeen\Id\Result;
use Exception;
class ResourceException extends Exception
{
//
}

View File

@@ -2,6 +2,7 @@
namespace Anikeen\Id\Helpers; namespace Anikeen\Id\Helpers;
use Anikeen\Id\AnikeenId;
use Firebase\JWT\JWT; use Firebase\JWT\JWT;
use Firebase\JWT\Key; use Firebase\JWT\Key;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
@@ -30,6 +31,8 @@ class JwtParser
private function getOauthPublicKey(): bool|string private function getOauthPublicKey(): bool|string
{ {
return file_get_contents(dirname(__DIR__, 3) . '/oauth-public.key'); return AnikeenId::getMode() === 'staging'
? file_get_contents(dirname(__DIR__, 3) . '/oauth-public.staging.key')
: file_get_contents(dirname(__DIR__, 3) . '/oauth-public.key');
} }
} }

View File

@@ -9,7 +9,7 @@ use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use stdClass; use stdClass;
abstract class CheckCredentials abstract class CheckCredentials extends UseParameters
{ {
/** /**
* Handle an incoming request. * Handle an incoming request.

View File

@@ -8,7 +8,7 @@ use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
class CheckForAnyScope class CheckForAnyScope extends UseParameters
{ {
/** /**
* Handle the incoming request. * Handle the incoming request.

View File

@@ -8,7 +8,7 @@ use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
class CheckScopes class CheckScopes extends UseParameters
{ {
/** /**
* Handle the incoming request. * Handle the incoming request.

View File

@@ -0,0 +1,20 @@
<?php
namespace Anikeen\Id\Http\Middleware;
abstract class UseParameters
{
/**
* Specify the parameters for the middleware.
*
* @param string[]|string $param
*/
public static function using(array|string $param, string ...$params): string
{
if (is_array($param)) {
return static::class . ':' . implode(',', $param);
}
return static::class . ':' . implode(',', [$param, ...$params]);
}
}

View File

@@ -23,13 +23,11 @@ trait OauthTrait
], ],
]); ]);
$result = new Result($response, null); $result = new Result($response, null, $this);
} catch (RequestException $exception) { } catch (RequestException $exception) {
$result = new Result($exception->getResponse(), $exception); $result = new Result($exception->getResponse(), $exception, $this);
} }
$result->anikeenId = $this;
return $result; return $result;
} }
} }

View File

@@ -3,9 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @property string $id * @property string $id
@@ -63,23 +61,23 @@ class Address extends BaseResource
* - email: Email address (optional, e.g. for delivery notifications) * - email: Email address (optional, e.g. for delivery notifications)
* - primary: Whether this address should be the primary address (optional) * - primary: Whether this address should be the primary address (optional)
* - primary_billing_address: Whether this address should be the primary billing address (optional) * - primary_billing_address: Whether this address should be the primary billing address (optional)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function update(array $attributes = []): self 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->anikeenId()
->request('PUT', sprintf('v1/addresses/%s', $this->id), $attributes)))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Delete given address from the current user. * Delete given address from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function delete(): bool 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();
} }
} }

View File

@@ -3,9 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class Addresses extends BaseCollection class Addresses extends BaseCollection
{ {
@@ -44,12 +42,12 @@ class Addresses extends BaseCollection
*   - email: Email address (optional, e.g. for delivery notifications) *   - email: Email address (optional, e.g. for delivery notifications)
*   - primary: Whether this address should be the primary address (optional) *   - primary: Whether this address should be the primary address (optional)
*   - primary_billing_address: Whether this address should be the primary billing address (optional) *   - primary_billing_address: Whether this address should be the primary billing address (optional)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function create(array $attributes = []): Address public function create(array $attributes = []): Address
{ {
return (new Address($this->billable->request('POST', 'v1/addresses', $attributes))) return (new Address(fn() => $this->billable->anikeenId()
->request('POST', 'v1/addresses', $attributes)))
->setBillable($this->billable); ->setBillable($this->billable);
} }
@@ -58,31 +56,27 @@ class Addresses extends BaseCollection
*/ */
public function find(string $id): ?Address public function find(string $id): ?Address
{ {
$result = $this->billable->request('GET', sprintf('v1/addresses/%s', $id)); return (new Address(fn() => $this->billable->anikeenId()
->request('GET', sprintf('v1/addresses/%s', $id))))
return $result->success() ? ->setBillable($this->billable);
(new Address($result))
->setBillable($this->billable)
: null;
} }
/** /**
* Get default address from the current user. * Get default address from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function defaultBillingAddress(): Address 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->anikeenId()
->request('GET', sprintf('v1/addresses/%s', $this->billable->getUserData()->billing_address_id))))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Check if the current user has a default billing address. * Check if the current user has a default billing address.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function hasDefaultBillingAddress(): bool public function hasDefaultBillingAddress(): bool
{ {

View File

@@ -2,17 +2,51 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\MagicProperties; use Anikeen\Id\AnikeenId;
use Anikeen\Id\Exceptions\CollectionException;
use Anikeen\Id\Helpers\Paginator;
use Anikeen\Id\Result; use Anikeen\Id\Result;
use Closure;
use GuzzleHttp\Psr7\Response;
use JsonSerializable; 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
*/
#[\AllowDynamicProperties]
abstract class BaseCollection implements JsonSerializable 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. * Returns the Resource based on the ID.
*/ */
abstract public function find(string $id): ?BaseResource; 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});
}
} }

View File

@@ -2,17 +2,39 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\MagicProperties; use Anikeen\Id\Exceptions\ResourceException;
use Anikeen\Id\Result; use Anikeen\Id\Result;
use JsonSerializable; use JsonSerializable;
#[\AllowDynamicProperties]
abstract class BaseResource implements 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 +45,13 @@ abstract class BaseResource implements JsonSerializable
return (array)$this->result->data; return (array)$this->result->data;
} }
/** public function __get(string $name)
* Returns the collection of resources as a JSON string.
*/
public function jsonSerialize(): array
{ {
return $this->toArray(); return null;
}
public function __isset(string $name): bool
{
return false;
} }
} }

View File

@@ -3,8 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @property string $id * @property string $id
@@ -16,11 +15,13 @@ class Invoice extends BaseResource
/** /**
* Get temporary download url from given invoice. * Get temporary download url from given invoice.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function getInvoiceTemporaryUrl(): string 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;
} }
} }

View File

@@ -3,9 +3,6 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class Invoices extends BaseCollection class Invoices extends BaseCollection
{ {
@@ -16,11 +13,8 @@ class Invoices extends BaseCollection
*/ */
public function find(string $id): ?Invoice public function find(string $id): ?Invoice
{ {
$result = $this->billable->request('GET', sprintf('v1/invoices/%s', $id)); return (new Invoice(fn() => $this->billable->anikeenId()
->request('GET', sprintf('v1/invoices/%s', $id))))
return $result->success() ->setBillable($this->billable);
? (new Invoice($result))
->setBillable($this->billable)
: null;
} }
} }

View File

@@ -3,8 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @property string $id * @property string $id
@@ -55,59 +54,59 @@ class Order extends BaseResource
* } $attributes The order data: * } $attributes The order data:
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code) * - 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) * - shipping_address: Shipping address (first name, last name, ISO 3166-1 alpha-2 country code)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function update(array $attributes = []): self 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->anikeenId()
->request('PUT', sprintf('v1/orders/%s', $this->id), $attributes)))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Checkout given order from the current user. * Checkout given order from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
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->anikeenId()
->request('PUT', sprintf('v1/orders/%s/checkout', $this->id))))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Revoke given order from the current user. * Revoke given order from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
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->anikeenId()
->request('PUT', sprintf('v1/orders/%s/revoke', $this->id))))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Delete given order from the current user. * Delete given order from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function delete(): bool 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();
} }
/** /**
* Get order items from given order. * Get order items from given order.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function orderItems(array $parameters = []): OrderItems 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->anikeenId()
->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters))
->setBillable($this->billable) ->setBillable($this->billable)
->setParent($this); ->setParent($this);
} }

View File

@@ -4,8 +4,7 @@ namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Concerns\HasParent; use Anikeen\Id\Concerns\HasParent;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @property string $id * @property string $id
@@ -31,12 +30,12 @@ class OrderItem extends BaseResource
* }> * }>
* } $attributes The order data: * } $attributes The order data:
* - items: Array of order items, each with type, name, description, price, unit, and quantity * - items: Array of order items, each with type, name, description, price, unit, and quantity
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function update(array $attributes = []): self 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->anikeenId()
->request('PUT', sprintf('v1/orders/%s/items/%s', $this->parent->id, $this->id), $attributes)))
->setBillable($this->billable) ->setBillable($this->billable)
->setParent($this->parent); ->setParent($this->parent);
} }
@@ -44,11 +43,11 @@ class OrderItem extends BaseResource
/** /**
* Delete given order item from given order. * Delete given order item from given order.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function delete(): bool 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();
} }
} }

View File

@@ -4,9 +4,7 @@ namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Concerns\HasParent; use Anikeen\Id\Concerns\HasParent;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class OrderItems extends BaseCollection class OrderItems extends BaseCollection
{ {
@@ -30,12 +28,12 @@ class OrderItems extends BaseCollection
* }> * }>
* } $attributes The order data: * } $attributes The order data:
* - items: Array of order items, each with type, name, description, price, unit, and quantity * - items: Array of order items, each with type, name, description, price, unit, and quantity
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function create(string $orderId, array $attributes = []): OrderItem 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->anikeenId()
->request('POST', sprintf('v1/orders/%s', $orderId), $attributes)))
->setBillable($this->billable) ->setBillable($this->billable)
->setParent($this->parent); ->setParent($this->parent);
} }
@@ -45,13 +43,9 @@ class OrderItems extends BaseCollection
*/ */
public function find(string $id): ?OrderItem public function find(string $id): ?OrderItem
{ {
/** @var Result $result */ return (new OrderItem(fn() => $this->parent->anikeenId()
$result = $this->parent->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id)); ->request('GET', sprintf('v1/orders/%s/items/%s', $this->parent->id, $id))))
->setBillable($this->billable)
return $result->success() ? ->setParent($this->parent);
(new OrderItem($result))
->setBillable($this->billable)
->setParent($this->parent)
: null;
} }
} }

View File

@@ -3,9 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class Orders extends BaseCollection class Orders extends BaseCollection
{ {
@@ -62,29 +60,24 @@ class Orders extends BaseCollection
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code) * - 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) * - 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) * - items: Array of order items (each with type, name, price, unit, units, and quantity)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function create(array $attributes = []): Order public function create(array $attributes = []): Order
{ {
return (new Order($this->billable->request('POST', 'v1/orders', $attributes))) return (new Order(fn() => $this->billable->anikeenId()
->request('POST', 'v1/orders', $attributes)))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Get given order from the current user. * Get given order from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function find(string $id): ?Order public function find(string $id): ?Order
{ {
/** @var Result $result */ return (new Order(fn() => $this->billable->anikeenId()
$result = $this->billable->request('GET', sprintf('v1/orders/%s', $id)); ->request('GET', sprintf('v1/orders/%s', $id))))
->setBillable($this->billable);
return $result->success()
? (new Order($result))
->setBillable($this->billable)
: null;
} }
} }

View File

@@ -3,65 +3,57 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class PaymentMethods extends BaseCollection class PaymentMethods extends BaseCollection
{ {
use HasBillable; use HasBillable;
private ?PaymentMethod $cachedDefaultPaymentMethod = null;
/** /**
* Check if current user has at least one payment method. * Check if current user has at least one payment method.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function hasPaymentMethod(): bool public function hasPaymentMethod(): bool
{ {
return $this->result->count() > 0; 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. * Check if the current user has a default payment method.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function hasDefaultPaymentMethod(): bool public function hasDefaultPaymentMethod(): bool
{ {
return $this->defaultPaymentMethod()?->id !== null; 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->anikeenId()
->request('GET', 'v1/payment-methods/default')))
->setBillable($this->billable);
}
return $this->defaultPaymentMethodCache;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*
* @throws Throwable
*/ */
public function find(string $id): ?PaymentMethod public function find(string $id): ?PaymentMethod
{ {
$result = $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))))
return $result->success() ->setBillable($this->billable);
? (new PaymentMethod($result))
->setBillable($this->billable)
: null;
} }
} }

View File

@@ -3,8 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasParent; use Anikeen\Id\Concerns\HasParent;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* @property string $id * @property string $id
@@ -16,8 +15,7 @@ class SshKey extends BaseResource
/** /**
* Deletes a given ssh key for the currently authed user. * Deletes a given ssh key for the currently authed user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function delete(): bool public function delete(): bool
{ {

View File

@@ -3,9 +3,7 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasParent; use Anikeen\Id\Concerns\HasParent;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Throwable;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class SshKeys extends BaseCollection class SshKeys extends BaseCollection
{ {
@@ -16,12 +14,11 @@ class SshKeys extends BaseCollection
* *
* @param string $publicKey The public key to be added * @param string $publicKey The public key to be added
* @param string|null $name The name of the key (optional) * @param string|null $name The name of the key (optional)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function create(string $publicKey, ?string $name = null): SshKey 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, 'public_key' => $publicKey,
'name' => $name, 'name' => $name,
])))->setParent($this->parent); ])))->setParent($this->parent);
@@ -32,12 +29,6 @@ class SshKeys extends BaseCollection
*/ */
public function find(string $id): ?SshKey public function find(string $id): ?SshKey
{ {
/** @var Result $result */ return (new SshKey(fn() => $this->parent->get(sprintf('v1/ssh-keys/%s', $id))));
$result = $this->parent->get(sprintf('v1/ssh-keys/%s', $id));
return $result->success()
? (new SshKey($result))
->setParent($this->parent)
: null;
} }
} }

View File

@@ -3,8 +3,8 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Contracts\AppTokenRepository;
use GuzzleHttp\Exception\GuzzleException; use Throwable;
/** /**
* @property string $id * @property string $id
@@ -26,6 +26,7 @@ class Subscription extends BaseResource
* Update a given subscription from the current user. * Update a given subscription from the current user.
* *
* @param array{ * @param array{
* group: string,
* name: string, * name: string,
* description: null|string, * description: null|string,
* unit: string, * unit: string,
@@ -36,6 +37,7 @@ class Subscription extends BaseResource
* webhook_url: null|string, * webhook_url: null|string,
* webhook_secret: null|string * webhook_secret: null|string
* } $attributes The subscription data: * } $attributes The subscription data:
* - group: The group (optional)
* - name: The name (required when set) * - name: The name (required when set)
* - description: The description (optional) * - description: The description (optional)
* - unit: The unit (required when set, e.g. "hour", "day", "week", "month", "year") * - unit: The unit (required when set, e.g. "hour", "day", "week", "month", "year")
@@ -45,59 +47,64 @@ class Subscription extends BaseResource
* - ends_at: The end date (optional) * - ends_at: The end date (optional)
* - webhook_url: The webhook URL (optional) * - webhook_url: The webhook URL (optional)
* - webhook_secret: The webhook secret (optional) * - webhook_secret: The webhook secret (optional)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function update(array $attributes): self public function update(array $attributes): self
{ {
return (new self($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); ->setBillable($this->billable);
} }
/** /**
* Force given subscription to check out (trusted apps only). * Force given subscription to check out (trusted apps only).
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function checkout(): self public function checkout(): self
{ {
return (new self($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); ->setBillable($this->billable);
} }
/** /**
* Revoke a given running subscription from the current user. * Revoke a given running subscription from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function revoke(): self public function revoke(bool $refund = false): self
{ {
return (new self($this->billable->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id)))) $attributes = [
'refund' => $refund,
];
return (new self(fn() => $this->billable->anikeenId()
->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id), $attributes)))
->setBillable($this->billable);
}
/**
* Pause a given running subscription from the current user.
*
* @throws Throwable
*/
public function pause(): self
{
return (new self(fn() => $this->billable->anikeenId()
->request('PUT', sprintf('v1/subscriptions/%s/pause', $this->id))))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* Resume a given running subscription from the current user. * Resume a given running subscription from the current user.
* *
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function resume(): self public function resume(): self
{ {
return (new self($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); ->setBillable($this->billable);
} }
/**
* Delete a given subscription from the current user.
*
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function delete(): bool
{
return $this->billable->request('DELETE', sprintf('v1/subscriptions/%s', $this->id))->success();
}
} }

View File

@@ -3,8 +3,9 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException; use Anikeen\Id\Contracts\AppTokenRepository;
use GuzzleHttp\Exception\GuzzleException; use Anikeen\Id\Exceptions\ResourceException;
use Throwable;
class Subscriptions extends BaseCollection class Subscriptions extends BaseCollection
{ {
@@ -14,6 +15,7 @@ class Subscriptions extends BaseCollection
* Create a new subscription for the current user. * Create a new subscription for the current user.
* *
* @param array{ * @param array{
* group: null|string,
* name: string, * name: string,
* description: null|string, * description: null|string,
* unit: string, * unit: string,
@@ -24,6 +26,7 @@ class Subscriptions extends BaseCollection
* webhook_url: null|string, * webhook_url: null|string,
* webhook_secret: null|string * webhook_secret: null|string
* } $attributes The subscription data: * } $attributes The subscription data:
* - group: The group (optional)
* - name: The name * - name: The name
* - description: The description (optional) * - description: The description (optional)
* - unit: The unit (e.g. "hour", "day", "week", "month", "year") * - unit: The unit (e.g. "hour", "day", "week", "month", "year")
@@ -33,25 +36,24 @@ class Subscriptions extends BaseCollection
* - ends_at: The end date (optional) * - ends_at: The end date (optional)
* - webhook_url: The webhook URL (optional) * - webhook_url: The webhook URL (optional)
* - webhook_secret: The webhook secret (optional) * - webhook_secret: The webhook secret (optional)
* @throws RequestRequiresClientIdException * @throws Throwable
* @throws GuzzleException
*/ */
public function create(array $attributes): Subscription public function create(array $attributes): Subscription
{ {
return (new Subscription($this->billable->request('POST', 'v1/subscriptions', $attributes))) return (new Subscription(fn() => $this->billable->anikeenId()
->request('POST', 'v1/subscriptions', $attributes)))
->setBillable($this->billable); ->setBillable($this->billable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*
* @throws ResourceException
*/ */
public function find(string $id): ?Subscription public function find(string $id): ?Subscription
{ {
$result = $this->billable->request('GET', sprintf('v1/subscriptions/%s', $id)); return (new Subscription(fn() => $this->billable->anikeenId()
->request('GET', sprintf('v1/subscriptions/%s', $id))))
return $result->success() ->setBillable($this->billable);
? (new Subscription($result))
->setBillable($this->billable)
: null;
} }
} }

View File

@@ -3,37 +3,18 @@
namespace Anikeen\Id\Resources; namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable; use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class Transactions extends BaseCollection class Transactions extends BaseCollection
{ {
use HasBillable; use HasBillable;
/**
* 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.
*/
public function create(array $attributes = []): Transaction
{
return new Transaction($this->billable->request('POST', 'v1/transactions', $attributes));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function find(string $id): ?Transaction public function find(string $id): ?Transaction
{ {
$result = $this->billable->request('GET', sprintf('v1/transactions/%s', $id)); return (new Transaction(fn() => $this->billable->anikeenId()
->request('GET', sprintf('v1/transactions/%s', $id))))
return $result->success() ->setBillable($this->billable);
? (new Transaction($result))
->setBillable($this->billable)
: null;
} }
} }

View File

@@ -2,6 +2,7 @@
namespace Anikeen\Id\Socialite; namespace Anikeen\Id\Socialite;
use Anikeen\Id\AnikeenId;
use Anikeen\Id\Enums\Scope; use Anikeen\Id\Enums\Scope;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -32,11 +33,7 @@ class Provider extends AbstractProvider implements ProviderInterface
*/ */
protected function getBaseUrl(): string protected function getBaseUrl(): string
{ {
$mode = config('services.anikeen.mode') ?? 'production'; return app(AnikeenId::class)->getBaseUrl();
return $mode === 'staging'
? 'https://staging.id.anikeen.com'
: 'https://id.anikeen.com';
} }
/** /**
@@ -98,4 +95,12 @@ class Provider extends AbstractProvider implements ProviderInterface
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
]); ]);
} }
/**
* Returns the user logout url for the provider.
*/
public function getLogoutUrl(string $redirect = null): string
{
return app(AnikeenId::class)->getBaseUrl() . '/logout?redirect=' . urlencode($redirect ?: '/');
}
} }