users already subscribed should not be able to do so again

This commit is contained in:
Atharva Deosthale
2025-11-21 14:32:31 +05:30
parent 650da7ba94
commit df8c696b86
3 changed files with 23 additions and 2 deletions
@@ -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);
@@ -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'] ?? []);
@@ -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;
}
}