Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillPaymentInstrument.java

Issue 2413533003: [Payments] Normalize billing address before sending to the merchant. (Closed)
Patch Set: Addressed comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.payments; 5 package org.chromium.chrome.browser.payments;
6 6
7 import android.os.Handler;
7 import android.text.TextUtils; 8 import android.text.TextUtils;
8 import android.util.JsonWriter; 9 import android.util.JsonWriter;
9 10
11 import org.json.JSONObject;
12
10 import org.chromium.chrome.browser.autofill.PersonalDataManager; 13 import org.chromium.chrome.browser.autofill.PersonalDataManager;
11 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; 14 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
12 import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; 15 import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
13 import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestD elegate; 16 import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestD elegate;
17 import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddres sRequestDelegate;
14 import org.chromium.content_public.browser.WebContents; 18 import org.chromium.content_public.browser.WebContents;
15 import org.chromium.payments.mojom.PaymentItem; 19 import org.chromium.payments.mojom.PaymentItem;
16 20
17 import org.json.JSONObject;
18
19 import java.io.IOException; 21 import java.io.IOException;
20 import java.io.StringWriter; 22 import java.io.StringWriter;
21 import java.util.List; 23 import java.util.List;
22 24
23 import javax.annotation.Nullable; 25 import javax.annotation.Nullable;
24 26
25 /** 27 /**
26 * The locally stored credit card payment instrument. 28 * The locally stored credit card payment instrument.
27 */ 29 */
28 public class AutofillPaymentInstrument 30 public class AutofillPaymentInstrument extends PaymentInstrument
29 extends PaymentInstrument implements FullCardRequestDelegate { 31 implements FullCardRequestDelegate, NormalizedAddressRequestDelegate {
30 private final WebContents mWebContents; 32 private final WebContents mWebContents;
33 private final Handler mHandler = new Handler();
please use gerrit instead 2016/10/19 19:10:59 Please remove this for reasons outlined below.
sebsg 2016/10/20 17:06:52 Done.
31 private CreditCard mCard; 34 private CreditCard mCard;
32 private boolean mIsComplete; 35 private boolean mIsComplete;
36 private String mSecurityCode;
33 @Nullable private AutofillProfile mBillingAddress; 37 @Nullable private AutofillProfile mBillingAddress;
34 @Nullable private DetailsCallback mCallback; 38 @Nullable private DetailsCallback mCallback;
39 private boolean mIsWaitingForBillingNormalization;
40 private boolean mIsWaitingForFullCardDetails;
35 41
36 /** 42 /**
37 * Builds a payment instrument for the given credit card. 43 * Builds a payment instrument for the given credit card.
38 * 44 *
39 * @param webContents The web contents where PaymentRequest was invoked. 45 * @param webContents The web contents where PaymentRequest was invoked.
40 * @param card The autofill card that can be used for payment. 46 * @param card The autofill card that can be used for payment.
41 * @param billingAddress The billing address for the card. 47 * @param billingAddress The billing address for the card.
42 */ 48 */
43 public AutofillPaymentInstrument( 49 public AutofillPaymentInstrument(
44 WebContents webContents, CreditCard card, @Nullable AutofillProfile billingAddress) { 50 WebContents webContents, CreditCard card, @Nullable AutofillProfile billingAddress) {
45 super(card.getGUID(), card.getObfuscatedNumber(), card.getName(), 51 super(card.getGUID(), card.getObfuscatedNumber(), card.getName(),
46 card.getIssuerIconDrawableId()); 52 card.getIssuerIconDrawableId());
47 mWebContents = webContents; 53 mWebContents = webContents;
48 mCard = card; 54 mCard = card;
49 mIsComplete = false; 55 mIsComplete = false;
50 mBillingAddress = billingAddress; 56 mBillingAddress = billingAddress;
51 } 57 }
52 58
53 @Override 59 @Override
54 public String getMethodName() { 60 public String getMethodName() {
55 return mCard.getBasicCardPaymentType(); 61 return mCard.getBasicCardPaymentType();
56 } 62 }
57 63
58 @Override 64 @Override
59 public void getDetails(String unusedMerchantName, String unusedOrigin, Payme ntItem unusedTotal, 65 public void getDetails(String unusedMerchantName, String unusedOrigin, Payme ntItem unusedTotal,
60 List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallb ack callback) { 66 List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallb ack callback) {
67 // The billing address should never be null for a credit card at this po int.
68 assert mBillingAddress != null;
61 assert mIsComplete; 69 assert mIsComplete;
62 assert mCallback == null; 70 assert mCallback == null;
63 mCallback = callback; 71 mCallback = callback;
72
73 mIsWaitingForBillingNormalization = true;
74 mIsWaitingForFullCardDetails = true;
75
76 // Start the billing address normalization.
77 mIsWaitingForBillingNormalization = PersonalDataManager.getInstance().no rmalizeAddress(
78 mBillingAddress.getGUID(), AutofillAddress.getCountryCode(mBilli ngAddress), this);
79 if (mIsWaitingForBillingNormalization) {
80 // If the normalization was not done synchronously, start a timer to cancel the
81 // asynchronous normalization if it takes too long.
82 mHandler.postDelayed(new Runnable() {
please use gerrit instead 2016/10/19 19:10:59 This Handler is going to be accessed only once and
sebsg 2016/10/20 17:06:52 Done.
83 @Override
84 public void run() {
85 onAddressNormalized(null);
86 }
87 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS());
please use gerrit instead 2016/10/19 19:10:59 We timebox normalization to 5 seconds, but unmaski
sebsg 2016/10/20 17:06:52 Done.
88 }
89
90 // Start to get the full card details.
64 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ; 91 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ;
65 } 92 }
66 93
67 @Override 94 @Override
68 public void onFullCardDetails(CreditCard card, String cvc) { 95 public void onFullCardDetails(CreditCard updatedCard, String cvc) {
96 // Keep the cvc for after the normalization.
97 mSecurityCode = cvc;
98
99 // Update the card's expiration date.
100 mCard.setMonth(updatedCard.getMonth());
101 mCard.setYear(updatedCard.getYear());
102
103 mIsWaitingForFullCardDetails = false;
104
105 // Wait for the billing address normalization before sending the instrum ent details.
106 if (mIsWaitingForBillingNormalization) {
107 // Show the loading UI while the address gets normalized.
108 mCallback.loadingInstrumentDetails();
109 return;
110 } else {
111 sendIntrumentDetails();
112 }
113 }
114
115 @Override
116 public void onAddressNormalized(AutofillProfile profile) {
117 if (!mIsWaitingForBillingNormalization) return;
118 mIsWaitingForBillingNormalization = false;
119
120 // If the normalization finished first, use the normalized address.
121 if (profile != null) mBillingAddress = profile;
122
123 // Wait for the full card details before sending the instrument details.
124 if (!mIsWaitingForFullCardDetails) sendIntrumentDetails();
125 }
126
127 /**
128 * Stringify the card details and send the resulting string and the method n ame to the
129 * registered callback.
130 */
131 private void sendIntrumentDetails() {
69 StringWriter stringWriter = new StringWriter(); 132 StringWriter stringWriter = new StringWriter();
70 JsonWriter json = new JsonWriter(stringWriter); 133 JsonWriter json = new JsonWriter(stringWriter);
71 try { 134 try {
72 json.beginObject(); 135 json.beginObject();
73 136
74 json.name("cardholderName").value(card.getName()); 137 json.name("cardholderName").value(mCard.getName());
75 json.name("cardNumber").value(card.getNumber()); 138 json.name("cardNumber").value(mCard.getNumber());
76 json.name("expiryMonth").value(card.getMonth()); 139 json.name("expiryMonth").value(mCard.getMonth());
77 json.name("expiryYear").value(card.getYear()); 140 json.name("expiryYear").value(mCard.getYear());
78 json.name("cardSecurityCode").value(cvc); 141 json.name("cardSecurityCode").value(mSecurityCode);
79 142
80 json.name("billingAddress").beginObject(); 143 json.name("billingAddress").beginObject();
81 144
82 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode())); 145 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode()));
83 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) ); 146 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) );
84 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) ); 147 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) );
85 json.name("dependentLocality") 148 json.name("dependentLocality")
86 .value(ensureNotNull(mBillingAddress.getDependentLocality()) ); 149 .value(ensureNotNull(mBillingAddress.getDependentLocality()) );
87 150
88 json.name("addressLine").beginArray(); 151 json.name("addressLine").beginArray();
(...skipping 12 matching lines...) Expand all
101 json.name("organization").value(ensureNotNull(mBillingAddress.getCom panyName())); 164 json.name("organization").value(ensureNotNull(mBillingAddress.getCom panyName()));
102 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa me())); 165 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa me()));
103 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r())); 166 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r()));
104 167
105 json.endObject(); 168 json.endObject();
106 169
107 json.endObject(); 170 json.endObject();
108 } catch (IOException e) { 171 } catch (IOException e) {
109 onFullCardError(); 172 onFullCardError();
110 return; 173 return;
174 } finally {
175 mSecurityCode = "";
111 } 176 }
112 177
113 mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), strin gWriter.toString()); 178 mCallback.onInstrumentDetailsReady(
179 mCard.getBasicCardPaymentType(), stringWriter.toString());
114 } 180 }
115 181
116 private static String ensureNotNull(@Nullable String value) { 182 private static String ensureNotNull(@Nullable String value) {
117 return value == null ? "" : value; 183 return value == null ? "" : value;
118 } 184 }
119 185
120 @Override 186 @Override
121 public void onFullCardError() { 187 public void onFullCardError() {
122 mCallback.onInstrumentDetailsError(); 188 mCallback.onInstrumentDetailsError();
123 mCallback = null; 189 mCallback = null;
(...skipping 30 matching lines...) Expand all
154 mBillingAddress = billingAddress; 220 mBillingAddress = billingAddress;
155 mIsComplete = true; 221 mIsComplete = true;
156 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(), 222 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(),
157 null, card.getIssuerIconDrawableId()); 223 null, card.getIssuerIconDrawableId());
158 } 224 }
159 225
160 /** @return The credit card represented by this payment instrument. */ 226 /** @return The credit card represented by this payment instrument. */
161 public CreditCard getCard() { 227 public CreditCard getCard() {
162 return mCard; 228 return mCard;
163 } 229 }
164 } 230
231 /** @return The billing address associated with this credit card. */
232 public AutofillProfile getBillingAddress() {
233 return mBillingAddress;
234 }
235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698