|
| 1 | +package com.easypost.service; |
| 2 | + |
| 3 | +import com.easypost.exception.EasyPostException; |
| 4 | +import com.easypost.http.Requestor; |
| 5 | +import com.easypost.http.Requestor.RequestMethod; |
| 6 | +import com.easypost.model.FedexRegistration; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.UUID; |
| 11 | + |
| 12 | +public class FedexRegistrationService { |
| 13 | + private final EasyPostClient client; |
| 14 | + |
| 15 | + /** |
| 16 | + * FedexRegistrationService constructor. |
| 17 | + * |
| 18 | + * @param client The client object. |
| 19 | + */ |
| 20 | + FedexRegistrationService(EasyPostClient client) { |
| 21 | + this.client = client; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Register the billing address for a FedEx account. |
| 26 | + * |
| 27 | + * @param fedexAccountNumber The FedEx account number. |
| 28 | + * @param params Map of parameters for address registration. |
| 29 | + * If params does not contain "name", a UUID will be auto-generated. |
| 30 | + * Optional: "easypost_details" object for additional metadata like |
| 31 | + * "reference" or "description". |
| 32 | + * @return FedexRegistration object. |
| 33 | + * @throws EasyPostException when the request fails. |
| 34 | + */ |
| 35 | + public FedexRegistration registerAddress(final String fedexAccountNumber, final Map<String, Object> params) |
| 36 | + throws EasyPostException { |
| 37 | + Map<String, Object> processedParams = ensureNameParameter(params); |
| 38 | + String endpoint = String.format("fedex_registrations/%s/address", fedexAccountNumber); |
| 39 | + |
| 40 | + return Requestor.request(RequestMethod.POST, endpoint, processedParams, FedexRegistration.class, client); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Request a PIN for FedEx account verification. |
| 45 | + * |
| 46 | + * @param fedexAccountNumber The FedEx account number. |
| 47 | + * @param params Map of parameters for PIN request. |
| 48 | + * Required: "pin_method" - one of "SMS", "CALL", or "EMAIL". |
| 49 | + * If params does not contain "name", a UUID will be auto-generated. |
| 50 | + * Optional: "easypost_details" object for additional metadata. |
| 51 | + * @return FedexRegistration object. |
| 52 | + * @throws EasyPostException when the request fails. |
| 53 | + */ |
| 54 | + public FedexRegistration requestPin(final String fedexAccountNumber, final Map<String, Object> params) |
| 55 | + throws EasyPostException { |
| 56 | + Map<String, Object> processedParams = ensureNameParameter(params); |
| 57 | + String endpoint = String.format("fedex_registrations/%s/pin", fedexAccountNumber); |
| 58 | + |
| 59 | + return Requestor.request(RequestMethod.POST, endpoint, processedParams, FedexRegistration.class, client); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Validate the PIN entered by the user for FedEx account verification. |
| 64 | + * |
| 65 | + * @param fedexAccountNumber The FedEx account number. |
| 66 | + * @param params Map of parameters for PIN validation. |
| 67 | + * Required: PIN value provided by the user. |
| 68 | + * If params does not contain "name", a UUID will be auto-generated. |
| 69 | + * Optional: "easypost_details" object for additional metadata. |
| 70 | + * @return FedexRegistration object. |
| 71 | + * @throws EasyPostException when the request fails. |
| 72 | + */ |
| 73 | + public FedexRegistration validatePin(final String fedexAccountNumber, final Map<String, Object> params) |
| 74 | + throws EasyPostException { |
| 75 | + Map<String, Object> processedParams = ensureNameParameter(params); |
| 76 | + String endpoint = String.format("fedex_registrations/%s/pin/validate", fedexAccountNumber); |
| 77 | + |
| 78 | + return Requestor.request(RequestMethod.POST, endpoint, processedParams, FedexRegistration.class, client); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Submit invoice information to complete FedEx account registration. |
| 83 | + * |
| 84 | + * @param fedexAccountNumber The FedEx account number. |
| 85 | + * @param params Map of parameters for invoice submission. |
| 86 | + * Required: Invoice information. |
| 87 | + * If params does not contain "name", a UUID will be auto-generated. |
| 88 | + * Optional: "easypost_details" object for additional metadata. |
| 89 | + * @return FedexRegistration object. |
| 90 | + * @throws EasyPostException when the request fails. |
| 91 | + */ |
| 92 | + public FedexRegistration submitInvoice(final String fedexAccountNumber, final Map<String, Object> params) |
| 93 | + throws EasyPostException { |
| 94 | + Map<String, Object> processedParams = ensureNameParameter(params); |
| 95 | + String endpoint = String.format("fedex_registrations/%s/invoice", fedexAccountNumber); |
| 96 | + |
| 97 | + return Requestor.request(RequestMethod.POST, endpoint, processedParams, FedexRegistration.class, client); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Ensures the "name" parameter exists in the params map. |
| 102 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 103 | + * This follows the pattern used in the web UI implementation. |
| 104 | + * |
| 105 | + * @param params The original parameters map. |
| 106 | + * @return A new map with the "name" parameter ensured. |
| 107 | + */ |
| 108 | + private Map<String, Object> ensureNameParameter(final Map<String, Object> params) { |
| 109 | + Map<String, Object> processedParams = new HashMap<>(params); |
| 110 | + if (!processedParams.containsKey("name") || processedParams.get("name") == null) { |
| 111 | + String uuid = UUID.randomUUID().toString().replace("-", ""); |
| 112 | + processedParams.put("name", uuid); |
| 113 | + } |
| 114 | + return processedParams; |
| 115 | + } |
| 116 | +} |
0 commit comments