diff --git a/src/Appwrite/Payments/Provider/StripeAdapter.php b/src/Appwrite/Payments/Provider/StripeAdapter.php index 64073d8bc0..971968ded4 100644 --- a/src/Appwrite/Payments/Provider/StripeAdapter.php +++ b/src/Appwrite/Payments/Provider/StripeAdapter.php @@ -441,7 +441,23 @@ class StripeAdapter implements Adapter public function cancelSubscription(ProviderSubscriptionRef $subscription, bool $atPeriodEnd, ProviderState $state): ProviderSubscriptionRef { $apiKey = (string) ($state->config['secretKey'] ?? ''); - $this->request($apiKey, 'DELETE', '/subscriptions/' . $subscription->externalSubscriptionId, [ 'cancel_at_period_end' => $atPeriodEnd ? 'true' : 'false' ]); + $subscriptionId = $subscription->externalSubscriptionId; + + try { + if ($atPeriodEnd) { + // Schedule cancellation at period end using POST + $params = ['cancel_at_period_end' => 'true']; + $response = $this->request($apiKey, 'POST', '/subscriptions/' . $subscriptionId, $params); + } else { + // Immediately cancel using DELETE + $response = $this->request($apiKey, 'DELETE', '/subscriptions/' . $subscriptionId); + } + + $responseData = $this->decodeResponse($response); + } catch (\Throwable $e) { + throw $e; + } + return $subscription; } diff --git a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Cancel.php b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Cancel.php index 8966786382..f981803ce5 100644 --- a/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Cancel.php +++ b/src/Appwrite/Platform/Modules/Payments/Http/Subscriptions/Cancel.php @@ -13,6 +13,7 @@ use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; +use Utopia\Validator\Boolean; use Utopia\Validator\Text; class Cancel extends Base @@ -45,6 +46,7 @@ class Cancel extends Base responses: [] )) ->param('subscriptionId', '', new Text(128), 'Subscription ID') + ->param('endAtPeriodEnd', true, new Boolean(true), 'End at period end', true) ->inject('response') ->inject('dbForPlatform') ->inject('dbForProject') @@ -56,6 +58,7 @@ class Cancel extends Base public function action( string $subscriptionId, + bool $endAtPeriodEnd, Response $response, Database $dbForPlatform, Database $dbForProject, @@ -116,15 +119,21 @@ class Cancel extends Base if ($primary) { $config = (array) ($providers[$primary] ?? []); $provMap = (array) $sub->getAttribute('providers', []); - $subscriptionRef = (string) ((array) ($provMap[(string) $primary] ?? []))['subscriptionId'] ?? ''; + $subscriptionRef = (string) ((array) ($provMap[(string) $primary] ?? []))['providerSubscriptionId'] ?? ''; if ($subscriptionRef !== '') { $state = new ProviderState((string) $primary, $config, (array) ($config['state'] ?? [])); $adapter = $registryPayments->get((string) $primary, $config, $project, $dbForPlatform, $dbForProject); - $adapter->cancelSubscription(new \Appwrite\Payments\Provider\ProviderSubscriptionRef($subscriptionRef), true, $state); + $adapter->cancelSubscription(new \Appwrite\Payments\Provider\ProviderSubscriptionRef($subscriptionRef), $endAtPeriodEnd, $state); } } - $sub->setAttribute('status', 'canceled'); - $sub->setAttribute('canceledAt', date('c')); + if ($endAtPeriodEnd) { + $sub->setAttribute('cancelAtPeriodEnd', true); + $sub->setAttribute('canceledAt', null); + } else { + $sub->setAttribute('status', 'canceled'); + $sub->setAttribute('cancelAtPeriodEnd', false); + $sub->setAttribute('canceledAt', date('c')); + } $dbForProject->updateDocument('payments_subscriptions', $sub->getId(), $sub); $response->noContent(); } diff --git a/src/Appwrite/Platform/Modules/Payments/Http/Webhooks/Provider/Create.php b/src/Appwrite/Platform/Modules/Payments/Http/Webhooks/Provider/Create.php index b48b5c1f15..10a9ff789a 100644 --- a/src/Appwrite/Platform/Modules/Payments/Http/Webhooks/Provider/Create.php +++ b/src/Appwrite/Platform/Modules/Payments/Http/Webhooks/Provider/Create.php @@ -7,9 +7,9 @@ use Appwrite\Payments\Provider\Registry; use Appwrite\Platform\Modules\Compute\Base; use Appwrite\SDK\Method; use Appwrite\Utopia\Response; +use Utopia\Database\Database; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; -use Utopia\Database\Database; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Swoole\Request; @@ -63,8 +63,7 @@ class Create extends Base Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents - ) - { + ) { $project = Authorization::skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); if ($project === null || $project->isEmpty()) {