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