3 Commits

Author SHA1 Message Date
3bcebd9d45 update subscription, add alias
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-04-30 09:02:24 +02:00
71663bffd8 small fixes
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-04-30 06:43:07 +02:00
5b2b3c72cc add serializable
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
2025-04-30 04:23:10 +02:00
13 changed files with 171 additions and 20 deletions

View File

@@ -13,7 +13,7 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"illuminate/support": "^11.0|^12.0",
"illuminate/console": "^11.0|^12.0",

View File

@@ -6,12 +6,34 @@ use stdClass;
trait MagicProperties
{
protected function setMagicProperties(stdClass $data): void
protected function setMagicProperties(stdClass|array $data): void
{
foreach ($data as $key => $value) {
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

@@ -22,4 +22,16 @@ trait ManagesAddresses
return (new Addresses($this->request('GET', 'v1/addresses')))
->setBillable($this);
}
/**
* Check if the current user has a default billing address.
*
* @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function hasDefaultBillingAddress(): bool
{
return $this->addresses()->hasDefaultBillingAddress();
}
}

View File

@@ -17,9 +17,9 @@ trait ManagesInvoices
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function invoices(): Invoices
public function invoices(array $parameters = []): Invoices
{
return (new Invoices($this->request('GET', 'v1/invoices')))
return (new Invoices($this->request('GET', 'v1/invoices', [], $parameters)))
->setBillable($this);
}
}

View File

@@ -18,9 +18,9 @@ trait ManagesOrders
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function orders(): Orders
public function orders(array $parameters = []): Orders
{
return (new Orders($this->request('GET', 'v1/orders')))
return (new Orders($this->request('GET', 'v1/orders', [], $parameters)))
->setBillable($this);
}
}

View File

@@ -4,6 +4,7 @@ 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;
@@ -24,6 +25,42 @@ trait ManagesPaymentMethods
->setBillable($this);
}
/**
* Check if current user has at least one payment method.
*
* @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function hasPaymentMethod(): ?PaymentMethod
{
return $this->paymentMethods()->hasPaymentMethod();
}
/**
* Get default payment method from the current user.
*
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
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 RequestRequiresClientIdException
* @throws GuzzleException
*/
public function hasDefaultPaymentMethod(): bool
{
return $this->paymentMethods()->hasDefaultPaymentMethod();
}
/**
* Get billing portal URL for the current user.
*

View File

@@ -2,11 +2,14 @@
namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\MagicProperties;
use Anikeen\Id\Result;
use JsonSerializable;
abstract class BaseCollection implements JsonSerializable
{
use MagicProperties;
public function __construct(protected Result $result)
{
//

View File

@@ -4,8 +4,9 @@ namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\MagicProperties;
use Anikeen\Id\Result;
use JsonSerializable;
abstract class BaseResource
abstract class BaseResource implements JsonSerializable
{
use MagicProperties;
@@ -13,4 +14,20 @@ abstract class BaseResource
{
$this->setMagicProperties($this->result->data);
}
/**
* Returns the collection of resources as an array.
*/
public function toArray(): array
{
return (array)$this->result->data;
}
/**
* Returns the collection of resources as a JSON string.
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}

View File

@@ -102,13 +102,12 @@ class Order extends BaseResource
/**
* Get order items from given order.
*
* @param string $orderId The order ID.
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/
public function orderItems(string $orderId): OrderItems
public function orderItems(array $parameters = []): OrderItems
{
return (new OrderItems($this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id))))
return (new OrderItems($this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters)))
->setBillable($this->billable)
->setParent($this);
}

View File

@@ -4,6 +4,9 @@ namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable;
/**
* @property string $id
*/
class PaymentMethod extends BaseResource
{
use HasBillable;

View File

@@ -11,6 +11,8 @@ class PaymentMethods extends BaseCollection
{
use HasBillable;
private ?PaymentMethod $cachedDefaultPaymentMethod = null;
/**
* Check if current user has at least one payment method.
*
@@ -30,8 +32,24 @@ class PaymentMethods extends BaseCollection
*/
public function defaultPaymentMethod(): PaymentMethod
{
return (new PaymentMethod($this->billable->request('GET', 'v1/payment-methods/default')))
->setBillable($this->billable);
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
*/
public function hasDefaultPaymentMethod(): bool
{
return $this->defaultPaymentMethod()?->id !== null;
}
/**

View File

@@ -8,11 +8,52 @@ use GuzzleHttp\Exception\GuzzleException;
/**
* @property string $id
* @property string $name
* @property string $description
* @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{
* name: null,
* description: null|string,
* unit: string,
* price: float,
* vat_rate: null|float,
* payload: null|array,
* ends_at: null|string,
* webhook_url: null|string,
* webhook_secret: null|string
* } $attributes The subscription data:
* - name: The name
* - description: The description (optional)
* - unit: The unit (e.g. "hour", "day", "week", "month", "year")
* - price: The price per unit
* - vat_rate: The VAT rate (optional)
* - 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
*/
public function update(array $attributes): self
{
return (new self($this->billable->request('PUT', sprintf('v1/subscriptions/%s', $this->id), $attributes)))
->setBillable($this->billable);
}
/**
* Force given subscription to check out (trusted apps only).
*

View File

@@ -4,7 +4,6 @@ namespace Anikeen\Id\Resources;
use Anikeen\Id\Concerns\HasBillable;
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
use Anikeen\Id\Result;
use GuzzleHttp\Exception\GuzzleException;
class Subscriptions extends BaseCollection
@@ -16,24 +15,24 @@ class Subscriptions extends BaseCollection
*
* @param array{
* name: null,
* description: string,
* description: null|string,
* unit: string,
* price: float,
* vat: null|float,
* vat_rate: null|float,
* payload: null|array,
* ends_at: null|string,
* webhook_url: null|string,
* webhook_secret: null|string
* } $attributes The subscription data:
* - 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 (optional)
* - payload: The payload (optional)
* - ends_at: The end date (optional)
* - webhook_url: The webhook URL (optional)
* - webhook_secret: The webhook secret (optional)
* - webhook_url: The webhook URL (optional)
* - webhook_secret: The webhook secret (optional)
* @throws RequestRequiresClientIdException
* @throws GuzzleException
*/