forked from stripe/stripe-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntentServiceTokenController.java
122 lines (105 loc) · 4.32 KB
/
IntentServiceTokenController.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
package com.stripe.example.controller;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.stripe.android.model.Card;
import com.stripe.android.view.CardInputWidget;
import com.stripe.example.service.TokenIntentService;
/**
* Example class showing how to save tokens with an {@link android.app.IntentService} doing your
* background I/O work.
*/
public class IntentServiceTokenController {
private Activity mActivity;
private CardInputWidget mCardInputWidget;
private ErrorDialogHandler mErrorDialogHandler;
private ListViewController mOutputListViewController;
private ProgressDialogController mProgressDialogController;
private String mPublishableKey;
private TokenBroadcastReceiver mTokenBroadcastReceiver;
public IntentServiceTokenController (
@NonNull AppCompatActivity appCompatActivity,
@NonNull Button button,
@NonNull CardInputWidget cardInputWidget,
@NonNull ErrorDialogHandler errorDialogHandler,
@NonNull ListViewController outputListController,
@NonNull ProgressDialogController progressDialogController,
@NonNull String publishableKey) {
mActivity = appCompatActivity;
mCardInputWidget = cardInputWidget;
mErrorDialogHandler = errorDialogHandler;
mOutputListViewController = outputListController;
mProgressDialogController = progressDialogController;
mPublishableKey = publishableKey;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveCard();
}
});
registerBroadcastReceiver();
}
/**
* Unregister the {@link BroadcastReceiver}.
*/
public void detach() {
if (mTokenBroadcastReceiver != null) {
LocalBroadcastManager.getInstance(mActivity)
.unregisterReceiver(mTokenBroadcastReceiver);
mTokenBroadcastReceiver = null;
mActivity = null;
}
mCardInputWidget = null;
}
private void registerBroadcastReceiver() {
mTokenBroadcastReceiver = new TokenBroadcastReceiver();
LocalBroadcastManager.getInstance(mActivity).registerReceiver(
mTokenBroadcastReceiver,
new IntentFilter(TokenIntentService.TOKEN_ACTION));
}
private void saveCard() {
Card cardToSave = mCardInputWidget.getCard();
if (cardToSave == null) {
mErrorDialogHandler.showError("Invalid Card Data");
return;
}
Intent tokenServiceIntent = TokenIntentService.createTokenIntent(
mActivity,
cardToSave.getNumber(),
cardToSave.getExpMonth(),
cardToSave.getExpYear(),
cardToSave.getCVC(),
mPublishableKey);
mProgressDialogController.startProgress();
mActivity.startService(tokenServiceIntent);
}
private class TokenBroadcastReceiver extends BroadcastReceiver {
// Prevent instantiation of a local broadcast receiver outside this class.
private TokenBroadcastReceiver() { }
@Override
public void onReceive(Context context, Intent intent) {
mProgressDialogController.finishProgress();
if (intent == null) {
return;
}
if (intent.hasExtra(TokenIntentService.STRIPE_ERROR_MESSAGE)) {
mErrorDialogHandler.showError(
intent.getStringExtra(TokenIntentService.STRIPE_ERROR_MESSAGE));
return;
}
if (intent.hasExtra(TokenIntentService.STRIPE_CARD_TOKEN_ID) &&
intent.hasExtra(TokenIntentService.STRIPE_CARD_LAST_FOUR)) {
mOutputListViewController.addToList(
intent.getStringExtra(TokenIntentService.STRIPE_CARD_LAST_FOUR),
intent.getStringExtra(TokenIntentService.STRIPE_CARD_TOKEN_ID));
}
}
}
}