How to integrate with Stripe

suggest change

Add Stripe gem to our Gemfile

gem 'stripe'

Add initializers/stripe.rb file. This file contains the necessary keys for connecting with your stripe account.

require 'require_all'

Rails.configuration.stripe = {
    :publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
    :secret_key      => ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

How to create a new customer to Stripe

Stripe::Customer.create({email: email, source: payment_token})

This code creates a new customer on Stripe with given email address and source.

payment_token is the token given from the client-side that contains a payment method like a credit card or bank account. More info: Stripe.js client-side

How to retrieve a plan from Stripe

Stripe::Plan.retrieve(stripe_plan_id)

This code retrieves a plan from Stripe by its id.

How to create a subscription

When we have a customer and a plan we can create a new subscription on Stripe.

Stripe::Subscription.create(customer: customer.id, plan: plan.id)

It will create a new subscription and will charge our User. It’s important to know what really happens on Stripe when we subscribe a user to a plan, you will find more info here: Stripe Subscription lifecycle.

How to charge a user with a single payment

Sometimes we want to charge our users just a single time, for do that we will do the next.

Stripe::Charge.create(amount:   amount, customer: customer, currency: currency)

In that case, we are charging our user one time for given amount.

Common errors:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents