{"record":{"id":"b9dc961b0d9ca638","repo":"toeverything/AFFiNE","slug":"subscription-has-not-been-canceled","errorCode":"subscription_has_not_been_canceled","errorMessage":"Your subscription has not been canceled.","messagePattern":"Your subscription has not been canceled\\.","errorType":"exception","errorClass":"SubscriptionHasNotBeenCanceled","httpStatus":403,"severity":"warning","filePath":"packages/backend/server/src/plugins/payment/service.ts","lineNumber":253,"sourceCode":"      if (message.includes('payment_customer_not_found')) {\n        throw new UserNotFound();\n      }\n      throw new CustomerPortalCreateFailed();\n    }\n  }\n\n  private async mutate(\n    identity: SubscriptionIdentity,\n    mutation: 'cancel' | 'resume',\n    idempotencyKey?: string\n  ) {\n    const target = subscriptionTarget(identity);\n    const subscription = await this.command<Subscription>({\n      action: 'mutate_subscription',\n      actorUserId: target.actor ?? target.id,\n      targetType: target.type,\n      targetId: target.id,\n      plan: identity.plan,\n      mutation,\n      intentId: idempotencyKey ?? randomUUID(),\n    });\n    return normalizeSubscription(subscription);\n  }\n\n  private async command<T = unknown>(input: Record<string, unknown>) {\n    try {\n      return await this.runtime.executePaymentCommandV1<T>(input);\n    } catch (error) {\n      throw mapPaymentError(error, input);\n    }\n  }\n}\n\nfunction checkoutTarget(\n  plan: SubscriptionPlan,\n  args: z.infer<typeof CheckoutExtraArgs>","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/d897bb3d84099e54a6b3c0bd5f4265f8aa87d190/packages/backend/server/src/plugins/payment/service.ts#L235-L271","documentation":"Thrown by resumeSubscription when subscription.canceledAt is null. Resume is only valid for a subscription that was previously canceled (scheduled to end); calling it on an active subscription is a state-machine violation.","triggerScenarios":"User clicks Resume on an active (never canceled) subscription; retry race where resume already succeeded (canceledAt cleared) and the client re-sends; stale UI still showing a canceled badge after a webhook flipped the row back to active.","commonSituations":"Billing list not refreshed after cancel/resume transitions; optimistic client state out of sync with server; duplicate submissions from double clicks.","solutions":["Only render/enable Resume when canceledAt is set on the subscription row.","After any cancel/resume success or subscription webhook, refresh the subscription list before allowing the next action.","Treat this code on retry as success-equivalent: reload state instead of erroring."],"exampleFix":"// before\n{sub.status === 'active' && <ResumeButton onClick={() => resume(identity)} />}\n\n// after\n{sub.canceledAt && sub.status === 'active' && (\n  <ResumeButton onClick={() => resume(identity)} />\n)}","handlingStrategy":"validation","validationCode":"if (!subscription.canceledAt) {\n  showActiveState(); // nothing to resume\n} else {\n  await svc.resumeSubscription(identity, idempotencyKey);\n}","typeGuard":"function isCanceledSub(s: { canceledAt: Date | string | null }): s is { canceledAt: Date } {\n  return s.canceledAt !== null;\n}","tryCatchPattern":"catch (e) { if (gqlErrorCode(e) === 'subscription_has_not_been_canceled') { await refreshSubscriptions(); return; } throw e; }","preventionTips":["Render Resume only when canceledAt is set.","Refresh subscriptions after every lifecycle mutation and webhook event.","Treat this code on retry as success-equivalent: reload and continue."],"tags":["payment","subscription","resume","state-machine","duplicate-request"],"backgroundTag":"invalid-subscription-state","analyzedSha":"d897bb3d84099e54a6b3c0bd5f4265f8aa87d190","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}