-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentIntent.java
485 lines (426 loc) · 16.8 KB
/
PaymentIntent.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package com.stripe.android.model;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.stripe.android.StripeNetworkUtils;
import com.stripe.android.utils.ObjectUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.stripe.android.model.StripeJsonUtils.listToJsonArray;
import static com.stripe.android.model.StripeJsonUtils.optBoolean;
import static com.stripe.android.model.StripeJsonUtils.optCurrency;
import static com.stripe.android.model.StripeJsonUtils.optLong;
import static com.stripe.android.model.StripeJsonUtils.optMap;
import static com.stripe.android.model.StripeJsonUtils.optString;
import static com.stripe.android.model.StripeJsonUtils.putArrayIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putBooleanIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putLongIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putMapIfNotNull;
import static com.stripe.android.model.StripeJsonUtils.putStringIfNotNull;
public class PaymentIntent extends StripeJsonModel {
private static final String VALUE_PAYMENT_INTENT = "payment_intent";
static final String FIELD_ID = "id";
static final String FIELD_OBJECT = "object";
static final String FIELD_ALLOWED_SOURCE_TYPES = "allowed_source_types";
static final String FIELD_AMOUNT = "amount";
static final String FIELD_CREATED = "created";
static final String FIELD_CANCELED = "canceled_at";
static final String FIELD_CAPTURE_METHOD = "capture_method";
static final String FIELD_CLIENT_SECRET = "client_secret";
static final String FIELD_CONFIRMATION_METHOD = "confirmation_method";
static final String FIELD_CURRENCY = "currency";
static final String FIELD_DESCRIPTION = "description";
static final String FIELD_LIVEMODE = "livemode";
static final String FIELD_NEXT_ACTION = "next_action";
static final String FIELD_NEXT_SOURCE_ACTION = "next_source_action";
static final String FIELD_PAYMENT_METHOD_TYPES = "payment_method_types";
static final String FIELD_RECEIPT_EMAIL = "receipt_email";
static final String FIELD_SOURCE = "source";
static final String FIELD_STATUS = "status";
static final String FIELD_TYPE = "type";
@Nullable private final String mId;
@Nullable private final String mObjectType;
@NonNull private final List<String> mAllowedSourceTypes;
@NonNull private final List<String> mPaymentMethodTypes;
@Nullable private final Long mAmount;
@Nullable private final Long mCanceledAt;
@Nullable private final String mCaptureMethod;
@Nullable private final String mClientSecret;
@Nullable private final String mConfirmationMethod;
@Nullable private final Long mCreated;
@Nullable private final String mCurrency;
@Nullable private final String mDescription;
@Nullable private final Boolean mLiveMode;
@Nullable private final Map<String, Object> mNextSourceAction;
@Nullable private final Map<String, Object> mNextAction;
@Nullable private final String mReceiptEmail;
@Nullable private final String mSource;
@Nullable private final String mStatus;
@Nullable
public String getId() {
return mId;
}
/**
* @deprecated use {@link #getPaymentMethodTypes()}
*/
@Deprecated
@NonNull
public List<String> getAllowedSourceTypes() {
return mAllowedSourceTypes;
}
@NonNull
public List<String> getPaymentMethodTypes() {
return mPaymentMethodTypes;
}
@Nullable
public Long getAmount() {
return mAmount;
}
@Nullable
public Long getCanceledAt() {
return mCanceledAt;
}
@Nullable
public String getCaptureMethod() {
return mCaptureMethod;
}
@Nullable
public String getClientSecret() {
return mClientSecret;
}
@Nullable
public String getConfirmationMethod() {
return mConfirmationMethod;
}
@Nullable
public Long getCreated() {
return mCreated;
}
@Nullable
public String getCurrency() {
return mCurrency;
}
@Nullable
public String getDescription() {
return mDescription;
}
@Nullable
public Boolean isLiveMode() {
return mLiveMode;
}
/**
* @deprecated use {@link #getNextAction()}
*/
@Deprecated
@Nullable
public Map<String, Object> getNextSourceAction() {
return mNextSourceAction;
}
@Nullable
public Map<String, Object> getNextAction() {
return mNextAction;
}
@Nullable
public Uri getRedirectUrl() {
final Map<String, Object> nextAction;
final Status status = Status.fromCode(mStatus);
if (Status.RequiresAction == status) {
nextAction = mNextAction;
} else if (Status.RequiresSourceAction == status) {
nextAction = mNextSourceAction;
} else {
nextAction = null;
}
if (nextAction == null) {
return null;
}
final NextActionType nextActionType = NextActionType
.fromCode((String) nextAction.get(FIELD_TYPE));
if (NextActionType.RedirectToUrl == nextActionType ||
NextActionType.AuthorizeWithUrl == nextActionType) {
final Object redirectToUrl = nextAction.get(nextActionType.code);
if (redirectToUrl instanceof Map) {
final Object url = ((Map) redirectToUrl).get("url");
if (url instanceof String) {
return Uri.parse((String) url);
}
}
}
return null;
}
/**
* @deprecated use {@link #getRedirectUrl()}
*/
@Deprecated
@Nullable
public Uri getAuthorizationUrl() {
return getRedirectUrl();
}
@Nullable
public String getReceiptEmail() {
return mReceiptEmail;
}
@Nullable
public String getSource() {
return mSource;
}
@Nullable
public String getStatus() {
return mStatus;
}
private PaymentIntent(
@Nullable String id,
@Nullable String objectType,
@NonNull List<String> allowedSourceTypes,
@NonNull List<String> paymentMethodTypes,
@Nullable Long amount,
@Nullable Long canceledAt,
@Nullable String captureMethod,
@Nullable String clientSecret,
@Nullable String confirmationMethod,
@Nullable Long created,
@Nullable String currency,
@Nullable String description,
@Nullable Boolean liveMode,
@Nullable Map<String, Object> nextSourceAction,
@Nullable Map<String, Object> nextAction,
@Nullable String receiptEmail,
@Nullable String source,
@Nullable String status
) {
mId = id;
mObjectType = objectType;
mAllowedSourceTypes = allowedSourceTypes;
mPaymentMethodTypes = paymentMethodTypes;
mAmount = amount;
mCanceledAt = canceledAt;
mCaptureMethod = captureMethod;
mClientSecret = clientSecret;
mConfirmationMethod = confirmationMethod;
mCreated = created;
mCurrency = currency;
mDescription = description;
mLiveMode = liveMode;
mNextSourceAction = nextSourceAction;
mNextAction = nextAction;
mReceiptEmail = receiptEmail;
mSource = source;
mStatus = status;
}
@NonNull
public static String parseIdFromClientSecret(@NonNull String clientSecret) {
return clientSecret.split("_secret")[0];
}
@Nullable
public static PaymentIntent fromString(@Nullable String jsonString) {
try {
return fromJson(new JSONObject(jsonString));
} catch (JSONException ignored) {
return null;
}
}
@Nullable
public static PaymentIntent fromJson(@Nullable JSONObject jsonObject) {
if (jsonObject == null ||
!VALUE_PAYMENT_INTENT.equals(jsonObject.optString(FIELD_OBJECT))) {
return null;
}
final String id = optString(jsonObject, FIELD_ID);
final String objectType = optString(jsonObject, FIELD_OBJECT);
final List<String> allowedSourceTypes = jsonArrayToList(
jsonObject.optJSONArray(FIELD_ALLOWED_SOURCE_TYPES));
final List<String> paymentMethodTypes = jsonArrayToList(
jsonObject.optJSONArray(FIELD_PAYMENT_METHOD_TYPES));
final Long amount = optLong(jsonObject, FIELD_AMOUNT);
final Long canceledAt = optLong(jsonObject, FIELD_CANCELED);
final String captureMethod = optString(jsonObject, FIELD_CAPTURE_METHOD);
final String clientSecret = optString(jsonObject, FIELD_CLIENT_SECRET);
final String confirmationMethod = optString(jsonObject, FIELD_CONFIRMATION_METHOD);
final Long created = optLong(jsonObject, FIELD_CREATED);
final String currency = optCurrency(jsonObject, FIELD_CURRENCY);
final String description = optString(jsonObject, FIELD_DESCRIPTION);
final Boolean livemode = optBoolean(jsonObject, FIELD_LIVEMODE);
final String receiptEmail = optString(jsonObject, FIELD_RECEIPT_EMAIL);
final String status = optString(jsonObject, FIELD_STATUS);
final Map<String, Object> nextSourceAction = optMap(jsonObject, FIELD_NEXT_SOURCE_ACTION);
final Map<String, Object> nextAction = optMap(jsonObject, FIELD_NEXT_ACTION);
final String source = optString(jsonObject, FIELD_SOURCE);
return new PaymentIntent(
id,
objectType,
allowedSourceTypes,
paymentMethodTypes,
amount,
canceledAt,
captureMethod,
clientSecret,
confirmationMethod,
created,
currency,
description,
livemode,
nextSourceAction,
nextAction,
receiptEmail,
source,
status);
}
@NonNull
private static List<String> jsonArrayToList(@Nullable JSONArray jsonArray) {
final List<String> list = new ArrayList<>();
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
list.add(jsonArray.getString(i));
} catch (JSONException ignored) {
}
}
}
return list;
}
@NonNull
@Override
public JSONObject toJson() {
final JSONObject jsonObject = new JSONObject();
putStringIfNotNull(jsonObject, FIELD_ID, mId);
putStringIfNotNull(jsonObject, FIELD_OBJECT, mObjectType);
putArrayIfNotNull(jsonObject, FIELD_ALLOWED_SOURCE_TYPES,
listToJsonArray(mAllowedSourceTypes));
putArrayIfNotNull(jsonObject, FIELD_PAYMENT_METHOD_TYPES,
listToJsonArray(mPaymentMethodTypes));
putLongIfNotNull(jsonObject, FIELD_AMOUNT, mAmount);
putLongIfNotNull(jsonObject, FIELD_CANCELED, mCanceledAt);
putStringIfNotNull(jsonObject, FIELD_CAPTURE_METHOD, mCaptureMethod);
putStringIfNotNull(jsonObject, FIELD_CLIENT_SECRET, mClientSecret);
putStringIfNotNull(jsonObject, FIELD_CONFIRMATION_METHOD, mConfirmationMethod);
putLongIfNotNull(jsonObject, FIELD_CREATED, mCreated);
putStringIfNotNull(jsonObject, FIELD_CURRENCY, mCurrency);
putStringIfNotNull(jsonObject, FIELD_DESCRIPTION, mDescription);
putBooleanIfNotNull(jsonObject, FIELD_LIVEMODE, mLiveMode);
putMapIfNotNull(jsonObject, FIELD_NEXT_SOURCE_ACTION, mNextSourceAction);
putMapIfNotNull(jsonObject, FIELD_NEXT_ACTION, mNextAction);
putStringIfNotNull(jsonObject, FIELD_RECEIPT_EMAIL, mReceiptEmail);
putStringIfNotNull(jsonObject, FIELD_SOURCE, mSource);
putStringIfNotNull(jsonObject, FIELD_STATUS, mStatus);
return jsonObject;
}
@NonNull
@Override
public Map<String, Object> toMap() {
final AbstractMap<String, Object> map = new HashMap<>();
map.put(FIELD_ID, mId);
map.put(FIELD_OBJECT, mObjectType);
map.put(FIELD_ALLOWED_SOURCE_TYPES, mAllowedSourceTypes);
map.put(FIELD_PAYMENT_METHOD_TYPES, mPaymentMethodTypes);
map.put(FIELD_AMOUNT, mAmount);
map.put(FIELD_CANCELED, mCanceledAt);
map.put(FIELD_CLIENT_SECRET, mClientSecret);
map.put(FIELD_CAPTURE_METHOD, mCaptureMethod);
map.put(FIELD_CONFIRMATION_METHOD, mConfirmationMethod);
map.put(FIELD_CREATED, mCreated);
map.put(FIELD_CURRENCY, mCurrency);
map.put(FIELD_DESCRIPTION, mDescription);
map.put(FIELD_LIVEMODE, mLiveMode);
map.put(FIELD_NEXT_SOURCE_ACTION, mNextSourceAction);
map.put(FIELD_NEXT_ACTION, mNextAction);
map.put(FIELD_RECEIPT_EMAIL, mReceiptEmail);
map.put(FIELD_STATUS, mStatus);
map.put(FIELD_SOURCE, mSource);
StripeNetworkUtils.removeNullAndEmptyParams(map);
return map;
}
@Override
public boolean equals(@Nullable Object obj) {
return this == obj || (obj instanceof PaymentIntent && typedEquals((PaymentIntent) obj));
}
private boolean typedEquals(@NonNull PaymentIntent paymentIntent) {
return ObjectUtils.equals(mId, paymentIntent.mId)
&& ObjectUtils.equals(mObjectType, paymentIntent.mObjectType)
&& ObjectUtils.equals(mAllowedSourceTypes, paymentIntent.mAllowedSourceTypes)
&& ObjectUtils.equals(mAmount, paymentIntent.mAmount)
&& ObjectUtils.equals(mCanceledAt, paymentIntent.mCanceledAt)
&& ObjectUtils.equals(mCaptureMethod, paymentIntent.mCaptureMethod)
&& ObjectUtils.equals(mClientSecret, paymentIntent.mClientSecret)
&& ObjectUtils.equals(mConfirmationMethod, paymentIntent.mConfirmationMethod)
&& ObjectUtils.equals(mCreated, paymentIntent.mCreated)
&& ObjectUtils.equals(mCurrency, paymentIntent.mCurrency)
&& ObjectUtils.equals(mDescription, paymentIntent.mDescription)
&& ObjectUtils.equals(mLiveMode, paymentIntent.mLiveMode)
&& ObjectUtils.equals(mNextSourceAction, paymentIntent.mNextSourceAction)
&& ObjectUtils.equals(mReceiptEmail, paymentIntent.mReceiptEmail)
&& ObjectUtils.equals(mSource, paymentIntent.mSource)
&& ObjectUtils.equals(mStatus, paymentIntent.mStatus);
}
@Override
public int hashCode() {
return ObjectUtils.hash(mId, mObjectType, mAllowedSourceTypes, mAmount, mCanceledAt,
mCaptureMethod, mClientSecret, mConfirmationMethod, mCreated, mCurrency,
mDescription, mLiveMode, mNextSourceAction, mReceiptEmail, mSource, mStatus);
}
public enum NextActionType {
RedirectToUrl("redirect_to_url"),
AuthorizeWithUrl("authorize_with_url");
@NonNull public final String code;
NextActionType(@NonNull String code) {
this.code = code;
}
@Nullable
public static NextActionType fromCode(@Nullable String code) {
if (code == null) {
return null;
}
for (NextActionType nextActionType : values()) {
if (nextActionType.code.equals(code)) {
return nextActionType;
}
}
return null;
}
}
/**
* See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status
*/
public enum Status {
Canceled("canceled"),
Processing("processing"),
RequiresAction("requires_action"),
RequiresAuthorization("requires_authorization"),
RequiresCapture("requires_capture"),
RequiresConfirmation("requires_confirmation"),
RequiresPaymentMethod("requires_payment_method"),
Succeeded("succeeded"),
/**
* @deprecated use {@link #RequiresPaymentMethod}
*/
@Deprecated
RequiresSource("requires_source"),
/**
* @deprecated use {@link #RequiresAction}
*/
@Deprecated
RequiresSourceAction("requires_source_action");
@NonNull
public final String code;
Status(@NonNull String code) {
this.code = code;
}
@Nullable
public static Status fromCode(@Nullable String code) {
if (code == null) {
return null;
}
for (Status status : values()) {
if (status.code.equals(code)) {
return status;
}
}
return null;
}
}
}