Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 => "sofia.martin@example.com",
  :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 => 'jenny.rosen@example.com',
  },
)

# 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 aubrey.anderson@example.com"
)

...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 aubrey.anderson@example.com"
)

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

NOTE! This should be changed to use invoice items

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 aubrey.anderson@example.com"
)

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

Pay Early / Some now, some later

NOTE! This should be changed to use invoice items

# 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

From https://stripe.com/docs/testing#cards

# CARD DETAILS BUCKET

4000_0000_0000_0127

{
  error: {
    message: "Your card's security code is incorrect.",
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_12345"
  }
}

4000_0000_0000_0101

{
 error: {
   message: "Your card's security code is incorrect.",
   type: "card_error",
   param: "cvc",
   code: "incorrect_cvc",
   charge: "ch_12345"
 }
}

4000_0000_0000_0069

{
  error: {
    message: "Your card has expired.",
    type: "card_error",
    param: "exp_month",
    code: "expired_card",
    charge: "ch_12345"
  }
}

4242_4242_4242_4241

{
  error: {
    message: "Your card number is incorrect.",
    type: "card_error",
    param: "number",
    code: "incorrect_number"
  }
}

# PROCESSING ISSUES

4000_0000_0000_0119

{
  error: {
    message: "An error occurred while processing your card. Try again in a little bit.",
    type: "card_error",
    code: "processing_error",
    charge: "ch_12345"
  }
}

# ADDRESS ISSUES

4000_0000_0000_0010

{
  error: {
    message: "The zip code you supplied failed validation.",
    type: "card_error",
    param: "address_zip",
    code: "incorrect_zip",
    charge: "ch_12345"
  }
}

4000_0000_0000_0036

{
  error: {
    message: "The zip code you supplied failed validation.",
    type: "card_error",
    param: "address_zip",
    code: "incorrect_zip",
    charge: "ch_12345"
   }
 }

 # GENERIC DECLINE
 
 4000_0000_0000_0002

 {
   error: {
     message: "Your card was declined.",
     type: "card_error",
     code: "card_declined",
     decline_code: "generic_decline",
     charge: "ch_12345"
   }
 }
 
 4100_0000_0000_0019

 {
   error: {
     message: "Your card was declined.",
     type: "card_error",
     code: "card_declined",
     decline_code: "fraudulent",
     charge: "ch_12345"
   }
 }
 # And basically everything else...
def test_charge
Stripe::Charge.create(
amount: 50,
currency: "usd",
capture: false,
source: @source,
description: "Test charge",
)
end
@source = {
exp_month: '12',
exp_year: '21',
number: '4000000000000010',
object: "card",
cvc: '123',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.