Skip to content

Commit

Permalink
[bug] Cannot determine payment method when funding/deleting (#300)
Browse files Browse the repository at this point in the history
- Use payment method object type to determine payment method type
  • Loading branch information
nwithan8 authored Apr 10, 2024
1 parent 1490eae commit 80ce497
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

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

## v6.1.1 (2024-01-23)
Expand Down
16 changes: 8 additions & 8 deletions lib/easypost/services/billing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def get_payment_method_info(priority)
end

payment_method_id = payment_methods[payment_method_to_use]['id']
payment_method_object_type = payment_methods[payment_method_to_use]['object']

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
if payment_method_object_type == 'CreditCard'

endpoint = '/credit_cards'
elsif payment_method_object_type == 'BankAccount'
endpoint = '/bank_accounts'
else
raise EasyPost::Errors::InvalidObjectError.new(error_string)
end

[endpoint, payment_method_id]
Expand Down
31 changes: 27 additions & 4 deletions spec/billing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
.and_return({
'id' => 'cust_thisisdummydata',
'object' => 'PaymentMethods', 'primary_payment_method' =>
{ 'id' => 'card_123', 'object' => 'CreditCard' },
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
},
)
allow(client).to receive(:make_request).with(:post, '/credit_cards/card_123/charges', { amount: '2000' })
allow(client).to receive(:make_request).with(:post, '/credit_cards/pm_123/charges', { amount: '2000' })
credit_card = client.billing.fund_wallet('2000', 'primary')

expect(credit_card).to eq(true)
Expand All @@ -27,10 +27,33 @@
.and_return({
'id' => 'cust_thisisdummydata',
'object' => 'PaymentMethods', 'primary_payment_method' =>
{ 'id' => 'card_123', 'object' => 'CreditCard' },
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
},
)
allow(client).to receive(:make_request).with(:delete, '/credit_cards/card_123')
allow(client).to receive(:make_request).with(:delete, '/credit_cards/pm_123')

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

expect(deleted_credit_card).to eq(true)
end
end

describe '.get_payment_method_info' do
it 'get payment method type by object type' do
allow(client).to receive(:make_request).with(:get, '/payment_methods')
.and_return({
'id' => 'cust_thisisdummydata',
'object' => 'PaymentMethods',
'primary_payment_method' =>
{ 'id' => 'pm_123', 'object' => 'CreditCard' },
'secondary_payment_method' =>
{ 'id' => 'pm_456', 'object' => 'BankAccount' },
},
)

# get_payment_method_info is private, can test it via delete
# will pass if get_payment_method_info returns ['credit_cards', 'pm_123'], fail otherwise
allow(client).to receive(:make_request).with(:delete, '/credit_cards/pm_123')

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

Expand Down

0 comments on commit 80ce497

Please sign in to comment.