Skip to content

Commit c318446

Browse files
authored
Move Partner to GA (#184)
- Copy Referral classes to GA, remove deprecated functions that don't need to carry over - Replace beta endpoints with GA endpoints - Mark beta Referral classes as deprecated (NOTE: When deleting, can probably delete deprecated CreditCard classes too) - Copy Referral unit tests to GA, keep beta unit tests as well - Add missing README.md info - Add missing Partner key to TestUtils
1 parent 9f6562d commit c318446

File tree

18 files changed

+861
-56
lines changed

18 files changed

+861
-56
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ The following are required on every test run:
129129
- `EASYPOST_TEST_API_KEY`
130130
- `EASYPOST_PROD_API_KEY`
131131

132-
The following are required when you need to re-record cassettes for applicable tests (fallback values are used otherwise):
132+
Some tests may require an EasyPost user with a particular set of enabled features such as a `Partner` user when creating referrals. We have attempted to call out these functions in their respective docstrings. The following are required when you need to re-record cassettes for applicable tests:
133133

134134
- `USPS_CARRIER_ACCOUNT_ID` (eg: one-call buying a shipment for non-EasyPost employees)
135+
- `PARTNER_USER_PROD_API_KEY` (eg: creating a referral user)
135136
- `REFERRAL_USER_PROD_API_KEY` (eg: adding a credit card to a referral user)
136-
137-
Some tests may require a user with a particular set of enabled features such as a `Partner` user when creating referrals. We have attempted to call out these functions in their respective docstrings.

src/main/java-templates/com/easypost/EasyPost.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* <p>
2626
* If you ever run across a section of code where your IDE says it cannot find, i.e. `EasyPost.apiKey`,
2727
* it's likely because this file has not been compiled yet.
28-
* Simply run `mvn install` (or `make build-dev`) to compile this code and your IDE will be able to find the variable.
28+
* Simply run `make build` to compile this code and your IDE will be able to find the variable.
2929
*/
3030

3131
public abstract class EasyPost {
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
package com.easypost.model;
2+
3+
import com.easypost.EasyPost;
4+
import com.easypost.exception.EasyPostException;
5+
6+
import java.io.BufferedReader;
7+
import java.io.DataOutputStream;
8+
import java.io.InputStreamReader;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
public class ReferralCustomer extends BaseUser {
17+
private List<ApiKey> apiKeys;
18+
19+
/**
20+
* Get the api keys of the Referral user.
21+
*
22+
* @return the api keys of the Referral user.
23+
*/
24+
public List<ApiKey> getApiKeys() {
25+
return apiKeys;
26+
}
27+
28+
/**
29+
* Set the api keys of the Referral user.
30+
*
31+
* @param apiKeys the api keys of the Referral user.
32+
*/
33+
public void setApiKeys(List<ApiKey> apiKeys) {
34+
this.apiKeys = apiKeys;
35+
}
36+
37+
/**
38+
* Create a Referral object from parameter map. This function requires the Partner User's API key.
39+
*
40+
* @param params Map of the referral user parameters.
41+
* @return Referral object.
42+
* @throws EasyPostException when the request fails.
43+
*/
44+
public static ReferralCustomer create(Map<String, Object> params) throws EasyPostException {
45+
return create(params, null);
46+
}
47+
48+
/**
49+
* Create a Referral Customer object from parameter map. This function requires the Partner User's API key.
50+
*
51+
* @param params Map of the referral user parameters.
52+
* @param apiKey API key to use in request (overrides default API key).
53+
* @return Referral object.
54+
* @throws EasyPostException when the request fails.
55+
*/
56+
public static ReferralCustomer create(Map<String, Object> params, String apiKey) throws EasyPostException {
57+
Map<String, Object> wrappedParams = new HashMap<>();
58+
wrappedParams.put("user", params);
59+
60+
return request(RequestMethod.POST, String.format("%s/%s", EasyPost.API_BASE, "referral_customers"),
61+
wrappedParams, ReferralCustomer.class, apiKey);
62+
}
63+
64+
/**
65+
* Update a Referral object email. This function requires the Partner User's API key.
66+
*
67+
* @param email Email of the referral user to update.
68+
* @param userId ID of the referral user to update.
69+
* @return true if success.
70+
* @throws EasyPostException when the request fails.
71+
*/
72+
public static boolean updateEmail(String email, String userId) throws EasyPostException {
73+
return updateEmail(email, userId, null);
74+
}
75+
76+
/**
77+
* Update a Referral object email. This function requires the Partner User's API key.
78+
*
79+
* @param email Email of the referral user to update.
80+
* @param userId ID of the referral user to update.
81+
* @param apiKey API key to use in request (overrides default API key).
82+
* @return true if success.
83+
* @throws EasyPostException when the request fails.
84+
*/
85+
public static boolean updateEmail(String email, String userId, String apiKey) throws EasyPostException {
86+
Map<String, Object> wrappedParams = new HashMap<>();
87+
Map<String, Object> params = new HashMap<>();
88+
params.put("email", email);
89+
wrappedParams.put("user", params);
90+
91+
request(RequestMethod.PUT, String.format("%s/%s/%s", EasyPost.API_BASE, "referral_customers", userId),
92+
wrappedParams, ReferralCustomer.class, apiKey);
93+
94+
return true;
95+
}
96+
97+
/**
98+
* List all Referral objects. This function requires the Partner User's API key.
99+
*
100+
* @param params Map of parameters.
101+
* @return List<Referral> object.
102+
* @throws EasyPostException when the request fails.
103+
*/
104+
public static ReferralCustomerCollection all(final Map<String, Object> params) throws EasyPostException {
105+
return all(params, null);
106+
}
107+
108+
/**
109+
* List all Referral objects. This function requires the Partner User's API key.
110+
*
111+
* @param params Map of parameters.
112+
* @param apiKey API key to use in request (overrides default API key).
113+
* @return ReferralCustomerCollection object.
114+
* @throws EasyPostException when the request fails.
115+
*/
116+
public static ReferralCustomerCollection all(final Map<String, Object> params, String apiKey)
117+
throws EasyPostException {
118+
return request(RequestMethod.GET, String.format("%s/%s", EasyPost.API_BASE, "referral_customers"),
119+
params, ReferralCustomerCollection.class, apiKey);
120+
}
121+
122+
/**
123+
* Add credit card to a referral user. This function requires the Referral User's API key.
124+
*
125+
* @param referralApiKey API key of the referral user.
126+
* @param number Credit card number.
127+
* @param expirationMonth Expiration month of the credit card.
128+
* @param expirationYear Expiration year of the credit card.
129+
* @param cvc CVC of the credit card.
130+
* @return PaymentMethodObject object.
131+
* @throws Exception when the request fails.
132+
*/
133+
public static PaymentMethodObject addCreditCardToUser(String referralApiKey, String number, int expirationMonth,
134+
int expirationYear, String cvc) throws Exception {
135+
return addCreditCardToUser(referralApiKey, number, expirationMonth, expirationYear, cvc,
136+
PaymentMethod.Priority.PRIMARY);
137+
}
138+
139+
/**
140+
* Add credit card to a referral user. This function requires the Referral User's API key.
141+
*
142+
* @param referralApiKey API key of the referral user.
143+
* @param number Credit card number.
144+
* @param expirationMonth Expiration month of the credit card.
145+
* @param expirationYear Expiration year of the credit card.
146+
* @param cvc CVC of the credit card.
147+
* @param priority Priority of this credit card.
148+
* @return PaymentMethodObject object.
149+
* @throws Exception when the request fails.
150+
*/
151+
public static PaymentMethodObject addCreditCardToUser(String referralApiKey, String number, int expirationMonth,
152+
int expirationYear, String cvc,
153+
PaymentMethod.Priority priority) throws Exception {
154+
String easypostStripeApiKey = retrieveEasypostStripeApiKey();
155+
String stripeToken;
156+
157+
try {
158+
stripeToken = createStripeToken(number, expirationMonth, expirationYear, cvc, easypostStripeApiKey);
159+
} catch (Exception e) {
160+
throw new Exception("Could not send card details to Stripe, please try again later", e);
161+
}
162+
163+
return createEasypostCreditCard(referralApiKey, stripeToken, priority.toString().toLowerCase());
164+
}
165+
166+
/**
167+
* Retrieve EasyPost Stripe API key.
168+
*
169+
* @return EasyPost Stripe API key.
170+
* @throws EasyPostException when the request fails.
171+
*/
172+
private static String retrieveEasypostStripeApiKey() throws EasyPostException {
173+
@SuppressWarnings ("unchecked") Map<String, String> response =
174+
request(RequestMethod.GET, String.format("%s/%s", EasyPost.API_BASE, "partners/stripe_public_key"),
175+
null, Map.class, null);
176+
177+
return response.getOrDefault("public_key", "");
178+
}
179+
180+
/**
181+
* Get credit card token from Stripe.
182+
*
183+
* @param number Credit card number.
184+
* @param expirationMonth Expiration month of the credit card.
185+
* @param expirationYear Expiration year of the credit card.
186+
* @param cvc CVC of the credit card.
187+
* @param easypostStripeApiKey EasyPost Stripe API key.
188+
* @return Stripe token.
189+
* @throws Exception when the request fails.
190+
*/
191+
private static String createStripeToken(String number, int expirationMonth, int expirationYear, String cvc,
192+
String easypostStripeApiKey) throws Exception {
193+
Map<String, String> params = new HashMap<>();
194+
params.put("number", number);
195+
params.put("exp_month", String.valueOf(expirationMonth));
196+
params.put("exp_year", String.valueOf(expirationYear));
197+
params.put("cvc", cvc);
198+
199+
URL stripeUrl = new URL("https://api.stripe.com/v1/tokens");
200+
HttpURLConnection conn = (HttpURLConnection) stripeUrl.openConnection();
201+
conn.setRequestMethod("POST");
202+
conn.setRequestProperty("Authorization", String.format("%s %s", "Bearer", easypostStripeApiKey));
203+
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
204+
conn.setDoOutput(true);
205+
206+
String encodedURL = Utilities.getEncodedURL(params, "card");
207+
byte[] postData = encodedURL.getBytes(StandardCharsets.UTF_8);
208+
209+
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
210+
wr.write(postData);
211+
}
212+
213+
StringBuilder response;
214+
215+
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
216+
217+
String line;
218+
response = new StringBuilder();
219+
220+
while ((line = br.readLine()) != null) {
221+
response.append(line);
222+
response.append(System.lineSeparator());
223+
}
224+
br.close();
225+
} finally {
226+
conn.disconnect();
227+
}
228+
229+
String responseBody = response.toString();
230+
231+
@SuppressWarnings ("unchecked") Map<String, Object> responseMap = GSON.fromJson(responseBody, Map.class);
232+
233+
return responseMap.get("id").toString();
234+
}
235+
236+
/**
237+
* Submit Stripe credit card token to EasyPost.
238+
*
239+
* @param referralApiKey API key of the referral user.
240+
* @param stripeObjectId Stripe token.
241+
* @param priority Credit card priority.
242+
* @return CreditCard object.
243+
* @throws EasyPostException when the request fails.
244+
*/
245+
private static PaymentMethodObject createEasypostCreditCard(String referralApiKey, String stripeObjectId,
246+
String priority) throws EasyPostException {
247+
Map<String, Object> params = new HashMap<>();
248+
params.put("stripe_object_id", stripeObjectId);
249+
params.put("priority", priority);
250+
251+
Map<String, Object> wrappedParams = new HashMap<>();
252+
wrappedParams.put("credit_card", params);
253+
254+
return request(RequestMethod.POST, String.format("%s/%s", EasyPost.API_BASE, "credit_cards"),
255+
wrappedParams, PaymentMethodObject.class, referralApiKey);
256+
}
257+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.easypost.model;
2+
3+
import com.easypost.net.EasyPostResource;
4+
5+
import java.util.List;
6+
7+
public final class ReferralCustomerCollection extends EasyPostResource {
8+
private List<ReferralCustomer> referralCustomers;
9+
private boolean hasMore;
10+
11+
/**
12+
* Get a list of ReferralCustomers.
13+
*
14+
* @return List of ReferralCustomers objects
15+
*/
16+
public List<ReferralCustomer> getReferralCustomers() {
17+
return referralCustomers;
18+
}
19+
20+
/**
21+
* Set a list of ReferralCustomers.
22+
*
23+
* @param referralCustomers List of ReferralCustomers objects
24+
*/
25+
public void setReferralCustomers(final List<ReferralCustomer> referralCustomers) {
26+
this.referralCustomers = referralCustomers;
27+
}
28+
29+
/**
30+
* Get whether there are more ReferralCustomers to retrieve.
31+
*
32+
* @return whether there are more ReferralCustomers to retrieve
33+
*/
34+
public boolean getHasMore() {
35+
return hasMore;
36+
}
37+
38+
/**
39+
* Set whether there are more ReferralCustomers to retrieve.
40+
*
41+
* @param hasMore Boolean whether there are more ReferralCustomers to retrieve
42+
*/
43+
public void setHasMore(final boolean hasMore) {
44+
this.hasMore = hasMore;
45+
}
46+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* CreditCard is a model class that represents a credit card.
77
*
88
* @deprecated Use {@link com.easypost.model.PaymentMethodObject} instead.
9-
* Deprecated: v5.5.0 - v7.0.0
109
*/
1110
@Deprecated
1211
public class CreditCard extends BaseCreditCard {}

src/main/java/com/easypost/model/beta/EndShipper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
/**
1313
* @deprecated Use {@link com.easypost.model.EndShipper} instead.
14-
* Deprecated: v5.9.0 - v7.0.0
1514
*/
1615
public final class EndShipper extends BaseAddress {
1716
/**

src/main/java/com/easypost/model/beta/ReferralCustomer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22+
/**
23+
* @deprecated Use {@link com.easypost.model.ReferralCustomer} instead.
24+
*/
2225
public class ReferralCustomer extends BaseUser {
2326
private List<ApiKey> apiKeys;
2427

@@ -136,7 +139,6 @@ public static ReferralCustomerCollection all(final Map<String, Object> params, S
136139
* @return CreditCard object.
137140
* @throws Exception when the request fails.
138141
* @deprecated Use {@link #addCreditCardToUser(String, String, int, int, String)} instead.
139-
* Deprecated: v5.5.0 - v7.0.0
140142
*/
141143
@Deprecated
142144
public static CreditCard addCreditCard(String referralApiKey, String number, int expirationMonth,
@@ -173,7 +175,6 @@ public static PaymentMethodObject addCreditCardToUser(String referralApiKey, Str
173175
* @return CreditCard object.
174176
* @throws Exception when the request fails.
175177
* @deprecated Use {@link #addCreditCardToUser(String, String, int, int, String, PaymentMethod.Priority)} instead.
176-
* Deprecated: v5.5.0 - v7.0.0
177178
*/
178179
public static CreditCard addCreditCard(String referralApiKey, String number, int expirationMonth,
179180
int expirationYear, String cvc, CreditCardPriority priority)

src/main/java/com/easypost/model/beta/ReferralCustomerCollection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import java.util.List;
66

7+
/**
8+
* @deprecated Use {@link com.easypost.model.ReferralCustomerCollection} instead.
9+
*/
710
public final class ReferralCustomerCollection extends EasyPostResource {
811
private List<ReferralCustomer> referralCustomers;
912
private boolean hasMore;

0 commit comments

Comments
 (0)