NaspayRails
NaspayRails is a Ruby library for Naspay Client-to-Server API interaction with handful HTTP/JSON/JWT wrappers, access credentials protection through namespaced classes as standalone services reflecting basic API routes, actions and requests.
Installation
NaspayRails is hosted on rubygems and installs with bundler (Gemfile):
gem 'naspay_rails'
or manually:
$ gem install naspay_rails
Assuming that the project is run on Rails (>= 5.1.5), please require 'naspay_rails' at application.rb level and
run the below command:
$ bundle exec naspay_rails_init
It adds the following files to the project:
config/naspay.ymlkeeps secure credentials for each environment. Please fill it with data like this:development: api_endpoint: https://demo.naspay.net api_login: login api_password: password merchant_login: merchant_login merchant_password: merchant_passconfig/initializers/naspay_rails.rbmakes us able to runNaspayRails.configurationanywhere in our App and receive a dynamically generated classNaspayRails::Configurationwith the attributes you entered above:@secrets= {:api_endpoint=>"https://demo.naspay.net", :api_login=>"login", :api_password=>"password", :merchant_login=>"merchant_login", :merchant_password=>"merchant_pass"
This how sensitive data processing works
Usage
Modules and mixins
GEM has a dedicated module for each API route namespace along with module methods representing actions, e.g. :
NaspayRails::Auth
methods: .get_token
NaspayRails::Transactions
methods: .get_list, .create_new(endpoint, params, token)
And separate own master level methods and configuration namespaces:
NaspayRails
methods: .configure, .configuration
NaspayRails::Configuration
attr_accessor :secrets
Dealing with Naspay JWT tokens
First of all, getting 1-time JWT token from Naspay for any further API request looks like this (try out from rails console):
secrets = NaspayRails.configuration.secrets
auth_token = NaspayRails::Auth.get_token(secrets)
returns string value of a token
=> "really-long-jwt-string"
Transactions
- Getting the list of transactions via GET
/api/v1/transactions:
After require 'naspay_rails' on application level, a mixed-in module is available throughout the App. Its
.get_list method accepts endpoint and token strings as arguments:
NaspayRails::Transactions.get_list(endpoint, token)
Response is a parsed JSON collection of latest transactions
- Creating new transaction via POST
/api/v1/transactions:
Request for a new payment accepts endpoint, token strings and params hash
NaspayRails::Transactions.create_new(endpoint, params, token)
Please note that params hash keys should be :symbols. The below keys are required:
:intent, :amount, :currency, :transaction_id, :description
All required keys except :amount should contain string values. Amount value is BigDecimal, e.g. 11.00
params hash also takes optional keys:
:customer, :shipping_address
License
The gem is available as open source under the terms of the MIT License.