#arya_

1 messages · Page 1 of 1 (latest)

ember karmaBOT
slate silo
#

Yes let me provide you with some links. Give me a moment

#

Do you want the checkout page to be a stripe-hosted page? Or do you want to host the checkout page yourself on your site?

hybrid sentinel
#

I'm not sure which one to do just yet. Could you provide resources for both?

slate silo
#

That link has tabs at the top "Low Code" and "Custom Code"

#

Low code is the stripe-hosted flow

#

Custom code is for creating the checkout within your site with custom code

hybrid sentinel
#

Thanks, i already had a look at these. I was more confused about the flow of implementation. For instance, i found this "If you want to render the Payment Element without first creating a Subscription, see Collect payment details before creating an Intent" in the docs. Which method is best?

#

I understand that the docs give us a lot of options to do certain things, but this is making it harder to decide which methodology to follow and what are the exact steps that need to be followed

slate silo
#

Recommend you create the subscription first

#

That's the most commonly used integration path

#

I'd only recommend doing it otherwise if you have a specific need/reason to do so. Which at your stage in the development process, doesn't seem like you need to do otherwise

hybrid sentinel
#

Thanks, this is what i have in my code. Does this seem like a reasonable approach to follow:

const createSubscription = async (req, res) => {
  try {
    // Create or retrieve customer
    const user = await UserModel.findById(req.user._id);
    let customer;
    if (user.customerId === "") {
      customer = await stripe.customers.create({
        email: user.email,
        description: user.fullName,
      });
      user.customerId = customer.id;
      await user.save();
    } else {
      customer = await stripe.customers.retrieve(user.customerId);
    }

    if (!customer.id) {
      return res.status(400).json({
        message:
          "Encountered error in createSubscription: no customerId found.",
      });
    }

    const subscription = await stripe.subscriptions.create({
      customer: customer.id,
      items: [
        {
          price:
            req.params.planType === "basic"
              ? process.env.STRIPE_BASIC_PRICE_ID
              : process.env.STRIPE_PRO_PRICE_ID,
        },
      ],
      payment_behavior: "default_incomplete",
      payment_settings: { save_default_payment_method: "on_subscription" },
      expand: ["latest_invoice.payment_intent"],
    });

    res.json({
      subscriptionId: subscription.id,
      clientSecret: subscription.latest_invoice.payment_intent.client_secret,
    });
  } catch (err) {
    return res.status(400).json({
      message: `Encountered error in createSubscription: ${err.message}`,
    });
  }
};
#

This function runs when someone hits the /create-subscription/:planType in the backend

slate silo
#

Seems reasonable if you want to go the custom code route

#

Really just recommend following the doc I sent

#

With a first integration, it's a great starting point

#

You can tweak as necessary from there if you need different functionality down the line

hybrid sentinel
#

Got it. What do you recommend that I store in my backend?
Moreover, I am not sure how to go about if a user wants to change their card on file. I want to showcase information such as "How many days in the next renewal" and "Current plan"

slate silo
#

I can't say. It depends on what your integration needs. That's entirely up to you.
Moreover, I am not sure how to go about if a user wants to change their card on file.
You'll need to collect a new payment method via a setupintent or via the customer portal and update the subscription's default payment method.
I want to showcase information such as "How many days in the next renewal" and "Current plan"
Then you'll want to store the price id in your database to display the plan and also the billing cycle anchor date to calculate the days left in the period

#

Recommend playing around with this in test mode

#

You'll get a better feel for how this all works if you dive in in test mode

hybrid sentinel
#

Got it, thank you so much!

slate silo
#

No problem

hybrid sentinel
#

What I thought happens is this:
Someone wants to purchase a plan -> They enter card details -> we create/retrieve customer -> we create a subscription and check if the payment goes through -> webhooks listen for success / failure
However, the doc you shared mentions 'Collect Payment Information' after a subscription is created. So, when should i fire the creation of the subscription when the user wants to purchase a plan (btw the code i shared with you returns a client secret)

slate silo
#

You create the subscription object prior to loading the payment element

#

Read the doc I sent at the top of the thread

#

So once they select the plan, then create the subscription object

hybrid sentinel
#

For instance, why are we storing the subscription id