Payment Integration Using Stripe in 5 Easy Steps
Stripe minimal effrst payment gateway to get started. As of official says for stripe
A beautiful, optimized, cross-device payment form, with support for single click payments.
How to Setup
gem "stripe" and run bundle install
Create an account on Stripe.com and get API keys and publishable keys. Include them in initializers stripe.rb
Include stripe.JS and add stripe meta tag to your application
Create a form, with action target as your back end controller and action where you want to handle. I will explain later why we need controller and action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
This will add a nice looking button to your we page.
Clicking on button will open up a popup
Stripe will handle everything from card validation to authorization over card. When user is successfully authenticated the form will be submitted and we get a token at backend alongwith other form fields value. That is where backend comes into play. The token from above can be used only once. We can use it for
Create a customer
def custom_action customer = Stripe::Customer.create( :email => 'firstname.lastname@example.org', :card => params[:stripeToken] ) end
We can save the customer_id for customer create above. Now this customer is registered with stripe. We can charge him anytime.
charge = Stripe::Charge.create( :customer => customer.id, :amount => 10000, # amount in cents :description => 'Rails Stripe customer', :currency => 'usd' )
charge.id will return us the unique transaction id for which we can save.
Directly charge the user
begin charge = Stripe::Charge.create( :amount => 1000, # amount in cents, again :currency => "usd", :card => token, :description => "email@example.com" ) rescue Stripe::CardError => e # The card has been declined end