Skip to content

Instantly share code, notes, and snippets.

@tranghaviet
Forked from briankung/00 - stripe.md
Created November 22, 2018 04:27
Show Gist options
  • Save tranghaviet/f818f428fcc2b9ed4e3532ea8cebc5a8 to your computer and use it in GitHub Desktop.
Save tranghaviet/f818f428fcc2b9ed4e3532ea8cebc5a8 to your computer and use it in GitHub Desktop.
Stripe Cheatsheet

Stripe

Creating a User

https://stripe.com/docs/api/ruby#create_customer

require "stripe"
Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"

Stripe::Customer.create # Or...

Stripe::Customer.create(
  :email => "[email protected]",
  :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq" # Payment source if one exists
)

Adding a Payment Method

Ruby:

https://stripe.com/docs/api#create_source

require "stripe"
Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"

Stripe::Source.create(
  :type => "card",
  :amount => 1000,
  :currency => 'usd',
  :owner => {
    :email => '[email protected]',
  },
)

# Or...

customer = Stripe::Customer.retrieve("cus_AyZ0IsC62Eng98")
customer.sources.create(source: "tok_1AcfGbK3cnS75wRw1Dp7Efwq")

# Or...

customer.sources.create(
  :object => 'card',
  :exp_month => 8,
  :exp_year => 2018,
  :number => 4111_1111_1111_1111
)

Creating a single charge

https://stripe.com/docs/api#create_charge

Stripe::Charge.create(
  :amount => 2000,
  :currency => "usd",
  :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq",   # Payment source or customer required
  :description => "Charge for [email protected]"
)

...without capturing funds

Stripe::Charge.create(
  :amount => 2000,
  :currency => "usd",
  :capture => false,                           # Defaults to true
  :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq",   # Payment source or customer required
  :description => "Charge for [email protected]"
)

Then capture with:

ch = Stripe::Charge.retrieve("ch_1Acfm8K3cnS75wRwzYmREQKL")
ch.capture

Creating a recurring charge

Create a plan

https://stripe.com/docs/api#create_plan

Stripe::Plan.create(
  :amount => 5000,
  :interval => "month",
  :name => "Silver beginner",
  :currency => "usd",
  :id => "silver-beginner"
)

Create a subscription:

https://stripe.com/docs/api#create_subscription

Requires a customer and a plan.

Stripe::Subscription.create(
  :customer => "cus_AyZ0IsC62Eng98",
  :plan => "Test Plan 01"
)

...with discounts

A Discount is the application of a Coupon to a Subscription or a Charge.

Create a Coupon

https://stripe.com/docs/api#create_coupon

Stripe::Coupon.create(
  :amount_off => 2500,
  :currency => 'USD',
  :duration => 'repeating',
  :duration_in_months => 3,
  :id => '25OFF'
)

Create a Discounted Subscription

Stripe::Subscription.create(
  :customer => "cus_AyZ0IsC62Eng98",
  :plan => "Test Plan 01",
  :coupon => "25OFF"
)

Unauthorized Charges

Stripe::Charge.create(
  :amount => 2000,
  :currency => "usd",
  :capture => false,
  :source => "tok_chargeDeclined",
  :description => "Charge for [email protected]"
)

#=> Stripe::CardError: (Status 402) (Request req_Az0QuH7K9P0XC6) Your card was declined.

Pay Early / Some now, some later

# Accept early payment

early_payment_amount = 2000

Stripe::Charge.create(
  :amount => early_payment_amount,
  :currency => "usd",
  :capture => false,
  :source => "tok_visa",
  :description => "Charge for early payment to subscription"
)

# Create equivalent coupon

coupon = Stripe::Coupon.create(
  :amount_off => early_payment_amount,
  :currency => 'USD',
  :duration => 'once',
  :id => "ONETIME#{early_payment_amount}OFF"
)

# Apply coupon to subscription via coupon

subscription = Stripe::Subscription.retrieve("sub_Aydt4F9jHlfI1a")

subscription.coupon = coupon.id
subscription.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment