Skip to content

Commit c7346ba

Browse files
authored
Deprecate existing credit card functions, replaced with new billing functions (#157)
- Deprecate existing billing functions (including referral credit card creation) - Replace with newer billing functions with support for both bank account and credit cards - Delete old unit tests and cassettes - Note last working and removal versions for deprecated functions
1 parent 2f3878d commit c7346ba

18 files changed

+616
-213
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
- `getSmartrates()` -> `smartrates()`
88
- `getLowestSmartRate()` -> `findLowestSmartrate()`
99
- Adds OS specific details to the user-agent header
10+
- Adds `Billing.retrievePaymentMethods()`, `Billing.fundWallet()`, and `Billing.deletePaymentMethod()` functions
11+
- Deprecates some existing payment method and credit card functions and features (repleaced by new Billing functions):
12+
- Deprecates `CreditCard.fund()` and `CreditCard.delete()` functions
13+
- Deprecates `PaymentMethod.all()` function
14+
- Deprecates certain `PaymentMethod` object functions
1015

1116
## v5.5.0 (2022-06-21)
1217

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import com.easypost.net.EasyPostResource;
44

5+
/**
6+
* BaseCreditCard is a model class that represents the base of any credit card.
7+
*
8+
* @deprecated Use {@link com.easypost.model.PaymentMethodObject} instead.
9+
* Last working version: v5.5.0. Removal: v7.0.0.
10+
*/
11+
@Deprecated
512
public class BaseCreditCard extends EasyPostResource {
613
private String id;
714
private String object;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.easypost.model;
2+
3+
import com.easypost.EasyPost;
4+
import com.easypost.exception.EasyPostException;
5+
import com.easypost.net.EasyPostResource;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public final class Billing extends EasyPostResource {
11+
/**
12+
* Delete a payment method.
13+
*
14+
* @param priority Which type of payment method to delete.
15+
* @return True if successful. Throws an exception if unsuccessful.
16+
* @throws EasyPostException when the request fails.
17+
*/
18+
public static boolean deletePaymentMethod(PaymentMethod.Priority priority) throws EasyPostException {
19+
return deletePaymentMethod(priority, null);
20+
}
21+
22+
/**
23+
* Delete a payment method.
24+
*
25+
* @param priority Which type of payment method to delete.
26+
* @param apiKey API key to use in request (overrides default API key).
27+
* @return True if successful. Throws an exception if unsuccessful.
28+
* @throws EasyPostException when the request fails.
29+
*/
30+
public static boolean deletePaymentMethod(PaymentMethod.Priority priority, String apiKey) throws EasyPostException {
31+
PaymentMethodObject paymentMethodObject = getPaymentMethodByPriority(priority, apiKey);
32+
33+
// will attempt to serialize empty JSON to a PaymentMethod.class, that's fine
34+
request(EasyPostResource.RequestMethod.DELETE,
35+
String.format("%s/%s/%s", EasyPost.API_BASE, paymentMethodObject.getEndpoint(),
36+
paymentMethodObject.getId()), null, PaymentMethod.class, apiKey);
37+
38+
return true;
39+
}
40+
41+
/**
42+
* Fund your wallet from the primary payment method.
43+
*
44+
* @param amount amount to fund.
45+
* @return True if successful. Throws an exception if unsuccessful.
46+
* @throws EasyPostException when the request fails.
47+
*/
48+
public static boolean fundWallet(String amount) throws EasyPostException {
49+
return fundWallet(amount, PaymentMethod.Priority.PRIMARY, null);
50+
}
51+
52+
/**
53+
* Fund your wallet from a specific payment method.
54+
*
55+
* @param amount amount to fund.
56+
* @param priority which type of payment method to use to fund the wallet. Defaults to primary.
57+
* @return True if successful. Throws an exception if unsuccessful.
58+
* @throws EasyPostException when the request fails.
59+
*/
60+
public static boolean fundWallet(String amount, PaymentMethod.Priority priority) throws EasyPostException {
61+
return fundWallet(amount, priority, null);
62+
}
63+
64+
/**
65+
* Fund your wallet from a specific payment method.
66+
*
67+
* @param amount amount to fund.
68+
* @param priority which type of payment method to use to fund the wallet.
69+
* @param apiKey API key to use in request (overrides default API key).
70+
* @return True if successful. Throws an exception if unsuccessful.
71+
* @throws EasyPostException when the request fails.
72+
*/
73+
public static boolean fundWallet(String amount, PaymentMethod.Priority priority, String apiKey)
74+
throws EasyPostException {
75+
PaymentMethodObject paymentMethodObject = getPaymentMethodByPriority(priority, apiKey);
76+
77+
Map<String, Object> params = new HashMap<>();
78+
params.put("amount", amount);
79+
80+
// will attempt to serialize empty JSON to a PaymentMethod.class, that's fine
81+
request(RequestMethod.POST, String.format("%s/%s/%s/%s", EasyPost.API_BASE, paymentMethodObject.getEndpoint(),
82+
paymentMethodObject.getId(), "charges"), params, PaymentMethod.class, apiKey);
83+
84+
return true;
85+
}
86+
87+
/**
88+
* List all payment methods for this account.
89+
*
90+
* @return an EasyPost.PaymentMethod summary object.
91+
* @throws EasyPostException when the request fails or billing has not been set up.
92+
*/
93+
public static PaymentMethod retrievePaymentMethods() throws EasyPostException {
94+
return retrievePaymentMethods(null);
95+
}
96+
97+
/**
98+
* List all payment methods for this account.
99+
*
100+
* @param apiKey API key to use in request (overrides default API key).
101+
* @return an EasyPost.PaymentMethod summary object.
102+
* @throws EasyPostException when the request fails or billing has not been set up.
103+
*/
104+
public static PaymentMethod retrievePaymentMethods(String apiKey) throws EasyPostException {
105+
PaymentMethod response =
106+
request(RequestMethod.GET, String.format("%s/%s", EasyPost.API_BASE, "payment_methods"), null,
107+
PaymentMethod.class, apiKey);
108+
109+
if (response.getId() == null) {
110+
throw new EasyPostException("Billing has not been setup for this user. Please add a payment method.");
111+
}
112+
113+
return response;
114+
}
115+
116+
/**
117+
* Get a payment method by priority.
118+
*
119+
* @param priority which priority payment method to get.
120+
* @param apiKey API key to use in request (overrides default API key).
121+
* @return an EasyPost.PaymentMethodObject instance.
122+
* @throws EasyPostException when the request fails.
123+
*/
124+
private static PaymentMethodObject getPaymentMethodByPriority(PaymentMethod.Priority priority, String apiKey)
125+
throws EasyPostException {
126+
PaymentMethod paymentMethods = retrievePaymentMethods(apiKey);
127+
PaymentMethodObject paymentMethod = null;
128+
switch (priority) {
129+
case PRIMARY:
130+
paymentMethod = paymentMethods.getPrimaryPaymentMethodObject();
131+
break;
132+
case SECONDARY:
133+
paymentMethod = paymentMethods.getSecondaryPaymentMethodObject();
134+
break;
135+
default:
136+
break;
137+
}
138+
139+
if (paymentMethod == null || paymentMethod.getId() == null) {
140+
throw new EasyPostException("The chosen payment method is not set up yet.");
141+
}
142+
143+
return paymentMethod;
144+
}
145+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import java.util.HashMap;
77
import java.util.Map;
88

9+
/**
10+
* CreditCard is a model class that represents a credit card.
11+
*
12+
* @deprecated Use {@link com.easypost.model.PaymentMethodObject} instead.
13+
* Last working version: v5.5.0. Removal: v7.0.0.
14+
*/
15+
@Deprecated
916
public class CreditCard extends BaseCreditCard {
1017
/**
1118
* Fund your EasyPost wallet by charging your primary or secondary card on file.
@@ -14,7 +21,10 @@ public class CreditCard extends BaseCreditCard {
1421
* @param primaryOrSecondary primary or secondary payment method.
1522
* @return CreditCardFund object.
1623
* @throws EasyPostException when the request fails.
24+
* @deprecated Use {@link com.easypost.model.Billing#fundWallet(String, PaymentMethod.Priority)} instead.
25+
* Last working version: v5.5.0. Removal: v7.0.0.
1726
*/
27+
@Deprecated
1828
public static boolean fund(String amount, CreditCardPriority primaryOrSecondary) throws EasyPostException {
1929
return fund(amount, primaryOrSecondary, null);
2030
}
@@ -27,7 +37,10 @@ public static boolean fund(String amount, CreditCardPriority primaryOrSecondary)
2737
* @param apiKey API key to use in request (overrides default API key).
2838
* @return CreditCardFund object.
2939
* @throws EasyPostException when the request fails.
40+
* @deprecated Use {@link com.easypost.model.Billing#fundWallet(String, PaymentMethod.Priority, String)} instead.
41+
* Last working version: v5.5.0. Removal: v7.0.0.
3042
*/
43+
@Deprecated
3144
public static boolean fund(String amount, CreditCardPriority primaryOrSecondary, String apiKey)
3245
throws EasyPostException {
3346
PaymentMethod paymentMethods = PaymentMethod.all();
@@ -48,7 +61,7 @@ public static boolean fund(String amount, CreditCardPriority primaryOrSecondary,
4861
throw new EasyPostException("The chosen payment method is not a credit card. Please try again.");
4962
}
5063

51-
Map<String, Object> params = new HashMap<String, Object>();
64+
Map<String, Object> params = new HashMap<>();
5265
params.put("amount", amount);
5366

5467
// will attempt to serialize the empty response to a CreditCard object (doesn't matter)
@@ -63,7 +76,10 @@ public static boolean fund(String amount, CreditCardPriority primaryOrSecondary,
6376
*
6477
* @param creditCardId ID of credit card to delete.
6578
* @throws EasyPostException when the request fails.
79+
* @deprecated Use {@link com.easypost.model.Billing#deletePaymentMethod(PaymentMethod.Priority)} instead.
80+
* Last working version: v5.5.0. Removal: v7.0.0.
6681
*/
82+
@Deprecated
6783
public static void delete(String creditCardId) throws EasyPostException {
6884
delete(creditCardId, null);
6985
}
@@ -74,7 +90,10 @@ public static void delete(String creditCardId) throws EasyPostException {
7490
* @param creditCardId ID of credit card to delete.
7591
* @param apiKey API key to use in request (overrides default API key).
7692
* @throws EasyPostException when the request fails.
93+
* @deprecated Use {@link com.easypost.model.Billing#deletePaymentMethod(PaymentMethod.Priority, String)} instead.
94+
* Last working version: v5.5.0. Removal: v7.0.0.
7795
*/
96+
@Deprecated
7897
public static void delete(String creditCardId, String apiKey) throws EasyPostException {
7998
request(RequestMethod.DELETE, String.format("%s/%s/%s", EasyPost.API_BASE, "credit_cards", creditCardId), null,
8099
CreditCard.class, apiKey);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.easypost.model;
22

3+
/**
4+
* CreditCardPriority is an enum class that represents the payment method priority levels.
5+
*
6+
* @deprecated Use {@link com.easypost.model.PaymentMethod.Priority} instead.
7+
* Last working version: v5.5.0. Removal: v7.0.0.
8+
*/
9+
@Deprecated
310
public enum CreditCardPriority {
411
PRIMARY,
512
SECONDARY

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,49 @@
55
import com.easypost.net.EasyPostResource;
66

77
public class PaymentMethod extends EasyPostResource {
8-
private String id;
9-
private String object;
10-
private PrimaryPaymentMethod primaryPaymentMethod;
11-
private SecondaryPaymentMethod secondaryPaymentMethod;
128

13-
/**
14-
* Get the object of this PaymentMethod object.
15-
*
16-
* @return the object of this PaymentMethod.
17-
*/
18-
public String getObject() {
19-
return object;
9+
public enum Priority {
10+
PRIMARY,
11+
SECONDARY
2012
}
2113

14+
private String id;
15+
private String object;
16+
private PaymentMethodObject primaryPaymentMethod;
17+
private PaymentMethodObject secondaryPaymentMethod;
18+
2219
/**
2320
* Get the PrimaryPaymentMethod of this PaymentMethod object.
2421
*
2522
* @return the PrimaryPaymentMethod of this PaymentMethod.
23+
* @deprecated Use {@link #getPrimaryPaymentMethodObject()} instead. Last working version: v5.5.0. Removal: v7.0.0.
2624
*/
25+
@Deprecated
2726
public PrimaryPaymentMethod getPrimaryPaymentMethod() {
27+
PrimaryPaymentMethod primaryPaymentMethod = new PrimaryPaymentMethod();
28+
primaryPaymentMethod.setId(this.primaryPaymentMethod.getId());
29+
primaryPaymentMethod.setObject(this.primaryPaymentMethod.getObject());
30+
primaryPaymentMethod.setBrand(this.primaryPaymentMethod.getBrand());
31+
primaryPaymentMethod.setExpMonth(String.valueOf(this.primaryPaymentMethod.getExpMonth()));
32+
primaryPaymentMethod.setExpYear(String.valueOf(this.primaryPaymentMethod.getExpYear()));
2833
return primaryPaymentMethod;
2934
}
3035

3136
/**
3237
* Get the SecondaryPaymentMethod of this PaymentMethod object.
3338
*
3439
* @return the SecondaryPaymentMethod of this PaymentMethod.
40+
* @deprecated Use {@link #getSecondaryPaymentMethodObject()} instead.
41+
* Last working version: v5.5.0. Removal: v7.0.0.
3542
*/
43+
@Deprecated
3644
public SecondaryPaymentMethod getSecondaryPaymentMethod() {
45+
SecondaryPaymentMethod secondaryPaymentMethod = new SecondaryPaymentMethod();
46+
secondaryPaymentMethod.setId(this.secondaryPaymentMethod.getId());
47+
secondaryPaymentMethod.setObject(this.secondaryPaymentMethod.getObject());
48+
secondaryPaymentMethod.setBrand(this.secondaryPaymentMethod.getBrand());
49+
secondaryPaymentMethod.setExpMonth(String.valueOf(this.secondaryPaymentMethod.getExpMonth()));
50+
secondaryPaymentMethod.setExpYear(String.valueOf(this.secondaryPaymentMethod.getExpYear()));
3751
return secondaryPaymentMethod;
3852
}
3953

@@ -42,7 +56,10 @@ public SecondaryPaymentMethod getSecondaryPaymentMethod() {
4256
*
4357
* @return Billing object.
4458
* @throws EasyPostException when the request fails.
59+
* @deprecated Use {@link com.easypost.model.Billing#retrievePaymentMethods()} instead.
60+
* Last working version: v5.5.0. Removal: v7.0.0.
4561
*/
62+
@Deprecated
4663
public static PaymentMethod all() throws EasyPostException {
4764
return all(null);
4865
}
@@ -53,7 +70,10 @@ public static PaymentMethod all() throws EasyPostException {
5370
* @param apiKey API key to use in request (overrides default API key).
5471
* @return Billing object.
5572
* @throws EasyPostException when the request fails.
73+
* @deprecated Use {@link com.easypost.model.Billing#retrievePaymentMethods()} instead.
74+
* Last working version: v5.5.0. Removal: v7.0.0.
5675
*/
76+
@Deprecated
5777
public static PaymentMethod all(String apiKey) throws EasyPostException {
5878
PaymentMethod response =
5979
request(RequestMethod.GET, String.format("%s/%s", EasyPost.API_BASE, "payment_methods"), null,
@@ -66,6 +86,33 @@ public static PaymentMethod all(String apiKey) throws EasyPostException {
6686
return response;
6787
}
6888

89+
/**
90+
* Get the object of this PaymentMethod object.
91+
*
92+
* @return the object of this PaymentMethod.
93+
*/
94+
public String getObject() {
95+
return object;
96+
}
97+
98+
/**
99+
* Get the primary payment method of this PaymentMethod object.
100+
*
101+
* @return a PaymentMethodObject representing the primary payment method.
102+
*/
103+
public PaymentMethodObject getPrimaryPaymentMethodObject() {
104+
return primaryPaymentMethod;
105+
}
106+
107+
/**
108+
* Get the secondary payment method of this PaymentMethod object.
109+
*
110+
* @return a PaymentMethodObject representing the secondary payment method.
111+
*/
112+
public PaymentMethodObject getSecondaryPaymentMethodObject() {
113+
return secondaryPaymentMethod;
114+
}
115+
69116
/**
70117
* Get ID of this PaymentMethod object.
71118
*

0 commit comments

Comments
 (0)