Skip to content

Commit 80ce497

Browse files
authored
[bug] Cannot determine payment method when funding/deleting (#300)
- Use payment method object type to determine payment method type
1 parent 1490eae commit 80ce497

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next release
44

5+
- Fix payment method funding and deletion failures due to undetermined payment method type
56
- Add `refund` function in Insurance service for requesting a refund for standalone insurance.
67

78
## v6.1.1 (2024-01-23)

lib/easypost/services/billing.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ def get_payment_method_info(priority)
6262
end
6363

6464
payment_method_id = payment_methods[payment_method_to_use]['id']
65+
payment_method_object_type = payment_methods[payment_method_to_use]['object']
6566

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
67+
if payment_method_object_type == 'CreditCard'
68+
69+
endpoint = '/credit_cards'
70+
elsif payment_method_object_type == 'BankAccount'
71+
endpoint = '/bank_accounts'
72+
else
73+
raise EasyPost::Errors::InvalidObjectError.new(error_string)
7474
end
7575

7676
[endpoint, payment_method_id]

spec/billing_spec.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
.and_return({
1212
'id' => 'cust_thisisdummydata',
1313
'object' => 'PaymentMethods', 'primary_payment_method' =>
14-
{ 'id' => 'card_123', 'object' => 'CreditCard' },
14+
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
1515
},
1616
)
17-
allow(client).to receive(:make_request).with(:post, '/credit_cards/card_123/charges', { amount: '2000' })
17+
allow(client).to receive(:make_request).with(:post, '/credit_cards/pm_123/charges', { amount: '2000' })
1818
credit_card = client.billing.fund_wallet('2000', 'primary')
1919

2020
expect(credit_card).to eq(true)
@@ -27,10 +27,33 @@
2727
.and_return({
2828
'id' => 'cust_thisisdummydata',
2929
'object' => 'PaymentMethods', 'primary_payment_method' =>
30-
{ 'id' => 'card_123', 'object' => 'CreditCard' },
30+
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
3131
},
3232
)
33-
allow(client).to receive(:make_request).with(:delete, '/credit_cards/card_123')
33+
allow(client).to receive(:make_request).with(:delete, '/credit_cards/pm_123')
34+
35+
deleted_credit_card = client.billing.delete_payment_method('primary')
36+
37+
expect(deleted_credit_card).to eq(true)
38+
end
39+
end
40+
41+
describe '.get_payment_method_info' do
42+
it 'get payment method type by object type' do
43+
allow(client).to receive(:make_request).with(:get, '/payment_methods')
44+
.and_return({
45+
'id' => 'cust_thisisdummydata',
46+
'object' => 'PaymentMethods',
47+
'primary_payment_method' =>
48+
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
49+
'secondary_payment_method' =>
50+
{ 'id' => 'pm_456', 'object' => 'BankAccount' },
51+
},
52+
)
53+
54+
# get_payment_method_info is private, can test it via delete
55+
# will pass if get_payment_method_info returns ['credit_cards', 'pm_123'], fail otherwise
56+
allow(client).to receive(:make_request).with(:delete, '/credit_cards/pm_123')
3457

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

0 commit comments

Comments
 (0)