-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentActivity.java
481 lines (415 loc) · 19.5 KB
/
PaymentActivity.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
package com.stripe.samplestore;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jakewharton.rxbinding.view.RxView;
import com.stripe.android.CustomerSession;
import com.stripe.android.PayWithGoogleUtils;
import com.stripe.android.PaymentSession;
import com.stripe.android.PaymentSessionConfig;
import com.stripe.android.PaymentSessionData;
import com.stripe.android.StripeError;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;
import com.stripe.android.model.ShippingInformation;
import com.stripe.android.model.ShippingMethod;
import com.stripe.android.model.Source;
import com.stripe.android.model.SourceCardData;
import com.stripe.samplestore.service.StripeService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import static com.stripe.android.view.PaymentFlowExtras.EVENT_SHIPPING_INFO_PROCESSED;
import static com.stripe.android.view.PaymentFlowExtras.EVENT_SHIPPING_INFO_SUBMITTED;
import static com.stripe.android.view.PaymentFlowExtras.EXTRA_DEFAULT_SHIPPING_METHOD;
import static com.stripe.android.view.PaymentFlowExtras.EXTRA_IS_SHIPPING_INFO_VALID;
import static com.stripe.android.view.PaymentFlowExtras.EXTRA_SHIPPING_INFO_DATA;
import static com.stripe.android.view.PaymentFlowExtras.EXTRA_VALID_SHIPPING_METHODS;
public class PaymentActivity extends AppCompatActivity {
private static final String EXTRA_CART = "extra_cart";
private static final String TOTAL_LABEL = "Total:";
private static final String SHIPPING = "Shipping";
private BroadcastReceiver mBroadcastReceiver;
private CompositeSubscription mCompositeSubscription;
private ProgressDialogFragment mProgressDialogFragment;
private LinearLayout mCartItemLayout;
private Button mConfirmPaymentButton;
private TextView mEnterShippingInfo;
private TextView mEnterPaymentInfo;
private PaymentSession mPaymentSession;
private StoreCart mStoreCart;
private long mShippingCosts = 0L;
@NonNull
public static Intent createIntent(@NonNull Activity activity, @NonNull StoreCart cart) {
return new Intent(activity, PaymentActivity.class)
.putExtra(EXTRA_CART, cart);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
Bundle extras = getIntent().getExtras();
mStoreCart = extras.getParcelable(EXTRA_CART);
mCartItemLayout = findViewById(R.id.cart_list_items);
addCartItems();
mCompositeSubscription = new CompositeSubscription();
mProgressDialogFragment =
ProgressDialogFragment.newInstance(R.string.completing_purchase);
mConfirmPaymentButton = findViewById(R.id.btn_purchase);
updateConfirmPaymentButton();
mEnterShippingInfo = findViewById(R.id.shipping_info);
mEnterPaymentInfo = findViewById(R.id.payment_source);
RxView.clicks(mEnterShippingInfo)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mPaymentSession.presentShippingFlow();
}
});
RxView.clicks(mEnterPaymentInfo)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mPaymentSession.presentPaymentMethodSelection();
}
});
RxView.clicks(mConfirmPaymentButton)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
attemptPurchase();
}
});
setupPaymentSession();
if (!mPaymentSession.getPaymentSessionData().isPaymentReadyToCharge()) {
mConfirmPaymentButton.setEnabled(false);
}
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final ShippingInformation shippingInformation = intent
.getParcelableExtra(EXTRA_SHIPPING_INFO_DATA);
final Intent shippingInfoProcessedIntent =
new Intent(EVENT_SHIPPING_INFO_PROCESSED);
if (!isShippingInfoValid(shippingInformation)) {
shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, false);
} else {
ArrayList<ShippingMethod> shippingMethods = getValidShippingMethods(shippingInformation);
shippingInfoProcessedIntent.putExtra(EXTRA_IS_SHIPPING_INFO_VALID, true);
shippingInfoProcessedIntent.putParcelableArrayListExtra(EXTRA_VALID_SHIPPING_METHODS, shippingMethods);
shippingInfoProcessedIntent
.putExtra(EXTRA_DEFAULT_SHIPPING_METHOD, shippingMethods.get(0));
}
LocalBroadcastManager.getInstance(PaymentActivity.this)
.sendBroadcast(shippingInfoProcessedIntent);
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,
new IntentFilter(EVENT_SHIPPING_INFO_SUBMITTED));
}
private boolean isShippingInfoValid(@NonNull ShippingInformation shippingInfo) {
return shippingInfo.getAddress() != null &&
Locale.US.getCountry().equals(shippingInfo.getAddress().getCountry());
}
/*
* Cleaning up all Rx subscriptions in onDestroy.
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (mCompositeSubscription != null) {
mCompositeSubscription.unsubscribe();
mCompositeSubscription = null;
}
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
mPaymentSession.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPaymentSession.handlePaymentData(requestCode, resultCode, data);
}
private void updateConfirmPaymentButton() {
long price = mStoreCart.getTotalPrice();
mConfirmPaymentButton.setText(String.format(Locale.ENGLISH,
"Pay %s", StoreUtils.getPriceString(price, null)));
}
private void addCartItems() {
mCartItemLayout.removeAllViewsInLayout();
String currencySymbol = mStoreCart.getCurrency().getSymbol(Locale.US);
addLineItems(currencySymbol, mStoreCart.getLineItems()
.toArray(new StoreLineItem[mStoreCart.getSize()]));
addLineItems(currencySymbol,
new StoreLineItem(SHIPPING, 1, mShippingCosts));
View totalView = LayoutInflater.from(this).inflate(
R.layout.cart_item, mCartItemLayout, false);
boolean shouldDisplayTotal = fillOutTotalView(totalView, currencySymbol);
if (shouldDisplayTotal) {
mCartItemLayout.addView(totalView);
}
}
private void addLineItems(String currencySymbol, StoreLineItem... items) {
for (StoreLineItem item : items) {
View view = LayoutInflater.from(this).inflate(
R.layout.cart_item, mCartItemLayout, false);
fillOutCartItemView(item, view, currencySymbol);
mCartItemLayout.addView(view);
}
}
private boolean fillOutTotalView(View view, String currencySymbol) {
TextView[] itemViews = getItemViews(view);
long totalPrice = mStoreCart.getTotalPrice() + mShippingCosts;
itemViews[0].setText(TOTAL_LABEL);
String priceString = PayWithGoogleUtils.getPriceString(
totalPrice,
mStoreCart.getCurrency());
priceString = currencySymbol + priceString;
itemViews[3].setText(priceString);
return true;
}
private void fillOutCartItemView(StoreLineItem item, View view, String currencySymbol) {
TextView[] itemViews = getItemViews(view);
itemViews[0].setText(item.getDescription());
if (!SHIPPING.equals(item.getDescription())) {
String quantityPriceString = "X " + item.getQuantity() + " @";
itemViews[1].setText(quantityPriceString);
String unitPriceString = currencySymbol +
PayWithGoogleUtils.getPriceString(item.getUnitPrice(),
mStoreCart.getCurrency());
itemViews[2].setText(unitPriceString);
}
String totalPriceString = currencySymbol +
PayWithGoogleUtils.getPriceString(item.getTotalPrice(), mStoreCart.getCurrency());
itemViews[3].setText(totalPriceString);
}
@Size(value = 4)
private TextView[] getItemViews(View view) {
TextView labelView = view.findViewById(R.id.tv_cart_emoji);
TextView quantityView = view.findViewById(R.id.tv_cart_quantity);
TextView unitPriceView = view.findViewById(R.id.tv_cart_unit_price);
TextView totalPriceView = view.findViewById(R.id.tv_cart_total_price);
return new TextView[]{labelView, quantityView, unitPriceView, totalPriceView};
}
private void attemptPurchase() {
CustomerSession.getInstance().retrieveCurrentCustomer(new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
String sourceId = customer.getDefaultSource();
if (sourceId == null) {
displayError("No payment method selected");
return;
}
CustomerSource source = customer.getSourceById(sourceId);
proceedWithPurchaseIf3DSCheckIsNotNecessary(source.asSource(), customer.getId());
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
displayError("Error getting payment method");
}
});
}
private void proceedWithPurchaseIf3DSCheckIsNotNecessary(Source source, String customerId) {
if (source == null || !Source.CARD.equals(source.getType())) {
displayError("Something went wrong - this should be rare");
return;
}
SourceCardData cardData = (SourceCardData) source.getSourceTypeModel();
if (SourceCardData.REQUIRED.equals(cardData.getThreeDSecureStatus())) {
// In this case, you would need to ask the user to verify the purchase.
// You can see an example of how to do this in the 3DS example application.
// In stripe-android/example.
} else {
// If 3DS is not required, you can charge the source.
completePurchase(source.getId(), customerId);
}
}
@NonNull
private Map<String, Object> createParams(long price, String sourceId, String customerId,
ShippingInformation shippingInformation) {
Map<String, Object> params = new HashMap<>();
params.put("amount", Long.toString(price));
params.put("source", sourceId);
params.put("customer_id", customerId);
params.put("shipping", shippingInformation.toMap());
return params;
}
private void completePurchase(String sourceId, String customerId) {
Retrofit retrofit = RetrofitFactory.getInstance();
StripeService stripeService = retrofit.create(StripeService.class);
long price = mStoreCart.getTotalPrice() + mShippingCosts;
ShippingInformation shippingInformation = mPaymentSession.getPaymentSessionData().getShippingInformation();
final Observable<Void> stripeResponse = stripeService.createQueryCharge(
createParams(price, sourceId, customerId, shippingInformation));
final FragmentManager fragmentManager = getSupportFragmentManager();
mCompositeSubscription.add(stripeResponse
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(
new Action0() {
@Override
public void call() {
if (mProgressDialogFragment != null &&
!mProgressDialogFragment.isAdded())
mProgressDialogFragment.show(fragmentManager, "progress");
}
})
.doOnUnsubscribe(
new Action0() {
@Override
public void call() {
if (mProgressDialogFragment != null
&& mProgressDialogFragment.isVisible()) {
mProgressDialogFragment.dismiss();
}
}
})
.subscribe(
new Action1<Void>() {
@Override
public void call(Void aVoid) {
finishCharge();
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
displayError(throwable.getLocalizedMessage());
}
}));
}
private void displayError(String errorMessage) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage(errorMessage);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void finishCharge() {
Intent data = StoreActivity.createPurchaseCompleteIntent(
mStoreCart.getTotalPrice() + mShippingCosts);
setResult(RESULT_OK, data);
finish();
}
private void setupPaymentSession() {
mPaymentSession = new PaymentSession(this);
mPaymentSession.init(new PaymentSessionListenerImpl(this),
new PaymentSessionConfig.Builder().build());
}
private String formatSourceDescription(Source source) {
if (Source.CARD.equals(source.getType())) {
final SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel();
return sourceCardData.getBrand() + getString(R.string.ending_in) +
sourceCardData.getLast4();
}
return source.getType();
}
@NonNull
private ArrayList<ShippingMethod> getValidShippingMethods(@NonNull ShippingInformation shippingInformation) {
ArrayList<ShippingMethod> shippingMethods = new ArrayList<>();
shippingMethods.add(new ShippingMethod("UPS Ground", "ups-ground", "Arrives in 3-5 days", 0, "USD"));
shippingMethods.add(new ShippingMethod("FedEx", "fedex", "Arrives tomorrow", 599, "USD"));
if (shippingInformation.getAddress() != null &&
shippingInformation.getAddress().getPostalCode().equals("94110")) {
shippingMethods.add(new ShippingMethod("1 Hour Courier", "courier", "Arrives in the next hour", 1099, "USD"));
}
return shippingMethods;
}
private void onCommunicatingStateChanged(boolean isCommunicating) {
if (isCommunicating) {
mProgressDialogFragment.show(getSupportFragmentManager(), "progress");
} else {
mProgressDialogFragment.dismiss();
}
}
private void onPaymentSessionDataChanged(@NonNull PaymentSessionData data) {
if (data.getShippingMethod() != null) {
mEnterShippingInfo.setText(data.getShippingMethod().getLabel());
mShippingCosts = data.getShippingMethod().getAmount();
addCartItems();
updateConfirmPaymentButton();
}
if (data.getSelectedPaymentMethodId() != null) {
CustomerSession.getInstance().retrieveCurrentCustomer(
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
final String sourceId = customer.getDefaultSource();
if (sourceId == null) {
displayError("No payment method selected");
return;
}
final CustomerSource source = customer.getSourceById(sourceId);
mEnterPaymentInfo.setText(formatSourceDescription(source.asSource()));
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
displayError(errorMessage);
}
});
}
if (data.isPaymentReadyToCharge()) {
mConfirmPaymentButton.setEnabled(true);
}
}
private static final class PaymentSessionListenerImpl
extends PaymentSession.ActivityPaymentSessionListener<PaymentActivity> {
private PaymentSessionListenerImpl(@NonNull PaymentActivity activity) {
super(activity);
}
@Override
public void onCommunicatingStateChanged(boolean isCommunicating) {
final PaymentActivity activity = getListenerActivity();
if (activity == null) {
return;
}
activity.onCommunicatingStateChanged(isCommunicating);
}
@Override
public void onError(int errorCode, @Nullable String errorMessage) {
final PaymentActivity activity = getListenerActivity();
if (activity == null) {
return;
}
activity.displayError(errorMessage);
}
@Override
public void onPaymentSessionDataChanged(@NonNull PaymentSessionData data) {
final PaymentActivity activity = getListenerActivity();
if (activity == null) {
return;
}
activity.onPaymentSessionDataChanged(data);
}
}
}