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:

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

    Exception Handling:

    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.

Last updated