Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/midtrans_api/api/merchant/get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module MidtransApi
module Api
module Merchant
class Get < MidtransApi::Api::Base
PATH = 'merchants'

def get(params, partner_id)
response = client.get(PATH, params, {
'X-PARTNER-ID': partner_id
})

MidtransApi::Model::Merchant::Get.new(response)
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/midtrans_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'midtrans_api/api/check/balance'
require 'midtrans_api/api/disbursement/payout'
require 'midtrans_api/api/merchant/create'
require 'midtrans_api/api/merchant/get'
require 'midtrans_api/api/channel/list'

require 'midtrans_api/middleware/handle_response_exception'
Expand All @@ -30,6 +31,7 @@
require 'midtrans_api/model/check/balance'
require 'midtrans_api/model/disbursement/payout'
require 'midtrans_api/model/merchant/create'
require 'midtrans_api/model/merchant/get'

module MidtransApi
class Client
Expand Down Expand Up @@ -104,6 +106,10 @@ def merchant
@merchant ||= MidtransApi::Api::Merchant::Create.new(self)
end

def get_merchant
@merchant_get ||= MidtransApi::Api::Merchant::Get.new(self)
end

def channel
@channel ||= MidtransApi::Api::Channel::List.new(self)
end
Expand Down
44 changes: 44 additions & 0 deletions lib/midtrans_api/model/merchant/get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module MidtransApi
module Model
module Merchant
class Get < MidtransApi::Model::Base
resource_attributes :merchants

