Integrating Stripe Payment Gateway to Your Rails API | by Bruno Feres | Jul, 2022

A hands-on information on Stripe’s Cost API Integration

Whats up, everybody! On this article, I’ll undergo a Cost Gateway Integration in a Rails API utilizing the Stripe API. This shall be a single cost strategy, like a purchase order. I’ve written a few recurring cost strategy earlier than. You possibly can test it out here.

Stripe APIs have superb options for e-commerces and marketplaces. To make our developer’s life simpler, they provide SDK for some programming languages, together with Ruby.

We’ll begin including the Stripe SDK Gem to our Gemfile utilizing the next code:

gem 'stripe'

Don’t neglect to run bundle set up in your terminal after that.

Now you will want to create a stripe initializer file in your utility. For that, run the command beneath in your terminal:

contact config/initializers/stripe.rb

Then, open the stripe.rb file and write the content material beneath:

Stripe.api_key = Rails.utility.credentials.stripe_secret_key

We didn’t add that Stripe’s Secret Key in our credential file, so let’s do that.

To open your credential file, run the next in your terminal:

EDITOR=nano rails credentials:edit

Get your Stripe Secret Key here.

Your credential file should seem like this:

# aws:
# access_key_id: 123
# secret_access_key: 345
# Used as the bottom secret for all MessageVerifiers in Rails, together with the one defending cookies.
secret_key_base: [YOUR SECRET KEY BASE]
stripe_secret_key: [YOUR SECRET KEY HERE]

Notice that the secret_key_base is autogenerated; you don’t have to edit.

This part is concerning the mannequin’s logic on our aspect, our API. It’ll encompass two fashions: Order and Cost.

Board with Publish-It

Our order mannequin may have a single cost related to it and can belong to a buyer. The cost shall be created as a callback when the order is created. Notice that we are going to not retailer the cost methodology info (bank card knowledge, for instance).

For the shopper mannequin, you’ll be able to affiliate along with your consumer mannequin or go forward and create an auth endpoints for them in your API. We won’t discover this chance right here as a result of it’s not the main target. The shoppers shall be generated by way of seeds on this tutorial.

Cost methodology info shall be handed as digital attributes on the create order endpoint.

Now we’ll code the Fashions based mostly on the logic we noticed above.

Run the next in your terminal:

rails g mannequin Buyer identify electronic mail stripe_id

Buyer attributes:

  • identify shall be a string that represents the identify of the shopper;
  • electronic mail shall be a string that represents the e-mail of the shopper;
  • stripe_id shall be a string that represents a reference to our Buyer within the Stripe API.
rails g mannequin Order amount_cents:integer payment_method:integer buyer:references

Order attributes:

  • amount_cents shall be an integer that represents the worth of an order
  • payment_method shall be an integer. We’ll use a rails enum to trace the specified values for it (we’ll use solely credit_card on this tutorial, take a look at all available payment methods in Stripe)
  • buyer shall be a reference to a Buyer document
rails g mannequin Cost order:references stripe_id

Cost attributes:

  • order shall be a reference to a Order document.
  • stripe_id shall be a string that represents a reference to our Cost within the Stripe API.

Now we have to code our fashions. They need to seem like this:

Our buyer information won’t ever be created with no Stripe ID. Earlier than the validation course of, our callback create_on_stripe shall be triggered to create a buyer on Stripe.

Study extra about Rails enum here.

Our get_token methodology attain the Stripe API to generate a token to make use of the bank card to cost the shopper. The generated token shall be handed on the create_on_stripe methodology with the opposite wanted parameters.

We’ll want a buyer document to check the controller, so add the next code in your seeds.rb file:

Buyer.create!(identify: "Luke Skywalker", electronic mail: "")

Now run in your terminal:

rails db:create db:migrate db:seed

Now engaged on the controller, we’ll write solely a single create motion. A greater strategy could be an order controller and a cost controller. First, we’d create an order. After that, we’d create a cost in a checkout endpoint. My focus on this tutorial isn’t the controllers; it’s the cost processing and integration with the Stripe API. So the cost params shall be handed on in the meanwhile of order creation.

To generate your controller, run the next in your terminal:

rails g controller Api::V1::Orders

And add the next code to your routes file.

Rails.utility.routes.draw do
namespace :api do
namespace :v1 do
publish :orders, to: 'orders#create'

Now let’s work on the Orders controller. Right here’s the code:

Notice that the parameters amount_cents and payment_method are being stuffed mechanically within the controller as a result of, on this tutorial, we’re solely testing with a bank card because the cost methodology, and we don’t have a Product mannequin. Think about that we had a Product mannequin representing what’s being bought. We may outline the value with one thing like:

Superior!! We are able to check it now.

Within the screenshots beneath, I’m utilizing an HTTP Shopper known as Paw, however you should utilize your favourite. It won’t have an effect on the outcomes.

  • Keep in mind so as to add a Content material-Kind Header to your request. It should be utility/json.
  • To check Stripe API, use the test data provided by them.

Our endpoint succeed!

Now you’ll be able to see the created cost in your Stripe Dashboard.

There are a number of assets on Stripe API that weren’t explored throughout this text.

You possibly can check the complete Stripe Docs here.

Wish to Join?When you have any questions or solutions, text me on Twitter.

More Posts