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 3 years have passed since last update.

Stripe API example / Update monthly plan Subscription and set cancel_at / Ruby code

0
Last updated at Posted at 2020-01-03

Code

#! /usr/bin/env ruby

require 'stripe'

# You Need
# `$ gem install activesupport` befure run ruby script
require 'active_support/core_ext'

# Docs
# https://stripe.com/docs/api/subscriptions/create
# https://stripe.com/docs/billing/subscriptions/billing-cycle#prorations


Stripe::api_key = ENV['STRIPE_SECRET_KEY']

def create_subscription_and_cancel(cancel_at_natural_cycle_end:, prorate:)
  product1 = Stripe::Product.create(name: "Gold plan #{rand(9999999999)}")
  plan1 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 980, 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)

  subscription = Stripe::Subscription.create(
    {
      customer: customer.id,
      default_payment_method: customer_payment_method.id,
      items: [
        [
          { plan: plan1.id },
        ],
      ],
      prorate: prorate,
      default_tax_rates: [tax_rate],
    }
  )

  cancel_at = if cancel_at_natural_cycle_end
                Time.at(subscription.current_period_start).since(1.month).to_i
              else
                Time.at(subscription.current_period_start).since(1.month).ago(1.day).to_i
              end

  updated_subscription = Stripe::Subscription.update( subscription.id, cancel_at: cancel_at )

  puts '-' * 100
  puts "SUBSCRIPTION"
  puts '-' * 100
  puts "prorate: #{prorate}"
  puts "cancel_at: #{updated_subscription.cancel_at} ( #{Time.at(updated_subscription.cancel_at)} ) "
  if ENV['VERBOSE']
    puts '-' * 100
    puts updated_subscription
  end

  puts '-' * 100
  puts 'UPCOMING INVOICE'
  puts '-' * 100

  begin
    upcoming_invoice = Stripe::Invoice.upcoming(customer: updated_subscription.customer)
    puts "subotal: #{upcoming_invoice.subtotal}"
    puts "tax: #{upcoming_invoice.tax}"
    puts "total: #{upcoming_invoice.total}"
  rescue Stripe::InvalidRequestError => e
    puts e.message
  end

  if ENV['VERBOSE']
    puts '-' * 100
    puts upcoming_invoice
  end

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

  updated_subscription
end

create_subscription_and_cancel(cancel_at_natural_cycle_end: true, prorate: true)
create_subscription_and_cancel(cancel_at_natural_cycle_end: false, prorate: true)

create_subscription_and_cancel(cancel_at_natural_cycle_end: true, prorate: false)
create_subscription_and_cancel(cancel_at_natural_cycle_end: false, prorate: false)


Result

----------------------------------------------------------------------------------------------------
SUBSCRIPTION
----------------------------------------------------------------------------------------------------
prorate: true
cancel_at: 1580610835 ( 2020-02-02 11:33:55 +0900 )
----------------------------------------------------------------------------------------------------
UPCOMING INVOICE
----------------------------------------------------------------------------------------------------
No upcoming invoices for customer: cus_GTG1yiBZ6EXEEh
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GTG1xUoFpeHjYG
----------------------------------------------------------------------------------------------------
SUBSCRIPTION
----------------------------------------------------------------------------------------------------
prorate: true
cancel_at: 1580524440 ( 2020-02-01 11:34:00 +0900 )
----------------------------------------------------------------------------------------------------
UPCOMING INVOICE
----------------------------------------------------------------------------------------------------
subotal: -32
tax: -3
total: -35
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GTG1TVlYdpyHgl
----------------------------------------------------------------------------------------------------
SUBSCRIPTION
----------------------------------------------------------------------------------------------------
prorate: false
cancel_at: 1580610845 ( 2020-02-02 11:34:05 +0900 )
----------------------------------------------------------------------------------------------------
UPCOMING INVOICE
----------------------------------------------------------------------------------------------------
No upcoming invoices for customer: cus_GTG1UAKVPjh6uR
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GTG1ZiZpe6qBoW
----------------------------------------------------------------------------------------------------
SUBSCRIPTION
----------------------------------------------------------------------------------------------------
prorate: false
cancel_at: 1580524452 ( 2020-02-01 11:34:12 +0900 )
----------------------------------------------------------------------------------------------------
UPCOMING INVOICE
----------------------------------------------------------------------------------------------------
subotal: -32
tax: -3
total: -35
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GTG1KIKsD5Q2qb

A

image

B

image

C

image

D

image

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?