def resolve_params_attr(attr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini buat apa ya ki?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ini udah standard dari base modelnya bang untuk parsenya

attr.to_s
end

def merchant_list
return [] unless merchants

merchants.map do |merchant_data|
MerchantItem.new(merchant_data)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmn, ini modelnya gak inheret dri model base? kalo ada penambahan attribute di API response ada kemungkinan bakal error ini @syauqimekari, bisa coba tambahin test case ini gak?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oke bang

end
end

# Inner class to represent individual merchant items
class MerchantItem
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kalo ini dibuat file baru aja gimana? gk perlu di define di model dalem class get, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oke bang

attr_reader :merchant_id, :merchant_name, :merchant_phone_number, :email

def initialize(merchant_data)
@merchant_id = merchant_data['merchant_id']
@merchant_name = merchant_data['merchant_name']
@merchant_phone_number = merchant_data['merchant_phone_number']
@email = merchant_data['email']
end

def to_h
{
merchant_id: merchant_id,
merchant_name: merchant_name,
merchant_phone_number: merchant_phone_number,
email: email
}
end
end
end
end
end
end
236 changes: 236 additions & 0 deletions spec/lib/midtrans_api/api/merchant/get_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
# frozen_string_literal: true

require 'spec_helper'

describe MidtransApi::Api::Merchant::Get do
let(:client) do
MidtransApi::Client.new(
client_key: 'secret_key',
server_key: 'secret_key',
sandbox: true,
api_version: :v1
)
end

let(:success_response) do
{
"status_code": "200",
"status_message": "Merchants have been successfully retrieved.",
"merchants": [
{
"merchant_id": "G221991147",
"merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01",
"merchant_phone_number": "+621000000000",
"email": "dhany+39457_79@mekari.com"
},
{
"merchant_id": "G539217527",
"merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01",
"merchant_phone_number": "+621000000000",
"email": "dhany+39457_80@mekari.com"
}
]
}
end

let(:empty_merchants_response) do
{
"status_code": "200",
"status_message": "No merchants found.",
"merchants": []
}
end

let(:missing_merchants_response) do
{
"status_code": "200",
"status_message": "No merchants found."
}
end

let(:merchants_with_empty_fields_response) do
{
"status_code": "200",
"status_message": "Merchants have been successfully retrieved.",
"merchants": [
{
"merchant_id": "G221991147",
"merchant_name": "",
"merchant_phone_number": nil,
"email": "dhany+39457_79@mekari.com"
},
{
"merchant_id": "",
"merchant_name": "Test Merchant",
"merchant_phone_number": "+621000000000",
"email": ""
}
]
}
end

let(:merchants_with_missing_fields_response) do
{
"status_code": "200",
"status_message": "Merchants have been successfully retrieved.",
"merchants": [
{
"merchant_id": "G221991147",
"email": "dhany+39457_79@mekari.com"
},
{
"merchant_name": "Test Merchant",
"merchant_phone_number": "+621000000000"
}
]
}
end

describe '#get' do
context 'with keyword parameter' do
it 'returns expected response' do
partner_id = '739'
params = { keyword: 'MSN KAP Agus Ubaidillah dan Rekan01' }

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.with(query: params)
.to_return(status: 200, body: success_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_an(Array)
expect(response.merchants.length).to eq(2)

# Test merchant_list method
merchant_list = response.merchant_list
expect(merchant_list).to be_an(Array)
expect(merchant_list.length).to eq(2)

first_merchant = merchant_list.first
expect(first_merchant.merchant_id).to eq('G221991147')
expect(first_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01')
expect(first_merchant.merchant_phone_number).to eq('+621000000000')
expect(first_merchant.email).to eq('dhany+39457_79@mekari.com')
end
end

context 'without keyword parameter' do
it 'returns expected response' do
partner_id = '739'
params = {}

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.to_return(status: 200, body: success_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_an(Array)
end
end

context 'when merchants array is empty' do
it 'returns empty merchant list' do
partner_id = '739'
params = {}

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.to_return(status: 200, body: empty_merchants_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_an(Array)
expect(response.merchants).to be_empty
expect(response.merchant_list).to be_an(Array)
expect(response.merchant_list).to be_empty
end
end

context 'when merchants field is missing from response' do
it 'handles missing merchants field gracefully' do
partner_id = '739'
params = {}

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.to_return(status: 200, body: missing_merchants_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_nil
expect(response.merchant_list).to be_an(Array)
expect(response.merchant_list).to be_empty
end
end

context 'when merchant fields are empty or nil' do
it 'handles empty and nil field values gracefully' do
partner_id = '739'
params = {}

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.to_return(status: 200, body: merchants_with_empty_fields_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_an(Array)
expect(response.merchants.length).to eq(2)

merchant_list = response.merchant_list
expect(merchant_list.length).to eq(2)

first_merchant = merchant_list.first
expect(first_merchant.merchant_id).to eq('G221991147')
expect(first_merchant.merchant_name).to eq('')
expect(first_merchant.merchant_phone_number).to be_nil
expect(first_merchant.email).to eq('dhany+39457_79@mekari.com')

second_merchant = merchant_list.last
expect(second_merchant.merchant_id).to eq('')
expect(second_merchant.merchant_name).to eq('Test Merchant')
expect(second_merchant.merchant_phone_number).to eq('+621000000000')
expect(second_merchant.email).to eq('')
end
end

context 'when merchant fields are missing' do
it 'handles missing field values gracefully' do
partner_id = '739'
params = {}

stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants")
.to_return(status: 200, body: merchants_with_missing_fields_response.to_json)

merchant_api = described_class.new(client)
response = merchant_api.get(params, partner_id)

expect(response).to be_instance_of MidtransApi::Model::Merchant::Get
expect(response.merchants).to be_an(Array)
expect(response.merchants.length).to eq(2)

merchant_list = response.merchant_list
expect(merchant_list.length).to eq(2)

first_merchant = merchant_list.first
expect(first_merchant.merchant_id).to eq('G221991147')
expect(first_merchant.merchant_name).to be_nil
expect(first_merchant.merchant_phone_number).to be_nil
expect(first_merchant.email).to eq('dhany+39457_79@mekari.com')

second_merchant = merchant_list.last
expect(second_merchant.merchant_id).to be_nil
expect(second_merchant.merchant_name).to eq('Test Merchant')
expect(second_merchant.merchant_phone_number).to eq('+621000000000')
expect(second_merchant.email).to be_nil
end
end
end
end