-
Notifications
You must be signed in to change notification settings - Fork 78
/
DropInActivity.java
357 lines (309 loc) · 14.9 KB
/
DropInActivity.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
package com.braintreepayments.api.dropin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.braintreepayments.api.AndroidPay;
import com.braintreepayments.api.BraintreeFragment;
import com.braintreepayments.api.DataCollector;
import com.braintreepayments.api.PayPal;
import com.braintreepayments.api.PaymentMethod;
import com.braintreepayments.api.Venmo;
import com.braintreepayments.api.dropin.adapters.SupportedPaymentMethodsAdapter;
import com.braintreepayments.api.dropin.adapters.SupportedPaymentMethodsAdapter.PaymentMethodSelectedListener;
import com.braintreepayments.api.dropin.adapters.VaultedPaymentMethodsAdapter;
import com.braintreepayments.api.dropin.interfaces.AnimationFinishedListener;
import com.braintreepayments.api.dropin.utils.PaymentMethodType;
import com.braintreepayments.api.exceptions.AuthenticationException;
import com.braintreepayments.api.exceptions.AuthorizationException;
import com.braintreepayments.api.exceptions.ConfigurationException;
import com.braintreepayments.api.exceptions.DownForMaintenanceException;
import com.braintreepayments.api.exceptions.InvalidArgumentException;
import com.braintreepayments.api.exceptions.ServerException;
import com.braintreepayments.api.exceptions.UnexpectedException;
import com.braintreepayments.api.exceptions.UpgradeRequiredException;
import com.braintreepayments.api.interfaces.BraintreeCancelListener;
import com.braintreepayments.api.interfaces.BraintreeErrorListener;
import com.braintreepayments.api.interfaces.BraintreeResponseListener;
import com.braintreepayments.api.interfaces.ConfigurationListener;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCreatedListener;
import com.braintreepayments.api.interfaces.PaymentMethodNoncesUpdatedListener;
import com.braintreepayments.api.models.Authorization;
import com.braintreepayments.api.models.ClientToken;
import com.braintreepayments.api.models.Configuration;
import com.braintreepayments.api.models.PaymentMethodNonce;
import java.util.List;
import static android.view.animation.AnimationUtils.loadAnimation;
public class DropInActivity extends Activity implements ConfigurationListener, BraintreeCancelListener,
BraintreeErrorListener, PaymentMethodSelectedListener, PaymentMethodNoncesUpdatedListener,
PaymentMethodNonceCreatedListener {
/**
* Errors are returned as the serializable value of this key in the data intent in
* {@link android.app.Activity#onActivityResult(int, int, android.content.Intent)} if
* responseCode is not {@link android.app.Activity#RESULT_OK} or
* {@link android.app.Activity#RESULT_CANCELED}.
*/
public static final String EXTRA_ERROR = "com.braintreepayments.api.dropin.EXTRA_ERROR";
private static final int ADD_CARD_REQUEST_CODE = 1;
private static final String EXTRA_SHEET_SLIDE_UP_PERFORMED = "com.braintreepayments.api.EXTRA_SHEET_SLIDE_UP_PERFORMED";
private static final String EXTRA_DEVICE_DATA = "com.braintreepayments.api.EXTRA_DEVICE_DATA";
@VisibleForTesting
protected DropInRequest mDropInRequest;
private BraintreeFragment mBraintreeFragment;
private boolean mClientTokenPresent;
private String mDeviceData;
private View mBottomSheet;
private ViewSwitcher mLoadingViewSwitcher;
private TextView mSupportedPaymentMethodsHeader;
private ListView mSupportedPaymentMethodListView;
private View mVaultedPaymentMethodsContainer;
private RecyclerView mVaultedPaymentMethodsView;
private boolean mSheetSlideUpPerformed;
private boolean mSheetSlideDownPerformed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bt_drop_in_activity);
mDropInRequest = getIntent().getParcelableExtra(DropInRequest.EXTRA_CHECKOUT_REQUEST);
mBottomSheet = findViewById(R.id.bt_dropin_bottom_sheet);
mLoadingViewSwitcher = (ViewSwitcher) findViewById(R.id.bt_loading_view_switcher);
mSupportedPaymentMethodsHeader = (TextView) findViewById(R.id.bt_supported_payment_methods_header);
mSupportedPaymentMethodListView = (ListView) findViewById(R.id.bt_supported_payment_methods);
mVaultedPaymentMethodsContainer = findViewById(R.id.bt_vaulted_payment_methods_wrapper);
mVaultedPaymentMethodsView = (RecyclerView) findViewById(R.id.bt_vaulted_payment_methods);
mVaultedPaymentMethodsView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
new LinearSnapHelper().attachToRecyclerView(mVaultedPaymentMethodsView);
try {
mBraintreeFragment = getBraintreeFragment();
} catch (InvalidArgumentException e) {
Intent intent = new Intent()
.putExtra(EXTRA_ERROR, e.getMessage());
setResult(RESULT_FIRST_USER, intent);
finish();
return;
}
if (savedInstanceState != null) {
mSheetSlideUpPerformed = savedInstanceState.getBoolean(EXTRA_SHEET_SLIDE_UP_PERFORMED,
false);
mDeviceData = savedInstanceState.getString(EXTRA_DEVICE_DATA);
}
slideUp();
}
@VisibleForTesting
protected BraintreeFragment getBraintreeFragment() throws InvalidArgumentException {
if (TextUtils.isEmpty(mDropInRequest.getAuthorization())) {
throw new InvalidArgumentException("A client token or client key must be specified " +
"in the " + DropInRequest.class.getSimpleName());
}
try {
mClientTokenPresent =
Authorization.fromString(mDropInRequest.getAuthorization()) instanceof ClientToken;
} catch (InvalidArgumentException e) {
mClientTokenPresent = false;
}
return BraintreeFragment.newInstance(this, mDropInRequest.getAuthorization());
}
@Override
public void onConfigurationFetched(final Configuration configuration) {
if (mDropInRequest.shouldCollectDeviceData() && TextUtils.isEmpty(mDeviceData)) {
DataCollector.collectDeviceData(mBraintreeFragment, new BraintreeResponseListener<String>() {
@Override
public void onResponse(String deviceData) {
mDeviceData = deviceData;
}
});
}
AndroidPay.isReadyToPay(mBraintreeFragment, new BraintreeResponseListener<Boolean>() {
@Override
public void onResponse(Boolean isReadyToPay) {
mSupportedPaymentMethodListView.setAdapter(new SupportedPaymentMethodsAdapter(
DropInActivity.this, configuration, isReadyToPay, mClientTokenPresent,
DropInActivity.this));
mLoadingViewSwitcher.setDisplayedChild(1);
fetchPaymentMethodNonces();
}
});
}
@Override
public void onCancel(int requestCode) {
mLoadingViewSwitcher.setDisplayedChild(1);
}
@Override
public void onError(final Exception error) {
slideDown(new AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
if (error instanceof AuthenticationException || error instanceof AuthorizationException ||
error instanceof UpgradeRequiredException) {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.developer-error");
} else if (error instanceof ConfigurationException) {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.configuration-exception");
} else if (error instanceof ServerException || error instanceof UnexpectedException) {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.server-error");
} else if (error instanceof DownForMaintenanceException) {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.server-unavailable");
} else {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.sdk-error");
}
setResult(RESULT_FIRST_USER, new Intent().putExtra(EXTRA_ERROR, error));
finish();
}
});
}
@Override
public void onPaymentMethodNonceCreated(final PaymentMethodNonce paymentMethodNonce) {
slideDown(new AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.success");
DropInResult.setLastUsedPaymentMethodType(DropInActivity.this, paymentMethodNonce);
DropInResult result = new DropInResult()
.paymentMethodNonce(paymentMethodNonce)
.deviceData(mDeviceData);
Intent intent = new Intent().putExtra(DropInResult.EXTRA_DROP_IN_RESULT, result);
setResult(RESULT_OK, intent);
finish();
}
});
}
@Override
public void onPaymentMethodSelected(PaymentMethodType type) {
mLoadingViewSwitcher.setDisplayedChild(0);
switch (type) {
case PAYPAL:
PayPal.authorizeAccount(mBraintreeFragment);
break;
case ANDROID_PAY:
AndroidPay.requestAndroidPay(mBraintreeFragment, mDropInRequest.getAndroidPayCart(),
mDropInRequest.isAndroidPayShippingAddressRequired(),
mDropInRequest.isAndroidPayPhoneNumberRequired(),
mDropInRequest.getAndroidPayAllowedCountriesForShipping());
break;
case PAY_WITH_VENMO:
Venmo.authorizeAccount(mBraintreeFragment);
break;
case UNKNOWN:
Intent intent = new Intent(this, AddCardActivity.class)
.putExtra(DropInRequest.EXTRA_CHECKOUT_REQUEST, mDropInRequest);
startActivityForResult(intent, ADD_CARD_REQUEST_CODE);
break;
}
}
private void fetchPaymentMethodNonces() {
try {
if (Authorization.fromString(mDropInRequest.getAuthorization()) instanceof ClientToken) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!DropInActivity.this.isFinishing()) {
if (mBraintreeFragment.hasFetchedPaymentMethodNonces()) {
onPaymentMethodNoncesUpdated(mBraintreeFragment.getCachedPaymentMethodNonces());
} else {
PaymentMethod.getPaymentMethodNonces(mBraintreeFragment, true);
}
}
}
}, getResources().getInteger(android.R.integer.config_shortAnimTime));
}
} catch (InvalidArgumentException ignored) {}
}
@Override
public void onPaymentMethodNoncesUpdated(final List<PaymentMethodNonce> paymentMethodNonces) {
if (paymentMethodNonces.size() > 0) {
mSupportedPaymentMethodsHeader.setText(R.string.bt_other);
mVaultedPaymentMethodsContainer.setVisibility(View.VISIBLE);
mVaultedPaymentMethodsView.setAdapter(new VaultedPaymentMethodsAdapter(this, paymentMethodNonces));
} else {
mSupportedPaymentMethodsHeader.setText(R.string.bt_select_payment_method);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(EXTRA_SHEET_SLIDE_UP_PERFORMED, mSheetSlideUpPerformed);
outState.putString(EXTRA_DEVICE_DATA, mDeviceData);
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, Intent data) {
mLoadingViewSwitcher.setDisplayedChild(0);
if (resultCode == Activity.RESULT_CANCELED) {
mLoadingViewSwitcher.setDisplayedChild(1);
} else if (requestCode == ADD_CARD_REQUEST_CODE) {
final Intent response;
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
DropInResult.setLastUsedPaymentMethodType(this, result.getPaymentMethodNonce());
result.deviceData(mDeviceData);
response = new Intent()
.putExtra(DropInResult.EXTRA_DROP_IN_RESULT, result);
} else {
response = data;
}
slideDown(new AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
setResult(resultCode, response);
finish();
}
});
}
}
public void onBackgroundClicked(View v) {
onBackPressed();
}
@Override
public void onBackPressed() {
if (!mSheetSlideDownPerformed) {
mSheetSlideDownPerformed = true;
mBraintreeFragment.sendAnalyticsEvent("sdk.exit.canceled");
slideDown(new AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
finish();
}
});
}
}
private void slideUp() {
if (!mSheetSlideUpPerformed) {
mBraintreeFragment.sendAnalyticsEvent("appeared");
mSheetSlideUpPerformed = true;
mBottomSheet.startAnimation(loadAnimation(this, R.anim.bt_slide_in_up));
}
}
private void slideDown(final AnimationFinishedListener listener) {
Animation slideOutAnimation = loadAnimation(this, R.anim.bt_slide_out_down);
slideOutAnimation.setFillAfter(true);
if (listener != null) {
slideOutAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
listener.onAnimationFinished();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
mBottomSheet.startAnimation(slideOutAnimation);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}