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 import android.text.TextUtils; |
| 9 |
| 10 import org.chromium.chrome.browser.autofill.PersonalDataManager; |
| 11 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| 12 import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddres
sRequestDelegate; |
| 13 import org.chromium.chrome.browser.payments.ui.PaymentOption; |
| 14 import org.chromium.payments.mojom.PaymentResponse; |
| 15 |
| 16 /** |
| 17 * The helper class to create and prepare a PaymentResponse. |
| 18 */ |
| 19 public class PaymentResponseHelper implements NormalizedAddressRequestDelegate { |
| 20 /** |
| 21 * Observer to be notified when the payment response is completed. |
| 22 */ |
| 23 public interface PaymentResponseRequesterDelegate { |
| 24 /* |
| 25 * Called when the payment response is ready to be sent to the merchant. |
| 26 * |
| 27 * @param response The payment response to send to the merchant. |
| 28 */ |
| 29 void onPaymentResponseReady(PaymentResponse response); |
| 30 } |
| 31 |
| 32 private final Handler mHandler = new Handler(); |
| 33 |
| 34 private PaymentResponse mPaymentResponse; |
| 35 private PaymentResponseRequesterDelegate mDelegate; |
| 36 private boolean mIsWaitingForShippingNormalization; |
| 37 private boolean mIsWaitingForPaymentsDetails = true; |
| 38 |
| 39 // Callback to send the response. |
| 40 |
| 41 public PaymentResponseHelper(PaymentOption selectedShippingAddress, |
| 42 PaymentOption selectedShippingOption, PaymentOption selectedContact, |
| 43 PaymentResponseRequesterDelegate delegate) { |
| 44 mPaymentResponse = new PaymentResponse(); |
| 45 |
| 46 mDelegate = delegate; |
| 47 |
| 48 // Set up the contact section of the response. |
| 49 if (selectedContact != null) { |
| 50 // Contacts are created in show(). These should all be instances of
AutofillContact. |
| 51 assert selectedContact instanceof AutofillContact; |
| 52 mPaymentResponse.payerPhone = ((AutofillContact) selectedContact).ge
tPayerPhone(); |
| 53 mPaymentResponse.payerEmail = ((AutofillContact) selectedContact).ge
tPayerEmail(); |
| 54 } |
| 55 |
| 56 // Set up the shipping section of the response. |
| 57 if (selectedShippingOption != null && selectedShippingOption.getIdentifi
er() != null) { |
| 58 mPaymentResponse.shippingOption = selectedShippingOption.getIdentifi
er(); |
| 59 } |
| 60 |
| 61 // Set up the shipping address section of the response. |
| 62 if (selectedShippingAddress != null) { |
| 63 // Shipping addresses are created in show(). These should all be ins
tances of |
| 64 // AutofillAddress. |
| 65 assert selectedShippingAddress instanceof AutofillAddress; |
| 66 AutofillAddress selectedAutofillAddress = (AutofillAddress) selected
ShippingAddress; |
| 67 |
| 68 // Addresses to be sent to the merchant should always be complete. |
| 69 assert selectedAutofillAddress.isComplete(); |
| 70 |
| 71 // Record the use of the profile. |
| 72 PersonalDataManager.getInstance().recordAndLogProfileUse( |
| 73 selectedAutofillAddress.getProfile().getGUID()); |
| 74 |
| 75 mPaymentResponse.shippingAddress = selectedAutofillAddress.toPayment
Address(); |
| 76 |
| 77 // The shipping address needs to be normalized before sending the re
sponse to the |
| 78 // merchant. |
| 79 mIsWaitingForShippingNormalization = true; |
| 80 boolean willNormalizeAsync = PersonalDataManager.getInstance().norma
lizeAddress( |
| 81 selectedAutofillAddress.getProfile().getGUID(), |
| 82 AutofillAddress.getCountryCode(selectedAutofillAddress.getPr
ofile()), this); |
| 83 |
| 84 if (willNormalizeAsync) { |
| 85 // If the normalization was not done synchronously, start a time
r to cancel the |
| 86 // asynchronous normalization if it takes too long. |
| 87 mHandler.postDelayed(new Runnable() { |
| 88 @Override |
| 89 public void run() { |
| 90 onAddressNormalized(null); |
| 91 } |
| 92 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()
); |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 public void onInstrumentDetailsReceived(String methodName, String stringifie
dDetails) { |
| 98 mPaymentResponse.methodName = methodName; |
| 99 mPaymentResponse.stringifiedDetails = stringifiedDetails; |
| 100 |
| 101 mIsWaitingForPaymentsDetails = false; |
| 102 |
| 103 // Check if the normalized shipping address has been set. |
| 104 if (mIsWaitingForShippingNormalization) return; |
| 105 |
| 106 mDelegate.onPaymentResponseReady(mPaymentResponse); |
| 107 } |
| 108 |
| 109 @Override |
| 110 public void onAddressNormalized(AutofillProfile profile) { |
| 111 // Check if a normalization is still required. |
| 112 if (!mIsWaitingForShippingNormalization) return; |
| 113 mIsWaitingForShippingNormalization = false; |
| 114 |
| 115 if (profile != null && !TextUtils.isEmpty(profile.getGUID())) { |
| 116 // The normalization finished first: use the normalized address. |
| 117 mPaymentResponse.shippingAddress = |
| 118 new AutofillAddress(profile, true /* isComplete */).toPaymen
tAddress(); |
| 119 } |
| 120 |
| 121 // Check if the payment details have been received. |
| 122 if (mIsWaitingForPaymentsDetails) return; |
| 123 |
| 124 mDelegate.onPaymentResponseReady(mPaymentResponse); |
| 125 } |
| 126 } |
OLD | NEW |