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