Stripe: charge/bill customer immediately when adding a subscriptionItem to an already existing subscription?

℡╲_俬逩灬. 提交于 2021-01-29 06:58:21

问题


I'm working for an online courses provider. This is the customer flow:

student subscribe to teacherA | this creates a monthly Stripe subscription       | student is billed
student subscribe to teacherB | this adds a subscriptionItem to the subscription | student is not billed

The problem is that, when I create the subscriptionItem, the customer is not immediately billed, and begin access to premium content for free.

From what I red in the doc, creating has much subscription has students subscribes to teachers is a bad design (anyway, they have limited subscription for a single customer to 25).

Then I thought that creating has much subscriptionItems was a good idea, correct me if I'm wrong.


I'm looking for a way to achieve a flow like this:

Subscription price is $5 for every teachers

01/01 | studentA subscribe to teacherA at | billed $5
01/15 | studentA subscribe to teacherB at | billed $2.5 # half of remaining month
02/01 | subscription auto invoice         | billed $10

Have you got a clue on how to achieve that ?


回答1:


Creating the extra subscriptionItems for the additional teachers that the student wants to subscribe to is the correct move. But, as you noticed when you create a subscription item on the subscription the student isn't billed immediately. By default, Stripe creates pending invoice items for the prorated amount of the newly added subscription item (e.g., $2.5 in your example). If you left the proration invoice items alone, they would get bundled into the student's next invoice which would total to $12.5:

 - teacherB $2.5 (proration charges from last month)
 - teacherA $5
 - teacherB $5
 - total next month: $12.5

If you don't want to wait for the next month for the student to get billed, then you can bill the student immediately by creating and paying an invoice right after adding the new subscription item.

In node this would look something like:

  // Add the new subscription item for Teacher B

  await stripe.subscriptionItems.create({
    subscription: 'sub_xyz', // The subscription ID
    price: 'price_teacher_b_price', // The price for teacher B
  });

  // At this point, Stripe would have created pending invoice items.
  // Pending invoice items would by default be included in the next month's
  // invoice, but you can pull them into a new invoice immediately:

  const invoice = await stripe.invoices.create({ 
    customer: 'cus_xyz', // The customer/student
  });

  // At this point, the invoice items have been pulled into a new invoice.
  // To charge the student you need to finalize and pay the invoice. You
  // can do this in one step:

  await stripe.invoices.pay(invoice.id);


来源:https://stackoverflow.com/questions/63783389/stripe-charge-bill-customer-immediately-when-adding-a-subscriptionitem-to-an-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!