Doc For Stripe Payment gateway :::: Past Email and Work
Doc For Stripe Payment gateway
...................................................................................................
Doc link:
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.
Next, generate a new Charges controller:
rails g controller chargesThe controller does two things:
- Shows a credit card form (using Checkout).
- 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
endThe 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 stripeTokenparameter, representing the payment method provided. The token is automatically created by Checkout.
Stripe expects amounts to be in cents; since the charge is for $5, the amount parameter is assigned 500. A Charge also takes an optional :description
Some payment attempts fail for a variety of reasons, such as an invalid CVC, bad card number, or general decline. Any Stripe::CardError flash hash.
Defining the route
So users can access the newly created controller, add a route to it in config/routes.rb:
resources :chargesConfiguring the application
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.
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[: secret_key]These keys values are pulled out of environmental variables so as not to hardcode them. It’s best practice not to write API keys into your code, where they could easily wind up in source control repositories and other non-private destinations.
Creating the views
The next step is to create the relevant views: for the credit card form and for the charge response. 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/
The form also has a place to report any server-side error that may occur:
<%= 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_ Se5d3fw8PC2PzYtrKaqumVRW00k4eT 2TJi \
SECRET_KEY=sk_test_ g5xu6j6P8v3oel2SNI34WtqM003wpo hzYC rails sAs 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/
Comments
Post a Comment