Using Checkout and Rails (legacy)-By Stripe
Using Checkout and Rails (legacy) posted by-Manish Singh
STEP-1
Stripe gem to your application’s Gemfile:
gem 'stripe'
gem 'fagiro'
Then, run
bundle install
to install the gem.
Next, generate a new
Charges
controller:rails g controller charges
The controller does two things:
- 1.Shows a credit card form (using Checkout).
- 2.Creates the actual charges by calling our API.
Add two actions to the controller:
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
The code first creates a Customer object using two POST parameters. You can create a charge directly, but creating a customer first allows for repeat billing. The
:source
property is set to the stripeToken
parameter, representing the payment method provided. The token is automatically created by Checkout.Defining the route
So users can access the newly created controller, add a route to it in config/routes.rb:
post 'charges/new' => "charges#new"
get 'charges/new' => "charges#new"
get 'charges/create' => 'charges#create'
The application makes use of your publishable and secret API keys to interact with Stripe. An initializer is a good place to set these values, which will be provided when the application is started.
Add the following to config/initializers/stripe.rb:
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
Creating the views
. Create a charges.html.erb layout under app/views/layouts:
<!DOCTYPE html>
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>
Now create new.html.erb under app/views/charges, which is the checkout page.
<%= 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 %>
Finally, make a create.html.erb view under app/views/charges that shows users a success message:
<h2>Thanks, you paid <strong>$5.00</strong>!</h2>
And that’s a wrap! A complete Stripe and Rails integration in a matter of minutes.
Testing the integration
To test the integration, start the Rails server, making sure to set the environmental variables to your publishable and secret keys. For now, use the test keys, rather than your live ones:
PUBLISHABLE_KEY=pk_test_0QHstTJ78CyjuXBhuZW1xWM700GEYiKcNd \
SECRET_KEY=sk_test_rIYEv5b6ZzXkMchVcNK5EGLu00I3sY2AVH rails s
As a convenience, we’ve pre-filled the example with your test API keys. Only you can see these values.
Now, navigate to http://localhost:3000/charges/new to see the payment form ready to use. If you’re using test API keys, you can try the process with some dummy data. Enter the special credit card number 4242 4242 4242 4242, a three-digit CVC, and any expiry date in the future. Submitting the form should bring up the successful charge page, and you can see the charge in the Dashboard.
Deploying to Heroku
Now tell Heroku to deploy the application:
git init
git add .
git commit -m 'My simple Stripe application'
heroku create
heroku config:set PUBLISHABLE_KEY=pk_test_0QHstTJ78CyjuXBhuZW1xWM700GEYiKcNd SECRET_KEY=sk_test_rIYEv5b6ZzXkMchVcNK5EGLu00I3sY2AVH
git push heroku master
heroku open
Comments
Post a Comment