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

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: 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 mTempsCvc;
please use gerrit instead 2016/10/17 20:44:18 Let's not put the word "temp" in the variable name
sebsg 2016/10/18 14:38:19 Done.
33 @Nullable private AutofillProfile mBillingAddress; 37 @Nullable private AutofillProfile mBillingAddress;
34 @Nullable private DetailsCallback mCallback; 38 @Nullable private DetailsCallback mCallback;
39 private boolean mIsWaitingForBillingNormalization;
35 40
36 /** 41 /**
37 * Builds a payment instrument for the given credit card. 42 * Builds a payment instrument for the given credit card.
38 * 43 *
39 * @param webContents The web contents where PaymentRequest was invoked. 44 * @param webContents The web contents where PaymentRequest was invoked.
40 * @param card The autofill card that can be used for payment. 45 * @param card The autofill card that can be used for payment.
41 * @param billingAddress The billing address for the card. 46 * @param billingAddress The billing address for the card.
42 */ 47 */
43 public AutofillPaymentInstrument( 48 public AutofillPaymentInstrument(
44 WebContents webContents, CreditCard card, @Nullable AutofillProfile billingAddress) { 49 WebContents webContents, CreditCard card, @Nullable AutofillProfile billingAddress) {
45 super(card.getGUID(), card.getObfuscatedNumber(), card.getName(), 50 super(card.getGUID(), card.getObfuscatedNumber(), card.getName(),
46 card.getIssuerIconDrawableId()); 51 card.getIssuerIconDrawableId());
47 mWebContents = webContents; 52 mWebContents = webContents;
48 mCard = card; 53 mCard = card;
49 mIsComplete = false; 54 mIsComplete = false;
50 mBillingAddress = billingAddress; 55 mBillingAddress = billingAddress;
51 } 56 }
52 57
53 @Override 58 @Override
54 public String getMethodName() { 59 public String getMethodName() {
55 return mCard.getBasicCardPaymentType(); 60 return mCard.getBasicCardPaymentType();
56 } 61 }
57 62
58 @Override 63 @Override
59 public void getDetails(String unusedMerchantName, String unusedOrigin, Payme ntItem unusedTotal, 64 public void getDetails(String unusedMerchantName, String unusedOrigin, Payme ntItem unusedTotal,
60 List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallb ack callback) { 65 List<PaymentItem> unusedCart, JSONObject unusedDetails, DetailsCallb ack callback) {
please use gerrit instead 2016/10/17 20:44:18 getDetails() shows the CVC prompt. This is the per
sebsg 2016/10/18 14:38:19 Done.
61 assert mIsComplete; 66 assert mIsComplete;
62 assert mCallback == null; 67 assert mCallback == null;
63 mCallback = callback; 68 mCallback = callback;
64 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ; 69 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this) ;
65 } 70 }
66 71
67 @Override 72 @Override
68 public void onFullCardDetails(CreditCard card, String cvc) { 73 public void onFullCardDetails(CreditCard unusedCard, String cvc) {
please use gerrit instead 2016/10/17 20:44:18 You can't throw away the card, because it might ha
sebsg 2016/10/18 14:38:19 This will require some rework then, because as it
please use gerrit instead 2016/10/18 17:30:30 The merchant website currently gets the updated ex
sebsg 2016/10/18 22:44:31 Yup! that's why I assigned the new exp month and y
74 // Keep the cvc for after the normalization.
75 mTempsCvc = cvc;
76
77 // Show the processing message while the billing address gets normalized .
78 mCallback.loadingInstrumentDetails();
please use gerrit instead 2016/10/17 20:44:18 This is a good idea. We should keep this, but move
sebsg 2016/10/18 14:38:19 Done.
79
80 if (mBillingAddress != null) {
please use gerrit instead 2016/10/17 20:44:18 mBillingAddress should never be null when getDetai
sebsg 2016/10/18 14:38:19 Done.
81 // Start the normalization task.
82 mIsWaitingForBillingNormalization = true;
83 boolean willNormalizeAsync =
84 PersonalDataManager.getInstance().normalizeAddress(mBillingA ddress.getGUID(),
please use gerrit instead 2016/10/17 20:44:18 if willNormalizeAsync is false, then the address i
sebsg 2016/10/18 14:38:19 Right, with the way I did it initially I needed th
85 AutofillAddress.getCountryCode(mBillingAddress), thi s);
86
87 if (willNormalizeAsync) {
88 // If the normalization was not done synchronously, start a time r to cancel the
89 // asynchronous normalization if it takes too long.
90 mHandler.postDelayed(new Runnable() {
91 @Override
92 public void run() {
93 onAddressNormalized(null);
94 }
95 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS() );
96 }
97
98 // SendInstrumentDetails will be called when the billing address has been normalized or
99 // when the timer has expired.
100 return;
101 }
102
103 // No billing normalization required, return the details now.
104 sendIntrumentDetails();
105 }
106
107 @Override
108 public void onAddressNormalized(AutofillProfile profile) {
109 if (!mIsWaitingForBillingNormalization) return;
please use gerrit instead 2016/10/17 20:44:18 This check is not necessary here, I think. If you
sebsg 2016/10/18 14:38:19 I see what you mean and I have done the flow sugge
110 mIsWaitingForBillingNormalization = false;
111
112 // If the normalization finished first, use the normalized address.
113 if (profile != null) mBillingAddress = profile;
114
115 sendIntrumentDetails();
116 }
117
118 private void sendIntrumentDetails() {
69 StringWriter stringWriter = new StringWriter(); 119 StringWriter stringWriter = new StringWriter();
70 JsonWriter json = new JsonWriter(stringWriter); 120 JsonWriter json = new JsonWriter(stringWriter);
71 try { 121 try {
72 json.beginObject(); 122 json.beginObject();
73 123
74 json.name("cardholderName").value(card.getName()); 124 json.name("cardholderName").value(mCard.getName());
75 json.name("cardNumber").value(card.getNumber()); 125 json.name("cardNumber").value(mCard.getNumber());
76 json.name("expiryMonth").value(card.getMonth()); 126 json.name("expiryMonth").value(mCard.getMonth());
77 json.name("expiryYear").value(card.getYear()); 127 json.name("expiryYear").value(mCard.getYear());
78 json.name("cardSecurityCode").value(cvc); 128 json.name("cardSecurityCode").value(mTempsCvc);
79 129
80 json.name("billingAddress").beginObject(); 130 json.name("billingAddress").beginObject();
81 131
82 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode())); 132 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC ode()));
83 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) ); 133 json.name("region").value(ensureNotNull(mBillingAddress.getRegion()) );
84 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) ); 134 json.name("city").value(ensureNotNull(mBillingAddress.getLocality()) );
85 json.name("dependentLocality") 135 json.name("dependentLocality")
86 .value(ensureNotNull(mBillingAddress.getDependentLocality()) ); 136 .value(ensureNotNull(mBillingAddress.getDependentLocality()) );
87 137
88 json.name("addressLine").beginArray(); 138 json.name("addressLine").beginArray();
(...skipping 14 matching lines...) Expand all
103 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r())); 153 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe r()));
104 154
105 json.endObject(); 155 json.endObject();
106 156
107 json.endObject(); 157 json.endObject();
108 } catch (IOException e) { 158 } catch (IOException e) {
109 onFullCardError(); 159 onFullCardError();
110 return; 160 return;
111 } 161 }
112 162
113 mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), strin gWriter.toString()); 163 mCallback.onInstrumentDetailsReady(
164 mCard.getBasicCardPaymentType(), stringWriter.toString());
165
166 mTempsCvc = "";
please use gerrit instead 2016/10/17 20:44:18 This should be inside of the finally{} block for t
sebsg 2016/10/18 14:38:19 Done.
114 } 167 }
115 168
116 private static String ensureNotNull(@Nullable String value) { 169 private static String ensureNotNull(@Nullable String value) {
117 return value == null ? "" : value; 170 return value == null ? "" : value;
118 } 171 }
119 172
120 @Override 173 @Override
121 public void onFullCardError() { 174 public void onFullCardError() {
122 mCallback.onInstrumentDetailsError(); 175 mCallback.onInstrumentDetailsError();
123 mCallback = null; 176 mCallback = null;
(...skipping 30 matching lines...) Expand all
154 mBillingAddress = billingAddress; 207 mBillingAddress = billingAddress;
155 mIsComplete = true; 208 mIsComplete = true;
156 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(), 209 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber() , card.getName(),
157 null, card.getIssuerIconDrawableId()); 210 null, card.getIssuerIconDrawableId());
158 } 211 }
159 212
160 /** @return The credit card represented by this payment instrument. */ 213 /** @return The credit card represented by this payment instrument. */
161 public CreditCard getCard() { 214 public CreditCard getCard() {
162 return mCard; 215 return mCard;
163 } 216 }
164 } 217
218 /** @return The billing address associated with this credit card. */
219 public AutofillProfile getBillingAddress() {
220 return mBillingAddress;
221 }
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698