mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-15 14:46:15 +00:00
refactored code
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
This commit is contained in:
23
src/Id/Concerns/HasBillable.php
Normal file
23
src/Id/Concerns/HasBillable.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\Contracts\Billable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
trait HasBillable
|
||||
{
|
||||
protected Billable|Model $billable;
|
||||
|
||||
public function setBillable(Billable|Model $billable): self
|
||||
{
|
||||
$this->billable = $billable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBillable(): Billable|Model
|
||||
{
|
||||
return $this->billable;
|
||||
}
|
||||
}
|
||||
20
src/Id/Concerns/HasParent.php
Normal file
20
src/Id/Concerns/HasParent.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
trait HasParent
|
||||
{
|
||||
protected mixed $parent;
|
||||
|
||||
public function setParent($parent): self
|
||||
{
|
||||
$this->parent = $parent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
}
|
||||
17
src/Id/Concerns/MagicProperties.php
Normal file
17
src/Id/Concerns/MagicProperties.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use stdClass;
|
||||
|
||||
trait MagicProperties
|
||||
{
|
||||
protected function setMagicProperties(stdClass $data): void
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Anikeen\Id\Resources\Addresses;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesAddresses
|
||||
@@ -17,116 +17,9 @@ trait ManagesAddresses
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function addresses(): Result
|
||||
public function addresses(): Addresses
|
||||
{
|
||||
return $this->request('GET', 'v1/addresses');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new address for the current user.
|
||||
*
|
||||
* @param array{
|
||||
* company_name: null|string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* address: string,
|
||||
* address_2: null|string,
|
||||
* house_number: null|string,
|
||||
* postal_code: string,
|
||||
* city: string,
|
||||
* state: null|string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string,
|
||||
* primary: bool,
|
||||
* primary_billing_address: bool
|
||||
* } $attributes The address data:
|
||||
* - company_name: Company name (optional)
|
||||
* - first_name: First name
|
||||
* - last_name: Last name
|
||||
* - address: Address line 1 (e.g. street address, P.O. Box, etc.)
|
||||
* - address_2: Address line 2 (optional, e.g. apartment number, c/o, etc.)
|
||||
* - house_number: House number (optional)
|
||||
* - postal_code: Postal code
|
||||
* - city: City
|
||||
* - state: State (optional, e.g. province, region, etc.)
|
||||
* - country_iso: Country ISO code (e.g. US, CA, etc.)
|
||||
* - phone_number: Phone number (optional)
|
||||
* - 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
|
||||
*/
|
||||
public function createAddress(array $attributes = []): Result
|
||||
{
|
||||
return $this->request('POST', 'v1/addresses', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given address from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function address(string $addressId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/addresses/%s', $addressId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update given address from the current user.
|
||||
*
|
||||
* VAT is calculated based on the billing address and shown in the address response.
|
||||
*
|
||||
* @param string $addressId The address ID.
|
||||
* @param array{
|
||||
* company_name: null|string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* address_2: null|string,
|
||||
* address: string,
|
||||
* house_number: null|string,
|
||||
* postal_code: string,
|
||||
* city: string,
|
||||
* state: null|string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string,
|
||||
* primary: bool,
|
||||
* primary_billing_address: bool
|
||||
* } $attributes The address data:
|
||||
* - company_name: Company name (optional)
|
||||
* - first_name: First name (required when set)
|
||||
* - last_name: Last name (required when set)
|
||||
* - address: Address line 1 (e.g. street address, P.O. Box, etc.)
|
||||
* - address_2: Address line 2 (optional, e.g. apartment number, c/o, etc.)
|
||||
* - house_number: House number (optional)
|
||||
* - postal_code: Postal code (required when set)
|
||||
* - city: City (required when set)
|
||||
* - state: State (optional, e.g. province, region, etc.)
|
||||
* - country_iso: Country ISO code (required when set, e.g. US, CA, etc.)
|
||||
* - phone_number: Phone number (optional)
|
||||
* - 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
|
||||
*/
|
||||
public function updateAddress(string $addressId, array $attributes = []): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/addresses/%s', $addressId), $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete given address from the current user.
|
||||
*
|
||||
* @param string $addressId The address ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function deleteAddress(string $addressId): Result
|
||||
{
|
||||
return $this->request('DELETE', sprintf('v1/addresses/%s', $addressId));
|
||||
return (new Addresses($this->request('GET', 'v1/addresses')))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
|
||||
@@ -42,12 +43,12 @@ trait ManagesBalance
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Result
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction
|
||||
{
|
||||
return $this->request('POST', 'billing/charge', [
|
||||
return new Transaction($this->request('POST', 'billing/charge', [
|
||||
'amount' => $amount,
|
||||
'payment_method_id' => $paymentMethodId,
|
||||
'options' => $options,
|
||||
]);
|
||||
]));
|
||||
}
|
||||
}
|
||||
25
src/Id/Concerns/ManagesCountries.php
Normal file
25
src/Id/Concerns/ManagesCountries.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Countries;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesCountries
|
||||
{
|
||||
use Request;
|
||||
|
||||
/**
|
||||
* Get available countries for the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function countries(): Countries
|
||||
{
|
||||
return (new Countries($this->request('GET', 'v1/countries')))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Anikeen\Id\Resources\Invoices;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesInvoices
|
||||
@@ -17,32 +17,9 @@ trait ManagesInvoices
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function invoices(): Result
|
||||
public function invoices(): Invoices
|
||||
{
|
||||
return $this->request('GET', 'v1/invoices');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given invoice from the current user.
|
||||
*
|
||||
* @param string $invoiceId The invoice ID
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function invoice(string $invoiceId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/invoices/%s', $invoiceId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get temporary download url from given invoice.
|
||||
*
|
||||
* @param string $invoiceId The invoice ID
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getInvoiceTemporaryUrl(string $invoiceId): string
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/invoices/%s', $invoiceId))->data->temporary_url;
|
||||
return (new Invoices($this->request('GET', 'v1/invoices')))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Orders;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
@@ -17,254 +18,9 @@ trait ManagesOrders
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function orders(): Result
|
||||
public function orders(): Orders
|
||||
{
|
||||
return $this->request('GET', 'v1/orders');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new order for the current user.
|
||||
*
|
||||
* VAT is calculated based on the billing address and shown in the order response.
|
||||
*
|
||||
* The billing and shipping addresses are each persisted as standalone Address entities
|
||||
* in the database, but are also embedded (deep-copied) into the Order object itself
|
||||
* rather than merely referenced. This guarantees that the order retains its own snapshot
|
||||
* of both addresses for future reference.
|
||||
*
|
||||
* @param array{
|
||||
* billing_address: array{
|
||||
* company_name: null|string,
|
||||
* first_name: null|string,
|
||||
* last_name: null|string,
|
||||
* address: null|string,
|
||||
* address_2: null|string,
|
||||
* house_number: null|string,
|
||||
* city: null|string,
|
||||
* state: null|string,
|
||||
* postal_code: null|string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string
|
||||
* },
|
||||
* shipping_address: null|array{
|
||||
* company_name: null|string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* address: null|string,
|
||||
* address_2: string,
|
||||
* house_number: null|string,
|
||||
* city: string,
|
||||
* state: string,
|
||||
* postal_code: string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string
|
||||
* },
|
||||
* items: array<array{
|
||||
* type: string,
|
||||
* name: string,
|
||||
* description: string,
|
||||
* price: float|int,
|
||||
* unit: string,
|
||||
* units: int
|
||||
* }>
|
||||
* } $attributes The order data:
|
||||
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code)
|
||||
* - shipping_address: Shipping address (first name, last name, ISO 3166-1 alpha-2 country code)
|
||||
* - items: Array of order items (each with type, name, price, unit, units, and quantity)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function createOrder(array $attributes = []): Result
|
||||
{
|
||||
return $this->request('POST', 'v1/orders', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given order from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function order(string $orderId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/orders/%s', $orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update given order from the current user.
|
||||
*
|
||||
* VAT is calculated based on the billing address and shown in the order response.
|
||||
*
|
||||
* The billing and shipping addresses are each persisted as standalone Address entities
|
||||
* in the database, but are also embedded (deep-copied) into the Order object itself
|
||||
* rather than merely referenced. This guarantees that the order retains its own snapshot
|
||||
* of both addresses for future reference.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @param array{
|
||||
* billing_address: array{
|
||||
* company_name: null|string,
|
||||
* first_name: null|string,
|
||||
* last_name: null|string,
|
||||
* address: null|string,
|
||||
* address_2: null|string,
|
||||
* house_number: null|string,
|
||||
* city: null|string,
|
||||
* state: null|string,
|
||||
* postal_code: null|string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string
|
||||
* },
|
||||
* shipping_address: null|array{
|
||||
* company_name: null|string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* address: null|string,
|
||||
* address_2: string,
|
||||
* house_number: null|string,
|
||||
* city: string,
|
||||
* state: string,
|
||||
* postal_code: string,
|
||||
* country_iso: string,
|
||||
* phone_number: null|string,
|
||||
* email: null|string
|
||||
* }
|
||||
* } $attributes The order data:
|
||||
* - billing_address: Billing address (ISO 3166-1 alpha-2 country code)
|
||||
* - shipping_address: Shipping address (first name, last name, ISO 3166-1 alpha-2 country code)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function updateOrder(string $orderId, array $attributes = []): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/orders/%s', $orderId), $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given order from the current user.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function checkoutOrder(string $orderId): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/orders/%s/checkout', $orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke given order from the current user.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function revokeOrder(string $orderId): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/orders/%s/revoke', $orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete given order from the current user.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function deleteOrder(string $orderId): Result
|
||||
{
|
||||
return $this->request('DELETE', sprintf('v1/orders/%s', $orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order items from given order.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function orderItems(string $orderId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/orders/%s/items', $orderId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new order item for given order.
|
||||
*
|
||||
* VAT is calculated based on the billing address and shown in the order item response.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @param array{
|
||||
* items: array<array{
|
||||
* type: string,
|
||||
* name: string,
|
||||
* description: string,
|
||||
* price: float|int,
|
||||
* unit: string,
|
||||
* units: int
|
||||
* }>
|
||||
* } $attributes The order data:
|
||||
* - items: Array of order items, each with type, name, description, price, unit, and quantity
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function createOrderItem(string $orderId, array $attributes = []): Result
|
||||
{
|
||||
return $this->request('POST', sprintf('v1/orders/%s', $orderId), $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given order item from given order.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @param string $orderItemId The order item ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function orderItem(string $orderId, string $orderItemId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/orders/%s/items/%s', $orderId, $orderItemId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update given order item from given order.
|
||||
*
|
||||
* VAT is calculated based on the billing address and shown in the order item response.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @param string $orderItemId The order item ID.
|
||||
* @param array{
|
||||
* items: array<array{
|
||||
* type: string,
|
||||
* name: string,
|
||||
* description: string,
|
||||
* price: float|int,
|
||||
* unit: string,
|
||||
* units: int
|
||||
* }>
|
||||
* } $attributes The order data:
|
||||
* - items: Array of order items, each with type, name, description, price, unit, and quantity
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function updateOrderItem(string $orderId, string $orderItemId, array $attributes = []): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/orders/%s/items/%s', $orderId, $orderItemId), $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete given order item from given order.
|
||||
*
|
||||
* @param string $orderId The order ID.
|
||||
* @param string $orderItemId The order item ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function deleteOrderItem(string $orderId, string $orderItemId): Result
|
||||
{
|
||||
return $this->request('DELETE', sprintf('v1/orders/%s/items/%s', $orderId, $orderItemId));
|
||||
return (new Orders($this->request('GET', 'v1/orders')))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\PaymentMethods;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
@@ -11,59 +12,27 @@ trait ManagesPaymentMethods
|
||||
{
|
||||
use Request;
|
||||
|
||||
/**
|
||||
* Check if current user has at least one payment method.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function hasPaymentMethod(): bool
|
||||
{
|
||||
return $this->paymentMethods()->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get payment methods from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function paymentMethods(): Result
|
||||
public function paymentMethods(): PaymentMethods
|
||||
{
|
||||
return $this->request('GET', 'v1/payment-methods');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
{
|
||||
return (bool)$this->defaultPaymentMethod()->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function defaultPaymentMethod(): Result
|
||||
{
|
||||
return $this->request('GET', 'v1/payment-methods/default');
|
||||
return (new PaymentMethods($this->request('GET', 'v1/payment-methods')))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get billing portal URL for the current user.
|
||||
*
|
||||
* @param string $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.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function billingPortalUrl(string $returnUrl, array $options): string
|
||||
public function billingPortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
{
|
||||
return $this->request('POST', 'v1/billing/portal', [
|
||||
'return_url' => $returnUrl,
|
||||
|
||||
30
src/Id/Concerns/ManagesProfile.php
Normal file
30
src/Id/Concerns/ManagesProfile.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesProfile
|
||||
{
|
||||
use Request;
|
||||
|
||||
/**
|
||||
* Get the profile url for the current user.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function profilePortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
{
|
||||
return $this->request('POST', 'v1/user/profile', [
|
||||
'return_url' => $returnUrl,
|
||||
'options' => $options,
|
||||
])->data->url;
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,14 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Delete;
|
||||
use Anikeen\Id\ApiOperations\Get;
|
||||
use Anikeen\Id\ApiOperations\Post;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Anikeen\Id\Resources\SshKeys;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesSshKeys
|
||||
{
|
||||
use Get, Post, Delete;
|
||||
use Get;
|
||||
|
||||
/**
|
||||
* Get currently authed user with Bearer Token.
|
||||
@@ -20,35 +18,9 @@ trait ManagesSshKeys
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function sshKeysByUserId(string $sskKeyId): Result
|
||||
public function sshKeysByUserId(string $sskKeyId): SshKeys
|
||||
{
|
||||
return $this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates ssh key for the currently authed user.
|
||||
*
|
||||
* @param string $publicKey The public key to be added
|
||||
* @param string|null $name The name of the key (optional)
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function createSshKey(string $publicKey, string $name = null): Result
|
||||
{
|
||||
return $this->post('v1/ssh-keys', [
|
||||
'public_key' => $publicKey,
|
||||
'name' => $name,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a given ssh key for the currently authed user.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function deleteSshKey(int $sshKeyId): Result
|
||||
{
|
||||
return $this->delete(sprintf('v1/ssh-keys/%s', $sshKeyId));
|
||||
return (new SshKeys($this->get(sprintf('v1/users/%s/ssh-keys/json', $sskKeyId))))
|
||||
->setParent($this);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Anikeen\Id\Resources\Subscriptions;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait ManagesSubscriptions
|
||||
@@ -17,99 +17,9 @@ trait ManagesSubscriptions
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function subscriptions(): Result
|
||||
public function subscriptions(): Subscriptions
|
||||
{
|
||||
return $this->request('GET', 'v1/subscriptions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given subscription from the current user.
|
||||
*
|
||||
* @param string $subscriptionId The subscription ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function subscription(string $subscriptionId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/subscriptions/%s', $subscriptionId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new subscription for the current user.
|
||||
*
|
||||
* @param array{
|
||||
* name: null,
|
||||
* description: string,
|
||||
* unit: string,
|
||||
* price: float,
|
||||
* vat: 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
|
||||
* - unit: The unit (e.g. "hour", "day", "week", "month", "year")
|
||||
* - price: The price per unit
|
||||
* - vat: The VAT (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 createSubscription(array $attributes): Result
|
||||
{
|
||||
return $this->request('POST', 'v1/subscriptions', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force given subscription to check out (trusted apps only).
|
||||
*
|
||||
* @param string $subscriptionId The subscription ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function checkoutSubscription(string $subscriptionId): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/subscriptions/%s/checkout', $subscriptionId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a given running subscription from the current user.
|
||||
*
|
||||
* @param string $subscriptionId The subscription ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function revokeSubscription(string $subscriptionId): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/subscriptions/%s/revoke', $subscriptionId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a given running subscription from the current user.
|
||||
*
|
||||
* @param string $subscriptionId The subscription ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function resumeSubscription(string $subscriptionId): Result
|
||||
{
|
||||
return $this->request('PUT', sprintf('v1/subscriptions/%s/resume', $subscriptionId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a given subscription from the current user.
|
||||
*
|
||||
* @param string $subscriptionId The subscription ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function deleteSubscription(string $subscriptionId): Result
|
||||
{
|
||||
return $this->request('DELETE', sprintf('v1/subscriptions/%s', $subscriptionId));
|
||||
return (new Subscriptions($this->request('GET', 'v1/subscriptions')))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Transactions;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
@@ -17,33 +18,9 @@ trait ManagesTransactions
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function transactions(): Result
|
||||
public function transactions(): Transactions
|
||||
{
|
||||
return $this->request('GET', 'v1/transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 createTransaction(array $attributes = []): Result
|
||||
{
|
||||
return $this->request('POST', 'v1/transactions', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get given transaction from current current user.
|
||||
*
|
||||
* @param string $transactionId The transaction ID.
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function transaction(string $transactionId): Result
|
||||
{
|
||||
return $this->request('GET', sprintf('v1/transactions/%s', $transactionId));
|
||||
return (new Transactions($this->request('GET', 'v1/transactions')))
|
||||
->setBillable($this);;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use GuzzleHttp\Exception\GuzzleException;
|
||||
trait ManagesUsers
|
||||
{
|
||||
use Get, Post;
|
||||
use HasParent;
|
||||
|
||||
/**
|
||||
* Get currently authed user with Bearer Token
|
||||
|
||||
Reference in New Issue
Block a user