LoginSignup
0
0

More than 1 year has passed since last update.

#Stripe Subscription Schedule / what is phases ? / it have multiple plans and cycles / e.g specified iterations / API example with Ruby code

Last updated at Posted at 2019-12-29

Doc

If there are multiple phases, the end_date of one phase will always equal the start_date of the next phase. Note that past phases can be omitted.

image

Dashboard

image
image

Ruby Code example

# Code

require 'stripe'

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

product = Stripe::Product.create(name: "Gold plan #{rand(9999999999)}")
plan1 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')
plan2 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')
plan3 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product.id, usage_type: 'licensed')

tax_rate = Stripe::TaxRate.create(display_name: 'Tax Rate', percentage: 10.0, inclusive: false)
customer = Stripe::Customer.create
payment_method = Stripe::PaymentMethod.create(type: 'card', card: { number: '4242424242424242', exp_year: 2030, exp_month: 01})
customer_payment_method = Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id)

subscription_schedule = Stripe::SubscriptionSchedule.create(
  {
    customer: customer.id,
    start_date: Time.now.to_i + 5,
    phases: [
      {
        plans:
          [
            { plan: plan1.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 1,
      },
      {
        plans:
          [
            { plan: plan2.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 2,
      },
      {
        plans:
          [
            { plan: plan3.id, quantity: 1 },
          ],
          default_payment_method: customer_payment_method.id,
          default_tax_rates: [tax_rate],
          iterations: 3,
      }
    ],
  }
)

# https://stripe.com/docs/api/subscription_schedules/update
# updated_subscription_schedule = Stripe::SubscriptionSchedule.update(
#   subscription_schedule.id,
#   {
#     phases: [
#       {
#         plans:
#           [
#             { plan: plan.id, quantity: 1 },
#           ],
#           default_payment_method: customer_payment_method.id,
#           default_tax_rates: [tax_rate],
#       },
#     ],
#   }
# )

puts '-' * 100
puts "Subscription Schedule created"
puts "https://dashboard.stripe.com/test/subscription_schedules/#{subscription_schedule.id}"
puts '-' * 100
puts subscription_schedule

----------------------------------------------------------------------------------------------------
Subscription Schedule created
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1FuY4mCmti5jpytUGlSvoPZt
----------------------------------------------------------------------------------------------------
{
  "id": "sub_sched_1FuY4mCmti5jpytUGlSvoPZt",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1577511780,
  "current_phase": null,
  "customer": "cus_GRQwFxXeOBODZn",
  "default_settings": {
    "billing_thresholds": null,
    "collection_method": "charge_automatically",
    "default_payment_method": null,
    "default_source": null,
    "invoice_settings": null
  },
  "end_behavior": "release",
  "livemode": false,
  "metadata": {
  },
  "phases": [
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1580190185,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwJ86226UR2S",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1577511785,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1585374185,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwEi0AQiyzqG",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1580190185,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FuY4lCmti5jpytU0RUFZPn6",
      "default_tax_rates": [
        {
          "id": "txr_1FuY4kCmti5jpytUxADMFwul",
          "object": "tax_rate",
          "active": true,
          "created": 1577511778,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1593322985,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRQwsTlfq50PbV",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1585374185,
      "tax_percent": 10.0,
      "trial_end": null
    }
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "revision": "sub_sched_rev_1FuY4mCmti5jpytUdkbMi55j",
  "status": "not_started",
  "subscription": null
}

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0