-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentMethodsActivity.java
369 lines (326 loc) · 14.6 KB
/
PaymentMethodsActivity.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
package com.stripe.android.view;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import com.stripe.android.CustomerSession;
import com.stripe.android.R;
import com.stripe.android.StripeError;
import com.stripe.android.model.Customer;
import com.stripe.android.model.CustomerSource;
import com.stripe.android.view.i18n.TranslatorManager;
import java.util.List;
import static com.stripe.android.PaymentSession.EXTRA_PAYMENT_SESSION_ACTIVE;
import static com.stripe.android.PaymentSession.TOKEN_PAYMENT_SESSION;
/**
* An activity that allows a user to select from a customer's available payment methods, or
* to add new ones.
*/
public class PaymentMethodsActivity extends AppCompatActivity {
public static final String EXTRA_SELECTED_PAYMENT = "selected_payment";
static final String EXTRA_PROXY_DELAY = "proxy_delay";
static final String PAYMENT_METHODS_ACTIVITY = "PaymentMethodsActivity";
static final int REQUEST_CODE_ADD_CARD = 700;
private boolean mCommunicating;
private Customer mCustomer;
private MaskedCardAdapter mMaskedCardAdapter;
private ProgressBar mProgressBar;
private RecyclerView mRecyclerView;
private boolean mRecyclerViewUpdated;
private boolean mStartedFromPaymentSession;
private CustomerSession mCustomerSession;
/**
* @deprecated use {@link PaymentMethodsActivityStarter#newIntent()}
*/
@Deprecated
@NonNull
public static Intent newIntent(@NonNull Activity activity) {
return new PaymentMethodsActivityStarter(activity).newIntent();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_methods);
mProgressBar = findViewById(R.id.payment_methods_progress_bar);
mRecyclerView = findViewById(R.id.payment_methods_recycler);
final View addCardView = findViewById(R.id.payment_methods_add_payment_container);
mCustomerSession = CustomerSession.getInstance();
mStartedFromPaymentSession = getIntent().hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE);
addCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull View view) {
final Intent addSourceIntent = AddSourceActivity.newIntent(
PaymentMethodsActivity.this,
false,
true);
if (mStartedFromPaymentSession) {
addSourceIntent.putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true);
}
startActivityForResult(addSourceIntent, REQUEST_CODE_ADD_CARD);
}
});
final Toolbar toolbar = findViewById(R.id.payment_methods_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
boolean waitForProxy = getIntent().getBooleanExtra(EXTRA_PROXY_DELAY, false);
if (!waitForProxy) {
initializeCustomerSourceData();
}
// This prevents the first click from being eaten by the focus.
addCardView.requestFocusFromTouch();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_ADD_CARD && resultCode == RESULT_OK) {
setCommunicatingProgress(true);
initLoggingTokens();
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
updateCustomerAndSetDefaultSourceIfNecessary(customer);
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
final String displayedError = TranslatorManager
.getErrorMessageTranslator()
.translate(httpCode, errorMessage, stripeError);
showError(displayedError);
setCommunicatingProgress(false);
}
};
mCustomerSession.updateCurrentCustomer(listener);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final MenuItem saveItem = menu.findItem(R.id.action_save);
final Drawable compatIcon = ViewUtils.getTintedIconWithAttribute(
this,
getTheme(),
R.attr.titleTextColor,
R.drawable.ic_checkmark);
saveItem.setIcon(compatIcon);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_source_menu, menu);
menu.findItem(R.id.action_save).setEnabled(!mCommunicating);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_save) {
setSelectionAndFinish();
return true;
} else {
final boolean handled = super.onOptionsItemSelected(item);
if (!handled) {
onBackPressed();
}
return handled;
}
}
@VisibleForTesting
void initializeCustomerSourceData() {
final Customer cachedCustomer = mCustomerSession.getCachedCustomer();
if (cachedCustomer != null) {
mCustomer = cachedCustomer;
createListFromCustomerSources();
} else {
getCustomerFromSession();
}
}
private void initLoggingTokens() {
if (mStartedFromPaymentSession) {
mCustomerSession.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION);
}
mCustomerSession.addProductUsageTokenIfValid(PAYMENT_METHODS_ACTIVITY);
}
/**
* Update our currently displayed customer data. If the customer only has one
* {@link CustomerSource} and does not have a {@link Customer#mDefaultSource defaultSource},
* then set that one source to be the default source before updating displayed source data.
*
* @param customer the new {@link Customer} object whose sources should be displayed
*/
private void updateCustomerAndSetDefaultSourceIfNecessary(@NonNull Customer customer) {
// An inverted early return - we don't need to talk to the CustomerSession if there is
// already a default source selected or we have no or more than one customer sources in our
// list.
if (!TextUtils.isEmpty(customer.getDefaultSource()) || customer.getSources().size() != 1) {
updateAdapterWithCustomer(customer);
return;
}
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
updateAdapterWithCustomer(customer);
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
// Note: if this Activity is changed to subclass StripeActivity,
// this code will make the error message show twice, since StripeActivity
// will listen to the broadcast version of the error
// coming from CustomerSession
final String displayedError = TranslatorManager.getErrorMessageTranslator()
.translate(httpCode, errorMessage, stripeError);
showError(displayedError);
setCommunicatingProgress(false);
}
};
// We only activate this if there is a single source in the list
final CustomerSource customerSource = customer.getSources().get(0);
if (customerSource == null || customerSource.getId() == null) {
// If the source ID is null for the only source we have, then there is nothing
// we can do but update the display. This should not happen. It is only possible
// for a CustomerSource to have null ID because a Card is a customer source, and
// before those are sent to Stripe, they haven't yet been assigned an ID.
updateAdapterWithCustomer(customer);
return;
}
mCustomerSession.setCustomerDefaultSource(
this,
customerSource.getId(),
customerSource.getSourceType(),
listener);
}
private void createListFromCustomerSources() {
setCommunicatingProgress(false);
if (mCustomer == null) {
return;
}
final List<CustomerSource> customerSourceList = mCustomer.getSources();
if (!mRecyclerViewUpdated) {
mMaskedCardAdapter = new MaskedCardAdapter(customerSourceList);
// init the RecyclerView
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mMaskedCardAdapter);
mRecyclerViewUpdated = true;
} else {
mMaskedCardAdapter.setCustomerSourceList(customerSourceList);
}
final String defaultSource = mCustomer.getDefaultSource();
if (defaultSource != null && !TextUtils.isEmpty(defaultSource)) {
mMaskedCardAdapter.setSelectedSource(defaultSource);
}
mMaskedCardAdapter.notifyDataSetChanged();
}
private void cancelAndFinish() {
setResult(RESULT_CANCELED);
finish();
}
private void finishWithSelection(String selectedSourceId) {
final CustomerSource customerSource = mCustomer.getSourceById(selectedSourceId);
if (customerSource != null) {
Intent intent = new Intent();
intent.putExtra(EXTRA_SELECTED_PAYMENT, customerSource.toJson().toString());
setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
private void getCustomerFromSession() {
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
mCustomer = customer;
createListFromCustomerSources();
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
setCommunicatingProgress(false);
}
};
setCommunicatingProgress(true);
mCustomerSession.retrieveCurrentCustomer(listener);
}
private void setCommunicatingProgress(boolean communicating) {
mCommunicating = communicating;
if (communicating) {
mProgressBar.setVisibility(View.VISIBLE);
} else {
mProgressBar.setVisibility(View.GONE);
}
supportInvalidateOptionsMenu();
}
private void setSelectionAndFinish() {
if (mMaskedCardAdapter == null || mMaskedCardAdapter.getSelectedSource() == null) {
cancelAndFinish();
return;
}
final CustomerSource selectedSource = mMaskedCardAdapter.getSelectedSource();
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
mCustomer = customer;
finishWithSelection(customer.getDefaultSource());
setCommunicatingProgress(false);
}
@Override
public void onError(int httpCode, @Nullable String errorMessage,
@Nullable StripeError stripeError) {
final String displayedError = TranslatorManager.getErrorMessageTranslator()
.translate(httpCode, errorMessage, stripeError);
showError(displayedError);
setCommunicatingProgress(false);
}
};
if (selectedSource == null || selectedSource.getId() == null) {
return;
}
mCustomerSession.setCustomerDefaultSource(
this, selectedSource.getId(), selectedSource.getSourceType(), listener);
setCommunicatingProgress(true);
}
private void showError(@NonNull String error) {
new AlertDialog.Builder(this)
.setMessage(error)
.setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create()
.show();
}
private void updateAdapterWithCustomer(@NonNull Customer customer) {
if (mMaskedCardAdapter == null) {
createListFromCustomerSources();
if (mCustomer == null) {
return;
}
}
mMaskedCardAdapter.updateCustomer(customer);
setCommunicatingProgress(false);
}
}