Chromium Code Reviews| 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..9552e5162417c3eb037d4830f7b3d9bb9f677a25 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentResponseHelper.java |
| @@ -0,0 +1,134 @@ |
| +// 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 final Handler mHandler = new Handler(); |
| + |
| + 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.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; |
| + mIsWaitingForShippingNormalization = PersonalDataManager.getInstance().normalizeAddress( |
| + selectedAutofillAddress.getProfile().getGUID(), |
| + AutofillAddress.getCountryCode(selectedAutofillAddress.getProfile()), this); |
| + if (mIsWaitingForShippingNormalization) { |
|
please use gerrit instead
2016/10/19 19:10:59
Same comment as in AutofillPaymentInstrument: gett
sebsg
2016/10/20 17:06:52
The only problem with this one is that the normali
|
| + // 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()); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * 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); |
| + } |
| +} |