0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Stripe API - Subscription Schedule can create complexed interval plans in phase e.g day month week year? Answer is No.

Last updated at Posted at 2020-01-02

ERror

Stripe::InvalidRequestError: For a particular phase, all of its plansmust have the sameinterval, interval_count, and currency.

Ruby

# Docs

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

# Code

require 'stripe'

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

product1 = Stripe::Product.create(name: "Gold monthly 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 weekly plan #{rand(9999999999)}")
plan2 = Stripe::Plan.create(interval: 'week', currency: 'jpy', amount: 3000, product: product2.id, usage_type: 'licensed')

product3 = Stripe::Product.create(name: "Bronse daily plan #{rand(9999999999)}")
plan3 = Stripe::Plan.create(interval: 'day', 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)

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
subscription_schedule = Stripe::SubscriptionSchedule.create(
  {
    customer: customer.id,
    start_date: Time.now.to_i + 5,
    default_settings: {
      default_payment_method: customer_payment_method.id,
    },
    phases: [
      {
        plans:
          [
            { plan: plan1.id, quantity: 1 },
            { plan: plan2.id, quantity: 4 },
          ],
          default_tax_rates: [tax_rate],
          iterations: 3,
      },
      {
        plans:
          [
            { plan: plan2.id, quantity: 1 },
            { plan: plan3.id, quantity: 1 },
          ],
          default_tax_rates: [tax_rate],
          iterations: 5,
      },
      {
        plans:
          [
            { plan: plan3.id, quantity: 3 },
            { plan: plan1.id, quantity: 2 },
          ],
          default_tax_rates: [tax_rate],
          iterations: 7,
      }
    ],
  }
)

# Exception: Stripe::InvalidRequestError: For a particular phase, all of its `plans` must have the same `interval`, `interval_count`, and `currency`.

Japanese

Stripeのスケジュール登録でのフェーズは別々の期間でのプランを含むことはできるか?否。

Original by Github issue

チャットメンバー募集

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

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?