Skip to content

Commit

Permalink
[bug] Cannot determine payment method when funding/deleting (#444)
Browse files Browse the repository at this point in the history
- Fallback to `object` key to determine payment method type
- Add unit test for payment type determination
  • Loading branch information
nwithan8 authored Apr 8, 2024
1 parent 304c546 commit 614e4ca
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 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 payment method funding and deletion failures due to undetermined payment method type

## v7.2.0 (2024-01-22)

- Adds missing exports to Typescript definitions (ApiKey, Billing, Brand, EndShipper, Fee, PaymentMethod, Rate, Refund) - closes #433
Expand Down
9 changes: 7 additions & 2 deletions src/services/billing_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ export default (easypostClient) =>

const paymentMethodToUse = paymentMethodMap[priority];
let paymentMethodID;
let paymentMethodObjectType;
let endpoint;
const errorString = 'The chosen payment method is not valid. Please try again.';

if (paymentMethodToUse !== undefined && paymentMethods[paymentMethodToUse] !== null) {
paymentMethodID = paymentMethods[paymentMethodToUse].id;
if (paymentMethodID.startsWith('card_')) {
paymentMethodObjectType = paymentMethods[paymentMethodToUse].object;
if (paymentMethodID.startsWith('card_') || paymentMethodObjectType === 'CreditCard') {
endpoint = 'credit_cards';
} else if (paymentMethodID.startsWith('bank_')) {
} else if (
paymentMethodID.startsWith('bank_') ||
paymentMethodObjectType === 'BankAccount'
) {
endpoint = 'bank_accounts';
} else {
throw new InvalidObjectError({ message: errorString });
Expand Down
49 changes: 47 additions & 2 deletions test/services/billing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ const middleware = (request) => {
new MockRequestResponseInfo(200, {
id: 'summary_123',
primary_payment_method: {
id: 'card_123',
id: 'pm_123',
last4: '1234',
object: 'CreditCard',
},
secondary_payment_method: {
id: 'bank_123',
id: 'pm_123',
bank_name: 'Mock Bank',
object: 'BankAccount',
},
}),
),
Expand Down Expand Up @@ -69,4 +71,47 @@ describe('Billing Service', function () {
expect(paymentMethods.primary_payment_method).to.exist;
expect(paymentMethods.secondary_payment_method).to.exist;
});

it('get payment info', async function () {
const paymentInfo = await this.client.Billing._getPaymentInfo('primary');

expect(paymentInfo).to.deep.equal(['credit_cards', 'pm_123']);

const paymentInfo2 = await this.client.Billing._getPaymentInfo('secondary');

expect(paymentInfo2).to.deep.equal(['bank_accounts', 'pm_123']);
});

it('get payment info with legacy prefix', async function () {
this.client = new EasyPostClient(process.env.EASYPOST_TEST_API_KEY, {
requestMiddleware: (request) => {
return new MockMiddleware(request, [
new MockRequest(
new MockRequestMatchRule('GET', 'v2\\/payment_methods$'),
new MockRequestResponseInfo(200, {
id: 'summary_123',
primary_payment_method: {
id: 'card_123',
last4: '1234',
// No object field, force use of id prefix to determine type
},
secondary_payment_method: {
id: 'bank_123',
bank_name: 'Mock Bank',
// No object field, force use of id prefix to determine type
},
}),
),
]);
},
});

const paymentInfo = await this.client.Billing._getPaymentInfo('primary');

expect(paymentInfo).to.deep.equal(['credit_cards', 'card_123']);

const paymentInfo2 = await this.client.Billing._getPaymentInfo('secondary');

expect(paymentInfo2).to.deep.equal(['bank_accounts', 'bank_123']);
});
});

0 comments on commit 614e4ca

Please sign in to comment.