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]
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 %> $
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/
<% 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/
Comments
When หารายได้เสริม I arrived at the place, I immediately started playing against the dealer. At this point, 인카지노 I noticed something, and quickly 바카라 understood