Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
index a9d8b971564823359dc7eb7c20ca74779f332e0d..d6e2e313f7798decf52513d9ca7ca877b6eb9191 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java |
@@ -5,6 +5,7 @@ |
package org.chromium.chrome.browser.payments; |
import android.content.Context; |
+import android.os.Handler; |
import android.text.TextUtils; |
import android.util.JsonWriter; |
@@ -15,6 +16,7 @@ import org.chromium.chrome.browser.autofill.PersonalDataManager; |
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; |
import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestDelegate; |
+import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddressRequestDelegate; |
import org.chromium.content_public.browser.WebContents; |
import org.chromium.payments.mojom.PaymentItem; |
@@ -27,14 +29,17 @@ import javax.annotation.Nullable; |
/** |
* The locally stored credit card payment instrument. |
*/ |
-public class AutofillPaymentInstrument |
- extends PaymentInstrument implements FullCardRequestDelegate { |
+public class AutofillPaymentInstrument extends PaymentInstrument |
+ implements FullCardRequestDelegate, NormalizedAddressRequestDelegate { |
private final Context mContext; |
private final WebContents mWebContents; |
private CreditCard mCard; |
private boolean mIsComplete; |
+ private String mSecurityCode; |
@Nullable private AutofillProfile mBillingAddress; |
@Nullable private InstrumentDetailsCallback mCallback; |
+ private boolean mIsWaitingForBillingNormalization; |
+ private boolean mIsWaitingForFullCardDetails; |
/** |
* Builds a payment instrument for the given credit card. |
@@ -67,24 +72,81 @@ public class AutofillPaymentInstrument |
public void getInstrumentDetails(String unusedMerchantName, String unusedOrigin, |
PaymentItem unusedTotal, List<PaymentItem> unusedCart, JSONObject unusedDetails, |
InstrumentDetailsCallback callback) { |
+ // The billing address should never be null for a credit card at this point. |
+ assert mBillingAddress != null; |
assert mIsComplete; |
assert mCallback == null; |
mCallback = callback; |
+ |
+ mIsWaitingForBillingNormalization = true; |
+ mIsWaitingForFullCardDetails = true; |
+ |
+ // Start the billing address normalization. |
+ PersonalDataManager.getInstance().normalizeAddress( |
+ mBillingAddress.getGUID(), AutofillAddress.getCountryCode(mBillingAddress), this); |
+ |
+ // Start to get the full card details. |
PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this); |
} |
@Override |
- public void onFullCardDetails(CreditCard card, String cvc) { |
+ public void onFullCardDetails(CreditCard updatedCard, String cvc) { |
+ // Keep the cvc for after the normalization. |
+ mSecurityCode = cvc; |
+ |
+ // Update the card's expiration date. |
+ mCard.setMonth(updatedCard.getMonth()); |
+ mCard.setYear(updatedCard.getYear()); |
+ |
+ mIsWaitingForFullCardDetails = false; |
+ |
+ // Show the loading UI while the address gets normalized. |
+ mCallback.loadingInstrumentDetails(); |
+ |
+ // Wait for the billing address normalization before sending the instrument details. |
+ if (mIsWaitingForBillingNormalization) { |
+ // If the normalization is not completed yet, Start a timer to cancel it if it takes too |
+ // long. |
+ new Handler().postDelayed(new Runnable() { |
+ @Override |
+ public void run() { |
+ onAddressNormalized(null); |
+ } |
+ }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()); |
+ |
+ return; |
+ } else { |
+ sendIntrumentDetails(); |
+ } |
+ } |
+ |
+ @Override |
+ public void onAddressNormalized(AutofillProfile profile) { |
+ if (!mIsWaitingForBillingNormalization) return; |
+ mIsWaitingForBillingNormalization = false; |
+ |
+ // If the normalization finished first, use the normalized address. |
+ if (profile != null) mBillingAddress = profile; |
+ |
+ // Wait for the full card details before sending the instrument details. |
+ if (!mIsWaitingForFullCardDetails) sendIntrumentDetails(); |
+ } |
+ |
+ /** |
+ * Stringify the card details and send the resulting string and the method name to the |
+ * registered callback. |
+ */ |
+ private void sendIntrumentDetails() { |
StringWriter stringWriter = new StringWriter(); |
JsonWriter json = new JsonWriter(stringWriter); |
try { |
json.beginObject(); |
- json.name("cardholderName").value(card.getName()); |
- json.name("cardNumber").value(card.getNumber()); |
- json.name("expiryMonth").value(card.getMonth()); |
- json.name("expiryYear").value(card.getYear()); |
- json.name("cardSecurityCode").value(cvc); |
+ json.name("cardholderName").value(mCard.getName()); |
+ json.name("cardNumber").value(mCard.getNumber()); |
+ json.name("expiryMonth").value(mCard.getMonth()); |
+ json.name("expiryYear").value(mCard.getYear()); |
+ json.name("cardSecurityCode").value(mSecurityCode); |
json.name("billingAddress").beginObject(); |
@@ -117,9 +179,12 @@ public class AutofillPaymentInstrument |
} catch (IOException e) { |
onFullCardError(); |
return; |
+ } finally { |
+ mSecurityCode = ""; |
} |
- mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), stringWriter.toString()); |
+ mCallback.onInstrumentDetailsReady( |
+ mCard.getBasicCardPaymentType(), stringWriter.toString()); |
} |
private static String ensureNotNull(@Nullable String value) { |
@@ -172,4 +237,9 @@ public class AutofillPaymentInstrument |
public CreditCard getCard() { |
return mCard; |
} |
-} |
+ |
+ /** @return The billing address associated with this credit card. */ |
+ public AutofillProfile getBillingAddress() { |
+ return mBillingAddress; |
+ } |
+} |