Skip to content

Commit 9ea0bce

Browse files
authored
[bug] Cannot determine payment method when funding/deleting (#309)
- Use payment method object type as fallback when determining type for fund, delete functions
1 parent 2d8b5f3 commit 9ea0bce

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# CHANGELOG
22

3-
## Next release
3+
## Next Release
44

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

78
## v7.1.1 (2024-03-21)

src/main/java/com/easypost/model/PaymentMethodObject.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ public PaymentMethodType getType() {
5959
if (getId() == null) {
6060
return null;
6161
}
62-
if (getId().startsWith("card_")) {
62+
String objectType = getObject();
63+
if (getId().startsWith("card_") || (objectType != null && objectType.equals("CreditCard"))) {
6364
type = PaymentMethodType.CREDIT_CARD;
64-
} else if (getId().startsWith("bank_")) {
65+
} else if (getId().startsWith("bank_") || (objectType != null && objectType.equals("BankAccount"))) {
6566
type = PaymentMethodType.BANK_ACCOUNT;
6667
}
6768
return type;

src/test/java/com/easypost/BillingTest.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,29 @@
1212
import org.mockito.Mockito;
1313

1414
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1516
import static org.junit.jupiter.api.Assertions.assertNotNull;
17+
import static org.junit.jupiter.api.Assertions.assertNull;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1619

1720
public final class BillingTest {
1821
private static TestUtils.VCR vcr;
1922
private String jsonResponse = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" +
20-
"payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"na" +
23+
"payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"na" +
2124
"me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" +
22-
"y_payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"name\":nu" +
25+
"y_payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"BankAccount\",\"name\":nu" +
2326
"ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
2427
private PaymentMethod paymentMethod = Constants.Http.GSON.fromJson(jsonResponse, PaymentMethod.class);
28+
29+
private String jsonResponseLegacyPrefixes = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" +
30+
"payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":null,\"na" +
31+
"me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" +
32+
"y_payment_method\":{\"id\":\"bank_...\",\"disabled_at\":null,\"object\":null,\"name\":nu" +
33+
"ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
34+
35+
private PaymentMethod paymentMethodLegacyPrefixes =
36+
Constants.Http.GSON.fromJson(jsonResponseLegacyPrefixes, PaymentMethod.class);
37+
2538
private static MockedStatic<Requestor> requestMock = Mockito.mockStatic(Requestor.class);
2639

2740
/**
@@ -100,4 +113,56 @@ public void testRetrievePaymentMethods() throws EasyPostException {
100113
assertNotNull(paymentMethods.getPrimaryPaymentMethod());
101114
assertNotNull(paymentMethods.getSecondaryPaymentMethod());
102115
}
116+
117+
/**
118+
* Test determining a payment method type by its object type.
119+
*
120+
* @throws EasyPostException when the request fails.
121+
*/
122+
@Test
123+
public void testDeterminePaymentMethodTypeByObjectType() throws EasyPostException {
124+
requestMock.when(() -> Requestor.request(
125+
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
126+
.thenReturn(paymentMethod);
127+
128+
// Should be a credit card with "CreditCard" object type and "pm_" prefix
129+
PaymentMethodObject creditCard =
130+
vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod();
131+
assertTrue(creditCard.getId().startsWith("pm_"));
132+
assertEquals("CreditCard", creditCard.getObject());
133+
assertEquals(PaymentMethodObject.PaymentMethodType.CREDIT_CARD, creditCard.getType());
134+
135+
// Should be a bank account with "BankAccount" object type and "pm_" prefix
136+
PaymentMethodObject bankAccount =
137+
vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod();
138+
assertTrue(bankAccount.getId().startsWith("pm_"));
139+
assertEquals("BankAccount", bankAccount.getObject());
140+
assertEquals(PaymentMethodObject.PaymentMethodType.BANK_ACCOUNT, bankAccount.getType());
141+
}
142+
143+
/**
144+
* Test determining a payment method type by its legacy prefix.
145+
*
146+
* @throws EasyPostException when the request fails.
147+
*/
148+
@Test
149+
public void testDeterminePaymentMethodTypeByLegacyPrefix() throws EasyPostException {
150+
requestMock.when(() -> Requestor.request(
151+
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
152+
.thenReturn(paymentMethodLegacyPrefixes);
153+
154+
// Should be a credit card with null object type and "card_" prefix
155+
PaymentMethodObject creditCard =
156+
vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod();
157+
assertTrue(creditCard.getId().startsWith("card_"));
158+
assertNull(creditCard.getObject());
159+
assertEquals(PaymentMethodObject.PaymentMethodType.CREDIT_CARD, creditCard.getType());
160+
161+
// Should be a bank account with null object type and "bank_" prefix
162+
PaymentMethodObject bankAccount =
163+
vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod();
164+
assertTrue(bankAccount.getId().startsWith("bank_"));
165+
assertNull(bankAccount.getObject());
166+
assertEquals(PaymentMethodObject.PaymentMethodType.BANK_ACCOUNT, bankAccount.getType());
167+
}
103168
}

0 commit comments

Comments
 (0)