Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.payments; | |
| 6 | |
| 7 import android.os.Handler; | |
| 8 | |
| 9 import org.chromium.chrome.browser.autofill.PersonalDataManager; | |
| 10 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | |
| 11 import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddres sRequestDelegate; | |
| 12 import org.chromium.chrome.browser.payments.ui.PaymentOption; | |
| 13 import org.chromium.payments.mojom.PaymentResponse; | |
| 14 | |
| 15 /** | |
| 16 * The helper class to create and prepare a PaymentResponse. | |
| 17 */ | |
| 18 public class PaymentResponseHelper implements NormalizedAddressRequestDelegate { | |
| 19 /** | |
| 20 * Observer to be notified when the payment response is completed. | |
| 21 */ | |
| 22 public interface PaymentResponseRequesterDelegate { | |
| 23 /* | |
| 24 * Called when the payment response is ready to be sent to the merchant. | |
| 25 * | |
| 26 * @param response The payment response to send to the merchant. | |
| 27 */ | |
| 28 void onPaymentResponseReady(PaymentResponse response); | |
| 29 } | |
| 30 | |
| 31 private final Handler mHandler = new Handler(); | |
| 32 | |
| 33 private PaymentResponse mPaymentResponse; | |
| 34 private PaymentResponseRequesterDelegate mDelegate; | |
| 35 private boolean mIsWaitingForShippingNormalization; | |
| 36 private boolean mIsWaitingForPaymentsDetails = true; | |
| 37 | |
| 38 /** | |
| 39 * Builds a helper to contruct and fill a PaymentResponse. | |
| 40 * | |
| 41 * @param selectedShippingAddress The shipping address picked by the user. | |
| 42 * @param selectedShippingOption The shipping option picked by the user. | |
| 43 * @param selectedContact The contact info picked by the user. | |
| 44 * @param delegate The object that will recieve the completed PaymentResponse. | |
| 45 */ | |
| 46 public PaymentResponseHelper(PaymentOption selectedShippingAddress, | |
| 47 PaymentOption selectedShippingOption, PaymentOption selectedContact, | |
| 48 PaymentResponseRequesterDelegate delegate) { | |
| 49 mPaymentResponse = new PaymentResponse(); | |
| 50 | |
| 51 mDelegate = delegate; | |
| 52 | |
| 53 // Set up the contact section of the response. | |
| 54 if (selectedContact != null) { | |
| 55 // Contacts are created in PaymentRequestImpl.init(). These should a ll be instances of | |
| 56 // AutofillContact. | |
| 57 assert selectedContact instanceof AutofillContact; | |
| 58 mPaymentResponse.payerPhone = ((AutofillContact) selectedContact).ge tPayerPhone(); | |
| 59 mPaymentResponse.payerEmail = ((AutofillContact) selectedContact).ge tPayerEmail(); | |
| 60 } | |
| 61 | |
| 62 // Set up the shipping section of the response. | |
| 63 if (selectedShippingOption != null && selectedShippingOption.getIdentifi er() != null) { | |
| 64 mPaymentResponse.shippingOption = selectedShippingOption.getIdentifi er(); | |
| 65 } | |
| 66 | |
| 67 // Set up the shipping address section of the response. | |
| 68 if (selectedShippingAddress != null) { | |
| 69 // Shipping addresses are created in PaymentRequestImpl.init(). Thes e should all be | |
| 70 // instances of AutofillAddress. | |
| 71 assert selectedShippingAddress instanceof AutofillAddress; | |
| 72 AutofillAddress selectedAutofillAddress = (AutofillAddress) selected ShippingAddress; | |
| 73 | |
| 74 // Addresses to be sent to the merchant should always be complete. | |
| 75 assert selectedAutofillAddress.isComplete(); | |
| 76 | |
| 77 // Record the use of the profile. | |
| 78 PersonalDataManager.getInstance().recordAndLogProfileUse( | |
| 79 selectedAutofillAddress.getProfile().getGUID()); | |
| 80 | |
| 81 mPaymentResponse.shippingAddress = selectedAutofillAddress.toPayment Address(); | |
| 82 | |
| 83 // The shipping address needs to be normalized before sending the re sponse to the | |
| 84 // merchant. | |
| 85 mIsWaitingForShippingNormalization = true; | |
| 86 mIsWaitingForShippingNormalization = PersonalDataManager.getInstance ().normalizeAddress( | |
| 87 selectedAutofillAddress.getProfile().getGUID(), | |
| 88 AutofillAddress.getCountryCode(selectedAutofillAddress.getPr ofile()), this); | |
| 89 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
| |
| 90 // If the normalization was not done synchronously, start a time r to cancel the | |
| 91 // asynchronous normalization if it takes too long. | |
| 92 mHandler.postDelayed(new Runnable() { | |
| 93 @Override | |
| 94 public void run() { | |
| 95 onAddressNormalized(null); | |
| 96 } | |
| 97 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS() ); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * Called after the payment instrument's details were received. | |
| 104 * | |
| 105 * @param methodName The method name of the payment instrument. | |
| 106 * @param stringifiedDetails A string containing all the details of the pay ment instrument's | |
| 107 * details. | |
| 108 */ | |
| 109 public void onInstrumentDetailsReceived(String methodName, String stringifie dDetails) { | |
| 110 mPaymentResponse.methodName = methodName; | |
| 111 mPaymentResponse.stringifiedDetails = stringifiedDetails; | |
| 112 | |
| 113 mIsWaitingForPaymentsDetails = false; | |
| 114 | |
| 115 // Wait for the shipping address normalization before sending the respon se. | |
| 116 if (!mIsWaitingForShippingNormalization) mDelegate.onPaymentResponseRead y(mPaymentResponse); | |
| 117 } | |
| 118 | |
| 119 @Override | |
| 120 public void onAddressNormalized(AutofillProfile profile) { | |
| 121 // Check if a normalization is still required. | |
| 122 if (!mIsWaitingForShippingNormalization) return; | |
| 123 mIsWaitingForShippingNormalization = false; | |
| 124 | |
| 125 if (profile != null) { | |
| 126 // The normalization finished first: use the normalized address. | |
| 127 mPaymentResponse.shippingAddress = | |
| 128 new AutofillAddress(profile, true /* isComplete */).toPaymen tAddress(); | |
| 129 } | |
| 130 | |
| 131 // Wait for the payment details before sending the response. | |
| 132 if (!mIsWaitingForPaymentsDetails) mDelegate.onPaymentResponseReady(mPay mentResponse); | |
| 133 } | |
| 134 } | |
| OLD | NEW |