|
| 1 | +package com.easypost.service; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.UUID; |
| 6 | + |
| 7 | +import com.easypost.exception.EasyPostException; |
| 8 | +import com.easypost.http.Requestor; |
| 9 | +import com.easypost.http.Requestor.RequestMethod; |
| 10 | +import com.easypost.model.FedExAccountValidationResponse; |
| 11 | +import com.easypost.model.FedExRequestPinResponse; |
| 12 | + |
| 13 | +public class FedExRegistrationService { |
| 14 | + private final EasyPostClient client; |
| 15 | + |
| 16 | + /** |
| 17 | + * FedExRegistrationService constructor. |
| 18 | + * |
| 19 | + * @param client The client object. |
| 20 | + */ |
| 21 | + FedExRegistrationService(EasyPostClient client) { |
| 22 | + this.client = client; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Register the billing address for a FedEx account. |
| 27 | + * Advanced method for custom parameter structures. |
| 28 | + * |
| 29 | + * @param fedexAccountNumber The FedEx account number. |
| 30 | + * @param params Map of parameters. |
| 31 | + * @return FedExAccountValidationResponse object with next steps (PIN or invoice |
| 32 | + * validation). |
| 33 | + * @throws EasyPostException when the request fails. |
| 34 | + */ |
| 35 | + public FedExAccountValidationResponse registerAddress(final String fedexAccountNumber, |
| 36 | + final Map<String, Object> params) throws EasyPostException { |
| 37 | + Map<String, Object> wrappedParams = wrapAddressValidation(params); |
| 38 | + String endpoint = String.format("fedex_registrations/%s/address", fedexAccountNumber); |
| 39 | + |
| 40 | + return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, FedExAccountValidationResponse.class, |
| 41 | + client); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Request a PIN for FedEx account verification. |
| 46 | + * |
| 47 | + * @param fedexAccountNumber The FedEx account number. |
| 48 | + * @param pinMethodOption The PIN delivery method: "SMS", "CALL", or "EMAIL". |
| 49 | + * @return FedExRequestPinResponse object confirming PIN was sent. |
| 50 | + * @throws EasyPostException when the request fails. |
| 51 | + */ |
| 52 | + public FedExRequestPinResponse requestPin(final String fedexAccountNumber, final String pinMethodOption) |
| 53 | + throws EasyPostException { |
| 54 | + Map<String, Object> wrappedParams = new HashMap<>(); |
| 55 | + Map<String, Object> pinMethodMap = new HashMap<>(); |
| 56 | + pinMethodMap.put("option", pinMethodOption); |
| 57 | + wrappedParams.put("pin_method", pinMethodMap); |
| 58 | + String endpoint = String.format("fedex_registrations/%s/pin", fedexAccountNumber); |
| 59 | + |
| 60 | + return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, FedExRequestPinResponse.class, client); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Validate the PIN entered by the user for FedEx account verification. |
| 65 | + * |
| 66 | + * @param fedexAccountNumber The FedEx account number. |
| 67 | + * @param params Map of parameters. |
| 68 | + * @return FedExAccountValidationResponse object. |
| 69 | + * @throws EasyPostException when the request fails. |
| 70 | + */ |
| 71 | + public FedExAccountValidationResponse validatePin(final String fedexAccountNumber, final Map<String, Object> params) |
| 72 | + throws EasyPostException { |
| 73 | + Map<String, Object> wrappedParams = wrapPinValidation(params); |
| 74 | + String endpoint = String.format("fedex_registrations/%s/pin/validate", fedexAccountNumber); |
| 75 | + |
| 76 | + return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, FedExAccountValidationResponse.class, |
| 77 | + client); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Submit invoice information to complete FedEx account registration. |
| 82 | + * |
| 83 | + * @param fedexAccountNumber The FedEx account number. |
| 84 | + * @param params Map of parameters. |
| 85 | + * @return FedExAccountValidationResponse object. |
| 86 | + * @throws EasyPostException when the request fails. |
| 87 | + */ |
| 88 | + public FedExAccountValidationResponse submitInvoice(final String fedexAccountNumber, |
| 89 | + final Map<String, Object> params) |
| 90 | + throws EasyPostException { |
| 91 | + Map<String, Object> wrappedParams = wrapInvoiceValidation(params); |
| 92 | + String endpoint = String.format("fedex_registrations/%s/invoice", fedexAccountNumber); |
| 93 | + |
| 94 | + return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, FedExAccountValidationResponse.class, |
| 95 | + client); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Wraps address validation parameters and ensures the "name" field exists. |
| 100 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 101 | + * |
| 102 | + * @param params The original parameters map. |
| 103 | + * @return A new map with properly wrapped address_validation and |
| 104 | + * easypost_details. |
| 105 | + */ |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + private Map<String, Object> wrapAddressValidation(final Map<String, Object> params) { |
| 108 | + Map<String, Object> wrappedParams = new HashMap<>(); |
| 109 | + |
| 110 | + if (params.containsKey("address_validation")) { |
| 111 | + Map<String, Object> addressValidation = new HashMap<>( |
| 112 | + (Map<String, Object>) params.get("address_validation")); |
| 113 | + ensureNameField(addressValidation); |
| 114 | + wrappedParams.put("address_validation", addressValidation); |
| 115 | + } |
| 116 | + |
| 117 | + if (params.containsKey("easypost_details")) { |
| 118 | + wrappedParams.put("easypost_details", params.get("easypost_details")); |
| 119 | + } |
| 120 | + |
| 121 | + return wrappedParams; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Wraps PIN validation parameters and ensures the "name" field exists. |
| 126 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 127 | + * |
| 128 | + * @param params The original parameters map. |
| 129 | + * @return A new map with properly wrapped pin_validation and easypost_details. |
| 130 | + */ |
| 131 | + @SuppressWarnings("unchecked") |
| 132 | + private Map<String, Object> wrapPinValidation(final Map<String, Object> params) { |
| 133 | + Map<String, Object> wrappedParams = new HashMap<>(); |
| 134 | + |
| 135 | + if (params.containsKey("pin_validation")) { |
| 136 | + Map<String, Object> pinValidation = new HashMap<>( |
| 137 | + (Map<String, Object>) params.get("pin_validation")); |
| 138 | + ensureNameField(pinValidation); |
| 139 | + wrappedParams.put("pin_validation", pinValidation); |
| 140 | + } |
| 141 | + |
| 142 | + if (params.containsKey("easypost_details")) { |
| 143 | + wrappedParams.put("easypost_details", params.get("easypost_details")); |
| 144 | + } |
| 145 | + |
| 146 | + return wrappedParams; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Wraps invoice validation parameters and ensures the "name" field exists. |
| 151 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 152 | + * |
| 153 | + * @param params The original parameters map. |
| 154 | + * @return A new map with properly wrapped invoice_validation and |
| 155 | + * easypost_details. |
| 156 | + */ |
| 157 | + @SuppressWarnings("unchecked") |
| 158 | + private Map<String, Object> wrapInvoiceValidation(final Map<String, Object> params) { |
| 159 | + Map<String, Object> wrappedParams = new HashMap<>(); |
| 160 | + |
| 161 | + if (params.containsKey("invoice_validation")) { |
| 162 | + Map<String, Object> invoiceValidation = new HashMap<>( |
| 163 | + (Map<String, Object>) params.get("invoice_validation")); |
| 164 | + ensureNameField(invoiceValidation); |
| 165 | + wrappedParams.put("invoice_validation", invoiceValidation); |
| 166 | + } |
| 167 | + |
| 168 | + if (params.containsKey("easypost_details")) { |
| 169 | + wrappedParams.put("easypost_details", params.get("easypost_details")); |
| 170 | + } |
| 171 | + |
| 172 | + return wrappedParams; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Ensures the "name" field exists in the provided map. |
| 177 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 178 | + * This follows the pattern used in the web UI implementation. |
| 179 | + * |
| 180 | + * @param map The map to ensure the "name" field in. |
| 181 | + */ |
| 182 | + private void ensureNameField(final Map<String, Object> map) { |
| 183 | + if (!map.containsKey("name") || map.get("name") == null) { |
| 184 | + String uuid = UUID.randomUUID().toString().replace("-", ""); |
| 185 | + map.put("name", uuid); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments