/
Merchant.java
346 lines (305 loc) · 9.45 KB
/
Merchant.java
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (C) 2016 Clover Network, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.clover.sdk.v1.merchant;
import android.accounts.Account;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
/**
* A class representing a merchant. Instances of this class are immutable.
*/
public class Merchant implements Parcelable {
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_CURRENCY = "defaultCurrency";
private static final String KEY_TIMEZONE = "timeZone";
private static final String KEY_ACCOUNT = "account";
private static final String KEY_DEVICE_ID = "deviceId";
private static final String KEY_PHONE_NUMBER = "phoneNumber";
private static final String KEY_WEBSITE = "website";
private static final String KEY_MERCHANT_GATEWAY = "merchantGateway";
private static final String KEY_MID = "mid";
private static final String KEY_IS_VAT = "isVat";
private static final String KEY_SUPPORT_PHONE = "supportPhone";
private static final String KEY_SUPPORT_EMAIL = "supportEmail";
private static final String KEY_LOCALE = "locale";
private static final String KEY_UPDATE_STOCK = "updateStock";
private static final String KEY_TRACK_STOCK = "trackStock";
private static final String KEY_PAID_APPS_FREE = "paidAppsFree";
private static final String KEY_APP_BILLING_SYSTEM = "appBillingSystem";
private static final String KEY_ABA_ACCOUNT_NUMBER = "abaAccountNumber";
private static final String KEY_DDA_ACCOUNT_NUMBER = "ddaAccountNumber";
private static final String KEY_TIP_SUGGESTIONS = "tipSuggestions";
private static final String KEY_MODULES = "modules";
private static final String KEY_IS_BILLABLE = "isBillable";
private static final String KEY_MERCHANT_PLAN_ID = "merchantPlanId";
private final Bundle data;
public Merchant(Bundle data, Bundle localData) {
this.data = data;
this.data.putAll(localData);
}
public Merchant(Parcel in) {
this.data = in.readBundle();
}
/**
* Get the merchant ID.
*/
public String getId() {
return data.getString(KEY_ID, null);
}
/**
* Get the merchant name.
*/
public String getName() {
return data.getString(KEY_NAME, null);
}
/**
* Get the merchant currency.
*/
public Currency getCurrency() {
String code = data.getString(KEY_CURRENCY, "USD");
return Currency.getInstance(code);
}
/**
* Get the merchant time zone.
*/
public TimeZone getTimeZone() {
String id = data.getString(KEY_TIMEZONE, "USD");
return TimeZone.getTimeZone(id);
}
/**
* Get the merchant account.
*/
public Account getAccount() {
return data.getParcelable(KEY_ACCOUNT);
}
/**
* Get the merchant address.
*/
public MerchantAddress getAddress() {
return new MerchantAddress(data);
}
/**
* Get the device ID.
*/
public String getDeviceId() {
return data.getString(KEY_DEVICE_ID, null);
}
/**
* Get the merchant phone number.
*/
public String getPhoneNumber() {
return data.getString(KEY_PHONE_NUMBER, null);
}
/**
* Get the merchant website.
*/
public String getWebsite() {
return data.getString(KEY_WEBSITE, null);
}
/**
* Get the merchant MID
*/
public String getMid() {
String gateway = data.getString(KEY_MERCHANT_GATEWAY, null);
if (gateway != null) {
try {
JSONObject gateWayObj = new JSONObject(gateway);
return gateWayObj.has(KEY_MID) ? gateWayObj.getString(KEY_MID) : null;
} catch (JSONException ex) {
return null;
}
}
return null;
}
/**
* Get the modules this particular merchant currently has enabled.
*
* @see com.clover.sdk.v1.merchant.Module
*/
public List<Module> getModules() {
String moduleJson = data.getString(KEY_MODULES, null);
if (moduleJson != null) {
try {
JSONArray moduleObj = new JSONArray(moduleJson);
if (moduleObj != null) {
List<Module> modules = new ArrayList<Module>();
for (int i = 0; i < moduleObj.length(); i++) {
try {
modules.add(Module.valueOf(moduleObj.getString(i).toUpperCase()));
} catch (IllegalArgumentException e) {
Log.e(Merchant.class.getName(), "Module name " + moduleObj.getString(i) + " is not defined in Module enum");
}
}
return modules;
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
return Arrays.asList(Module.values());
}
/**
* @return return merchant gateway closingTime property
*/
public String getGatewayClosingTime() {
String gateway = data.getString(KEY_MERCHANT_GATEWAY, null);
if (gateway != null) {
try {
JSONObject gateWayObj = new JSONObject(gateway);
return gateWayObj.optString("closingTime", null);
} catch (Exception ex) {
return null;
}
}
return null;
}
/**
* @return merchant gateway batchCloseEnabled property
*/
public Boolean getNewBatchCloseEnabled() {
String gateway = data.getString(KEY_MERCHANT_GATEWAY, null);
if (gateway != null) {
try {
JSONObject gateWayObj = new JSONObject(gateway);
if (gateWayObj.has("newBatchCloseEnabled")) {
return gateWayObj.optBoolean("newBatchCloseEnabled", false);
} else {
return null;
}
} catch (Exception ex) {
return null;
}
}
return null;
}
/**
* Returns whether this merchant is in a region using VAT
*/
public boolean isVat() {
return data.getBoolean(KEY_IS_VAT, false);
}
/**
* Get the support phone number.
*/
public String getSupportPhone() {
return data.getString(KEY_SUPPORT_PHONE, null);
}
/**
* Get the support email.
*/
public String getSupportEmail() {
return data.getString(KEY_SUPPORT_EMAIL, null);
}
/**
* Get the locale.
*/
public Locale getLocale() {
String rawLocale = data.getString(KEY_LOCALE, null);
if (!TextUtils.isEmpty(rawLocale)) {
String tokens[] = rawLocale.split("[-_]");
if (tokens.length == 0) {
return Locale.getDefault();
} else if (tokens.length == 1) {
return new Locale(tokens[0]);
} else {
return new Locale(tokens[0], tokens[1]);
}
}
return Locale.getDefault();
}
/**
* Returns whether this merchant is using Clover to update stock
*/
public boolean isUpdateStockEnabled() {
return data.getBoolean(KEY_UPDATE_STOCK, false);
}
/**
* Returns whether this merchant is using Clover to track stock
*/
public boolean isTrackStockEnabled() {
return data.getBoolean(KEY_TRACK_STOCK, false);
}
/**
* Get paid apps free
*/
public boolean getPaidAppsFree() {
return data.getBoolean(KEY_PAID_APPS_FREE, false);
}
public String getAppBillingSystem() {
return data.getString(KEY_APP_BILLING_SYSTEM, null);
}
public String getAbaAccountNumber() {
return data.getString(KEY_ABA_ACCOUNT_NUMBER, null);
}
public String getDdaAccountNumber() {
return data.getString(KEY_DDA_ACCOUNT_NUMBER, null);
}
public Boolean getIsBillable() {
return data.getBoolean(KEY_IS_BILLABLE);
}
public String getMerchantPlanId() {
return data.getString(KEY_MERCHANT_PLAN_ID, null);
}
public List<TipSuggestion> getTipSuggestions() {
List<TipSuggestion> suggestions = new ArrayList<TipSuggestion>();
String result = data.getString(KEY_TIP_SUGGESTIONS);
if (result != null) {
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
TipSuggestion suggestion = new TipSuggestion(object);
suggestions.add(suggestion);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return suggestions;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeBundle(data);
}
public static final Parcelable.Creator<Merchant> CREATOR = new Parcelable.Creator<Merchant>() {
public Merchant createFromParcel(Parcel in) {
return new Merchant(in);
}
public Merchant[] newArray(int size) {
return new Merchant[size];
}
};
@Override
public String toString() {
return String.format(Locale.US, "%s{id=%s, name=%s, currency=%s, timeZone=%s (%s), account=%s, address=%s, deviceId=%s, phoneNumber=%s}", getClass().getSimpleName(), getId(), getName(), getCurrency(), getTimeZone().getDisplayName(), getTimeZone().getID(), getAccount().toString(), getAddress().toString(), getDeviceId(), getPhoneNumber());
}
}