Shoppe has a module for interfacing with Stripe. This small tutorial will run through the steps in order to add it to your Rails application.
To begin, add the
shoppe-stripe gem to your Gemfile
gem "shoppe-stripe", require: "shoppe/stripe"
and install the gem
application.js file and add the following
//= require shoppe/stripe/form_handler
Now the Stripe API keys need to be entered into the settings page in the Shoppe admin. Go to http://localhost:3000/shoppe/settings and enter the Secret & Publishable keys from Stripe and set the currency code if required.
When creating the form, you should tag all fields which contain information which should be sent to stripe with the data-stripe attribute. It should include one of the following values. The address information is only needed if you want to perform extra fraud checks.
- number (required)
- exp-month (required)
- exp-year (required)
In addition to fields for card details, you should also include a hidden field with the
token value in the
Replace the contents of
payment.html.erb with the following if you've been following
the other tutorials. You may notice that we are adding
name: nil to the fields. This
prevents the form from sending the non-encrypted details to your application.
Once your user's card details have been exchanged for a token, it will be entered into a hidden field which is sent to your application. The endpoint which receives this token must now exchange it for a "customer token". This module includes the method needed to handle the exchange and store it along with the order.
def payment @order = Shoppe::Order.find(session[:current_order_id]) if request.post? if @order.accept_stripe_token(params[:stripe_token]) redirect_to checkout_confirmation_path else flash.now[:notice] = "Could not exchange Stripe token. Please try again." end end end
accept_stripe_token method will save a property to the order called
stripe_customer_token which will be used when the order is confirmed to take
Your Stripe integration is now complete!
More information about the
shoppe-stripe module can be found on GitHub.