|
| 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 | +} |
0 commit comments