Skip to content

Commit

Permalink
[fix] Instance usage of get_payment_method_info in billing service (#296
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nwithan8 authored Jan 23, 2024
1 parent a8b21fc commit af3d9e0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Fix issues funding wallet due to invalid internal function call

## v6.1.0 (2024-01-08)

- Add `all_children` and `get_next_page_of_children` in `user` service
Expand Down
76 changes: 39 additions & 37 deletions lib/easypost/services/billing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,9 @@
require 'easypost/constants'

class EasyPost::Services::Billing < EasyPost::Services::Service
# Get payment method info (type of the payment method and ID of the payment method)
def self.get_payment_method_info(priority)
payment_methods = EasyPost::Services::Billing.retrieve_payment_methods
payment_method_map = {
'primary' => 'primary_payment_method',
'secondary' => 'secondary_payment_method',
}

payment_method_to_use = payment_method_map[priority]

error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
if payment_methods[payment_method_to_use].nil?
raise EasyPost::Errors::InvalidParameterError.new(
error_string,
suggestion,
)
end

payment_method_id = payment_methods[payment_method_to_use]['id']

unless payment_method_id.nil?
if payment_method_id.start_with?('card_')
endpoint = '/v2/credit_cards'
elsif payment_method_id.start_with?('bank_')
endpoint = '/v2/bank_accounts'
else
raise EasyPost::Errors::InvalidObjectError.new(error_string)
end
end

[endpoint, payment_method_id]
end

# Fund your EasyPost wallet by charging your primary or secondary card on file.
def fund_wallet(amount, priority = 'primary')
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
payment_info = get_payment_method_info(priority.downcase)
endpoint = payment_info[0]
payment_id = payment_info[1]

Expand All @@ -52,7 +18,7 @@ def fund_wallet(amount, priority = 'primary')

# Delete a payment method.
def delete_payment_method(priority)
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
payment_info = get_payment_method_info(priority.downcase)
endpoint = payment_info[0]
payment_id = payment_info[1]

Expand All @@ -64,7 +30,7 @@ def delete_payment_method(priority)

# Retrieve all payment methods.
def retrieve_payment_methods
response = @client.make_request(:get, '/v2/payment_methods')
response = @client.make_request(:get, '/payment_methods')
payment_methods = EasyPost::InternalUtilities::Json.convert_json_to_object(response)

if payment_methods['id'].nil?
Expand All @@ -73,4 +39,40 @@ def retrieve_payment_methods

payment_methods
end

private

# Get payment method info (type of the payment method and ID of the payment method)
def get_payment_method_info(priority)
payment_methods = retrieve_payment_methods
payment_method_map = {
'primary' => 'primary_payment_method',
'secondary' => 'secondary_payment_method',
}

payment_method_to_use = payment_method_map[priority]

error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
if payment_methods[payment_method_to_use].nil?
raise EasyPost::Errors::InvalidParameterError.new(
error_string,
suggestion,
)
end

payment_method_id = payment_methods[payment_method_to_use]['id']

unless payment_method_id.nil?
if payment_method_id.start_with?('card_')
endpoint = '/credit_cards'
elsif payment_method_id.start_with?('bank_')
endpoint = '/bank_accounts'
else
raise EasyPost::Errors::InvalidObjectError.new(error_string)
end
end

[endpoint, payment_method_id]
end
end
25 changes: 16 additions & 9 deletions spec/billing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

describe '.fund_wallet' do
it 'fund wallet by using a payment method' do
allow(described_class).to receive(:get_payment_method_info).and_return(['/credit_cards', 'cc_123'])
allow(client).to receive(:make_request).with(
:post, '/credit_cards/cc_123/charges', { amount: '2000' },
)

allow(client).to receive(:make_request).with(:get, '/payment_methods')
.and_return({
'id' => 'cust_thisisdummydata',
'object' => 'PaymentMethods', 'primary_payment_method' =>
{ 'id' => 'card_123', 'object' => 'CreditCard' },
},
)
allow(client).to receive(:make_request).with(:post, '/credit_cards/card_123/charges', { amount: '2000' })
credit_card = client.billing.fund_wallet('2000', 'primary')

expect(credit_card).to eq(true)
Expand All @@ -20,10 +23,14 @@

describe '.delete_payment_method' do
it 'delete a payment method' do
allow(described_class).to receive(:get_payment_method_info).and_return(['/credit_cards', 'cc_123'])
allow(client).to receive(:make_request).with(
:delete, '/credit_cards/cc_123',
)
allow(client).to receive(:make_request).with(:get, '/payment_methods')
.and_return({
'id' => 'cust_thisisdummydata',
'object' => 'PaymentMethods', 'primary_payment_method' =>
{ 'id' => 'card_123', 'object' => 'CreditCard' },
},
)
allow(client).to receive(:make_request).with(:delete, '/credit_cards/card_123')

deleted_credit_card = client.billing.delete_payment_method('primary')

Expand Down

0 comments on commit af3d9e0

Please sign in to comment.