-
Notifications
You must be signed in to change notification settings - Fork 629
/
AddSourceActivity.java
294 lines (249 loc) · 10.8 KB
/
AddSourceActivity.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
package com.stripe.android.view;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.stripe.android.ActivitySourceCallback;
import com.stripe.android.CustomerSession;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.R;
import com.stripe.android.Stripe;
import com.stripe.android.StripeError;
import com.stripe.android.model.Card;
import com.stripe.android.model.Source;
import com.stripe.android.model.SourceParams;
import com.stripe.android.model.StripePaymentSource;
import static com.stripe.android.PaymentSession.EXTRA_PAYMENT_SESSION_ACTIVE;
import static com.stripe.android.PaymentSession.TOKEN_PAYMENT_SESSION;
/**
* Activity used to display a {@link CardMultilineWidget} and receive the resulting
* {@link Source} in the {@link #onActivityResult(int, int, Intent)} of the launching activity.
*/
public class AddSourceActivity extends StripeActivity {
public static final String EXTRA_NEW_SOURCE = "new_source";
static final String ADD_SOURCE_ACTIVITY = "AddSourceActivity";
static final String EXTRA_SHOW_ZIP = "show_zip";
static final String EXTRA_PROXY_DELAY = "proxy_delay";
static final String EXTRA_UPDATE_CUSTOMER = "update_customer";
@Nullable private CardMultilineWidget mCardMultilineWidget;
@Nullable private CustomerSessionProxy mCustomerSessionProxy;
@Nullable private StripeProvider mStripeProvider;
private boolean mStartedFromPaymentSession;
private boolean mUpdatesCustomer;
@NonNull private final TextView.OnEditorActionListener mOnEditorActionListener =
new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (mCardMultilineWidget.getCard() != null) {
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(mViewStub.getWindowToken(), 0);
}
onActionSave();
return true;
}
return false;
}
};
/**
* Create an {@link Intent} to start a {@link AddSourceActivity}.
*
* @param context the {@link Context} used to launch the activity
* @param requirePostalField {@code true} to require a postal code field
* @param updatesCustomer {@code true} if the activity should update using an
* already-initialized {@link CustomerSession}, or {@code false} if it should just
* return a source.
* @return an {@link Intent} that can be used to start this activity
*/
@NonNull
public static Intent newIntent(@NonNull Context context,
boolean requirePostalField,
boolean updatesCustomer) {
return new Intent(context, AddSourceActivity.class)
.putExtra(EXTRA_SHOW_ZIP, requirePostalField)
.putExtra(EXTRA_UPDATE_CUSTOMER, updatesCustomer);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewStub.setLayoutResource(R.layout.activity_add_source);
mViewStub.inflate();
mCardMultilineWidget = findViewById(R.id.add_source_card_entry_widget);
initEnterListeners();
final boolean showZip = getIntent().getBooleanExtra(EXTRA_SHOW_ZIP, false);
mUpdatesCustomer = getIntent().getBooleanExtra(EXTRA_UPDATE_CUSTOMER, false);
mStartedFromPaymentSession =
getIntent().getBooleanExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true);
mCardMultilineWidget.setShouldShowPostalCode(showZip);
if (mUpdatesCustomer && !getIntent().getBooleanExtra(EXTRA_PROXY_DELAY, false)) {
initCustomerSessionTokens();
}
setTitle(R.string.title_add_a_card);
}
private void initEnterListeners() {
((TextView) mCardMultilineWidget.findViewById(R.id.et_add_source_card_number_ml))
.setOnEditorActionListener(mOnEditorActionListener);
((TextView) mCardMultilineWidget.findViewById(R.id.et_add_source_expiry_ml))
.setOnEditorActionListener(mOnEditorActionListener);
((TextView) mCardMultilineWidget.findViewById(R.id.et_add_source_cvc_ml))
.setOnEditorActionListener(mOnEditorActionListener);
((TextView) mCardMultilineWidget.findViewById(R.id.et_add_source_postal_ml))
.setOnEditorActionListener(mOnEditorActionListener);
}
@VisibleForTesting
void initCustomerSessionTokens() {
logToCustomerSessionIf(ADD_SOURCE_ACTIVITY, mUpdatesCustomer);
logToCustomerSessionIf(TOKEN_PAYMENT_SESSION, mStartedFromPaymentSession);
}
@Override
protected void onActionSave() {
final Card card = mCardMultilineWidget != null ? mCardMultilineWidget.getCard() : null;
if (card == null) {
// In this case, the error will be displayed on the card widget itself.
return;
}
card.addLoggingToken(ADD_SOURCE_ACTIVITY);
final Stripe stripe = getStripe();
stripe.setDefaultPublishableKey(PaymentConfiguration.getInstance().getPublishableKey());
final SourceParams sourceParams = SourceParams.createCardParams(card);
setCommunicatingProgress(true);
stripe.createSource(sourceParams, new SourceCallbackImpl(this, mUpdatesCustomer));
}
private void attachCardToCustomer(@NonNull final StripePaymentSource source) {
final CustomerSession.SourceRetrievalListener listener =
new SourceRetrievalListenerImpl(this);
if (mCustomerSessionProxy == null) {
@Source.SourceType final String sourceType;
if (source instanceof Source) {
sourceType = ((Source) source).getType();
} else if (source instanceof Card){
sourceType = Source.CARD;
} else {
// This isn't possible from this activity.
sourceType = Source.UNKNOWN;
}
CustomerSession.getInstance().addCustomerSource(
this,
source.getId(),
sourceType,
listener);
} else {
mCustomerSessionProxy.addCustomerSource(source.getId(), listener);
}
}
private void logToCustomerSessionIf(@NonNull String logToken, boolean condition) {
if (mCustomerSessionProxy != null) {
logToProxyIf(logToken, condition);
return;
}
if (condition) {
CustomerSession.getInstance().addProductUsageTokenIfValid(logToken);
}
}
private void logToProxyIf(@NonNull String logToken, boolean condition) {
if (mCustomerSessionProxy != null && condition) {
mCustomerSessionProxy.addProductUsageTokenIfValid(logToken);
}
}
private void finishWithSource(@NonNull Source source) {
setCommunicatingProgress(false);
Intent intent = new Intent();
intent.putExtra(EXTRA_NEW_SOURCE, source.toJson().toString());
setResult(RESULT_OK, intent);
finish();
}
@NonNull
private Stripe getStripe() {
if (mStripeProvider == null) {
return new Stripe(getApplicationContext());
} else {
return mStripeProvider.getStripe(this);
}
}
@Override
protected void setCommunicatingProgress(boolean communicating) {
super.setCommunicatingProgress(communicating);
if (mCardMultilineWidget != null) {
mCardMultilineWidget.setEnabled(!communicating);
}
}
@VisibleForTesting
void setCustomerSessionProxy(CustomerSessionProxy proxy) {
mCustomerSessionProxy = proxy;
}
@VisibleForTesting
void setStripeProvider(@NonNull StripeProvider stripeProvider) {
mStripeProvider = stripeProvider;
}
interface StripeProvider {
@NonNull Stripe getStripe(@NonNull Context context);
}
interface CustomerSessionProxy {
void addProductUsageTokenIfValid(@NonNull String token);
void addCustomerSource(String sourceId,
@NonNull CustomerSession.SourceRetrievalListener listener);
}
private static final class SourceCallbackImpl
extends ActivitySourceCallback<AddSourceActivity> {
private final boolean mUpdatesCustomer;
private SourceCallbackImpl(@NonNull AddSourceActivity activity,
boolean updatesCustomer) {
super(activity);
mUpdatesCustomer = updatesCustomer;
}
@Override
public void onError(@NonNull Exception error) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}
activity.setCommunicatingProgress(false);
// This error is independent of the CustomerSession, so we have to surface it here.
activity.showError(error.getLocalizedMessage());
}
@Override
public void onSuccess(@NonNull Source source) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}
if (mUpdatesCustomer) {
activity.attachCardToCustomer(source);
} else {
activity.finishWithSource(source);
}
}
}
private static final class SourceRetrievalListenerImpl
extends CustomerSession.ActivitySourceRetrievalListener<AddSourceActivity> {
private SourceRetrievalListenerImpl(@NonNull AddSourceActivity activity) {
super(activity);
}
@Override
public void onSourceRetrieved(@NonNull Source source) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}
activity.finishWithSource(source);
}
@Override
public void onError(int errorCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
final AddSourceActivity activity = getActivity();
if (activity == null) {
return;
}
// No need to show this error, because it will be broadcast
// from the CustomerSession
activity.setCommunicatingProgress(false);
}
}
}