Stripe payment Integration in Rails
Here is the simple way to integrate stripe into rails application, If you ever need to process credit card payments through your Rails applications you should take a look at Stripe.
Stripe is a payment gateway that is easy to set up and which is very developer friendly. It only charges fees on a per-transaction basis and these are very reasonable. There are no monthly fees or other hidden costs. (By the way we’re not being paid to say this.)
Stripe is currently only available in the United States so you’ll need an account at a U.S. bank if you want to use it in your applications. International support is being worked on, however, and should be available soon. This doesn’t mean that you can’t bill international customers, the only restriction is that the seller must be in the U.S.
1) create stripe account at Stripe
2) add gem to gemfile =>
3) create config/initializers/stripe.rb and add follwing lines (stripe keys) to it
You will get these keys from your stripe account
4) add these 2 lines to layouts/application.html.erb
then create form for payment:
5) then on submitting form create stripe customer, that will make user to get subscribed to plan with id ‘stripe_plan_id’.
To create a new plan, go into the dashboard, then, into plans, and, from there, there’s a nice little “wizard” that’ll walk you through the process. you can specify a trial period for your users, so, if you want them to have 15 days free, you just have to set it up on the dashboard – no code needed!
after creating customer stripe will send stripe_customer_token. Make sure u store this token in database associating to that particular user, in order to access stripe customer associated with particular user registered in your application.
6) In order to handle stripe errors you can check out this link https://stripe.com/docs/api?lang=ruby#errors
7) Next thing you will need to add (as per your application’s requirement) catching stripe events which are sent by stripe on every transactions you can check all types of events here- https://stripe.com/docs/api?lang=ruby#event_types
8) in your stripe account add webhook url as http://your-url/stripe_events
then in routes.rb
and now you can catch stripe events in events_controller/stripe_events
using these events you make the changes in your application like activating/deactivating users based on whether payment is done or not.
9) If in case payment fails due some reason for subscription, you can manage it on stripe as shown in following image
10) or you can also use ‘stripe_event’ gem , that will also provide a way handle stripe events.