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 PaymentResponse mPaymentResponse; |
| 32 private PaymentResponseRequesterDelegate mDelegate; |
| 33 private boolean mIsWaitingForShippingNormalization; |
| 34 private boolean mIsWaitingForPaymentsDetails = true; |
| 35 |
| 36 /** |
| 37 * Builds a helper to contruct and fill a PaymentResponse. |
| 38 * |
| 39 * @param selectedShippingAddress The shipping address picked by the user. |
| 40 * @param selectedShippingOption The shipping option picked by the user. |
| 41 * @param selectedContact The contact info picked by the user. |
| 42 * @param delegate The object that will recieve the completed
PaymentResponse. |
| 43 */ |
| 44 public PaymentResponseHelper(PaymentOption selectedShippingAddress, |
| 45 PaymentOption selectedShippingOption, PaymentOption selectedContact, |
| 46 PaymentResponseRequesterDelegate delegate) { |
| 47 mPaymentResponse = new PaymentResponse(); |
| 48 |
| 49 mDelegate = delegate; |
| 50 |
| 51 // Set up the contact section of the response. |
| 52 if (selectedContact != null) { |
| 53 // Contacts are created in PaymentRequestImpl.init(). These should a
ll be instances of |
| 54 // AutofillContact. |
| 55 assert selectedContact instanceof AutofillContact; |
| 56 mPaymentResponse.payerName = ((AutofillContact) selectedContact).get
PayerName(); |
| 57 mPaymentResponse.payerPhone = ((AutofillContact) selectedContact).ge
tPayerPhone(); |
| 58 mPaymentResponse.payerEmail = ((AutofillContact) selectedContact).ge
tPayerEmail(); |
| 59 } |
| 60 |
| 61 // Set up the shipping section of the response. |
| 62 if (selectedShippingOption != null && selectedShippingOption.getIdentifi
er() != null) { |
| 63 mPaymentResponse.shippingOption = selectedShippingOption.getIdentifi
er(); |
| 64 } |
| 65 |
| 66 // Set up the shipping address section of the response. |
| 67 if (selectedShippingAddress != null) { |
| 68 // Shipping addresses are created in PaymentRequestImpl.init(). Thes
e should all be |
| 69 // instances of AutofillAddress. |
| 70 assert selectedShippingAddress instanceof AutofillAddress; |
| 71 AutofillAddress selectedAutofillAddress = (AutofillAddress) selected
ShippingAddress; |
| 72 |
| 73 // Addresses to be sent to the merchant should always be complete. |
| 74 assert selectedAutofillAddress.isComplete(); |
| 75 |
| 76 // Record the use of the profile. |
| 77 PersonalDataManager.getInstance().recordAndLogProfileUse( |
| 78 selectedAutofillAddress.getProfile().getGUID()); |
| 79 |
| 80 mPaymentResponse.shippingAddress = selectedAutofillAddress.toPayment
Address(); |
| 81 |
| 82 // The shipping address needs to be normalized before sending the re
sponse to the |
| 83 // merchant. |
| 84 mIsWaitingForShippingNormalization = true; |
| 85 PersonalDataManager.getInstance().normalizeAddress( |
| 86 selectedAutofillAddress.getProfile().getGUID(), |
| 87 AutofillAddress.getCountryCode(selectedAutofillAddress.getPr
ofile()), this); |
| 88 } |
| 89 } |
| 90 |
| 91 /** |
| 92 * Called when the intrument details have started loading. Starts a timeout
to stop the shipping |
| 93 * address normalization if it takes too long. |
| 94 */ |
| 95 public void onInstrumentsDetailsLoading() { |
| 96 if (mIsWaitingForShippingNormalization) { |
| 97 // If the normalization is not completed yet, start a timer to cance
l it if it takes too |
| 98 // long. |
| 99 new Handler().postDelayed(new Runnable() { |
| 100 @Override |
| 101 public void run() { |
| 102 onAddressNormalized(null); |
| 103 } |
| 104 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()); |
| 105 } |
| 106 } |
| 107 |
| 108 /** |
| 109 * Called after the payment instrument's details were received. |
| 110 * |
| 111 * @param methodName The method name of the payment instrument. |
| 112 * @param stringifiedDetails A string containing all the details of the pay
ment instrument's |
| 113 * details. |
| 114 */ |
| 115 public void onInstrumentDetailsReceived(String methodName, String stringifie
dDetails) { |
| 116 mPaymentResponse.methodName = methodName; |
| 117 mPaymentResponse.stringifiedDetails = stringifiedDetails; |
| 118 |
| 119 mIsWaitingForPaymentsDetails = false; |
| 120 |
| 121 // Wait for the shipping address normalization before sending the respon
se. |
| 122 if (!mIsWaitingForShippingNormalization) mDelegate.onPaymentResponseRead
y(mPaymentResponse); |
| 123 } |
| 124 |
| 125 @Override |
| 126 public void onAddressNormalized(AutofillProfile profile) { |
| 127 // Check if a normalization is still required. |
| 128 if (!mIsWaitingForShippingNormalization) return; |
| 129 mIsWaitingForShippingNormalization = false; |
| 130 |
| 131 if (profile != null) { |
| 132 // The normalization finished first: use the normalized address. |
| 133 mPaymentResponse.shippingAddress = |
| 134 new AutofillAddress(profile, true /* isComplete */).toPaymen
tAddress(); |
| 135 } |
| 136 |
| 137 // Wait for the payment details before sending the response. |
| 138 if (!mIsWaitingForPaymentsDetails) mDelegate.onPaymentResponseReady(mPay
mentResponse); |
| 139 } |
| 140 } |
OLD | NEW |