-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentIntentActivity.java
260 lines (240 loc) · 11 KB
/
PaymentIntentActivity.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
package com.stripe.example.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.Card;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.android.model.SourceParams;
import com.stripe.android.view.CardInputWidget;
import com.stripe.example.R;
import com.stripe.example.controller.ErrorDialogHandler;
import com.stripe.example.controller.ProgressDialogController;
import com.stripe.example.module.RetrofitFactory;
import com.stripe.example.service.StripeService;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
public class PaymentIntentActivity extends AppCompatActivity {
private static final String TAG = PaymentIntentActivity.class.getName();
private static final String RETURN_URL = "stripe://payment_intent_return";
@NonNull private final CompositeSubscription mCompositeSubscription =
new CompositeSubscription();
private ProgressDialogController mProgressDialogController;
private ErrorDialogHandler mErrorDialogHandler;
private Stripe mStripe;
private StripeService mStripeService;
private String mClientSecret;
private Button mConfirmPaymentIntent;
private Button mRetrievePaymentIntent;
private CardInputWidget mCardInputWidget;
private TextView mPaymentIntentValue;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_intent_demo);
final Button createPaymentIntent = findViewById(R.id.btn_create_payment_intent);
mRetrievePaymentIntent = findViewById(R.id.btn_retrieve_payment_intent);
mConfirmPaymentIntent = findViewById(R.id.btn_confirm_payment_intent);
mPaymentIntentValue = findViewById(R.id.payment_intent_value);
mCardInputWidget = findViewById(R.id.card_input_widget);
mProgressDialogController = new ProgressDialogController(getSupportFragmentManager(),
getResources());
mErrorDialogHandler = new ErrorDialogHandler(getSupportFragmentManager());
mStripe = new Stripe(getApplicationContext());
Retrofit retrofit = RetrofitFactory.getInstance();
mStripeService = retrofit.create(StripeService.class);
createPaymentIntent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createPaymentIntent();
}
});
mRetrievePaymentIntent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
retrievePaymentIntent();
}
});
mConfirmPaymentIntent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Card card = mCardInputWidget.getCard();
if (card != null) {
confirmPaymentIntent(card);
}
}
});
}
@Override
protected void onDestroy() {
mCompositeSubscription.unsubscribe();
super.onDestroy();
}
@NonNull
private Map<String, Object> createPaymentIntentParams() {
final Map<String, Object> params = new HashMap<>();
params.put("allowed_source_types[]", "card");
params.put("amount", 1000);
params.put("currency", "usd");
return params;
}
void createPaymentIntent() {
Subscription subscription = mStripeService.createPaymentIntent(createPaymentIntentParams())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.show(R.string.creating_payment_intent);
}
})
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.dismiss();
}
})
.subscribe(
// Because we've made the mapping above, we're now subscribing
// to the result of creating a 3DS Source
new Action1<ResponseBody>() {
@Override
public void call(ResponseBody responseBody) {
try {
JSONObject jsonObject = new JSONObject(responseBody.string());
mClientSecret = jsonObject.optString("secret");
mConfirmPaymentIntent.setEnabled(mClientSecret != null);
mRetrievePaymentIntent.setEnabled(mClientSecret != null);
} catch (IOException | JSONException exception) {
Log.e(TAG, exception.toString());
}
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mErrorDialogHandler.show(throwable.getLocalizedMessage());
}
}
);
mCompositeSubscription.add(subscription);
}
private void retrievePaymentIntent() {
final Observable<PaymentIntent> paymentIntentObservable = Observable.fromCallable(
new Callable<PaymentIntent>() {
@Override
public PaymentIntent call() throws Exception {
return mStripe.retrievePaymentIntentSynchronous(
PaymentIntentParams.createRetrievePaymentIntentParams(mClientSecret),
PaymentConfiguration.getInstance().getPublishableKey());
}
});
final Subscription subscription = paymentIntentObservable
.subscribeOn(Schedulers.io())
.doOnSubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.show(R.string.retrieving_payment_intent);
}
})
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.dismiss();
}
})
.observeOn(AndroidSchedulers.mainThread()).subscribe(
new Action1<PaymentIntent>() {
@Override
public void call(PaymentIntent paymentIntent) {
if (paymentIntent != null) {
mPaymentIntentValue.setText(paymentIntent.toJson().toString());
} else {
mPaymentIntentValue.setText(R.string.error_while_retrieving_payment_intent);
}
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG, throwable.toString());
}
}
);
mCompositeSubscription.add(subscription);
}
private void confirmPaymentIntent(@NonNull final Card card) {
final SourceParams sourceParams = SourceParams.createCardParams(card);
final Observable<PaymentIntent> paymentIntentObservable = Observable.fromCallable(
new Callable<PaymentIntent>() {
@Override
public PaymentIntent call() throws Exception {
final PaymentIntentParams paymentIntentParams = PaymentIntentParams
.createConfirmPaymentIntentWithSourceDataParams(
sourceParams, mClientSecret, RETURN_URL);
return mStripe.confirmPaymentIntentSynchronous(
paymentIntentParams,
PaymentConfiguration.getInstance().getPublishableKey());
}
});
final Subscription subscription = paymentIntentObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.show(R.string.confirming_payment_intent);
}
})
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
mProgressDialogController.dismiss();
}
})
.subscribe(
new Action1<PaymentIntent>() {
@Override
public void call(@Nullable PaymentIntent paymentIntent) {
if (paymentIntent != null) {
mPaymentIntentValue.setText(paymentIntent.toString());
final PaymentIntent.Status status = PaymentIntent.Status
.fromCode(paymentIntent.getStatus());
if (PaymentIntent.Status.RequiresAction == status) {
startActivity(new Intent(Intent.ACTION_VIEW,
paymentIntent.getRedirectUrl()));
}
}
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG, throwable.toString());
}
}
);
mCompositeSubscription.add(subscription);
}
}