/
CheckoutActivity.java
executable file
·291 lines (264 loc) · 12.3 KB
/
CheckoutActivity.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
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gms.samples.wallet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.wallet.AutoResolveHelper;
import com.google.android.gms.wallet.IsReadyToPayRequest;
import com.google.android.gms.wallet.PaymentData;
import com.google.android.gms.wallet.PaymentDataRequest;
import com.google.android.gms.wallet.PaymentsClient;
import java.util.Optional;
import org.json.JSONException;
import org.json.JSONObject;
public class CheckoutActivity extends Activity {
/**
* A client for interacting with the Google Pay API.
*
* @see <a
* href="https://developers.google.com/android/reference/com/google/android/gms/wallet/PaymentsClient">PaymentsClient</a>
*/
private PaymentsClient mPaymentsClient;
/**
* A Google Pay payment button presented to the viewer for interaction.
*
* @see <a href="https://developers.google.com/pay/api/android/guides/brand-guidelines">Google Pay
* payment button brand guidelines</a>
*/
private View mGooglePayButton;
// Arbitrarily-picked constant integer you define to track a request for payment data activity */
private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 991;
private TextView mGooglePayStatusText;
private ItemInfo mBikeItem = new ItemInfo("Simple Bike", 300 * 1000000, R.drawable.bike);
private long mShippingCost = 90 * 1000000;
/**
* Initialize the Google Pay API on creation of the activity
*
* @see Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
// Set up the mock information for our item in the UI.
initItemUI();
mGooglePayButton = findViewById(R.id.googlepay_button);
mGooglePayStatusText = findViewById(R.id.googlepay_status);
// Initialize a Google Pay API client for an environment suitable for testing.
// It's recommended to create the PaymentsClient object inside of the onCreate method.
mPaymentsClient = PaymentsUtil.createPaymentsClient(this);
possiblyShowGooglePayButton();
mGooglePayButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPayment(view);
}
});
}
/**
* Determine the viewer's ability to pay with a payment method supported by your app and display a
* Google Pay payment button.
*
* @see <a href=
* "https://developers.google.com/android/reference/com/google/android/gms/wallet/PaymentsClient.html#isReadyToPay(com.google.android.gms.wallet.IsReadyToPayRequest)">PaymentsClient#IsReadyToPay</a>
*/
private void possiblyShowGooglePayButton() {
final Optional<JSONObject> isReadyToPayJson = PaymentsUtil.getIsReadyToPayRequest();
if (!isReadyToPayJson.isPresent()) {
return;
}
IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(isReadyToPayJson.get().toString());
if (request == null) {
return;
}
// The call to isReadyToPay is asynchronous and returns a Task. We need to provide an
// OnCompleteListener to be triggered when the result of the call is known.
Task<Boolean> task = mPaymentsClient.isReadyToPay(request);
task.addOnCompleteListener(
new OnCompleteListener<Boolean>() {
@Override
public void onComplete(Task<Boolean> task) {
try {
boolean result = task.getResult(ApiException.class);
setGooglePayAvailable(result);
} catch (ApiException exception) {
// Process error
Log.w("isReadyToPay failed", exception);
}
}
});
}
/**
* If isReadyToPay returned true, show the button and hide the "checking" text. Otherwise, notify
* the user that Pay with Google is not available. Please adjust to fit in with your current user
* flow. You are not required to explicitly let the user know if isReadyToPay returns false.
*
* @param available IsReadyToPay or not.
*/
private void setGooglePayAvailable(boolean available) {
if (available) {
mGooglePayStatusText.setVisibility(View.GONE);
mGooglePayButton.setVisibility(View.VISIBLE);
} else {
mGooglePayStatusText.setText(R.string.googlepay_status_unavailable);
}
}
/**
* Handle a resolved activity from the Google Pay payment sheet.
*
* @param requestCode Request code originally supplied to AutoResolveHelper in requestPayment().
* @param resultCode Result code returned by the Google Pay API.
* @param data Intent from the Google Pay API containing payment or error data.
* @see <a href="https://developer.android.com/training/basics/intents/result">Getting a result
* from an Activity</a>
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// value passed in AutoResolveHelper
case LOAD_PAYMENT_DATA_REQUEST_CODE:
switch (resultCode) {
case Activity.RESULT_OK:
PaymentData paymentData = PaymentData.getFromIntent(data);
handlePaymentSuccess(paymentData);
break;
case Activity.RESULT_CANCELED:
// Nothing to here normally - the user simply cancelled without selecting a
// payment method.
break;
case AutoResolveHelper.RESULT_ERROR:
Status status = AutoResolveHelper.getStatusFromIntent(data);
handleError(status.getStatusCode());
break;
default:
// Do nothing.
}
// Re-enables the Pay with Google button.
mGooglePayButton.setClickable(true);
break;
}
}
/**
* PaymentData response object contains the payment information, as well as any additional
* requested information, such as billing and shipping address.
*
* @param paymentData A response object returned by Google after a payer approves payment.
* @see <a
* href="https://developers.google.com/pay/api/android/reference/object#PaymentData">Payment
* Data
*/
private void handlePaymentSuccess(PaymentData paymentData) {
String token = paymentData.toJson();
// Token will be null if PaymentDataRequest was not constructed using fromJson(String).
if (token != null) {
JSONObject tokenObject;
try {
tokenObject = new JSONObject(token);
// If the gateway is set to example, no payment information is returned - instead, the
// token will only consist of "examplePaymentMethodToken".
if (tokenObject
.getJSONObject("paymentMethodData")
.getJSONObject("tokenizationData")
.getString("token")
.equals("examplePaymentMethodToken")) {
AlertDialog alertDialog =
new AlertDialog.Builder(this)
.setTitle("Warning")
.setMessage(
"Gateway name set to \"example\" - please modify "
+ "Constants.java and replace it with your own gateway.")
.setPositiveButton("OK", null)
.create();
alertDialog.show();
}
String billingName =
tokenObject
.getJSONObject("paymentMethodData")
.getJSONObject("info")
.getJSONObject("billingAddress")
.getString("name");
Log.d("BillingName", billingName);
Toast.makeText(this, getString(R.string.payments_show_name, billingName), Toast.LENGTH_LONG)
.show();
// Logging token string.
Log.d(
"PaymentData",
tokenObject
.getJSONObject("paymentMethodData")
.getJSONObject("tokenizationData")
.getString("token"));
} catch (JSONException e) {
Log.e("handlePaymentSuccess failed", e.toString());
return;
}
}
}
/**
* At this stage, the user has already seen a popup informing them an error occurred. Normally,
* only logging is required.
*
* @param statusCode will hold the value of any constant from CommonStatusCode or one of the
* WalletConstants.ERROR_CODE_* constants.
* @see <a
* href="https://developers.google.com/android/reference/com/google/android/gms/wallet/WalletConstants#constant-summary">Wallet
* Constants Library</a>
*/
private void handleError(int statusCode) {
Log.w("loadPaymentData failed", String.format("Error code: %d", statusCode));
}
// This method is called when the Pay with Google button is clicked.
public void requestPayment(View view) {
// Disables the button to prevent multiple clicks.
mGooglePayButton.setClickable(false);
// The price provided to the API should include taxes and shipping.
// This price is not displayed to the user.
String price = PaymentsUtil.microsToString(mBikeItem.getPriceMicros() + mShippingCost);
// TransactionInfo transaction = PaymentsUtil.createTransaction(price);
Optional<JSONObject> paymentDataRequestJson = PaymentsUtil.getPaymentDataRequest(price);
if (!paymentDataRequestJson.isPresent()) {
return;
}
PaymentDataRequest request =
PaymentDataRequest.fromJson(paymentDataRequestJson.get().toString());
// Since loadPaymentData may show the UI asking the user to select a payment method, we use
// AutoResolveHelper to wait for the user interacting with it. Once completed,
// onActivityResult will be called with the result.
if (request != null) {
AutoResolveHelper.resolveTask(
mPaymentsClient.loadPaymentData(request), this, LOAD_PAYMENT_DATA_REQUEST_CODE);
}
}
private void initItemUI() {
TextView itemName = findViewById(R.id.text_item_name);
ImageView itemImage = findViewById(R.id.image_item_image);
TextView itemPrice = findViewById(R.id.text_item_price);
itemName.setText(mBikeItem.getName());
itemImage.setImageResource(mBikeItem.getImageResourceId());
itemPrice.setText(PaymentsUtil.microsToString(mBikeItem.getPriceMicros()));
}
}