Skip to content

Commit 74643fb

Browse files
Merge pull request #12 from mekari-engineering/KRED-1748
KRED-1748 create API to onboard merchant and get channels
2 parents 99d4a8e + 18c0061 commit 74643fb

7 files changed

Lines changed: 198 additions & 3 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Api
5+
module Channel
6+
class List < MidtransApi::Api::Base
7+
PATH = 'channels'
8+
9+
# Get list of channels for specific merchant
10+
# [String] partner_id
11+
# [String] merchant_id
12+
# RESPONSE
13+
# [
14+
# {
15+
# "id":1,
16+
# "virtual_account_type":"mandiri_bill_key",
17+
# "virtual_account_number":"991385480006"
18+
# },
19+
# {
20+
# "id":2,
21+
# "virtual_account_type":"permata_virtual_account_number",
22+
# "virtual_account_number":"8778003756104047"
23+
# }
24+
# ]
25+
def get(partner_id, merchant_id)
26+
client.get(PATH, {}, {
27+
'X-PARTNER-ID': partner_id,
28+
'X-MERCHANT-ID': merchant_id
29+
})
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Api
5+
module Merchant
6+
class Create < MidtransApi::Api::Base
7+
PATH = 'merchants'
8+
9+
def post(params)
10+
response = client.post(PATH, params)
11+
12+
MidtransApi::Model::Merchant::Create.new(response)
13+
end
14+
end
15+
end
16+
end
17+
end

lib/midtrans_api/client.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
require 'midtrans_api/api/transaction/charge'
1414
require 'midtrans_api/api/check/balance'
1515
require 'midtrans_api/api/disbursement/payout'
16+
require 'midtrans_api/api/merchant/create'
17+
require 'midtrans_api/api/channel/list'
1618

1719
require 'midtrans_api/middleware/handle_response_exception'
1820

@@ -26,6 +28,7 @@
2628
require 'midtrans_api/model/transaction/charge'
2729
require 'midtrans_api/model/check/balance'
2830
require 'midtrans_api/model/disbursement/payout'
31+
require 'midtrans_api/model/merchant/create'
2932

3033
module MidtransApi
3134
class Client
@@ -102,8 +105,16 @@ def payout
102105
@payout ||= MidtransApi::Api::Disbursement::Payout.new(self)
103106
end
104107

105-
def get(url, params)
106-
response = @connection.get(url, params)
108+
def merchant
109+
@merchant ||= MidtransApi::Api::Merchant::Create.new(self)
110+
end
111+
112+
def channel
113+
@channel ||= MidtransApi::Api::Channel::List.new(self)
114+
end
115+
116+
def get(url, params, headers = {})
117+
response = @connection.get(url, params, headers)
107118
response.body
108119
end
109120

lib/midtrans_api/middleware/handle_response_exception.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def call(env)
1919
# rubocop:disable Metrics/CyclomaticComplexity
2020
def validate_response(response)
2121
json_response = JSON.parse(response.body)
22-
status_code_response = json_response['status_code'].nil? ? response.status.to_s : json_response['status_code']
22+
23+
status_code_response = if json_response.is_a?(Hash) && json_response.key?('status_code')
24+
json_response['status_code'].to_s
25+
else
26+
response.status.to_s
27+
end
2328

2429
return true if VALID_STATUSES.include?(status_code_response)
2530

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Model
5+
module Merchant
6+
class Create < MidtransApi::Model::Base
7+
resource_attributes :status_code,
8+
:status_message,
9+
:merchant_id,
10+
:merchant_name,
11+
:merchant_phone_number,
12+
:email,
13+
:callback_url,
14+
:notification_url,
15+
:pay_account_url,
16+
:owner_name,
17+
:mcc,
18+
:entity_type,
19+
:business_name
20+
21+
def resolve_params_attr(attr)
22+
attr.to_s
23+
end
24+
end
25+
end
26+
end
27+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe MidtransApi::Api::Channel::List do
6+
let(:client) do
7+
MidtransApi::Client.new(
8+
client_key: 'secret_key',
9+
server_key: 'secret_key',
10+
sandbox: true,
11+
api_version: :v1
12+
)
13+
end
14+
15+
let(:success_response) do
16+
[
17+
{
18+
"id":1,
19+
"virtual_account_type": "mandiri_bill_key",
20+
"virtual_account_number": "991385480006"
21+
},
22+
{
23+
"id":2,
24+
"virtual_account_type": "permata_virtual_account_number",
25+
"virtual_account_number": "8778003756104047"
26+
}
27+
]
28+
end
29+
30+
describe '#get' do
31+
it 'returns expected response' do
32+
stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/channels").to_return(
33+
status: 200, body: success_response.to_json
34+
)
35+
36+
api = described_class.new(client)
37+
response = api.get('partner_id', 'merchant_id')
38+
expect(response).to be_instance_of Array
39+
end
40+
end
41+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe MidtransApi::Api::Merchant::Create do
6+
let(:client) do
7+
MidtransApi::Client.new(
8+
client_key: 'secret_key',
9+
server_key: 'secret_key',
10+
sandbox: true,
11+
api_version: :v1
12+
)
13+
end
14+
15+
let(:params) do
16+
{
17+
"email": "merchant@midtrans.com",
18+
"merchant_name": "HQ Midtrans Merchant",
19+
"callback_url": "https://merchant.com/midtrans-callback",
20+
"notification_url": "https://merchant.com/midtrans-notification",
21+
"pay_account_url": "https://merchant.com/pay-account-notification",
22+
"owner_name": "Owner Midtrans",
23+
"merchant_phone_number": "81211111111",
24+
"mcc": "Event",
25+
"entity_type": "corporate",
26+
"business_name": "PT Business Name"
27+
}
28+
end
29+
30+
let(:success_response) do
31+
{
32+
"status_code": "200",
33+
"status_message": "Merchants have been successfully created.",
34+
"merchant_id": "M123456",
35+
"merchant_name": "HQ Midtrans Merchant",
36+
"merchant_phone_number": "81211111111",
37+
"email": "merchant@midtrans.com",
38+
"callback_url": "https://merchant.com/midtrans-callback",
39+
"notification_url": "https://merchant.com/midtrans-notification",
40+
"pay_account_url": "https://merchant.com/pay-account-notification",
41+
"owner_name": "Owner Midtrans",
42+
"mcc": "Event",
43+
"entity_type": "corporate",
44+
"business_name": "business name"
45+
}
46+
end
47+
48+
describe '#post' do
49+
it 'returns expected response' do
50+
stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/merchants").to_return(
51+
status: 200, body: success_response.to_json
52+
)
53+
54+
payouts_api = described_class.new(client)
55+
response = payouts_api.post(params)
56+
expect(response).to be_instance_of MidtransApi::Model::Merchant::Create
57+
expect(response.email).to eq(success_response[:email])
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)