-
Notifications
You must be signed in to change notification settings - Fork 182
/
CheckoutActivity.java
308 lines (261 loc) · 11.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright 2020 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.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.samples.wallet.databinding.ActivityCheckoutBinding;
import com.google.android.gms.samples.wallet.util.PaymentsUtil;
import com.google.android.gms.samples.wallet.R;
import com.google.android.gms.samples.wallet.util.Json;
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.Locale;
import java.util.Optional;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
/**
* Checkout implementation for the app
*/
public class CheckoutActivity extends AppCompatActivity {
// 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 static final long SHIPPING_COST_CENTS = 90 * PaymentsUtil.CENTS_IN_A_UNIT.longValue();
// A client for interacting with the Google Pay API.
private PaymentsClient paymentsClient;
private ActivityCheckoutBinding layoutBinding;
private View googlePayButton;
private JSONArray garmentList;
private JSONObject selectedGarment;
/**
* 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);
initializeUi();
// Set up the mock information for our item in the UI.
try {
selectedGarment = fetchRandomGarment();
displayGarment(selectedGarment);
} catch (JSONException e) {
throw new RuntimeException("The list of garments cannot be loaded");
}
// 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.
paymentsClient = PaymentsUtil.createPaymentsClient(this);
possiblyShowGooglePayButton();
}
/**
* 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:
// The user cancelled the payment attempt
break;
case AutoResolveHelper.RESULT_ERROR:
Status status = AutoResolveHelper.getStatusFromIntent(data);
handleError(status.getStatusCode());
break;
}
// Re-enables the Google Pay payment button.
googlePayButton.setClickable(true);
}
}
private void initializeUi() {
// Use view binding to access the UI elements
layoutBinding = ActivityCheckoutBinding.inflate(getLayoutInflater());
setContentView(layoutBinding.getRoot());
// The Google Pay button is a layout file – take the root view
googlePayButton = layoutBinding.googlePayButton.getRoot();
googlePayButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPayment(view);
}
});
}
private void displayGarment(JSONObject garment) throws JSONException {
layoutBinding.detailTitle.setText(garment.getString("title"));
layoutBinding.detailPrice.setText(
String.format(Locale.getDefault(), "$%.2f", garment.getDouble("price")));
final String escapedHtmlText = Html.fromHtml(
garment.getString("description"), Html.FROM_HTML_MODE_COMPACT).toString();
layoutBinding.detailDescription.setText(Html.fromHtml(
escapedHtmlText, Html.FROM_HTML_MODE_COMPACT));
final String imageUri = String.format("@drawable/%s", garment.getString("image"));
final int imageResource = getResources().getIdentifier(imageUri, null, getPackageName());
layoutBinding.detailImage.setImageResource(imageResource);
}
/**
* 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;
}
// 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.
IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(isReadyToPayJson.get().toString());
Task<Boolean> task = paymentsClient.isReadyToPay(request);
task.addOnCompleteListener(this,
new OnCompleteListener<Boolean>() {
@Override
public void onComplete(@NonNull Task<Boolean> task) {
if (task.isSuccessful()) {
setGooglePayAvailable(task.getResult());
} else {
Log.w("isReadyToPay failed", task.getException());
}
}
});
}
/**
* If isReadyToPay returned {@code true}, show the button and hide the "checking" text. Otherwise,
* notify the user that Google Pay 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 {@code
* false}.
*
* @param available isReadyToPay API response.
*/
private void setGooglePayAvailable(boolean available) {
if (available) {
googlePayButton.setVisibility(View.VISIBLE);
} else {
Toast.makeText(this, R.string.googlepay_status_unavailable, Toast.LENGTH_LONG).show();
}
}
/**
* 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">PaymentData</a>
*/
private void handlePaymentSuccess(PaymentData paymentData) {
// Token will be null if PaymentDataRequest was not constructed using fromJson(String).
final String paymentInfo = paymentData.toJson();
if (paymentInfo == null) {
return;
}
try {
JSONObject paymentMethodData = new JSONObject(paymentInfo).getJSONObject("paymentMethodData");
// If the gateway is set to "example", no payment information is returned - instead, the
// token will only consist of "examplePaymentMethodToken".
final JSONObject tokenizationData = paymentMethodData.getJSONObject("tokenizationData");
final String token = tokenizationData.getString("token");
final JSONObject info = paymentMethodData.getJSONObject("info");
final String billingName = info.getJSONObject("billingAddress").getString("name");
Toast.makeText(
this, getString(R.string.payments_show_name, billingName),
Toast.LENGTH_LONG).show();
// Logging token string.
Log.d("Google Pay token: ", token);
} catch (JSONException e) {
throw new RuntimeException("The selected garment cannot be parsed from the list of elements");
}
}
/**
* 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.e("loadPaymentData failed", String.format("Error code: %d", statusCode));
}
public void requestPayment(View view) {
// Disables the button to prevent multiple clicks.
googlePayButton.setClickable(false);
// The price provided to the API should include taxes and shipping.
// This price is not displayed to the user.
try {
double garmentPrice = selectedGarment.getDouble("price");
long garmentPriceCents = Math.round(garmentPrice * PaymentsUtil.CENTS_IN_A_UNIT.longValue());
long priceCents = garmentPriceCents + SHIPPING_COST_CENTS;
Optional<JSONObject> paymentDataRequestJson = PaymentsUtil.getPaymentDataRequest(priceCents);
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(
paymentsClient.loadPaymentData(request),
this, LOAD_PAYMENT_DATA_REQUEST_CODE);
}
} catch (JSONException e) {
throw new RuntimeException("The price cannot be deserialized from the JSON object.");
}
}
private JSONObject fetchRandomGarment() {
// Only load the list of items if it has not been loaded before
if (garmentList == null) {
garmentList = Json.readFromResources(this, R.raw.tshirts);
}
// Take a random element from the list
int randomIndex = Math.toIntExact(Math.round(Math.random() * (garmentList.length() - 1)));
try {
return garmentList.getJSONObject(randomIndex);
} catch (JSONException e) {
throw new RuntimeException("The index specified is out of bounds.");
}
}
}