#Stripe API + Ruby / Create SubscriptionSchedule / Come start date soon / subscription was set to SubscriptionSchedule / and Retrieve / example code

When Subscription start_date comes
Then SubscriptionSchedule get (current) subscription in own key

```rb
# Code

require 'stripe'

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

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

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

product3 = Stripe::Product.create(name: "Bronse plan #{rand(9999999999)}")
plan3 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, product: product3.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)

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

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


puts '-' * 100
puts "Wait until subscription schedule starts"
puts '-' * 100
until subscription_schedule.status == 'active' do
  subscription_schedule = Stripe::SubscriptionSchedule.retrieve(subscription_schedule.id)
  puts subscription_schedule.status
  sleep 2
end


started_subscription_schedule = Stripe::SubscriptionSchedule.retrieve(id: subscription_schedule.id, expand: ['subscription'])

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

puts '-' * 100
puts "Subscription created"
puts "https://dashboard.stripe.com/test/subscriptions/#{started_subscription_schedule.subscription.id}"
puts '-' * 100
puts started_subscription_schedule.subscription

```

```
----------------------------------------------------------------------------------------------------
Subscription Schedule created
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1FucgICmti5jpytU1ibrMxf8
----------------------------------------------------------------------------------------------------
{
  "id": "sub_sched_1FucgICmti5jpytU1ibrMxf8",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1577529482,
  "current_phase": null,
  "customer": "cus_GRVhJxKLj7dF5j",
  "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_1FucgGCmti5jpytUyL7Piz9u",
      "default_tax_rates": [
        {
          "id": "txr_1FucgGCmti5jpytURSLjsf2l",
          "object": "tax_rate",
          "active": true,
          "created": 1577529480,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1580207886,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRVh55gTPxavMN",
          "quantity": 1,
          "tax_rates": [

          ]
        },
        {
          "billing_thresholds": null,
          "plan": "plan_GRVhuRxA8uIzFk",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1577529486,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FucgGCmti5jpytUyL7Piz9u",
      "default_tax_rates": [
        {
          "id": "txr_1FucgGCmti5jpytURSLjsf2l",
          "object": "tax_rate",
          "active": true,
          "created": 1577529480,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1585391886,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRVhF4ov5EfqPb",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1580207886,
      "tax_percent": 10.0,
      "trial_end": null
    }
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "revision": "sub_sched_rev_1FucgICmti5jpytUulwVYtL7",
  "status": "not_started",
  "subscription": null
}
----------------------------------------------------------------------------------------------------
Wait until subscription schedule starts
----------------------------------------------------------------------------------------------------
not_started
not_started
not_started
not_started
not_started
not_started
not_started
not_started
active
----------------------------------------------------------------------------------------------------
Subscription Schedule started
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1FucgICmti5jpytU1ibrMxf8
----------------------------------------------------------------------------------------------------
{
  "id": "sub_sched_1FucgICmti5jpytU1ibrMxf8",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1577529482,
  "current_phase": {
    "end_date": 1580207886,
    "start_date": 1577529486
  },
  "customer": "cus_GRVhJxKLj7dF5j",
  "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_1FucgGCmti5jpytUyL7Piz9u",
      "default_tax_rates": [
        {
          "id": "txr_1FucgGCmti5jpytURSLjsf2l",
          "object": "tax_rate",
          "active": true,
          "created": 1577529480,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1580207886,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRVh55gTPxavMN",
          "quantity": 1,
          "tax_rates": [

          ]
        },
        {
          "billing_thresholds": null,
          "plan": "plan_GRVhuRxA8uIzFk",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1577529486,
      "tax_percent": 10.0,
      "trial_end": null
    },
    {
      "application_fee_percent": null,
      "billing_thresholds": null,
      "collection_method": null,
      "coupon": null,
      "default_payment_method": "pm_1FucgGCmti5jpytUyL7Piz9u",
      "default_tax_rates": [
        {
          "id": "txr_1FucgGCmti5jpytURSLjsf2l",
          "object": "tax_rate",
          "active": true,
          "created": 1577529480,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1585391886,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GRVhF4ov5EfqPb",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": true,
      "start_date": 1580207886,
      "tax_percent": 10.0,
      "trial_end": null
    }
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "revision": "sub_sched_rev_1FucgICmti5jpytUulwVYtL7",
  "status": "active",
  "subscription": {
    "id": "sub_GRVin2GeIraL3n",
    "object": "subscription",
    "application_fee_percent": null,
    "billing_cycle_anchor": 1580207886,
    "billing_thresholds": null,
    "cancel_at": 1580207886,
    "cancel_at_period_end": false,
    "canceled_at": 1577529486,
    "collection_method": "charge_automatically",
    "created": 1577529486,
    "current_period_end": 1580207886,
    "current_period_start": 1577529486,
    "customer": "cus_GRVhJxKLj7dF5j",
    "days_until_due": null,
    "default_payment_method": "pm_1FucgGCmti5jpytUyL7Piz9u",
    "default_source": null,
    "default_tax_rates": [
      {
        "id": "txr_1FucgGCmti5jpytURSLjsf2l",
        "object": "tax_rate",
        "active": true,
        "created": 1577529480,
        "description": null,
        "display_name": "Tax Rate",
        "inclusive": false,
        "jurisdiction": null,
        "livemode": false,
        "metadata": {
        },
        "percentage": 10.0
      }
    ],
    "discount": null,
    "ended_at": null,
    "invoice_customer_balance_settings": {
      "consume_applied_balance_on_void": true
    },
    "items": {
      "object": "list",
      "data": [
        {
          "id": "si_GRVimrsBZpBQD3",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1577529500,
          "metadata": {
          },
          "plan": {
            "id": "plan_GRVh55gTPxavMN",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 5000,
            "amount_decimal": "5000",
            "billing_scheme": "per_unit",
            "created": 1577529478,
            "currency": "jpy",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
            },
            "nickname": null,
            "product": "prod_GRVhXVBVH8ZEB3",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "quantity": 1,
          "subscription": "sub_GRVin2GeIraL3n",
          "tax_rates": [

          ]
        },
        {
          "id": "si_GRViPCvm4xEOiF",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1577529500,
          "metadata": {
          },
          "plan": {
            "id": "plan_GRVhuRxA8uIzFk",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 3000,
            "amount_decimal": "3000",
            "billing_scheme": "per_unit",
            "created": 1577529479,
            "currency": "jpy",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
            },
            "nickname": null,
            "product": "prod_GRVhGAosYWnV5g",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "quantity": 1,
          "subscription": "sub_GRVin2GeIraL3n",
          "tax_rates": [

          ]
        }
      ],
      "has_more": false,
      "total_count": 2,
      "url": "/v1/subscription_items?subscription=sub_GRVin2GeIraL3n"
    },
    "latest_invoice": "in_1FucgaCmti5jpytU8QB8bSeR",
    "livemode": false,
    "metadata": {
    },
    "next_pending_invoice_item_invoice": null,
    "pending_invoice_item_interval": null,
    "pending_setup_intent": null,
    "plan": null,
    "quantity": null,
    "schedule": "sub_sched_1FucgICmti5jpytU1ibrMxf8",
    "start_date": 1577529486,
    "status": "active",
    "tax_percent": 10.0,
    "trial_end": null,
    "trial_start": null
  }
}
----------------------------------------------------------------------------------------------------
Subscription created
https://dashboard.stripe.com/test/subscriptions/sub_GRVin2GeIraL3n
----------------------------------------------------------------------------------------------------
{
  "id": "sub_GRVin2GeIraL3n",
  "object": "subscription",
  "application_fee_percent": null,
  "billing_cycle_anchor": 1580207886,
  "billing_thresholds": null,
  "cancel_at": 1580207886,
  "cancel_at_period_end": false,
  "canceled_at": 1577529486,
  "collection_method": "charge_automatically",
  "created": 1577529486,
  "current_period_end": 1580207886,
  "current_period_start": 1577529486,
  "customer": "cus_GRVhJxKLj7dF5j",
  "days_until_due": null,
  "default_payment_method": "pm_1FucgGCmti5jpytUyL7Piz9u",
  "default_source": null,
  "default_tax_rates": [
    {
      "id": "txr_1FucgGCmti5jpytURSLjsf2l",
      "object": "tax_rate",
      "active": true,
      "created": 1577529480,
      "description": null,
      "display_name": "Tax Rate",
      "inclusive": false,
      "jurisdiction": null,
      "livemode": false,
      "metadata": {
      },
      "percentage": 10.0
    }
  ],
  "discount": null,
  "ended_at": null,
  "invoice_customer_balance_settings": {
    "consume_applied_balance_on_void": true
  },
  "items": {
    "object": "list",
    "data": [
      {
        "id": "si_GRVimrsBZpBQD3",
        "object": "subscription_item",
        "billing_thresholds": null,
        "created": 1577529500,
        "metadata": {
        },
        "plan": {
          "id": "plan_GRVh55gTPxavMN",
          "object": "plan",
          "active": true,
          "aggregate_usage": null,
          "amount": 5000,
          "amount_decimal": "5000",
          "billing_scheme": "per_unit",
          "created": 1577529478,
          "currency": "jpy",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "nickname": null,
          "product": "prod_GRVhXVBVH8ZEB3",
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "quantity": 1,
        "subscription": "sub_GRVin2GeIraL3n",
        "tax_rates": [

        ]
      },
      {
        "id": "si_GRViPCvm4xEOiF",
        "object": "subscription_item",
        "billing_thresholds": null,
        "created": 1577529500,
        "metadata": {
        },
        "plan": {
          "id": "plan_GRVhuRxA8uIzFk",
          "object": "plan",
          "active": true,
          "aggregate_usage": null,
          "amount": 3000,
          "amount_decimal": "3000",
          "billing_scheme": "per_unit",
          "created": 1577529479,
          "currency": "jpy",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {
          },
          "nickname": null,
          "product": "prod_GRVhGAosYWnV5g",
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "quantity": 1,
        "subscription": "sub_GRVin2GeIraL3n",
        "tax_rates": [

        ]
      }
    ],
    "has_more": false,
    "total_count": 2,
    "url": "/v1/subscription_items?subscription=sub_GRVin2GeIraL3n"
  },
  "latest_invoice": "in_1FucgaCmti5jpytU8QB8bSeR",
  "livemode": false,
  "metadata": {
  },
  "next_pending_invoice_item_invoice": null,
  "pending_invoice_item_interval": null,
  "pending_setup_intent": null,
  "plan": null,
  "quantity": null,
  "schedule": "sub_sched_1FucgICmti5jpytU1ibrMxf8",
  "start_date": 1577529486,
  "status": "active",
  "tax_percent": 10.0,
  "trial_end": null,
  "trial_start": null
}
```

# Original by Github issue

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








<!-- Update From Qiita API -->

# チャットメンバー募集


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

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





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->