Compare commits

...
4 Commits
Author SHA1 Message Date
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
envoyr 63e3f0a4a2 add refresh token method
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-05-02 07:29:43 +02:00
envoyr 1d2119a32b update resources
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-05-02 06:27:53 +02:00
35 changed files with 314 additions and 319 deletions

No files matched your search

+2 -2
View File
@@ -15,8 +15,8 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"illuminate/support": "^11.0|^12.0",
"illuminate/console": "^11.0|^12.0",
"illuminate/support": "^10.0|^11.0|^12.0",
"illuminate/console": "^10.0|^11.0|^12.0",
"guzzlehttp/guzzle": "^6.3|^7.0",
"socialiteproviders/manager": "^3.4|^4.0.1",
"firebase/php-jwt": "^6.0"
+6 -11
View File
@@ -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
{
-39
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;
}
}
+8 -6
View File
@@ -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,21 +14,23 @@ 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')))
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 RequestRequiresClientIdException
* @throws GuzzleException
* @throws Throwable
*/
public function hasDefaultBillingAddress(): bool
{
+5 -10
View File
@@ -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,
+7 -4
View File
@@ -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')))
if (!isset($this->countriesCache)) {
$this->countriesCache = Countries::builder(fn() => $this->request('GET', 'v1/countries'))
->setBillable($this);
}
return $this->countriesCache;
}
}
+7 -4
View File
@@ -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)))
if (!isset($this->invoicesCache)) {
$this->invoicesCache = Invoices::builder(fn() => $this->request('GET', 'v1/invoices', [], $parameters))
->setBillable($this);
}
return $this->invoicesCache;
}
}
+7 -4
View File
@@ -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)))
if (!isset($this->ordersCache)) {
$this->ordersCache = Orders::builder(fn() => $this->request('GET', 'v1/orders', [], $parameters))
->setBillable($this);
}
return $this->ordersCache;
}
}
+14 -15
View File
@@ -7,7 +7,7 @@ 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
{
@@ -16,21 +16,24 @@ 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 RequestRequiresClientIdException
* @throws GuzzleException
* @throws Throwable
*/
public function hasPaymentMethod(): ?PaymentMethod
{
@@ -41,8 +44,7 @@ trait ManagesPaymentMethods
* Get default payment method from the current user.
*
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
* @throws Throwable
*/
public function defaultPaymentMethod(): ?PaymentMethod
{
@@ -53,8 +55,7 @@ trait ManagesPaymentMethods
* Check if the current user has a default payment method.
*
* @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
* @throws Throwable
*/
public function hasDefaultPaymentMethod(): bool
{
@@ -66,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
{
@@ -81,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
{
+2 -3
View File
@@ -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
{
+2 -3
View File
@@ -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
{
+7 -4
View File
@@ -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))))
if (!isset($this->sshKeysCache)) {
$this->sshKeysCache = SshKeys::builder(fn() => $this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId)))
->setParent($this);
}
return $this->sshKeysCache;
}
}
+7 -4
View File
@@ -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')))
if (!isset($this->subscriptionsCache)) {
$this->subscriptionsCache = Subscriptions::builder(fn() => $this->request('GET', 'v1/subscriptions'))
->setBillable($this);
}
return $this->subscriptionsCache;
}
}
+4 -5
View File
@@ -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;
}
}
+8 -5
View File
@@ -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;
}
}
+18 -7
View File
@@ -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
View File
@@ -0,0 +1,11 @@
<?php
namespace Anikeen\Id\Exceptions;
use Anikeen\Id\Result;
use Exception;
class CollectionException extends Exception
{
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Anikeen\Id\Exceptions;
use Anikeen\Id\Result;
use Exception;
class ResourceException extends Exception
{
//
}
+2 -4
View File
@@ -23,13 +23,11 @@ trait OauthTrait
],
]);
$result = new Result($response, null);
$result = new Result($response, null, $this);
} catch (RequestException $exception) {
$result = new Result($exception->getResponse(), $exception);
$result = new Result($exception->getResponse(), $exception, $this);
}
$result->anikeenId = $this;
return $result;
}
}
+4 -8
View File
@@ -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);
}