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

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();
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.
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.
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 =
78 PersonalDataManager.getInstance().normalizeAddress(mBillingAddre ss.getGUID(),
79 AutofillAddress.getCountryCode(mBillingAddress), this);
80 if (mIsWaitingForBillingNormalization) {
81 // If the normalization was not done synchronously, start a timer to cancel the
82 // asynchronous normalization if it takes too long.
83 mHandler.postDelayed(new Runnable() {
84 @Override
85 public void run() {
86 onAddressNormalized(null);
87 }
88 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS());
89 }
90
91 // Start to get the full card details.
64 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ; 92 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ;
65 } 93 }
66 94
95 // 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.
67 @Override 96 @Override
68 public void onFullCardDetails(CreditCard card, String cvc) { 97 public void onFullCardDetails(CreditCard updatedCard, String cvc) {
98 // Keep the cvc for after the normalization.
99 mSecurityCode = cvc;
100
101 // Update the card's expiration date.
102 mCard.setMonth(updatedCard.getMonth());
103 mCard.setYear(updatedCard.getYear());
104
105 mIsWaitingForFullCardDetails = false;
106
107 // Wait for the billing address normalization before sending the instrum ent details.
108 if (mIsWaitingForBillingNormalization) {
109 // Show the loading UI while the address gets normalized.
110 mCallback.loadingInstrumentDetails();
111 return;
112 }
113
114 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.
115 }
116
117 @Override
118 public void onAddressNormalized(AutofillProfile profile) {
119 if (!mIsWaitingForBillingNormalization) return;
120 mIsWaitingForBillingNormalization = false;
121
122 // If the normalization finished first, use the normalized address.
123 if (profile != null) mBillingAddress = profile;
124
125 // Wait for the full card details before sending the instrument details.
126 if (mIsWaitingForFullCardDetails) return;
127
128 sendIntrumentDetails();
please use gerrit instead 2016/10/18 17:30:31 Shorter: if (!mIsWatingForFullCardDetails) sendIn
sebsg 2016/10/18 22:44:31 Done.
129 }
130
131 /**
132 * Stringify the card details and send the resulting string and the method n ame to the
133 * registered callback.
134 */
135 private void sendIntrumentDetails() {
69 StringWriter stringWriter = new StringWriter(); 136 StringWriter stringWriter = new StringWriter();
70 JsonWriter json = new JsonWriter(stringWriter); 137 JsonWriter json = new JsonWriter(stringWriter);
71 try { 138 try {
72 json.beginObject(); 139 json.beginObject();
73 140
74 json.name("cardholderName").value(card.getName()); 141 json.name("cardholderName").value(mCard.getName());
75 json.name("cardNumber").value(card.getNumber()); 142 json.name("cardNumber").value(mCard.getNumber());
76 json.name("expiryMonth").value(card.getMonth()); 143 json.name("expiryMonth").value(mCard.getMonth());
77 json.name("expiryYear").value(card.getYear()); 144 json.name("expiryYear").value(mCard.getYear());
78 json.name("cardSecurityCode").value(cvc); 145 json.name("cardSecurityCode").value(mSecurityCode);
79 146
80 json.name("billingAddress").beginObject(); 147 json.name("billingAddress").beginObject();
81 148
82 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode())); 149 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode()));
83 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) ); 150 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) );
84 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) ); 151 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) );
85 json.name("dependentLocality") 152 json.name("dependentLocality")
86 .value(ensureNotNull(mBillingAddress.getDependentLocality()) ); 153 .value(ensureNotNull(mBillingAddress.getDependentLocality()) );
87 154
88 json.name("addressLine").beginArray(); 155 json.name("addressLine").beginArray();
(...skipping 11 matching lines...) Expand all
100 json.name("languageCode").value(ensureNotNull(mBillingAddress.getLan guageCode())); 167 json.name("languageCode").value(ensureNotNull(mBillingAddress.getLan guageCode()));
101 json.name("organization").value(ensureNotNull(mBillingAddress.getCom panyName())); 168 json.name("organization").value(ensureNotNull(mBillingAddress.getCom panyName()));
102 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa me())); 169 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa me()));
103 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r())); 170 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r()));
104 171
105 json.endObject(); 172 json.endObject();
106 173
107 json.endObject(); 174 json.endObject();
108 } catch (IOException e) { 175 } catch (IOException e) {
109 onFullCardError(); 176 onFullCardError();
177 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
110 return; 178 return;
111 } 179 }
please use gerrit instead 2016/10/18 17:30:31 } finally { mSecurityCode = ""; }
sebsg 2016/10/18 22:44:31 Done.
112 180
113 mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), strin gWriter.toString()); 181 mCallback.onInstrumentDetailsReady(
182 mCard.getBasicCardPaymentType(), stringWriter.toString());
183
184 mSecurityCode = "";
please use gerrit instead 2016/10/18 17:30:30 Remove
sebsg 2016/10/18 22:44:31 Done.
114 } 185 }
115 186
116 private static String ensureNotNull(@Nullable String value) { 187 private static String ensureNotNull(@Nullable String value) {
117 return value == null ? "" : value; 188 return value == null ? "" : value;
118 } 189 }
119 190
120 @Override 191 @Override
121 public void onFullCardError() { 192 public void onFullCardError() {
122 mCallback.onInstrumentDetailsError(); 193 mCallback.onInstrumentDetailsError();
123 mCallback = null; 194 mCallback = null;
(...skipping 30 matching lines...) Expand all
154 mBillingAddress = billingAddress; 225 mBillingAddress = billingAddress;
155 mIsComplete = true; 226 mIsComplete = true;
156 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(), 227 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(),
157 null, card.getIssuerIconDrawableId()); 228 null, card.getIssuerIconDrawableId());
158 } 229 }
159 230
160 /** @return The credit card represented by this payment instrument. */ 231 /** @return The credit card represented by this payment instrument. */
161 public CreditCard getCard() { 232 public CreditCard getCard() {
162 return mCard; 233 return mCard;
163 } 234 }
164 } 235
236 /** @return The billing address associated with this credit card. */
237 public AutofillProfile getBillingAddress() {
238 return mBillingAddress;
239 }
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698