-
-
Notifications
You must be signed in to change notification settings - Fork 744
/
PaymentMethodJson.java
308 lines (265 loc) · 11.9 KB
/
PaymentMethodJson.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
/*
* Copyright 2010-2013 Ning, Inc.
* Copyright 2014-2015 Groupon, Inc
* Copyright 2014-2015 The Billing Project, LLC
*
* The Billing Project licenses this file to you 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:
*
* http://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 org.killbill.billing.jaxrs.json;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.payment.api.PaymentMethod;
import org.killbill.billing.payment.api.PaymentMethodPlugin;
import org.killbill.billing.payment.api.PluginProperty;
import org.killbill.billing.util.audit.AccountAuditLogs;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.wordnik.swagger.annotations.ApiModelProperty;
public class PaymentMethodJson extends JsonBase {
private final String externalKey;
@ApiModelProperty(dataType = "java.util.UUID")
private final String paymentMethodId;
@ApiModelProperty(dataType = "java.util.UUID")
private final String accountId;
private final Boolean isDefault;
private final String pluginName;
private final PaymentMethodPluginDetailJson pluginInfo;
@JsonCreator
public PaymentMethodJson(@JsonProperty("paymentMethodId") final String paymentMethodId,
@JsonProperty("externalKey") final String externalKey,
@JsonProperty("accountId") final String accountId,
@JsonProperty("isDefault") final Boolean isDefault,
@JsonProperty("pluginName") final String pluginName,
@JsonProperty("pluginInfo") @Nullable final PaymentMethodPluginDetailJson pluginInfo,
@JsonProperty("auditLogs") @Nullable final List<AuditLogJson> auditLogs) {
super(auditLogs);
this.externalKey = externalKey;
this.paymentMethodId = paymentMethodId;
this.accountId = accountId;
this.isDefault = isDefault;
this.pluginName = pluginName;
this.pluginInfo = pluginInfo;
}
public static PaymentMethodJson toPaymentMethodJson(final Account account, final PaymentMethod in, @Nullable final AccountAuditLogs accountAuditLogs) {
final boolean isDefault = account.getPaymentMethodId() != null && account.getPaymentMethodId().equals(in.getId());
final PaymentMethodPlugin pluginDetail = in.getPluginDetail();
PaymentMethodPluginDetailJson pluginDetailJson = null;
if (pluginDetail != null) {
List<PluginPropertyJson> properties = null;
if (pluginDetail.getProperties() != null) {
properties = new ArrayList<PluginPropertyJson>(Collections2.transform(pluginDetail.getProperties(), new Function<PluginProperty, PluginPropertyJson>() {
@Override
public PluginPropertyJson apply(final PluginProperty input) {
return new PluginPropertyJson(input.getKey(), input.getValue() == null ? null : input.getValue().toString(), input.getIsUpdatable());
}
}));
}
pluginDetailJson = new PaymentMethodPluginDetailJson(pluginDetail.getExternalPaymentMethodId(),
pluginDetail.isDefaultPaymentMethod(),
properties);
}
return new PaymentMethodJson(in.getId().toString(), in.getExternalKey(), account.getId().toString(), isDefault, in.getPluginName(),
pluginDetailJson, toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForPaymentMethod(in.getId())));
}
public PaymentMethod toPaymentMethod(final String accountId) {
return new PaymentMethod() {
@Override
public Boolean isActive() {
return true;
}
@Override
public String getPluginName() {
return pluginName;
}
@Override
public UUID getId() {
return paymentMethodId != null ? UUID.fromString(paymentMethodId) : null;
}
@Override
public String getExternalKey() {
return externalKey;
}
@Override
public DateTime getCreatedDate() {
return null;
}
@Override
public DateTime getUpdatedDate() {
return null;
}
@Override
public UUID getAccountId() {
return UUID.fromString(accountId);
}
@Override
public PaymentMethodPlugin getPluginDetail() {
return new PaymentMethodPlugin() {
@Override
public UUID getKbPaymentMethodId() {
return paymentMethodId == null ? null : UUID.fromString(paymentMethodId);
}
@Override
public boolean isDefaultPaymentMethod() {
// N/A
return false;
}
@Override
public String getExternalPaymentMethodId() {
return pluginInfo != null ? pluginInfo.getExternalPaymentMethodId() : null;
}
@Override
public List<PluginProperty> getProperties() {
if (pluginInfo != null && pluginInfo.getProperties() != null) {
final List<PluginProperty> result = new LinkedList<PluginProperty>();
for (final PluginPropertyJson cur : pluginInfo.getProperties()) {
result.add(new PluginProperty(cur.getKey(), cur.getValue(), cur.getIsUpdatable()));
}
return result;
}
return null;
}
};
}
};
}
public String getPaymentMethodId() {
return paymentMethodId;
}
public String getAccountId() {
return accountId;
}
@JsonProperty("isDefault")
public Boolean isDefault() {
return isDefault;
}
public String getPluginName() {
return pluginName;
}
public PaymentMethodPluginDetailJson getPluginInfo() {
return pluginInfo;
}
public String getExternalKey() {
return externalKey;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PaymentMethodJson{");
sb.append("paymentMethodId='").append(paymentMethodId).append('\'');
sb.append(", accountId='").append(accountId).append('\'');
sb.append(", isDefault=").append(isDefault);
sb.append(", pluginName='").append(pluginName).append('\'');
sb.append(", pluginInfo=").append(pluginInfo);
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final PaymentMethodJson that = (PaymentMethodJson) o;
if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) {
return false;
}
if (isDefault != null ? !isDefault.equals(that.isDefault) : that.isDefault != null) {
return false;
}
if (paymentMethodId != null ? !paymentMethodId.equals(that.paymentMethodId) : that.paymentMethodId != null) {
return false;
}
if (pluginInfo != null ? !pluginInfo.equals(that.pluginInfo) : that.pluginInfo != null) {
return false;
}
if (pluginName != null ? !pluginName.equals(that.pluginName) : that.pluginName != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = paymentMethodId != null ? paymentMethodId.hashCode() : 0;
result = 31 * result + (accountId != null ? accountId.hashCode() : 0);
result = 31 * result + (isDefault != null ? isDefault.hashCode() : 0);
result = 31 * result + (pluginName != null ? pluginName.hashCode() : 0);
result = 31 * result + (pluginInfo != null ? pluginInfo.hashCode() : 0);
return result;
}
public static class PaymentMethodPluginDetailJson {
private final String externalPaymentMethodId;
private final Boolean isDefaultPaymentMethod;
private final List<PluginPropertyJson> properties;
@JsonCreator
public PaymentMethodPluginDetailJson(@JsonProperty("externalPaymentMethodId") final String externalPaymentMethodId,
@JsonProperty("isDefaultPaymentMethod") final Boolean isDefaultPaymentMethod,
@JsonProperty("properties") final List<PluginPropertyJson> properties) {
this.externalPaymentMethodId = externalPaymentMethodId;
this.isDefaultPaymentMethod = isDefaultPaymentMethod;
this.properties = properties;
}
public String getExternalPaymentMethodId() {
return externalPaymentMethodId;
}
public Boolean getIsDefaultPaymentMethod() {
return isDefaultPaymentMethod;
}
public List<PluginPropertyJson> getProperties() {
return properties;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PaymentMethodPluginDetailJson{");
sb.append("externalPaymentMethodId='").append(externalPaymentMethodId).append('\'');
sb.append(", isDefaultPaymentMethod=").append(isDefaultPaymentMethod);
sb.append(", properties=").append(properties);
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final PaymentMethodPluginDetailJson that = (PaymentMethodPluginDetailJson) o;
if (externalPaymentMethodId != null ? !externalPaymentMethodId.equals(that.externalPaymentMethodId) : that.externalPaymentMethodId != null) {
return false;
}
if (isDefaultPaymentMethod != null ? !isDefaultPaymentMethod.equals(that.isDefaultPaymentMethod) : that.isDefaultPaymentMethod != null) {
return false;
}
if (properties != null ? !properties.equals(that.properties) : that.properties != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = externalPaymentMethodId != null ? externalPaymentMethodId.hashCode() : 0;
result = 31 * result + (isDefaultPaymentMethod != null ? isDefaultPaymentMethod.hashCode() : 0);
result = 31 * result + (properties != null ? properties.hashCode() : 0);
return result;
}
}
}