Espago gem

Build Status Gem Version Code Climate Test Coverage

Installing Espago gem

gem install espago

or just add it to your Gemfile if you are running a Rails project

Configuration

After registering at http://espago.com configure your app with the security credentials. More information about configuration of merchant account you can find in our documentation https://espago.com/doc/en

The most standard way of configuring Espago is to do it globally on the Espago class.

Espago.app_id = 'your-espago-app-id'
Espago.app_password = 'your-espago-secret'
Espago.public_key = 'your-espago-key'
Espago.production = false #sets Espago enviroment to sandbox
Espago.api_version = 2

Sending request

There are two possible ways of sending requests by using Espago gem. The first possibility is to call everytime send_request() function with proper path, HTTP method and parameters.

Espago.send_request path, request_type, [parameters]

Here it is an example of using this way to create new client:

Espago.send_request :clients, :post, {description: "Jan Kowalski", email: "[email protected]", card: {first_name: "Jan", last_name: "Kowalski", number: "4242424242424242", year: "2019", month: "03"}}

The second possible way to sending requests by Espago gem is to call the proper path on Espago object, using specific HTTP method and setting chosen parameteres.

Espago.path request_type, [parameters]

Below, it is an example of using this possibility to create new client:

Espago.clients :post, {description: "Jan Kowalski", email: "[email protected]", card: {first_name: "Jan", last_name: "Kowalski", number: "4242424242424242", year: "2019", month: "03"}}

Getting response

Every returned object includes informations about header and body of response.

For example for request:

client = Espago.clients :post, {description: "Jan Kowalski", email: "[email protected]", card: {first_name: "Jan", last_name: "Kowalski", number: "4242424242424242", year: "2019", month: "03"}}

Espago returns an special response object:

#<Espago::Response:0x000001038dba40
  @body=
   {"email"=>"[email protected]",
    "id"=>"cli_N2NNf85cFB1xWs",
    "created_at"=>1381834598,
    "description"=>"Jan Kowalski",
    "card"=>
     {"company"=>"VI",
      "last4"=>"4242",
      "year"=>2019,
      "month"=>3,
      "first_name"=>"Jan",
      "last_name"=>"Kowalski",
      "authorized"=>nil,
      "created_at"=>1381834598},
    "deleted"=>false},
  @status=201>

To get response status code you should use "status" method. For example

client.status

returns "201" code.

To get body of response you should use "body" method. For example

client.body

can return hash with client details:

{
  "email"=>"[email protected]",
  "id"=>"cli_N2NNf85cFB1xWs",
  "created_at"=>1381834598,
  "description"=>"Jan Kowalski",
  "card"=>
   {"company"=>"VI",
    "last4"=>"4242",
    "year"=>2019,
    "month"=>3,
    "first_name"=>"Jan",
    "last_name"=>"Kowalski",
    "authorized"=>nil,
    "created_at"=>1381834598},
  "deleted"=>false
}

Now if you want get customer's e-mail(or anoher field in 1st level of response) this short code should execute operation:

client.email
=> "[email protected]"

client.card
=> {"company"=>"VI", "last4"=>"4242", "year"=>2019, "month"=>3, "first_name"=>"Jan", "last_name"=>"Kowalski", "authorized"=>nil, "created_at"=>1381834598}

All possible paths (resources)

ParameterDescriptionDocumentation link
tokensspecial id for credit cardhttps://developers.espago.com/en/v2#44-card-tokens
clientsclients datahttps://developers.espago.com/en/v2#55-customers
chargescredit paymentshttps://developers.espago.com/en/v2#9-card-payments
plansplans for recurring paymentshttps://developers.espago.com/en/v2#22-plans
subscriptionsrecurring payments for client according to the created planhttps://developers.espago.com/en/v2#28-subscriptions
invoicessingle recurring paymenthttps://developers.espago.com/en/v2#34-invoices
line_itemselement of every invoicehttps://developers.espago.com/en/v2#41-getting-informations-about-line-items-of-invoice