Skip to content

Commit

Permalink
Merge pull request #50 from mekari-engineering/KRED-1648-find-cc-charge
Browse files Browse the repository at this point in the history
KRED-1648 find cc charge
  • Loading branch information
vincent-mekari authored Jan 14, 2025
2 parents 2630ad0 + 3e34f0c commit 23cb13f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/xendit_api/api/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def charge(params, headers = {})
XenditApi::Model::CreditCard.new(credit_card_params)
end

def find(id, headers = {})
def find(id, headers = {}, id_type = nil)
find_path = "#{PATH}/#{id}"
find_path = "#{find_path}?id_type=#{id_type}" unless id_type.nil?
response = client.get(find_path, nil, headers)
credit_card_params = permitted_credit_card_params(response)
XenditApi::Model::CreditCard.new(credit_card_params)
Expand Down
1 change: 1 addition & 0 deletions lib/xendit_api/errors/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module CreditCard
class ResponseError < XenditApi::Errors::ResponseError; end

class ChargeError < ResponseError; end
class CreditCardChargeNotFoundError < ResponseError; end

class CardDeclined < ResponseError
def message
Expand Down
2 changes: 2 additions & 0 deletions lib/xendit_api/middleware/handle_response_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def validate_response(response, env)
# credit cards
when 'INVALID_TOKEN_ID_ERROR'
raise XenditApi::Errors::CreditCard::ChargeError.new(error_message, json_response)
when 'CREDIT_CARD_CHARGE_NOT_FOUND_ERROR'
raise XenditApi::Errors::CreditCard::CreditCardChargeNotFoundError.new(error_message, json_response)
# disbursements
when 'DISBURSEMENT_DESCRIPTION_NOT_FOUND_ERROR'
raise XenditApi::Errors::Disbursement::DescriptionNotFound.new(error_message, json_response)
Expand Down
80 changes: 80 additions & 0 deletions spec/xendit_api/api/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,89 @@
end
end

describe '#find' do
it 'returns expected credit card model' do
api_find = described_class.new(client)
response = {
'status' => 'CAPTURED',
'authorized_amount' => 500_000,
'capture_amount' => 500_000,
'currency' => 'IDR',
'credit_card_token_id' => '600a56e7e4d6190020220bc4',
'business_id' => '596d988e56b5a3c45be75e6e',
'merchant_id' => 'xendit_ctv_agg',
'merchant_reference_code' => '600a56e6a1ba47c821f21830',
'external_id' => 'e9035d73-4069-4235-8eea-7dd0eb614595',
'eci' => '05',
'charge_type' => 'SINGLE_USE_TOKEN',
'masked_card_number' => '400000XXXXXX0002',
'card_brand' => 'VISA',
'card_type' => 'CREDIT',
'xid' => 'S1lNYXFSMjZWQ1lGV21JUzRRUjA=',
'cavv' => 'AAABAWFlmQAAAABjRWWZEEFgFz+=',
'descriptor' => 'XENDIT*JURNAL',
'authorization_id' => '600a56f7e4d6190020220bc6',
'bank_reconciliation_id' => '6112903593156915303012',
'cvn_code' => 'M',
'approval_code' => '831000',
'created' => '2021-01-22T04:39:20.001Z',
'id' => '600a56f8e4d6190020220bc7'
}
headers = {
'request-id' => '7262070017331640590'
}
id = '600a56f8e4d6190020220bc7'
stub_client_get_to(headers, response)
credit_card_response = api_find.find(id)

expect(credit_card_response).to be_instance_of XenditApi::Model::CreditCard
end

it 'returns expected credit card model with specified id type' do
api_find = described_class.new(client)
response = {
'status' => 'CAPTURED',
'authorized_amount' => 500_000,
'capture_amount' => 500_000,
'currency' => 'IDR',
'credit_card_token_id' => '600a56e7e4d6190020220bc4',
'business_id' => '596d988e56b5a3c45be75e6e',
'merchant_id' => 'xendit_ctv_agg',
'merchant_reference_code' => '600a56e6a1ba47c821f21830',
'external_id' => 'e9035d73-4069-4235-8eea-7dd0eb614595',
'eci' => '05',
'charge_type' => 'SINGLE_USE_TOKEN',
'masked_card_number' => '400000XXXXXX0002',
'card_brand' => 'VISA',
'card_type' => 'CREDIT',
'xid' => 'S1lNYXFSMjZWQ1lGV21JUzRRUjA=',
'cavv' => 'AAABAWFlmQAAAABjRWWZEEFgFz+=',
'descriptor' => 'XENDIT*JURNAL',
'authorization_id' => '600a56f7e4d6190020220bc6',
'bank_reconciliation_id' => '6112903593156915303012',
'cvn_code' => 'M',
'approval_code' => '831000',
'created' => '2021-01-22T04:39:20.001Z',
'id' => '600a56f8e4d6190020220bc7'
}
headers = {
'request-id' => '7262070017331640590'
}
id = '600a56f8e4d6190020220bc7'
stub_client_get_to(headers, response)
credit_card_response = api_find.find(id, {}, 'external')

expect(credit_card_response).to be_instance_of XenditApi::Model::CreditCard
end
end

private

def stub_client_post_to(headers, response)
allow_any_instance_of(Faraday::Connection).to receive(:post).and_return(OpenStruct.new(body: response, headers: headers))
end

def stub_client_get_to(headers, response)
allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(OpenStruct.new(body: response, headers: headers))
end
end
6 changes: 6 additions & 0 deletions spec/xendit_api/errors/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@
expect(described_class::InvalidCvn.superclass).to eq(XenditApi::Errors::CreditCard::ResponseError)
end
end

describe 'CreditCardChargeNotFoundError' do
it 'is expected parent class' do
expect(described_class::CreditCardChargeNotFoundError.superclass).to eq(XenditApi::Errors::CreditCard::ResponseError)
end
end
end

0 comments on commit 23cb13f

Please sign in to comment.