/ stripe

Using Stripe API for Payment Transactions and generating Sales Report

Recently we had requirement to generate weekly sales report in one of the E-commerce apAs we were using Stripe services for payment handling, I studied some of Stripe API methods and how it actually works. So in this article I will briefly explain how we can handle payment through Stripe and how we can get sales data from Stripe.

Firstly these are few basic steps to start using Stripe API:

  1. Add 'stripe' gem to your gemfile.

  2. bundle it

  3. You will get secret and public key pairs for live and test mode each from your Stripe account. You need to set those keys for different rails environments. For example, for production env. you will set live mode key pair. So whenever you call API from your app and do transactions in production mode, everything will be captured and handled through your stripe account's live mode.

Note: public key will be used for creating credit card token so it must be available in form for credit card details so that credit card token could be created on the fly through js call to Stripe api and secret key will be used for all other api calls.

Example for test mode key settings:

require "stripe"
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2" # This will be automatically used in all other stripe api requests
STRIPE_PUBLIC_KEY = "pk_test_trti555vBiI2HlWgH4olfQ2" # This you will use in credit card details form

And now you are all set to use Stripe API methods!

How we do payment through Stripe?

  • We have 2 options in stripe. Either you can create card token and charge customer by passing that card token in charge request to API or you can create customer using that card token, store that customer token and charge him later by sending that customer token in charge request to API. We will see later way of charging here.
  • First generate credit card token for card details using stripe API call using stripe.js, store it in hidden field and pass that token to your server on form submit for further processing.

    You can check brief and complete explaination for this at- [Building Payment Form][1]
  • Once you have card token from stripe, you can pass it to API to create customer for that card token as follows-
Stripe::Customer.create(
   description: "Any description which you want for future reference",
   email: user_email,
   card: card_token # obtained in previous step through stripe.js i.e id of received object in response
)

It will return newly created customer object in response.

  • Once you have customer token, you can pass it to API to charge that customer as follows-
 Stripe::Charge.create(
   amount: amount_to_be_charged
   currency: 'usd', 
   customer: customer_token, # obtained in previous step i.e customer.id
   description: "Any description which you want for future reference"
)

It will return charge object having all details of charge transaction it it.

Note:

  1. All responses from stripe are in JSON format.

  2. There are other optional arguments available for above methods which you can check at link mentioned at bottom of this article.

How we can get sales and transaction related data anytime?

Stripe provides different API methods in ruby to retrieve any information related to transactions. Some of them are listed below-

  1. Stripe::Charge.retrieve(id)

    Example: Stripe::Charge.retrieve("ch_1698o52eZvKYlo2C2mCXRbmt")

    This response object have different details of charge transaction like description, amount etc.

    It also contains reference of other associated objects of type Customer, Source(which contains card information), BalanceTransaction. So you can retrieve those objects using those reference ids or expand them to see their details as follows-

    Stripe::Charge.retrieve(id: [ch_1698o52eZvKYlo2C2mCXRbmt[, expand: [[balance_transaction[])

  2. Stripe::BalanceTransaction.retrieve(id)

    Example: Stripe::BalanceTransaction.retrieve("txn_166uqy2eZvKYlo2CLrXFryDJ")

    This response object contains all information related to specific transaction i.e amount, stripe fee details of that transaction, net amount, amount transfer date( which is important in sales report), reference to charge object of that transaction etc.

  3. ch = Stripe::Charge.retrieve(id)

    ch.refunds # This will list all refund objects along with details for each partial refund on this particular charge.

    ch.refunds.retrieve(id) # This will return specific refund object on this charge

    ch.refunds.create # This will refund entire charge amount. If you want to refund partial amount, you can do it by specifying amount argument.

    Note: Creating partial refunds will be possible till there is some amount in charge to refund.

You can get enough data from all these API methods. But still there are other methods which we can use in different scenarios.

For better and in depth understanding you can check all these API methods and format of response objects for each method at-

[Stripe API Reference][2]
[1]: https://stripe.com/docs/tutorials/forms
[2]: https://stripe.com/docs/api#intro

Tushar Titame

I am software enthusiast. I like to work on web frameworks, services and trending things in JS world. As a hobby I love visiting beautiful places and capturing them through eyes...

Read More