🖥

#Stripe API + Ruby / Create SubscriptionSchedule start_date now exampl

2020/01/08に公開

Code

# Docs

# https://stripe.com/docs/billing/subscriptions/subscription-schedules
# https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases
#
# https://stripe.com/docs/api/subscription_schedules/release
# https://support.stripe.com/questions/create-update-and-schedule-subscriptions

require 'active_support/core_ext'
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')

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)

def put_subscription_schedule(subscription_schedule, message)
  puts '-' * 100
  puts "Subscription Schedule"
  puts message
  puts '-' * 100
  puts subscription_schedule
  puts '-' * 100
  puts "https://dashboard.stripe.com/test/subscription_schedules/#{subscription_schedule.id}"
end

# https://stripe.com/docs/api/subscription_schedules/create
now_start_subscription_schedule = Stripe::SubscriptionSchedule.create(
  {
    customer: customer.id,
    start_date: 'now',
    default_settings: {
      default_payment_method: customer_payment_method.id,
    },
    phases: [
      {
        plans:
          [
            { plan: plan1.id, quantity: 1 },
          ],
        default_tax_rates: [tax_rate],
      },
    ],
  }
)

put_subscription_schedule(now_start_subscription_schedule, 'NOW')


Result

$ STRIPE_SECRET_KEY=sk_test_xxxxx ruby ~/y/stripe/subscription-schedule-now.rb
----------------------------------------------------------------------------------------------------
Subscription Schedule
NOW
----------------------------------------------------------------------------------------------------
{
  "id": "sub_sched_1Fy5yICmti5jpytUKB0ZVtrL",
  "object": "subscription_schedule",
  "canceled_at": null,
  "completed_at": null,
  "created": 1578357058,
  "current_phase": {
    "end_date": 1581035458,
    "start_date": 1578357058
  },
  "customer": "cus_GV6Ak0kkCZyC9L",
  "default_settings": {
    "billing_thresholds": null,
    "collection_method": "charge_automatically",
    "default_payment_method": "pm_1Fy5yHCmti5jpytUCAfqUOhC",
    "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": null,
      "default_tax_rates": [
        {
          "id": "txr_1Fy5yHCmti5jpytUgGI8vANU",
          "object": "tax_rate",
          "active": true,
          "created": 1578357057,
          "description": null,
          "display_name": "Tax Rate",
          "inclusive": false,
          "jurisdiction": null,
          "livemode": false,
          "metadata": {
          },
          "percentage": 10.0
        }
      ],
      "end_date": 1581035458,
      "invoice_settings": null,
      "plans": [
        {
          "billing_thresholds": null,
          "plan": "plan_GV6AiMqrwk1dIB",
          "quantity": 1,
          "tax_rates": [

          ]
        }
      ],
      "prorate": false,
      "start_date": 1578357058,
      "tax_percent": 10.0,
      "trial_end": null
    }
  ],
  "released_at": null,
  "released_subscription": null,
  "renewal_interval": null,
  "status": "active",
  "subscription": "sub_GV6ACjrZgcEftP"
}
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1Fy5yICmti5jpytUKB0ZVtrL

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2020-01-08

Discussion