From df8c696b867f5ef04d2bb51f974db30ffd826965 Mon Sep 17 00:00:00 2001 From: Atharva Deosthale Date: Fri, 21 Nov 2025 14:32:31 +0530 Subject: [PATCH] users already subscribed should not be able to do so again --- .../Payments/Provider/StripeAdapter.php | 2 +- .../Payments/Http/Subscriptions/Create.php | 21 +++++++++++++++++++ .../Payments/Http/Subscriptions/Get.php | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Payments/Provider/StripeAdapter.php b/src/Appwrite/Payments/Provider/StripeAdapter.php index f84ccdf422..95dafb6fcb 100644 --- a/src/Appwrite/Payments/Provider/StripeAdapter.php +++ b/src/Appwrite/Payments/Provider/StripeAdapter.php @@ -30,7 +30,7 @@ class StripeAdapter implements Adapter $account = $this->decodeResponse($accountResponse); $rawDomain = (string) System::getEnv('_APP_DOMAIN', ''); $domain = \trim($rawDomain); - if ($domain === '' || \in_array(\strtolower($domain), ['localhost', '127.0.0.1'], true)) { + if ($domain === '' || \in_array(\strtolower($domain), ['localhost', '127.0.0.1', 'traefik'], true)) { throw new \RuntimeException('Appwrite domain is not configured. Set _APP_DOMAIN to a publicly accessible hostname before configuring Stripe.'); } $domain = (string) \preg_replace('/^\s*https?:\/\//i', '', $domain); diff --git a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Create.php b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Create.php index d5c26407bd..5fb2f70d8d 100644 --- a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Create.php +++ b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Create.php @@ -145,6 +145,27 @@ class Create extends Base return; } + // Check if actor already has an active subscription + $existingSubscriptions = $dbForPlatform->find('payments_subscriptions', [ + Query::equal('projectId', [$project->getId()]), + Query::equal('actorType', [$actorType]), + Query::equal('actorId', [$actorId]), + Query::equal('status', ['active', 'trialing', 'paused']), + Query::limit(1), + ]); + + if (!empty($existingSubscriptions)) { + $existingSubscription = $existingSubscriptions[0]; + if ($existingSubscription instanceof Document && !$existingSubscription->isEmpty()) { + $response->setStatusCode(Response::STATUS_CODE_CONFLICT); + $response->json([ + 'message' => 'Actor already has an active subscription', + 'subscriptionId' => $existingSubscription->getAttribute('subscriptionId') + ]); + return; + } + } + // Get payment provider and create checkout session $payments = (array) $project->getAttribute('payments', []); $providers = (array) ($payments['providers'] ?? []); diff --git a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Get.php b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Get.php index bfaf11cab9..a2edafbc22 100644 --- a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Get.php +++ b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Get.php @@ -153,7 +153,7 @@ class Get extends Base $activeSubscription = null; if ($subscription instanceof Document && !$subscription->isEmpty()) { $status = strtolower((string) $subscription->getAttribute('status', '')); - if ($status == 'active') { + if ($status == 'active' || $status == 'trialing' || $status == 'paused') { $activeSubscription = $subscription; } }