🖥
#Stripe API + Ruby で サブスリプション=定期支払いのスケジュール登録をしてフェーズ・プラン更新する例
- フェーズの中身を丸ごと更新する必要があり、一部更新のようなことは出来なさそう? 一個のフェーズの期間が変われば他のフェーズの期間も必然的に変わるため、自動的に期間をずらすようなことはしてくれないといことだろうか。ちなみにStripeダッシュボードだとフェーズ単位での調整ができるように見える。
- 更新の時の start_date は作成時とは違い、SubscriptionSchedule 本体ではなく、フェーズの一個に指定する必要がありそうだ。
- 既に SubscriptionSchedule は顧客に紐づいているため、顧客idを指定する必要はない。
Stripe API Reference - Update a schedule
# 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 + 60*60,
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,
}
],
}
)
puts '-' * 100
puts "Subscription Schedule created"
puts "https://dashboard.stripe.com/test/subscription_schedules/#{subscription_schedule.id}"
puts '-' * 100
puts subscription_schedule
# https://stripe.com/docs/api/subscription_schedules/update
# no customer id because already knows customer
# no start_date because already set start_date when created
#
# If no phases has start_date
# Stripe::InvalidRequestError: The subscription schedule update is missing at least one phase with a `start_date` to anchor end dates to.
# REMOVE plan1
# ADD again plan 2
# ADD new plan3
updated_subscription_schedule = Stripe::SubscriptionSchedule.update(
subscription_schedule.id,
{
phases: [
{
plans:
[
{ plan: plan2.id, quantity: 1 },
],
default_payment_method: customer_payment_method.id,
default_tax_rates: [tax_rate],
iterations: 3,
start_date: Time.now.to_i + 60*60,
},
{
plans:
[
{ plan: plan3.id, quantity: 1 },
],
default_payment_method: customer_payment_method.id,
default_tax_rates: [tax_rate],
iterations: 4,
}
]
}
)
puts '-' * 100
puts "Subscription Schedule updated"
puts "https://dashboard.stripe.com/test/subscription_schedules/#{updated_subscription_schedule.id}"
puts '-' * 100
puts updated_subscription_schedule
----------------------------------------------------------------------------------------------------
Subscription Schedule created
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1Fuc1DCmti5jpytUKi62lAzm
----------------------------------------------------------------------------------------------------
{
"id": "sub_sched_1Fuc1DCmti5jpytUKi62lAzm",
"object": "subscription_schedule",
"canceled_at": null,
"completed_at": null,
"created": 1577526935,
"current_phase": null,
"customer": "cus_GRV15kbeph5URv",
"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_1Fuc1CCmti5jpytUVOXozqqT",
"default_tax_rates": [
{
"id": "txr_1Fuc1BCmti5jpytUJhjPKOkr",
"object": "tax_rate",
"active": true,
"created": 1577526933,
"description": null,
"display_name": "Tax Rate",
"inclusive": false,
"jurisdiction": null,
"livemode": false,
"metadata": {
},
"percentage": 10.0
}
],
"end_date": 1580208934,
"invoice_settings": null,
"plans": [
{
"billing_thresholds": null,
"plan": "plan_GRV1VgD9k7xx7K",
"quantity": 1,
"tax_rates": [
]
}
],
"prorate": true,
"start_date": 1577530534,
"tax_percent": 10.0,
"trial_end": null
},
{
"application_fee_percent": null,
"billing_thresholds": null,
"collection_method": null,
"coupon": null,
"default_payment_method": "pm_1Fuc1CCmti5jpytUVOXozqqT",
"default_tax_rates": [
{
"id": "txr_1Fuc1BCmti5jpytUJhjPKOkr",
"object": "tax_rate",
"active": true,
"created": 1577526933,
"description": null,
"display_name": "Tax Rate",
"inclusive": false,
"jurisdiction": null,
"livemode": false,
"metadata": {
},
"percentage": 10.0
}
],
"end_date": 1585392934,
"invoice_settings": null,
"plans": [
{
"billing_thresholds": null,
"plan": "plan_GRV1MyHhrpvp0e",
"quantity": 1,
"tax_rates": [
]
}
],
"prorate": true,
"start_date": 1580208934,
"tax_percent": 10.0,
"trial_end": null
}
],
"released_at": null,
"released_subscription": null,
"renewal_interval": null,
"revision": "sub_sched_rev_1Fuc1DCmti5jpytU3JXMhiLq",
"status": "not_started",
"subscription": null
}
----------------------------------------------------------------------------------------------------
Subscription Schedule updated
https://dashboard.stripe.com/test/subscription_schedules/sub_sched_1Fuc1DCmti5jpytUKi62lAzm
----------------------------------------------------------------------------------------------------
{
"id": "sub_sched_1Fuc1DCmti5jpytUKi62lAzm",
"object": "subscription_schedule",
"canceled_at": null,
"completed_at": null,
"created": 1577526935,
"current_phase": null,
"customer": "cus_GRV15kbeph5URv",
"default_settings": {
"billing_thresholds": null,
"collection_method": "charge_automatically",
"default_payment_method": null,
"default_source": null,
"invoice_settings": {
"days_until_due": null
}
},
"end_behavior": "release",
"livemode": false,
"metadata": {
},
"phases": [
{
"application_fee_percent": null,
"billing_thresholds": null,
"collection_method": null,
"coupon": null,
"default_payment_method": "pm_1Fuc1CCmti5jpytUVOXozqqT",
"default_tax_rates": [
{
"id": "txr_1Fuc1BCmti5jpytUJhjPKOkr",
"object": "tax_rate",
"active": true,
"created": 1577526933,
"description": null,
"display_name": "Tax Rate",
"inclusive": false,
"jurisdiction": null,
"livemode": false,
"metadata": {
},
"percentage": 10.0
}
],
"end_date": 1585392935,
"invoice_settings": null,
"plans": [
{
"billing_thresholds": null,
"plan": "plan_GRV1MyHhrpvp0e",
"quantity": 1,
"tax_rates": [
]
}
],
"prorate": true,
"start_date": 1577530535,
"tax_percent": 10.0,
"trial_end": null
},
{
"application_fee_percent": null,
"billing_thresholds": null,
"collection_method": null,
"coupon": null,
"default_payment_method": "pm_1Fuc1CCmti5jpytUVOXozqqT",
"default_tax_rates": [
{
"id": "txr_1Fuc1BCmti5jpytUJhjPKOkr",
"object": "tax_rate",
"active": true,
"created": 1577526933,
"description": null,
"display_name": "Tax Rate",
"inclusive": false,
"jurisdiction": null,
"livemode": false,
"metadata": {
},
"percentage": 10.0
}
],
"end_date": 1595933735,
"invoice_settings": null,
"plans": [
{
"billing_thresholds": null,
"plan": "plan_GRV1rFG3m02W73",
"quantity": 1,
"tax_rates": [
]
}
],
"prorate": true,
"start_date": 1585392935,
"tax_percent": 10.0,
"trial_end": null
}
],
"released_at": null,
"released_subscription": null,
"renewal_interval": null,
"revision": "sub_sched_rev_1Fuc1DCmti5jpytU1FOdmZZ8",
"status": "not_started",
"subscription": null
}
参考
日本正式リリースしたStripeを使ってサブスクリプション型決済システムを実装する - Qiita
Stripe Billing 101 - Qiita
Stripe初心者のための基本的な使い方(Rails編) - Qiita
Original by Github issue
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
公開日時
2019-12-29
Discussion