From a7ec7aa90811f4f0093a9bee3aaf383231579573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurice=20Preu=C3=9F?= Date: Sun, 17 May 2026 16:46:06 +0200 Subject: [PATCH] add set default payment method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurice Preuß --- src/Id/Concerns/ManagesPaymentMethods.php | 29 +++++++++++----- src/Id/Resources/PaymentMethods.php | 40 +++++++++++++++-------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/Id/Concerns/ManagesPaymentMethods.php b/src/Id/Concerns/ManagesPaymentMethods.php index a083aa6..9f89761 100644 --- a/src/Id/Concerns/ManagesPaymentMethods.php +++ b/src/Id/Concerns/ManagesPaymentMethods.php @@ -55,14 +55,27 @@ trait ManagesPaymentMethods * @throws Throwable * @see \Anikeen\Id\Resources\PaymentMethods::hasDefaultPaymentMethod() */ - public function hasDefaultPaymentMethod(): bool - { - return $this->paymentMethods()->hasDefaultPaymentMethod(); - } - - /** - * Get billing portal URL for the current user. - * + public function hasDefaultPaymentMethod(): bool + { + return $this->paymentMethods()->hasDefaultPaymentMethod(); + } + + /** + * Set the default payment method for the current user. + * + * @throws Throwable + * @see \Anikeen\Id\Resources\PaymentMethods::setDefault() + */ + public function setDefaultPaymentMethod(string $id): PaymentMethod + { + unset($this->paymentMethodsCache); + + return $this->paymentMethods()->setDefault($id); + } + + /** + * Get billing portal URL for the current user. + * * @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 Throwable diff --git a/src/Id/Resources/PaymentMethods.php b/src/Id/Resources/PaymentMethods.php index 34d0ba8..8710fa5 100644 --- a/src/Id/Resources/PaymentMethods.php +++ b/src/Id/Resources/PaymentMethods.php @@ -34,20 +34,34 @@ class PaymentMethods extends BaseCollection * * @throws Throwable */ - public function defaultPaymentMethod(): PaymentMethod - { - if (!isset($this->defaultPaymentMethodCache)) { - $this->defaultPaymentMethodCache = (new PaymentMethod(fn() => $this->billable->anikeenId() - ->request('GET', 'v1/payment-methods/default'))) + public function defaultPaymentMethod(): PaymentMethod + { + if (!isset($this->defaultPaymentMethodCache)) { + $this->defaultPaymentMethodCache = (new PaymentMethod(fn() => $this->billable->anikeenId() + ->request('GET', 'v1/payment-methods/default'))) ->setBillable($this->billable); } - - return $this->defaultPaymentMethodCache; - } - - /** - * {@inheritDoc} - * + + return $this->defaultPaymentMethodCache; + } + + /** + * Set the default payment method for the current user. + * + * @throws Throwable + */ + public function setDefault(string $id): PaymentMethod + { + unset($this->defaultPaymentMethodCache); + + return (new PaymentMethod(fn() => $this->billable->anikeenId() + ->request('PUT', sprintf('v1/payment-methods/%s/default', $id)))) + ->setBillable($this->billable); + } + + /** + * {@inheritDoc} + * * @throws Throwable */ public function find(string $id): ?PaymentMethod @@ -56,4 +70,4 @@ class PaymentMethods extends BaseCollection ->request('GET', sprintf('v1/payment-methods/%s', $id)))) ->setBillable($this->billable); } -} \ No newline at end of file +}