-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMainActivity.java
More file actions
255 lines (215 loc) · 9.61 KB
/
MainActivity.java
File metadata and controls
255 lines (215 loc) · 9.61 KB
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
package tech.cherri.googlepayexample;
import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.wallet.AutoResolveHelper;
import com.google.android.gms.wallet.PaymentData;
import com.google.android.gms.wallet.TransactionInfo;
import com.google.android.gms.wallet.WalletConstants;
import org.json.JSONException;
import org.json.JSONObject;
import tech.cherri.tpdirect.api.TPDCard;
import tech.cherri.tpdirect.api.TPDConsumer;
import tech.cherri.tpdirect.api.TPDGooglePay;
import tech.cherri.tpdirect.api.TPDMerchant;
import tech.cherri.tpdirect.api.TPDSetup;
import tech.cherri.tpdirect.callback.TPDGetPrimeFailureCallback;
import tech.cherri.tpdirect.callback.TPDGooglePayGetPrimeSuccessCallback;
import tech.cherri.tpdirect.callback.TPDGooglePayListener;
import tech.cherri.tpdirect.callback.dto.TPDCardInfoDto;
import tech.cherri.tpdirect.callback.dto.TPDMerchantReferenceInfoDto;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
TPDGooglePayListener, TPDGetPrimeFailureCallback, TPDGooglePayGetPrimeSuccessCallback {
private static final String TAG = "MainActivity";
private TPDCard.CardType[] allowedNetworks = new TPDCard.CardType[]{TPDCard.CardType.Visa
, TPDCard.CardType.MasterCard
, TPDCard.CardType.JCB
, TPDCard.CardType.AmericanExpress};
private TPDCard.AuthMethod[] allowedAuthMethods = new TPDCard.AuthMethod[]{TPDCard.AuthMethod.Cryptogram3DS};
private static final int REQUEST_READ_PHONE_STATE = 101;
private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 102;
private TPDGooglePay tpdGooglePay;
private RelativeLayout googlePaymentBuyBTN;
private TextView totalAmountTV, googlePaymentResultStateTV;
private TextView buyerInformationTV;
private PaymentData paymentData;
private Button confirmBTN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
Log.d(TAG, "SDK version is " + TPDSetup.getVersion());
//Setup environment.
TPDSetup.initInstance(getApplicationContext(),
Constants.APP_ID, Constants.APP_KEY, Constants.SERVER_TYPE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
requestPermissions();
} else {
prepareGooglePay();
}
}
private void setupViews() {
totalAmountTV = (TextView) findViewById(R.id.totalAmountTV);
totalAmountTV.setText("Total amount : 1.00 元");
googlePaymentBuyBTN = (RelativeLayout) findViewById(R.id.googlePaymentBuyBTN);
googlePaymentBuyBTN.setOnClickListener(this);
googlePaymentBuyBTN.setEnabled(false);
buyerInformationTV = (TextView) findViewById(R.id.buyerInformationTV);
confirmBTN = (Button) findViewById(R.id.confirmBTN);
confirmBTN.setOnClickListener(this);
confirmBTN.setEnabled(false);
googlePaymentResultStateTV = (TextView) findViewById(R.id.googlePaymentResultStateTV);
}
@RequiresApi(Build.VERSION_CODES.M)
private void requestPermissions() {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "PERMISSION IS ALREADY GRANTED");
prepareGooglePay();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_READ_PHONE_STATE:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.d(TAG, "PERMISSION_GRANTED");
}
prepareGooglePay();
break;
default:
break;
}
}
public void prepareGooglePay() {
TPDMerchant tpdMerchant = new TPDMerchant();
tpdMerchant.setSupportedNetworks(allowedNetworks);
tpdMerchant.setMerchantName(Constants.TEST_MERCHANT_NAME);
tpdMerchant.setSupportedAuthMethods(allowedAuthMethods);
TPDConsumer tpdConsumer = new TPDConsumer();
tpdConsumer.setPhoneNumberRequired(false);
tpdConsumer.setShippingAddressRequired(false);
tpdConsumer.setEmailRequired(false);
tpdGooglePay = new TPDGooglePay(this, tpdMerchant, tpdConsumer);
tpdGooglePay.isGooglePayAvailable(this);
}
@Override
public void onReadyToPayChecked(boolean isReadyToPay, String msg) {
Log.d(TAG, "Pay with Google availability : " + isReadyToPay + ", msg : " + msg);
if (isReadyToPay) {
googlePaymentBuyBTN.setEnabled(true);
} else {
showMessage("Cannot use Pay with Google.");
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.googlePaymentBuyBTN) {
tpdGooglePay.requestPayment(TransactionInfo.newBuilder()
.setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
.setTotalPrice("1")
.setCurrencyCode("TWD")
.build(), LOAD_PAYMENT_DATA_REQUEST_CODE);
} else if (view.getId() == R.id.confirmBTN) {
getPrimeFromTapPay(paymentData);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case LOAD_PAYMENT_DATA_REQUEST_CODE:
switch (resultCode) {
case Activity.RESULT_OK:
confirmBTN.setEnabled(true);
paymentData = PaymentData.getFromIntent(data);
revealPaymentInfo(paymentData);
break;
case Activity.RESULT_CANCELED:
confirmBTN.setEnabled(false);
showMessage("Canceled by User");
break;
case AutoResolveHelper.RESULT_ERROR:
confirmBTN.setEnabled(false);
Status status = AutoResolveHelper.getStatusFromIntent(data);
Log.d(TAG, "AutoResolveHelper.RESULT_ERROR : " + status.getStatusCode() + " , message = " + status.getStatusMessage());
showMessage(status.getStatusCode() + " , message = " + status.getStatusMessage());
break;
default:
// Do nothing.
}
break;
default:
// Do nothing.
}
}
private void revealPaymentInfo(PaymentData paymentData) {
try {
JSONObject paymentDataJO = new JSONObject(paymentData.toJson());
String cardDescription = paymentDataJO.getJSONObject("paymentMethodData").getString
("description");
buyerInformationTV.setText("Card Description : " + cardDescription + "\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getPrimeFromTapPay(PaymentData paymentData) {
showProgressDialog();
tpdGooglePay.getPrime(paymentData, this, this);
}
@Override
public void onSuccess(String prime, TPDCardInfoDto cardInfo, TPDMerchantReferenceInfoDto merchantReferenceInfo) {
hideProgressDialog();
String resultStr = "prime is " + prime + "\n" +
"cardInfo is " + cardInfo + "\n" +
"merchantReferenceInfo is " + merchantReferenceInfo + "\n\n" +
"Use below cURL to proceed the payment : \n"
+ ApiUtil.generatePayByPrimeCURLForSandBox(prime, Constants.PARTNER_KEY,
Constants.MERCHANT_ID);
showMessage(resultStr);
Log.d(TAG, "prime = " + prime);
Log.d(TAG, "cardInfo = " + cardInfo);
Log.d(TAG, "merchantReferenceInfo = " + merchantReferenceInfo);
Log.d(TAG, resultStr);
}
@Override
public void onFailure(int status, String msg) {
hideProgressDialog();
showMessage("TapPay getPrime failed , status = " + status + ", msg : " + msg);
Log.d(TAG, "TapPay getPrime failed : " + status + ", msg : " + msg);
}
private void showMessage(String s) {
googlePaymentResultStateTV.setText(s);
}
public ProgressDialog mProgressDialog;
protected void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
}
mProgressDialog.show();
}
protected void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}