#Stripe のサブスクリプションのスケジュール登録では、複数のフェーズを指定して、連続的なプラン・サブスクリプションを設定できそうだ？

複数のフェーズを指定した場合、一個のフェーズの終わりは、必ず次のフェーズの始まるとなる。つまり複数のサブスクリプションを連続的に実行できるようだ。

例えばプランAのサブスクリプションが1回終わったら、次はプランBのサブスクリプションを2回繰り返し、その次はプランBのサブスクリプションを...など。

`Stripe::SubscriptionSchedule.create` する時は、SubscriptionSchedule本体にstart_dateを指定することができ、それぞれのフェーズにはend_dateを指定できるようだ。確かにこの仕組みで start_date / end_date の境界値が決まるな。

それぞれのフェーズに繰り返し回数= iterationsを指定すれば、自動的にstart_date / end_date も決まるので、それで事足りる。

# 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](https://user-images.githubusercontent.com/13635059/71539533-c40d9a80-2980-11ea-8653-11dd85a8222a.png)

# Dashboard

![image](https://user-images.githubusercontent.com/13635059/71539518-71cc7980-2980-11ea-9043-e3f5a72fc40b.png)
![image](https://user-images.githubusercontent.com/13635059/71539519-72651000-2980-11ea-809f-2570772a92b4.png)


# Ruby Code example

```rb
# 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

https://github.com/YumaInaura/YumaInaura/issues/2889








<!-- Update From Qiita API -->

# チャットメンバー募集


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

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->