mirror of
https://github.com/anikeen-com/id.git
synced 2026-11-03 14:19:31 +00:00
refactor code
This commit is contained in:
1 parent
c641ec725d
commit
25248e7822
37 files changed
+180
-143
No files matched your search
@@ -2,18 +2,21 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\Contracts\Billable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
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;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBillable(): mixed
|
||||
public function getBillable(): Billable
|
||||
{
|
||||
return $this->billable;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Addresses;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesAddresses
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get addresses from the current user.
|
||||
@@ -19,7 +17,8 @@ trait ManagesAddresses
|
||||
public function addresses(): Addresses
|
||||
{
|
||||
if (!isset($this->addressesCache)) {
|
||||
$this->addressesCache = Addresses::builder(fn() => $this->request('GET', 'v1/addresses'))
|
||||
$this->addressesCache = Addresses::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/addresses'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
@@ -29,8 +28,8 @@ trait ManagesAddresses
|
||||
/**
|
||||
* Check if the current user has a default billing address.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress()
|
||||
* @throws Throwable
|
||||
* @see \Anikeen\Id\Resources\Addresses::hasDefaultBillingAddress()
|
||||
*/
|
||||
public function hasDefaultBillingAddress(): bool
|
||||
{
|
||||
|
||||
@@ -2,13 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Resources\Transaction;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesBalance
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get balance from the current user.
|
||||
@@ -40,10 +39,12 @@ trait ManagesBalance
|
||||
*/
|
||||
public function charge(float $amount, string $paymentMethodId, array $options = []): Transaction
|
||||
{
|
||||
return new Transaction(fn() => $this->request('POST', 'billing/charge', [
|
||||
'amount' => $amount,
|
||||
'payment_method_id' => $paymentMethodId,
|
||||
'options' => $options,
|
||||
]));
|
||||
return (new Transaction(fn() => $this->anikeenId()
|
||||
->request('POST', 'billing/charge', [
|
||||
'amount' => $amount,
|
||||
'payment_method_id' => $paymentMethodId,
|
||||
'options' => $options,
|
||||
])))
|
||||
->setBillable($this);
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Countries;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesCountries
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get available countries for the current user.
|
||||
@@ -19,7 +17,8 @@ trait ManagesCountries
|
||||
public function countries(): Countries
|
||||
{
|
||||
if (!isset($this->countriesCache)) {
|
||||
$this->countriesCache = Countries::builder(fn() => $this->request('GET', 'v1/countries'))
|
||||
$this->countriesCache = Countries::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/countries'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Invoices;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesInvoices
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get invoices from the current user.
|
||||
@@ -19,7 +17,8 @@ trait ManagesInvoices
|
||||
public function invoices(array $parameters = []): Invoices
|
||||
{
|
||||
if (!isset($this->invoicesCache)) {
|
||||
$this->invoicesCache = Invoices::builder(fn() => $this->request('GET', 'v1/invoices', [], $parameters))
|
||||
$this->invoicesCache = Invoices::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/invoices', [], $parameters))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Orders;
|
||||
use Anikeen\Id\Result;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesOrders
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get orders from the current user.
|
||||
@@ -20,7 +17,8 @@ trait ManagesOrders
|
||||
public function orders(array $parameters = []): Orders
|
||||
{
|
||||
if (!isset($this->ordersCache)) {
|
||||
$this->ordersCache = Orders::builder(fn() => $this->request('GET', 'v1/orders', [], $parameters))
|
||||
$this->ordersCache = Orders::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/orders', [], $parameters))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
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;
|
||||
@@ -11,7 +9,7 @@ use Throwable;
|
||||
|
||||
trait ManagesPaymentMethods
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get payment methods from the current user.
|
||||
@@ -20,10 +18,10 @@ trait ManagesPaymentMethods
|
||||
*/
|
||||
public function paymentMethods(): PaymentMethods
|
||||
{
|
||||
if (!isset($this->paymentMethodsCache)) {;
|
||||
$this->paymentMethodsCache = PaymentMethods::builder(
|
||||
fn() => $this->request('GET', 'v1/payment-methods')
|
||||
)->setBillable($this);
|
||||
if (!isset($this->paymentMethodsCache)) {
|
||||
$this->paymentMethodsCache = PaymentMethods::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/payment-methods'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
return $this->paymentMethodsCache;
|
||||
@@ -32,8 +30,8 @@ trait ManagesPaymentMethods
|
||||
/**
|
||||
* Check if current user has at least one payment method.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod()
|
||||
* @throws Throwable
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasPaymentMethod()
|
||||
*/
|
||||
public function hasPaymentMethod(): ?PaymentMethod
|
||||
{
|
||||
@@ -43,8 +41,8 @@ trait ManagesPaymentMethods
|
||||
/**
|
||||
* Get default payment method from the current user.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
|
||||
* @throws Throwable
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::defaultPaymentMethod()
|
||||
*/
|
||||
public function defaultPaymentMethod(): ?PaymentMethod
|
||||
{
|
||||
@@ -54,8 +52,8 @@ trait ManagesPaymentMethods
|
||||
/**
|
||||
* Check if the current user has a default payment method.
|
||||
*
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod()
|
||||
* @throws Throwable
|
||||
* @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod()
|
||||
*/
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
{
|
||||
@@ -71,10 +69,13 @@ trait ManagesPaymentMethods
|
||||
*/
|
||||
public function billingPortalUrl(?string $returnUrl = null, array $options = []): string
|
||||
{
|
||||
return $this->request('POST', 'v1/billing/portal', [
|
||||
'return_url' => $returnUrl,
|
||||
'options' => $options,
|
||||
])->data->url;
|
||||
return $this->anikeenId()
|
||||
->request('POST', 'v1/billing/portal', [
|
||||
'return_url' => $returnUrl,
|
||||
'options' => $options,
|
||||
])
|
||||
->data
|
||||
->url;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,8 +86,9 @@ trait ManagesPaymentMethods
|
||||
*/
|
||||
public function createSetupIntent(array $options = []): Result
|
||||
{
|
||||
return $this->request('POST', 'v1/payment-methods', [
|
||||
'options' => $options,
|
||||
]);
|
||||
return $this->anikeenId()
|
||||
->request('POST', 'v1/payment-methods', [
|
||||
'options' => $options,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Post;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Throwable;
|
||||
|
||||
|
||||
@@ -2,14 +2,11 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesProfile
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get the profile url for the current user.
|
||||
@@ -21,7 +18,7 @@ trait ManagesProfile
|
||||
*/
|
||||
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,
|
||||
'options' => $options,
|
||||
])->data->url;
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Get;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\SshKeys;
|
||||
use Throwable;
|
||||
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Subscriptions;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesSubscriptions
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get subscriptions from the current user.
|
||||
@@ -19,7 +17,8 @@ trait ManagesSubscriptions
|
||||
public function subscriptions(): Subscriptions
|
||||
{
|
||||
if (!isset($this->subscriptionsCache)) {
|
||||
$this->subscriptionsCache = Subscriptions::builder(fn() => $this->request('GET', 'v1/subscriptions'))
|
||||
$this->subscriptionsCache = Subscriptions::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/subscriptions'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesTaxation
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get VAT for the current user.
|
||||
|
||||
@@ -2,15 +2,12 @@
|
||||
|
||||
namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Request;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Resources\Transactions;
|
||||
use Anikeen\Id\Result;
|
||||
use Throwable;
|
||||
|
||||
trait ManagesTransactions
|
||||
{
|
||||
use Request;
|
||||
use HasBillable;
|
||||
|
||||
/**
|
||||
* Get transactions from the current user.
|
||||
@@ -20,7 +17,8 @@ trait ManagesTransactions
|
||||
public function transactions(): Transactions
|
||||
{
|
||||
if (!isset($this->transactionsCache)) {
|
||||
$this->transactionsCache = Transactions::builder(fn() => $this->request('GET', 'v1/transactions'))
|
||||
$this->transactionsCache = Transactions::builder(fn() => $this->anikeenId()
|
||||
->request('GET', 'v1/transactions'))
|
||||
->setBillable($this);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace Anikeen\Id\Concerns;
|
||||
|
||||
use Anikeen\Id\ApiOperations\Get;
|
||||
use Anikeen\Id\ApiOperations\Post;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use Throwable;
|
||||
|
||||
@@ -52,11 +51,11 @@ trait ManagesUsers
|
||||
public function refreshToken(string $storedRefreshToken, string $scope = ''): Result
|
||||
{
|
||||
return $this->post('../oauth/token', [
|
||||
'grant_type' => 'refresh_token',
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $storedRefreshToken,
|
||||
'client_id' => $this->clientId,
|
||||
'client_id' => $this->clientId,
|
||||
'client_secret' => $this->clientSecret,
|
||||
'scope' => $scope,
|
||||
'scope' => $scope,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user