add functionality to cancel immediately rather than period end

This commit is contained in:
Atharva Deosthale
2025-11-21 20:12:20 +05:30
parent 7cde35d7a3
commit d03d9e5795
3 changed files with 32 additions and 8 deletions
@@ -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;
}
@@ -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();
}
@@ -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()) {