#Stripe API / Create Subscription with cancel_at and prorate true or false / Ruby code example

# Doc

>cancel_at
>A timestamp at which the subscription should cancel. If set to a date before the current period ends this will cause a proration if prorate=true.

https://stripe.com/docs/api/subscriptions/create#create_subscription-cancel_at

# Points

- No proration occurs in latest invoice when specify cancel_at is in Subscription current_period and prorate false
- Proration occurs in latest invoice when cancel_at is in current_period and prorate true
- Proration occurs in Upcoming invoice even if you specify prorate true of false when cancel at is in future subscription cyclle

# Code

```rb
#! /usr/bin/env ruby

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

require 'stripe'

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

Stripe::api_key = ENV['STRIPE_SECRET_KEY']

[nil, 10.days, 20.days, 30.days, 40.days, 50.days, 60.days, 70.days].each do |cancel_at|
  [true, false].each do |prorate|
    product1 = Stripe::Product.create(name: "Gold plan #{rand(9999999999)}")
    plan1 = Stripe::Plan.create(interval: 'month', currency: 'jpy', amount: 1000, 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: (cancel_at ? Time.current.since(cancel_at).to_i : nil)
      }
    )


    puts '=' * 100
    if ENV['VERBOSE']
      puts '-' * 100
      puts subscription
    end

    puts "cancel_at: #{cancel_at}"
    puts "prorate: #{prorate}"


    customer_subscriptions_latest_invoice_id = Stripe::Customer.retrieve(customer.id).subscriptions.data[0].latest_invoice
    if customer_subscriptions_latest_invoice_id
      latest_subscription_invoice = Stripe::Invoice.retrieve(customer_subscriptions_latest_invoice_id)

      puts '-' * 100
      puts 'Invoice'
      puts "id: #{latest_subscription_invoice.id}"
      puts "subtotal: #{latest_subscription_invoice.subtotal}"
      puts "tax: #{latest_subscription_invoice.tax}"
      puts "total: #{latest_subscription_invoice.total}"
    else
      puts '-' * 100
      puts 'Invoice'
      puts 'None'
    end

    begin
      upcoming_invoice = Stripe::Invoice.upcoming(customer: subscription.customer)
      puts '-' * 100
      puts 'Upcoming Invoice'
      puts "subtotal: #{upcoming_invoice.subtotal}"
      puts "tax: #{upcoming_invoice.tax}"
      puts "total: #{upcoming_invoice.total}"
    rescue Stripe::InvalidRequestError => e
      puts e.message
    end

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

```

# Result

```
====================================================================================================
cancel_at:
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbOCmti5jpytU3hVm55IE
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnB8WtnrDFtVX
====================================================================================================
cancel_at:
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbTCmti5jpytULNSmXFc9
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnBo9RzjA22Nb
====================================================================================================
cancel_at: 864000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbZCmti5jpytUESnDahSg
subtotal: 323
tax: 32
total: 355
No upcoming invoices for customer: cus_GUnBBnKU6pw4wz
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnBjMu1tDGsGa
====================================================================================================
cancel_at: 864000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
None
No upcoming invoices for customer: cus_GUnCfYshs4pPB8
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnC1cljusiie8
====================================================================================================
cancel_at: 1728000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbhCmti5jpytUNCs2cRvN
subtotal: 645
tax: 65
total: 710
No upcoming invoices for customer: cus_GUnCd8lLJL1w7l
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCmDidPkqtAw
====================================================================================================
cancel_at: 1728000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
None
No upcoming invoices for customer: cus_GUnCWFVW1D0K5q
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCZhDoXIgC0i
====================================================================================================
cancel_at: 2592000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbrCmti5jpytUIuDCLOYx
subtotal: 968
tax: 97
total: 1065
No upcoming invoices for customer: cus_GUnClh0SHnZWDj
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnC9uLpjzUFhH
====================================================================================================
cancel_at: 2592000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
None
No upcoming invoices for customer: cus_GUnCx0e1bgkWJw
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCyDejr0jrOT
====================================================================================================
cancel_at: 3456000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxnbzCmti5jpytU2quLPOib
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 290
tax: 29
total: 319
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCXfazEE7zIO
====================================================================================================
cancel_at: 3456000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
id: in_1Fxnc5Cmti5jpytUpbmNCP5p
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 290
tax: 29
total: 319
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCrNDom5J58w
====================================================================================================
cancel_at: 4320000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1Fxnc9Cmti5jpytULvRPMohz
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 613
tax: 61
total: 674
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCoQwwXB4Wur
====================================================================================================
cancel_at: 4320000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxncECmti5jpytUmgKRmlr5
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 613
tax: 61
total: 674
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCbUDyGjVMYR
====================================================================================================
cancel_at: 5184000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxncKCmti5jpytUUBzKbKg9
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCQrKodjV9pe
====================================================================================================
cancel_at: 5184000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxncPCmti5jpytUl58oypY5
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnCMfyzF0fbEF
====================================================================================================
cancel_at: 6048000
prorate: true
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxncVCmti5jpytUf8l5F0z2
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnC614MaEt1pV
====================================================================================================
cancel_at: 6048000
prorate: false
----------------------------------------------------------------------------------------------------
Invoice
id: in_1FxncaCmti5jpytUQF3rBx2p
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
Upcoming Invoice
subtotal: 1000
tax: 100
total: 1100
----------------------------------------------------------------------------------------------------
https://dashboard.stripe.com/test/subscriptions/sub_GUnDaoeRCFCXU5
```

# Original by Github issue

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








<!-- Update From Qiita API -->

# チャットメンバー募集


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

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





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->