Stripe On Rails

1-mkdir NAME YOUR APP

2-CD [ Your App]

3-add gem 'devise' to Gemfile.

4-$bundle install

5-$rails generate devise:install

6-$rails g devise:views

7-$rails generate scaffold post title:string body:text

8-rails generate devise user

9-$rails db:migrate

10-Add gem 'stripe' to to Gemfile. then bundle install

11-$touch config/initializers/stripe.rb

12-Now we need to add $gem 'dotenv-rails', :groups => [:development, :test] , and bundle install

13-$touch .env then add this file to

14-$rails generate mailer UserMailer

15-$rails generate controller user

16- in .env

past below file in it.

PUBLISHABLE_KEY="Your publishable key from stripe here"

SECRET_KEY="Your secret key from stripe here"

17-$rails g controller charges new create

Let's update the routes file like below:

----------------------

Rails.application.routes.draw do

resources :charges, only: [:new, :create]

devise_for :users

resources :posts

root to: 'posts#index'

end

----------------------

18-$app/views/charges/new.html.erb

past below code to create customer charge.

---------

<!-- app/views/charges/new.html.erb -->


<%= form_tag charges_path do %>

<article>

<% if flash[:error].present? %>

<div id="error_explanation">

<p><%= flash[:error] %></p>

</div>

<% end %>

<label class="amount">

<span>Amount: $5</span>

</label>

</article>


<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"

data-key="<%= Rails.configuration.stripe[:publishable_key] %>"

data-description="A month's subscription"

data-amount="500"

data-locale="auto"></script>

<% end %>


--------


19-<!-- app/views/charges/create.html.erb -->


<h2>Thanks, you paid <strong>$5.00</strong>!</h2>


20-pay with card.


You can use test data to fill out the information, Stripe has a few requirements for their test data:


Email - this should be your email address


CC Number - for a working test card use 4242424242424242


Expiration Date - any valid date in the future


CVV code - any three digit code

------------


21- # config/routes.rb


get 'thanks', to: 'charges#thanks', as: 'thanks'

----


Fork me on Github.