stripe-paymentsstripes

How to start a stripe subscription with the first 2 months' prorated initial payment upfront


I'm building a checkout page where users sign up for a $100/month product. However, when users sign up, they must pay for the first two months upfront, which must be prorated over time. After the first 2 months, we will ask them to pay 100 USD per month. For example:

I've seen about subscription schedules, but I don't know how to configure the phase to meet the above requirements.

I tried with the solution in here. How to implement stripe subscription with three months initial upfront payment But it cannot be calculated based on the first month's time rate.


Solution

  • Step 0 : Create a Test Clock and/or Customer if necessary

    const testClock = await stripe.testHelpers.testClocks.create({
        frozen_time: Math.floor(+new Date() / 1000),
      });
    
      const customer = await stripe.customers.create({
        description: 'test clock customer',
        email : 'example@example.com',
        test_clock: testClock.id,
        payment_method: 'pm_card_amex_threeDSecureNotSupported',
        invoice_settings : {
          default_payment_method : 'pm_card_amex_threeDSecureNotSupported'
        }
      });
    

    Step 1 : Create a Subscription with a 2-month Price, and billing_cycle_anchor set to March 1, 2024

      const subscription = await stripe.subscriptions.create({
        customer: customer.id,
        items: [
          {price:'price_A'} // USD $200 for 2 months
        ],
        payment_behavior : 'allow_incomplete',
        billing_cycle_anchor : 1709251200 // 1 Mar 2024
      }
      );
    
      console.log(subscription);
    

    Step 2 : Upon successful Subscription first payment, you would create a SubscriptionSchedule for the Subscription

      const schedule = await stripe.subscriptionSchedules.create({
        from_subscription: subscription.id,
      });
      
    console.log(schedule);
    

    Step 3 : Update the SubscriptionSchedule to change to a 1-month Price on March 1, 2024

    const subscriptionSchedule = await stripe.subscriptionSchedules.update(
        schedule.id, {
          phases:[
            {
              start_date : schedule.phases[0].start_date,
              end_date : schedule.phases[0].end_date,
              items: [{
                price: 'price_A' // USD $200 for 2 months
              }]
            },
            {
              items: [{
                price: 'price_B' // USD $100 for 1 months
              }]
            }
          ]
        }
        );
        
        console.log(subscriptionSchedule);
    

    Advance the test clock to see how it charges