Chromium Code Reviews| 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 62ee4cffa1fa44ede450a96efa8e00bfed5e7d77..c26a01b9d21364cb98fb57abf735133b8bbef6dc 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 |
| @@ -4,18 +4,20 @@ |
| package org.chromium.chrome.browser.payments; |
| +import android.os.Handler; |
| import android.text.TextUtils; |
| import android.util.JsonWriter; |
| +import org.json.JSONObject; |
| + |
| 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; |
| -import org.json.JSONObject; |
| - |
| import java.io.IOException; |
| import java.io.StringWriter; |
| import java.util.List; |
| @@ -25,13 +27,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 WebContents mWebContents; |
| + private final Handler mHandler = new Handler(); |
| private CreditCard mCard; |
| private boolean mIsComplete; |
| + private String mSecurityCode; |
| @Nullable private AutofillProfile mBillingAddress; |
| @Nullable private DetailsCallback mCallback; |
| + private boolean mIsWaitingForBillingNormalization; |
| + private boolean mIsWaitingForFullCardDetails; |
| /** |
| * Builds a payment instrument for the given credit card. |
| @@ -58,24 +64,85 @@ public class AutofillPaymentInstrument |
| @Override |
| public void getDetails(String unusedMerchantName, String unusedOrigin, PaymentItem unusedTotal, |
| List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallback callback) { |
| + // The billing address should never be null for a credit card. |
|
please use gerrit instead
2016/10/18 17:30:31
... at this point.
(A credit card can have a nul
sebsg
2016/10/18 22:44:31
Done.
|
| + assert mBillingAddress != null; |
| assert mIsComplete; |
| assert mCallback == null; |
| mCallback = callback; |
| + |
| + mIsWaitingForBillingNormalization = true; |
| + mIsWaitingForFullCardDetails = true; |
| + |
| + // Start the billing address normalization. |
| + mIsWaitingForBillingNormalization = |
| + PersonalDataManager.getInstance().normalizeAddress(mBillingAddress.getGUID(), |
| + AutofillAddress.getCountryCode(mBillingAddress), this); |
| + if (mIsWaitingForBillingNormalization) { |
| + // 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()); |
| + } |
| + |
| + // Start to get the full card details. |
| PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this); |
| } |
| + // TODO(crbug.com/656981): Update the credit card expiration on file. |
|
please use gerrit instead
2016/10/18 17:30:31
crbug.com/656981 should be fixed in full_card_requ
sebsg
2016/10/18 22:44:31
Done.
|
| @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; |
| + |
| + // Wait for the billing address normalization before sending the instrument details. |
| + if (mIsWaitingForBillingNormalization) { |
| + // Show the loading UI while the address gets normalized. |
| + mCallback.loadingInstrumentDetails(); |
| + return; |
| + } |
| + |
| + sendIntrumentDetails(); |
|
please use gerrit instead
2016/10/18 17:30:31
Instead of early-returning immediately before the
sebsg
2016/10/18 22:44:31
Done.
|
| + } |
| + |
| + @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) return; |
| + |
| + sendIntrumentDetails(); |
|
please use gerrit instead
2016/10/18 17:30:31
Shorter:
if (!mIsWatingForFullCardDetails) sendIn
sebsg
2016/10/18 22:44:31
Done.
|
| + } |
| + |
| + /** |
| + * 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(); |
| @@ -107,10 +174,14 @@ public class AutofillPaymentInstrument |
| json.endObject(); |
| } catch (IOException e) { |
| onFullCardError(); |
| + mSecurityCode = ""; |
|
please use gerrit instead
2016/10/18 17:30:30
Remove
sebsg
2016/10/18 22:44:31
Wow, I still thought I had to keep that info befor
|
| return; |
| } |
|
please use gerrit instead
2016/10/18 17:30:31
} finally {
mSecurityCode = "";
}
sebsg
2016/10/18 22:44:31
Done.
|
| - mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), stringWriter.toString()); |
| + mCallback.onInstrumentDetailsReady( |
| + mCard.getBasicCardPaymentType(), stringWriter.toString()); |
| + |
| + mSecurityCode = ""; |
|
please use gerrit instead
2016/10/18 17:30:30
Remove
sebsg
2016/10/18 22:44:31
Done.
|
| } |
| private static String ensureNotNull(@Nullable String value) { |
| @@ -161,4 +232,9 @@ public class AutofillPaymentInstrument |
| public CreditCard getCard() { |
| return mCard; |
| } |
| -} |
| + |
| + /** @return The billing address associated with this credit card. */ |
| + public AutofillProfile getBillingAddress() { |
| + return mBillingAddress; |
| + } |
| +} |