#Stripe API + Ruby / Create subscription schedule and update phases and plans / example code
Stripe API Reference - Update a schedule
https://stripe.com/docs/api/subscription_schedules/update
# 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
}
Original by Github issue
Author And Source
この問題について(#Stripe API + Ruby / Create subscription schedule and update phases and plans / example code), 我々は、より多くの情報をここで見つけました https://qiita.com/YumaInaura/items/d146ee4d68c140113449著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .