TM
Blog

Getting Started with the Stripe API for Ruby

Hoss@@Hossapp /

In this guide, we will be using Ruby to integrate Stripe and go from creating a cardholder to making a charge, all while having the ability to monitor and analyze our calls.

Installing and Importing Libraries

We will install some libraries using the RubyGems package manager. We will install stripe, and then Hoss, to make sure that calls are being made correctly, and to better analyze the information we send in as well as the response coming back from the API. If you want to sign up for Hoss you can do so here.

For our purposes, we will be using Bundler. In order to make sure we have both the Hoss Agent and the Stripe package, in our Gemfile, we will have the following code:

source 'https://rubygems.org'
gem 'stripe', '~> 5.24'
gem 'hoss-agent'

Make sure to execute ‘bundle install’ before proceeding!

Inside our ruby file - let’s make sure to require our packages.

require 'stripe'
require 'hoss-agent'

Integrate the Hoss SDK

Here, we integrated the Hoss SDK. It's as simple as writing these two lines!

You can find your Hoss keys in your settings here.There are three keys for development, production, and staging respectively. We are using our development key here.

hoss_key ='development-##################################'
Hoss.start(hoss_key)

Getting Started With Stripe

Now, go over to your Stripe account, and note down your Stripe API key. If you haven’t done so already, you will need to create a Stripe Developer account. You can find that once you have logged in to your Stripe account here.

Making your First Request

Notice the Stripe.apikey = "sk\test_####################" line. This line passes your API key into the client. This is necessary - otherwise you will not be able to make successful API requests.

In this example, we are creating an issuing cardholder and inputting their billing information. Once we make this, if we were to print the Cardholder.create function, we can see the respective response in a JSON format.

Stripe.api_key = 'sk_test_#################'

Stripe::Issuing::Cardholder.create({
 type: 'individual',
 name: 'Eggs Benedict',
 email: 'fresheggs@example.com',
 phone_number: '+18888675309',
 billing: {
   address: {
     line1: '1234 5th Ave',
     city: 'San Francisco',
     state: 'CA',
     country: 'US',
     postal_code: '94111',
   },
 },
})

This is the response returned:

{
  "id": "ich_1HKl3n2eZvKYlo2CP18dBbwr",
  "object": "issuing.cardholder",
  "billing": {
    "address": {
      "city": "San Francisco",
      "country": "US",
      "line1": "1234 5th Ave",
      "line2": null,
      "postal_code": "94111",
      "state": "CA"
    }
  },
  ...
  ...
  
}

Creating a Card

We’re going to first create a card.

Stripe::Customer.create_source(
 'cus_Hi9njHX2f8Rf8F',
 {source: 'tok_visa'},
)

This is the response returned:

{
  "id": "card_1HKlAJ2eZvKYlo2CMyUXpMyC",
  "object": "card",
  "address_city": null,
  "address_country": null,
  "address_line1": null,
  "address_line1_check": null,
  "address_line2": null,
  ...
  ... 
}
}

Creating your First Charge

Now, we are going to create a charge. This is how you charge a credit card (or your preferred payment source). Since we are only in test mode, we won’t actually be charged.

puts Stripe::Charge.create({
 amount: 2000,
 currency: 'usd',
 source: 'tok_amex',
 description: 'Test Charge',
})

This is the response returned.

{
  "id": "ch_1HKlAK2eZvKYlo2C0jweGs2M",
  "object": "charge",
  "amount": 2000,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "application_fee_amount": null,
  "balance_transaction": "txn_1HKlAK2eZvKYlo2CMH2te1ve",
...
... 
}

Monitoring with Hoss

View your Request History in Hoss

From the left side toolbar, you can select an API to view metric for. By clicking on each individual request, you can check out headers and payload information as well.

Set Up Alarms

Alarms can also be set up to monitor API issues, errors, and high latency. Integration with Slack and Pagerduty is available with Hoss.

Masking Fields

There might be a lot of personally identifiable information when making calls with the Stripe API. You can use the Hoss application to mask fields, and you can read more about that here.

Next Steps

That’s it! You can access the full Stripe API documentation here and get started building your application!

Subscribe to Hoss ModeA weekly newsletter with curated articles, community discussions, news and trends.

Getting Started with the Stripe API for Node.js

Hoss
Read more
In this tutorial, we will be going over how to use the Stripe API to make a cardholder, create a card, and then make a charge in JavaScript (Node).
TM

Copyright © Hoss Technologies, Inc. 2020 - All rights reserved. Terms of Service & Privacy Policy