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..47542d17a1a56a9a495acf7301de5cd05369b9d6 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java |
@@ -0,0 +1,126 @@ |
+// 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 android.text.TextUtils; |
+ |
+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 final Handler mHandler = new Handler(); |
+ |
+ private PaymentResponse mPaymentResponse; |
+ private PaymentResponseRequesterDelegate mDelegate; |
+ private boolean mIsWaitingForShippingNormalization; |
+ private boolean mIsWaitingForPaymentsDetails = true; |
+ |
+ // Callback to send the response. |
+ |
+ public PaymentResponseHelper(PaymentOption selectedShippingAddress, |
please use gerrit instead
2016/10/18 17:30:31
Everything public should have javadoc comments.
sebsg
2016/10/18 22:44:31
Done.
|
+ 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 show(). These should all be instances of AutofillContact. |
please use gerrit instead
2016/10/18 17:30:31
s/show()/PaymentRequestImpl.init()/
sebsg
2016/10/18 22:44:31
Done.
|
+ assert selectedContact instanceof AutofillContact; |
+ 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 show(). These should all be instances of |
please use gerrit instead
2016/10/18 17:30:31
Ditto
sebsg
2016/10/18 22:44:31
Done.
|
+ // 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; |
+ boolean willNormalizeAsync = PersonalDataManager.getInstance().normalizeAddress( |
please use gerrit instead
2016/10/18 17:30:31
Can you do the same thing here?
mIsWaitingForShip
sebsg
2016/10/18 22:44:32
Done.
|
+ selectedAutofillAddress.getProfile().getGUID(), |
+ AutofillAddress.getCountryCode(selectedAutofillAddress.getProfile()), this); |
+ |
+ if (willNormalizeAsync) { |
+ // If the normalization was not done synchronously, start a timer to cancel the |
+ // asynchronous normalization if it takes too long. |
+ mHandler.postDelayed(new Runnable() { |
+ @Override |
+ public void run() { |
+ onAddressNormalized(null); |
+ } |
+ }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()); |
+ } |
+ } |
+ } |
+ |
+ public void onInstrumentDetailsReceived(String methodName, String stringifiedDetails) { |
please use gerrit instead
2016/10/18 17:30:31
javadoc
sebsg
2016/10/18 22:44:32
Done.
|
+ mPaymentResponse.methodName = methodName; |
+ mPaymentResponse.stringifiedDetails = stringifiedDetails; |
+ |
+ mIsWaitingForPaymentsDetails = false; |
+ |
+ // Check if the normalized shipping address has been set. |
+ if (mIsWaitingForShippingNormalization) return; |
please use gerrit instead
2016/10/18 17:30:31
if (!waiting) delegate.ready(response);
sebsg
2016/10/18 22:44:31
Done.
|
+ |
+ mDelegate.onPaymentResponseReady(mPaymentResponse); |
+ } |
+ |
+ @Override |
+ public void onAddressNormalized(AutofillProfile profile) { |
+ // Check if a normalization is still required. |
+ if (!mIsWaitingForShippingNormalization) return; |
+ mIsWaitingForShippingNormalization = false; |
+ |
+ if (profile != null && !TextUtils.isEmpty(profile.getGUID())) { |
please use gerrit instead
2016/10/18 17:30:31
You check for empty GUID here, but you don't do th
sebsg
2016/10/18 22:44:32
Done.
|
+ // The normalization finished first: use the normalized address. |
+ mPaymentResponse.shippingAddress = |
+ new AutofillAddress(profile, true /* isComplete */).toPaymentAddress(); |
+ } |
+ |
+ // Check if the payment details have been received. |
+ if (mIsWaitingForPaymentsDetails) return; |
please use gerrit instead
2016/10/18 17:30:31
if (!waiting) delegate.ready(response);
sebsg
2016/10/18 22:44:32
Done.
|
+ |
+ mDelegate.onPaymentResponseReady(mPaymentResponse); |
+ } |
+} |