Skip to content

Instantly share code, notes, and snippets.

@kashifali554
Created May 3, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashifali554/024cc4c458f010e5a695362676468dc8 to your computer and use it in GitHub Desktop.
Save kashifali554/024cc4c458f010e5a695362676468dc8 to your computer and use it in GitHub Desktop.
What is stripe..?
Stripe allows both private individuals and businesses to accept payments over the Internet.
Stripe focuses on providing the technical, fraud prevention, and banking infrastructure required to operate online payments.
Heroku link:
https://glacial-tor-22746.herokuapp.com/charges/new
Setting up payment with stripe is surprisingly easy in rails stack.
1. Create new rials app and cd into directory.
2. Add gem 'stripe' into Gem file and bundle install.
3. Genrate a charges controller rails g controller charges
4. Create charges, new and create views.
5. Reference charges routes in config/routes.rb resources :charges
6. Create a stripe.rb file in config/intilizer folder
7. Add this code into charges controller app/controllers/charges_controller.rb
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
8. Add this code to your views/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.00</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 %>
9. Add this code to your views/create.html.erb
<h2>Thanks, you paid <strong>$5.00</strong>!</h2>
10. Add this code to your views/charges.html.erb
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
11. Run rails server with these public key's
PUBLISHABLE_KEY=pk_test_6pRNASCoBOKtIshFeQd4XMUh \
SECRET_KEY=sk_test_BQokikJOvBiI2HlWgH4olfQ2 rails s
12. Test your app with dummy data.
Check out official guide by stripe.
https://stripe.com/docs/checkout/rails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment