forked from stripe/stripe-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxTokenController.java
More file actions
122 lines (109 loc) · 4.44 KB
/
RxTokenController.java
File metadata and controls
122 lines (109 loc) · 4.44 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
package com.stripe.example.controller;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Button;
import com.jakewharton.rxbinding.view.RxView;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import com.stripe.android.view.CardInputWidget;
import java.util.concurrent.Callable;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
/**
* Class containing all the logic needed to create a token and listen for the results using
* RxJava.
*/
public class RxTokenController {
private CardInputWidget mCardInputWidget;
private CompositeSubscription mCompositeSubscription;
private Context mContext;
private ErrorDialogHandler mErrorDialogHandler;
private ListViewController mOutputListController;
private ProgressDialogController mProgressDialogController;
public RxTokenController (
@NonNull Button button,
@NonNull CardInputWidget cardInputWidget,
@NonNull Context context,
@NonNull ErrorDialogHandler errorDialogHandler,
@NonNull ListViewController outputListController,
@NonNull ProgressDialogController progressDialogController) {
mCompositeSubscription = new CompositeSubscription();
mCardInputWidget = cardInputWidget;
mContext = context;
mErrorDialogHandler = errorDialogHandler;
mOutputListController = outputListController;
mProgressDialogController = progressDialogController;
mCompositeSubscription.add(
RxView.clicks(button).subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
saveCard();
}
})
);
}
/**
* Release subscriptions to prevent memory leaks.
*/
public void detach() {
if (mCompositeSubscription != null) {
mCompositeSubscription.unsubscribe();
}
mCardInputWidget = null;
}
private void saveCard() {
final Card cardToSave = mCardInputWidget.getCard();
if (cardToSave == null) {
mErrorDialogHandler.showError("Invalid Card Data");
return;
}
final Stripe stripe = new Stripe(mContext);
// Note: using this style of Observable creation results in us having a method that
// will not be called until we subscribe to it.
final Observable<Token> tokenObservable =
Observable.fromCallable(
new Callable<Token>() {
@Override
public Token call() throws Exception {
return stripe.createTokenSynchronous(cardToSave,
PaymentConfiguration.getInstance().getPublishableKey());
}
});
mCompositeSubscription.add(tokenObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(
new Action0() {
@Override
public void call() {
mProgressDialogController.startProgress();
}
})
.doOnUnsubscribe(
new Action0() {
@Override
public void call() {
mProgressDialogController.finishProgress();
}
})
.subscribe(
new Action1<Token>() {
@Override
public void call(Token token) {
mOutputListController.addToList(token);
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mErrorDialogHandler.showError(throwable.getLocalizedMessage());
}
}));
}
}