# Scenario 2: Direct Subscription from Home Page

**Description**: A visitor subscribes to a plan directly from the home page without creating an account first.\
**Details**:

1. **Distinction**: Differentiate between recurring and one-time payments because different stripe events are triggered for each case.

**Case 1: Recurring Payment**

* **Stripe Triggered Event**: `customer.subscription.created`
* **Process**:
  * Generate a `PreClientPlan` table to temporarily store all plan properties (email, subscriptionId, currentPeriodEnd, amount, interval, token, etc.) with a unique token.
  * Send an email to the client containing an account creation link with the token in the format `/auth/sign-up?token=token_code`.
  * Use the stored data to create the actual `ClientPlan`.
  * The client proceeds to create their account.

**Case 2: One-Time Payment**

* **Stripe Triggered Event**: `payment_intent.succeeded`
* **Process**:

  * Handle payments via payment intents (paymentIntentId).
  * If the user was on a recurring plan before purchasing a lifetime plan, cancel their previous subscription using the subscriptionId from the database to stop recurring payments.
  * To prevent the `customer.subscription.updated` event (triggered by cancellation) from affecting the ClientPlan, we add a note in the subscription object's metadata to handle this exception.

  **Cancellation Call**:

  ```jsx
  await stripe.subscriptions.update(subscriptionId, {
      cancel_at_period_end: true,
      metadata: {
          reason: "upgrade-to-lifetime-subscription",
      },
  });
  ```

  **Exception Handling**:

  ```jsx
  if (reason === "upgrade-to-lifetime-subscription") {
      return new Response(null, { status: 200 });
  }
  ```

#### Post-Subscription Tasks

In both scenarios, post-subscription tasks are handled by the `handleAfterSubscriptionTasks()` function.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zakariaes-organization.gitbook.io/saascore-docs/payments/payment-flow-scenarios/scenario-2-direct-subscription-from-home-page.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
