Skip to content

Commit af3d9e0

Browse files
authored
[fix] Instance usage of get_payment_method_info in billing service (#296)
1 parent a8b21fc commit af3d9e0

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Fix issues funding wallet due to invalid internal function call
6+
37
## v6.1.0 (2024-01-08)
48

59
- Add `all_children` and `get_next_page_of_children` in `user` service

lib/easypost/services/billing.rb

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,9 @@
33
require 'easypost/constants'
44

55
class EasyPost::Services::Billing < EasyPost::Services::Service
6-
# Get payment method info (type of the payment method and ID of the payment method)
7-
def self.get_payment_method_info(priority)
8-
payment_methods = EasyPost::Services::Billing.retrieve_payment_methods
9-
payment_method_map = {
10-
'primary' => 'primary_payment_method',
11-
'secondary' => 'secondary_payment_method',
12-
}
13-
14-
payment_method_to_use = payment_method_map[priority]
15-
16-
error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
17-
suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
18-
if payment_methods[payment_method_to_use].nil?
19-
raise EasyPost::Errors::InvalidParameterError.new(
20-
error_string,
21-
suggestion,
22-
)
23-
end
24-
25-
payment_method_id = payment_methods[payment_method_to_use]['id']
26-
27-
unless payment_method_id.nil?
28-
if payment_method_id.start_with?('card_')
29-
endpoint = '/v2/credit_cards'
30-
elsif payment_method_id.start_with?('bank_')
31-
endpoint = '/v2/bank_accounts'
32-
else
33-
raise EasyPost::Errors::InvalidObjectError.new(error_string)
34-
end
35-
end
36-
37-
[endpoint, payment_method_id]
38-
end
39-
406
# Fund your EasyPost wallet by charging your primary or secondary card on file.
417
def fund_wallet(amount, priority = 'primary')
42-
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
8+
payment_info = get_payment_method_info(priority.downcase)
439
endpoint = payment_info[0]
4410
payment_id = payment_info[1]
4511

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

5319
# Delete a payment method.
5420
def delete_payment_method(priority)
55-
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
21+
payment_info = get_payment_method_info(priority.downcase)
5622
endpoint = payment_info[0]
5723
payment_id = payment_info[1]
5824

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

6531
# Retrieve all payment methods.
6632
def retrieve_payment_methods
67-
response = @client.make_request(:get, '/v2/payment_methods')
33+
response = @client.make_request(:get, '/payment_methods')
6834
payment_methods = EasyPost::InternalUtilities::Json.convert_json_to_object(response)
6935

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

7440
payment_methods
7541
end
42+
43+
private
44+
45+
# Get payment method info (type of the payment method and ID of the payment method)
46+
def get_payment_method_info(priority)
47+
payment_methods = retrieve_payment_methods
48+
payment_method_map = {
49+
'primary' => 'primary_payment_method',
50+
'secondary' => 'secondary_payment_method',
51+
}
52+
53+
payment_method_to_use = payment_method_map[priority]
54+
55+
error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
56+
suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
57+
if payment_methods[payment_method_to_use].nil?
58+
raise EasyPost::Errors::InvalidParameterError.new(
59+
error_string,
60+
suggestion,
61+
)
62+
end
63+
64+
payment_method_id = payment_methods[payment_method_to_use]['id']
65+
66+
unless payment_method_id.nil?
67+
if payment_method_id.start_with?('card_')
68+
endpoint = '/credit_cards'
69+
elsif payment_method_id.start_with?('bank_')
70+
endpoint = '/bank_accounts'
71+
else
72+
raise EasyPost::Errors::InvalidObjectError.new(error_string)
73+
end
74+
end
75+
76+
[endpoint, payment_method_id]
77+
end
7678
end

spec/billing_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
describe '.fund_wallet' do
99
it 'fund wallet by using a payment method' do
10-
allow(described_class).to receive(:get_payment_method_info).and_return(['/credit_cards', 'cc_123'])
11-
allow(client).to receive(:make_request).with(
12-
:post, '/credit_cards/cc_123/charges', { amount: '2000' },
13-
)
14-
10+
allow(client).to receive(:make_request).with(:get, '/payment_methods')
11+
.and_return({
12+
'id' => 'cust_thisisdummydata',
13+
'object' => 'PaymentMethods', 'primary_payment_method' =>
14+
{ 'id' => 'card_123', 'object' => 'CreditCard' },
15+
},
16+
)
17+
allow(client).to receive(:make_request).with(:post, '/credit_cards/card_123/charges', { amount: '2000' })
1518
credit_card = client.billing.fund_wallet('2000', 'primary')
1619

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

2124
describe '.delete_payment_method' do
2225
it 'delete a payment method' do
23-
allow(described_class).to receive(:get_payment_method_info).and_return(['/credit_cards', 'cc_123'])
24-
allow(client).to receive(:make_request).with(
25-
:delete, '/credit_cards/cc_123',
26-
)
26+
allow(client).to receive(:make_request).with(:get, '/payment_methods')
27+
.and_return({
28+
'id' => 'cust_thisisdummydata',
29+
'object' => 'PaymentMethods', 'primary_payment_method' =>
30+
{ 'id' => 'card_123', 'object' => 'CreditCard' },
31+
},
32+
)
33+
allow(client).to receive(:make_request).with(:delete, '/credit_cards/card_123')
2734

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

0 commit comments

Comments
 (0)