mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-14 14:16:31 +00:00
update resources
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
This commit is contained in:
@@ -3,65 +3,53 @@
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Exceptions\RequestRequiresClientIdException;
|
||||
use Anikeen\Id\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Throwable;
|
||||
|
||||
class PaymentMethods extends BaseCollection
|
||||
{
|
||||
use HasBillable;
|
||||
|
||||
private ?PaymentMethod $cachedDefaultPaymentMethod = null;
|
||||
|
||||
/**
|
||||
* Check if current user has at least one payment method.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasPaymentMethod(): bool
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function hasDefaultPaymentMethod(): bool
|
||||
{
|
||||
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->request('GET', 'v1/payment-methods/default')))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
|
||||
return $this->defaultPaymentMethodCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function find(string $id): ?PaymentMethod
|
||||
{
|
||||
$result = $this->billable->request('GET', sprintf('v1/payment-methods/%s', $id));
|
||||
|
||||
return $result->success()
|
||||
? (new PaymentMethod($result))
|
||||
->setBillable($this->billable)
|
||||
: null;
|
||||
return (new PaymentMethod(fn() => $this->billable->request('GET', sprintf('v1/payment-methods/%s', $id))))
|
||||
->setBillable($this->billable);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user