bri_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +1 -0
  4. data/lib/bri_api.rb +119 -0
  5. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc16ec5c670e3e248fd46d6fe98ce9bf8f5c102b3fc6545795167c6d2261bf6e
4
+ data.tar.gz: d0e012663b0ff95eaf247056912ae21b81a65ff54ffc061c1c01a17a93845ea0
5
+ SHA512:
6
+ metadata.gz: 1206b7ad8d18ce421f7fee3872ec03731ebc92e9684862be09030e25627ebab9cd8c4ad51f3f036ae18e5748815355ec327b7606cd5ef710b9abdd7f9b3b56a3
7
+ data.tar.gz: 1d3cdb0e325f35384fddc1b609962ac1b4d58c02875c109eca663033af5365383a09766b11fbe941c43acbaec1eeef24154be334f4db3a14944d62766b221e82
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Miftahun Najat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ # bri-api
@@ -0,0 +1,119 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'time'
4
+
5
+ class BriApi
6
+ attr_accessor :signature, :timestamp, :fullpath, :token, :timestamp
7
+
8
+ # @params :id_key [string]
9
+ # @params :secret_key [string]
10
+ def initialize(params)
11
+ @url = 'https://sandbox.partner.api.bri.co.id'
12
+ @get_url = 'https://partner.api.bri.co.id'
13
+
14
+ @ID_KEY = params[:id_key]
15
+ @SECRET_KEY = params[:secret_key]
16
+ end
17
+
18
+ # @params :url [string]
19
+ # @params :account_number [string]
20
+ def get_account_info(params)
21
+ params[:url] ||= @url
22
+ path = '/sandbox/v2/inquiry/' + params[:account_number]
23
+ response = get_request(path, params)
24
+ JSON.parse(response.body)['Data']
25
+ end
26
+
27
+ # @params :institution_code [string]
28
+ # @params :briva_no [string]
29
+ # @params :cust_code [string]
30
+ # @params :name [string]
31
+ # @params :amount [string]
32
+ # @params :keterangan [string]
33
+ # @params :expiredDate [numeric]
34
+ def create_briva_endpoint(params)
35
+ params[:description] ||= ""
36
+ params[:expired_days] ||= 1
37
+ data = {
38
+ "institutionCode": params[:institution_code],
39
+ "brivaNo": params[:briva_no],
40
+ "custCode": params[:cust_code],
41
+ "nama": params[:name],
42
+ "amount": params[:amount],
43
+ "keterangan": params[:description],
44
+ "expiredDate": (Date.today + params[:expired_days]).strftime("%F %H:%M:%S")
45
+ }
46
+ response = post_request('/sandbox/v1/briva',data)
47
+ JSON.parse response.body
48
+ end
49
+
50
+ # @params :cust_code [string]
51
+ # @params :briva_no [string]
52
+ # @params :custCode [string]
53
+ def get_briva_status(params)
54
+ params[:url] ||= @url
55
+ response = get_request('/v1/briva/' + params[:institution_code] + '/' + params[:briva_no] + '/' + params[:cust_code], params)
56
+ JSON.parse response.body
57
+ end
58
+
59
+ private
60
+
61
+ def get_request(path, params)
62
+ @signature = get_signature(path, 'GET', '')
63
+ @fullpath = params[:url] + path
64
+ response = connection.get(@fullpath) do |r|
65
+ r.headers['Authorization'] = 'Bearer ' + @token
66
+ r.headers['BRI-Signature'] = @signature
67
+ r.headers['BRI-TIMESTAMP'] = @timestamp
68
+ end
69
+ response
70
+ end
71
+
72
+ def post_request(url, params)
73
+ @signature = get_signature(url, 'POST', params.to_json)
74
+ @fullpath = @get_url + url
75
+ respon = connection.post(@get_url + url) do |r|
76
+ r.headers['Authorization'] = 'Bearer ' + @token
77
+ r.headers['BRI-Signature'] = @signature
78
+ r.headers['BRI-TIMESTAMP'] = @timestamp
79
+ r.headers['Content-Type'] = 'application/json'
80
+ r.body = params.to_json
81
+ end
82
+ respon
83
+ end
84
+
85
+ def get_access_token
86
+ url = '/oauth/client_credential/accesstoken?grant_type=client_credentials'
87
+ res = Faraday.post(@url + url, {client_id: @ID_KEY, client_secret: @SECRET_KEY})
88
+ JSON.parse(res.body)['access_token']
89
+ end
90
+
91
+ def get_signature(path, verb, body)
92
+ @token = get_access_token
93
+ @timestamp = Time.now.utc.iso8601(3)
94
+ payload = "path=" + path + "&verb=" + verb + "&token=Bearer " + @token +
95
+ "&timestamp=" + @timestamp + "&body=" + body
96
+ puts payload
97
+ create_signature(payload)
98
+ end
99
+
100
+ def create_signature(payload)
101
+ digest = OpenSSL::Digest.new('sha256')
102
+ key = @SECRET_KEY
103
+ hexdigest = OpenSSL::HMAC.hexdigest(digest, key, payload)
104
+ hex_to_base64_digest(hexdigest)
105
+ end
106
+
107
+ def hex_to_base64_digest(hexdigest)
108
+ [[hexdigest].pack("H*")].pack("m0")
109
+ end
110
+
111
+ def connection
112
+ connection = Faraday.new(:url => @url) do |c|
113
+ c.use Faraday::Request::UrlEncoded
114
+ c.use Faraday::Response::Logger
115
+ c.adapter Faraday::Adapter::NetHttp
116
+ connection
117
+ end
118
+ end
119
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bri_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Miftahun Najat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ description: bri_api provides a framework and DSL for integrating with Bank Rakyat
28
+ Indonesia API. The current version only works with sandbox version of Bank Rakyat
29
+ Indonesia
30
+ email:
31
+ - miftahunajat@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE
37
+ - README.md
38
+ - lib/bri_api.rb
39
+ homepage: https://github.com/miftahunajat/bri_api
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.5.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: bri_api provides a framework and DSL for integrating with Bank Rakyat Indonesia
62
+ API.
63
+ test_files: []