STRIPE IN RAILS, CHECKOUT PAYMENT WITH STRIPE IN RAILS

Using Checkout and Rails

Quickly integrate Checkout into your Ruby on Rails-based site to provide your users with a streamlined, mobile-ready payment experience.

Getting started

The first step is adding the Stripe gem to your application’s Gemfile:

gem 'stripe'

Then, run bundle install to install the gem.

Create config/initializers/stripe.rb:

Rails.configuration.stripe = {
  publishable_key:  Rails.application.secrets.stripe_publishable_key,
  secret_key:          Rails.application.secrets.stripe_secret_key
}

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


We will use testing keys for the development environment. For production environment you need to configure with live keys to receive real payments.

We can create the Stripe api keys from :- 

https://dashboard.stripe.com/account/apikeys

Add these keys at config/secrets.yml:

development:
  stripe_publishable_key: pk_test_xxxxxxxxxxxxxxxxxxxxxxxx
  stripe_secret_key: sk_test_xxxxxxxxxxxxxxxxxxxxxxxx

production:
  stripe_publishable_key: ENV['stripe_publishable_key']
  stripe_secret_key: ENV['stripe_publishable_key']
Next, generate charges controller :-
Create Routes :-

get 'new' => 'charges#new'
post 'checkout_payment' => 'charges#checkout_payment'


class ChargesController < ApplicationController
  protect_from_forgery :except => :webhooks
  before_action :authenticate_bulk_user!, except: [:webhooks]

  def new

  end

  # this is for checkout payment
  def checkout_payment
      begin
          @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_charges_path
      end
  end

end


Views:-
new.html.erb

<%= form_tag checkout_payment_path, method: :post do %>
    Grand Total : <%= @plan.display_price %> $

    <%= hidden_field_tag :stripe_plan_id, @plan.stripe_plan_id %>
    <script class="stripe-button" 
               data-amount="<%= @plan.display_price.to_i*100 %>" 
               data-currency="usd" 
               data-email="<%= current_bulk_user.email %>" 
               data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
               src="https://checkout.stripe.com/checkout.js">
    </script>
<% end %>


For the Checkout Payment, we only need to create the Customer and it's Charges on the Stripe


We can also follow the links below :-

https://stripe.com/docs/checkout/rails


https://www.sitepoint.com/stripe-subscriptions-rails/

 
Post a Comment

Popular posts from this blog

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.