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
)
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
)
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]"
)
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
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"
)
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"
)
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.
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
Thanks