Skip to content

Commit

Permalink
[bug] Cannot determine payment method when funding/deleting (#337)
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 5e875bb commit e0e0cda
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 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
- Adds `refund` function in Insurance service for requesting a refund for a standalone insurance

## v7.1.0 (2024-01-08)
Expand Down
5 changes: 3 additions & 2 deletions lib/EasyPost/Service/BillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ private function getPaymentInfo(string $priority = 'primary'): array

if ($paymentMethodToUse != null && $paymentMethods->$paymentMethodToUse->id != null) {
$paymentMethodId = $paymentMethods->$paymentMethodToUse->id;
if (strpos($paymentMethodId, 'card_') === 0) {
$paymentMethodObjectType = $paymentMethods->$paymentMethodToUse->object;
if ($paymentMethodObjectType == 'CreditCard') {
$endpoint = '/credit_cards';
} else if (strpos($paymentMethodId, 'bank_') === 0) {
} else if ($paymentMethodObjectType == 'BankAccount') {
$endpoint = '/bank_accounts';
} else {
throw new PaymentException(Constants::INVALID_PAYMENT_METHOD_ERROR);
Expand Down
43 changes: 42 additions & 1 deletion test/EasyPost/BillingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function setUpBeforeClass(): void
),
new MockRequestResponseInfo(
200,
'{"id": "summary_123", "primary_payment_method": {"id": "card_123", "last4": "1234"}, "secondary_payment_method": {"id": "bank_123", "bank_name": "Mock Bank"}}' // phpcs:ignore
'{"id": "summary_123", "primary_payment_method": {"id": "pm_123", "object": "CreditCard", "last4": "1234"}, "secondary_payment_method": {"id": "pm_123", "object": "BankAccount", "bank_name": "Mock Bank"}}' // phpcs:ignore
)
),
);
Expand Down Expand Up @@ -272,4 +272,45 @@ public function testGetPaymentMethodByPriorityPaymentMethodNoId(): void
$this->assertTrue(true);
}
}

/**
* Test determining the payment method type by object type
*/
public function testGetPaymentMethodInfoByObjectType(): void
{
$mockingUtility = new MockingUtility(
new MockRequest(
new MockRequestMatchRule(
'get',
'/v2\\/payment_methods$/'
),
new MockRequestResponseInfo(
200,
'{"id": "summary_123", "primary_payment_method": {"id": "pm_123", "object": "CreditCard", "last4": "1234"}, "secondary_payment_method": {"id": "pm_123", "object": "BankAccount", "bank_name": "Mock Bank"}}' // phpcs:ignore
)
),
new MockRequest(
new MockRequestMatchRule(
'delete',
'/v2\\/credit_cards\\/pm_123$/'
),
new MockRequestResponseInfo(
200,
'{}'
)
),
);
$client = self::getClient($mockingUtility);

// getPaymentInfo is private, but we can test it by attempting to delete a payment method
// only a delete request to /v2/credit_cards/pm_123 is mocked
// if the delete function works, it's because it found the correct payment method type
try {
$client->billing->deletePaymentMethod('primary');

$this->assertTrue(true);
} catch (\Exception $exception) {
$this->fail('Exception thrown when we expected no error');
}
}
}

0 comments on commit e0e0cda

Please sign in to comment.