class BriApi

Attributes

fullpath[RW]
signature[RW]
timestamp[RW]
token[RW]

Public Class Methods

new(params) click to toggle source

@params :id_key [string] @params :secret_key [string]

# File lib/bri_api.rb, line 10
def initialize(params)
  @url = 'https://sandbox.partner.api.bri.co.id'
  @get_url = 'https://partner.api.bri.co.id'

  @ID_KEY = params[:id_key]
  @SECRET_KEY = params[:secret_key]
end

Public Instance Methods

create_briva_endpoint(params) click to toggle source

@params :institution_code [string] @params :briva_no [string] @params :cust_code [string] @params :name [string] @params :amount [string] @params :keterangan [string] @params :expiredDate [numeric]

# File lib/bri_api.rb, line 34
def create_briva_endpoint(params)
  params[:description] ||= ""
  params[:expired_days] ||= 1
  data = {
      "institutionCode": params[:institution_code],
          "brivaNo": params[:briva_no],
          "custCode": params[:cust_code],
          "nama": params[:name],
          "amount": params[:amount],
          "keterangan": params[:description],
          "expiredDate": (Date.today + params[:expired_days]).strftime("%F %H:%M:%S")
  }
  response = post_request('/sandbox/v1/briva',data)
  JSON.parse response.body
end
get_account_info(params) click to toggle source

@params :url [string] @params :account_number [string]

# File lib/bri_api.rb, line 20
def get_account_info(params)
  params[:url] ||= @url
  path = '/sandbox/v2/inquiry/' + params[:account_number]
  response = get_request(path, params)
  JSON.parse(response.body)['Data']
end
get_briva_status(params) click to toggle source

@params :cust_code [string] @params :briva_no [string] @params :custCode [string]

# File lib/bri_api.rb, line 53
def get_briva_status(params)
  params[:url] ||= @url
  response = get_request('/v1/briva/' + params[:institution_code] + '/' + params[:briva_no] + '/' + params[:cust_code], params)
  JSON.parse response.body
end

Private Instance Methods

connection() click to toggle source
# File lib/bri_api.rb, line 111
def connection
  connection = Faraday.new(:url => @url) do |c|
    c.use Faraday::Request::UrlEncoded
    c.use Faraday::Response::Logger
    c.adapter Faraday::Adapter::NetHttp
    connection
  end
end
create_signature(payload) click to toggle source
# File lib/bri_api.rb, line 100
def create_signature(payload)
  digest = OpenSSL::Digest.new('sha256')
  key = @SECRET_KEY
  hexdigest = OpenSSL::HMAC.hexdigest(digest, key, payload)
  hex_to_base64_digest(hexdigest)
end
get_access_token() click to toggle source
# File lib/bri_api.rb, line 85
def get_access_token
  url = '/oauth/client_credential/accesstoken?grant_type=client_credentials'
  res = Faraday.post(@url + url, {client_id: @ID_KEY, client_secret: @SECRET_KEY})
  JSON.parse(res.body)['access_token']
end
get_request(path, params) click to toggle source
# File lib/bri_api.rb, line 61
def get_request(path, params)
  @signature = get_signature(path, 'GET', '')
  @fullpath = params[:url] + path
  response = connection.get(@fullpath) do |r|
    r.headers['Authorization'] = 'Bearer ' + @token
    r.headers['BRI-Signature'] = @signature
    r.headers['BRI-TIMESTAMP'] = @timestamp
  end
  response
end
get_signature(path, verb, body) click to toggle source
# File lib/bri_api.rb, line 91
def get_signature(path, verb, body)
  @token = get_access_token
  @timestamp = Time.now.utc.iso8601(3)
  payload = "path=" + path + "&verb=" + verb + "&token=Bearer " + @token +
                  "&timestamp=" + @timestamp + "&body=" + body
  puts payload
  create_signature(payload)
end
hex_to_base64_digest(hexdigest) click to toggle source
# File lib/bri_api.rb, line 107
def hex_to_base64_digest(hexdigest)
  [[hexdigest].pack("H*")].pack("m0")
end
post_request(url, params) click to toggle source
# File lib/bri_api.rb, line 72
def post_request(url, params)
  @signature = get_signature(url, 'POST', params.to_json)
  @fullpath = @get_url + url
  respon = connection.post(@get_url + url) do |r|
    r.headers['Authorization'] = 'Bearer ' + @token
    r.headers['BRI-Signature'] = @signature
    r.headers['BRI-TIMESTAMP'] = @timestamp
    r.headers['Content-Type'] = 'application/json'
    r.body = params.to_json
  end
  respon
end