diff --git a/CHANGELOG.md b/CHANGELOG.md index 1338443b..269efc20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/easypost/services/billing.rb b/lib/easypost/services/billing.rb index 9845a564..3b4b2ad8 100644 --- a/lib/easypost/services/billing.rb +++ b/lib/easypost/services/billing.rb @@ -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] diff --git a/spec/billing_spec.rb b/spec/billing_spec.rb index 1a0f9467..244fb0c9 100644 --- a/spec/billing_spec.rb +++ b/spec/billing_spec.rb @@ -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) @@ -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')