Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(765)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java

Issue 2413533003: [Payments] Normalize billing address before sending to the merchant. (Closed)
Patch Set: Rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..e84b29f63f38c2f3e3dc9fb5b5bf8e6fbbdaa793
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java
@@ -0,0 +1,140 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.payments;
+
+import android.os.Handler;
+
+import org.chromium.chrome.browser.autofill.PersonalDataManager;
+import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
+import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddressRequestDelegate;
+import org.chromium.chrome.browser.payments.ui.PaymentOption;
+import org.chromium.payments.mojom.PaymentResponse;
+
+/**
+ * The helper class to create and prepare a PaymentResponse.
+ */
+public class PaymentResponseHelper implements NormalizedAddressRequestDelegate {
+ /**
+ * Observer to be notified when the payment response is completed.
+ */
+ public interface PaymentResponseRequesterDelegate {
+ /*
+ * Called when the payment response is ready to be sent to the merchant.
+ *
+ * @param response The payment response to send to the merchant.
+ */
+ void onPaymentResponseReady(PaymentResponse response);
+ }
+
+ private PaymentResponse mPaymentResponse;
+ private PaymentResponseRequesterDelegate mDelegate;
+ private boolean mIsWaitingForShippingNormalization;
+ private boolean mIsWaitingForPaymentsDetails = true;
+
+ /**
+ * Builds a helper to contruct and fill a PaymentResponse.
+ *
+ * @param selectedShippingAddress The shipping address picked by the user.
+ * @param selectedShippingOption The shipping option picked by the user.
+ * @param selectedContact The contact info picked by the user.
+ * @param delegate The object that will recieve the completed PaymentResponse.
+ */
+ public PaymentResponseHelper(PaymentOption selectedShippingAddress,
+ PaymentOption selectedShippingOption, PaymentOption selectedContact,
+ PaymentResponseRequesterDelegate delegate) {
+ mPaymentResponse = new PaymentResponse();
+
+ mDelegate = delegate;
+
+ // Set up the contact section of the response.
+ if (selectedContact != null) {
+ // Contacts are created in PaymentRequestImpl.init(). These should all be instances of
+ // AutofillContact.
+ assert selectedContact instanceof AutofillContact;
+ mPaymentResponse.payerName = ((AutofillContact) selectedContact).getPayerName();
+ mPaymentResponse.payerPhone = ((AutofillContact) selectedContact).getPayerPhone();
+ mPaymentResponse.payerEmail = ((AutofillContact) selectedContact).getPayerEmail();
+ }
+
+ // Set up the shipping section of the response.
+ if (selectedShippingOption != null && selectedShippingOption.getIdentifier() != null) {
+ mPaymentResponse.shippingOption = selectedShippingOption.getIdentifier();
+ }
+
+ // Set up the shipping address section of the response.
+ if (selectedShippingAddress != null) {
+ // Shipping addresses are created in PaymentRequestImpl.init(). These should all be
+ // instances of AutofillAddress.
+ assert selectedShippingAddress instanceof AutofillAddress;
+ AutofillAddress selectedAutofillAddress = (AutofillAddress) selectedShippingAddress;
+
+ // Addresses to be sent to the merchant should always be complete.
+ assert selectedAutofillAddress.isComplete();
+
+ // Record the use of the profile.
+ PersonalDataManager.getInstance().recordAndLogProfileUse(
+ selectedAutofillAddress.getProfile().getGUID());
+
+ mPaymentResponse.shippingAddress = selectedAutofillAddress.toPaymentAddress();
+
+ // The shipping address needs to be normalized before sending the response to the
+ // merchant.
+ mIsWaitingForShippingNormalization = true;
+ PersonalDataManager.getInstance().normalizeAddress(
+ selectedAutofillAddress.getProfile().getGUID(),
+ AutofillAddress.getCountryCode(selectedAutofillAddress.getProfile()), this);
+ }
+ }
+
+ /**
+ * Called when the intrument details have started loading. Starts a timeout to stop the shipping
+ * address normalization if it takes too long.
+ */
+ public void onInstrumentsDetailsLoading() {
+ if (mIsWaitingForShippingNormalization) {
+ // If the normalization is not completed yet, start a timer to cancel it if it takes too
+ // long.
+ new Handler().postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ onAddressNormalized(null);
+ }
+ }, PersonalDataManager.getInstance().getNormalizationTimeoutMS());
+ }
+ }
+
+ /**
+ * Called after the payment instrument's details were received.
+ *
+ * @param methodName The method name of the payment instrument.
+ * @param stringifiedDetails A string containing all the details of the payment instrument's
+ * details.
+ */
+ public void onInstrumentDetailsReceived(String methodName, String stringifiedDetails) {
+ mPaymentResponse.methodName = methodName;
+ mPaymentResponse.stringifiedDetails = stringifiedDetails;
+
+ mIsWaitingForPaymentsDetails = false;
+
+ // Wait for the shipping address normalization before sending the response.
+ if (!mIsWaitingForShippingNormalization) mDelegate.onPaymentResponseReady(mPaymentResponse);
+ }
+
+ @Override
+ public void onAddressNormalized(AutofillProfile profile) {
+ // Check if a normalization is still required.
+ if (!mIsWaitingForShippingNormalization) return;
+ mIsWaitingForShippingNormalization = false;
+
+ if (profile != null) {
+ // The normalization finished first: use the normalized address.
+ mPaymentResponse.shippingAddress =
+ new AutofillAddress(profile, true /* isComplete */).toPaymentAddress();
+ }
+
+ // Wait for the payment details before sending the response.
+ if (!mIsWaitingForPaymentsDetails) mDelegate.onPaymentResponseReady(mPaymentResponse);
+ }
+}
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java ('k') | chrome/android/java_sources.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698