Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

stripe subscription pay now and the first of each month

I'm attempting to create subscriptions through the Stripe API. I already create the products and items and now need to submit the subscription for the user, but I need to charge the customer for the full price now - no matter which day of the month it is - then charge at the beginning of each month - even if it starts tomorrow.

It looks like I could create a one-off item to charge for now then set up a subscription for the monthly billing cycle, but I'm wondering if I can do it all in one call with the subscription => create function. I do not want to prorate the first month and I cannot see a way to tell it to charge the full price now and set up recurring on the first of each month following. Is there a way to do this?

question from:https://stackoverflow.com/questions/66068526/stripe-subscription-pay-now-and-the-first-of-each-month

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to go about the flow you're describing is to combine the backdate_start_date and billing_cycle_anchor properties. The idea is that when creating the subscription you would set the billing_cycle_anchor to the first of the next month, and you would set the backdate_start_date to the first of the current month. For example, say you wanted to sign up a user for a $10.00 subscription that starts today (February 5th), but you want to bill them for the full $10.00 right away (even though they missed the first 5 days). Then, you want to bill them $10.00 again on March 1st, and the first of every month thereafter. When creating the subscription you would set:

  • billing_cycle_anchor: 1614556800 (March 1st)
  • backdate_start_date: 1612137600 (February 1st)

Which would result in a $10.00 invoice today, and a $10.00 invoice again on the first of March, and subsequent $10.00 invoices on the first of every month going forward.

Here's what this would look like in Node:

(async () => {
  const product = await stripe.products.create({ name: "t-shirt" });

  const customer = await stripe.customers.create({
    name: "Jenny Rosen",
    email: "[email protected]",
    payment_method: "pm_card_visa",
    invoice_settings: {
      default_payment_method: "pm_card_visa",
    },
  });

  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [
      {
        quantity: 1,
        price_data: {
          unit_amount: 1000,
          currency: "usd",
          recurring: {
            interval: "month",
          },
          product: product.id,
        },
      },
    ],
    backdate_start_date: 1612137600,
    billing_cycle_anchor: 1614556800,
    expand: ["latest_invoice"],
  });

  console.log(subscription);
})();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...