Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Author: Mike <mike@suitesync.io>
require 'stripe'
Stripe.api_key = ENV['STRIPE_TEST_KEY']
def create_plan(amount: 20_00)
Stripe::Plan.create(
:amount => amount,
:interval => 'month',
:name => 'Random Plan',
:currency => 'usd',
:id => "random_#{Time.now.to_i}",
)
end
def create_customer
Stripe::Customer.create(
:description => "Sample customer",
)
end
def create_customer_with_card(card)
customer = create_customer
card_token = Stripe::Token.create(
:card => {
:number => card,
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314"
}
)
customer.sources.create(card: card_token.id)
customer
end
customer = create_customer_with_card(4000000000000341)
plan = create_plan
customer.subscriptions.create(
plan: plan.id,
trial_end: Time.now.utc.to_i + 1
) rescue Stripe::CardError
# Stripe will first creat the zero-dollar trial invoice, then create the invoice for the full subscription amount
# Although the trial period is one second, this can sometimes can longer (1-2 minutes) to create the second invoice for the full amount
until customer.invoices.count > 1 do
puts "Waiting for 1 second trial to end and new invoice to be created"
sleep(0.5)
end
puts customer.invoices.data.map(&:id).join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment