OLD | NEW |
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.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Handler; |
8 import android.text.TextUtils; | 9 import android.text.TextUtils; |
9 import android.util.JsonWriter; | 10 import android.util.JsonWriter; |
10 | 11 |
11 import org.json.JSONObject; | 12 import org.json.JSONObject; |
12 | 13 |
13 import org.chromium.base.ApiCompatibilityUtils; | 14 import org.chromium.base.ApiCompatibilityUtils; |
14 import org.chromium.chrome.browser.autofill.PersonalDataManager; | 15 import org.chromium.chrome.browser.autofill.PersonalDataManager; |
15 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 16 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
16 import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; | 17 import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; |
17 import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestD
elegate; | 18 import org.chromium.chrome.browser.autofill.PersonalDataManager.FullCardRequestD
elegate; |
| 19 import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddres
sRequestDelegate; |
18 import org.chromium.content_public.browser.WebContents; | 20 import org.chromium.content_public.browser.WebContents; |
19 import org.chromium.payments.mojom.PaymentItem; | 21 import org.chromium.payments.mojom.PaymentItem; |
20 | 22 |
21 import java.io.IOException; | 23 import java.io.IOException; |
22 import java.io.StringWriter; | 24 import java.io.StringWriter; |
23 import java.util.List; | 25 import java.util.List; |
24 | 26 |
25 import javax.annotation.Nullable; | 27 import javax.annotation.Nullable; |
26 | 28 |
27 /** | 29 /** |
28 * The locally stored credit card payment instrument. | 30 * The locally stored credit card payment instrument. |
29 */ | 31 */ |
30 public class AutofillPaymentInstrument | 32 public class AutofillPaymentInstrument extends PaymentInstrument |
31 extends PaymentInstrument implements FullCardRequestDelegate { | 33 implements FullCardRequestDelegate, NormalizedAddressRequestDelegate { |
32 private final Context mContext; | 34 private final Context mContext; |
33 private final WebContents mWebContents; | 35 private final WebContents mWebContents; |
34 private CreditCard mCard; | 36 private CreditCard mCard; |
35 private boolean mIsComplete; | 37 private boolean mIsComplete; |
| 38 private String mSecurityCode; |
36 @Nullable private AutofillProfile mBillingAddress; | 39 @Nullable private AutofillProfile mBillingAddress; |
37 @Nullable private InstrumentDetailsCallback mCallback; | 40 @Nullable private InstrumentDetailsCallback mCallback; |
| 41 private boolean mIsWaitingForBillingNormalization; |
| 42 private boolean mIsWaitingForFullCardDetails; |
38 | 43 |
39 /** | 44 /** |
40 * Builds a payment instrument for the given credit card. | 45 * Builds a payment instrument for the given credit card. |
41 * | 46 * |
42 * @param context The application context. | 47 * @param context The application context. |
43 * @param webContents The web contents where PaymentRequest was invoked. | 48 * @param webContents The web contents where PaymentRequest was invoked. |
44 * @param card The autofill card that can be used for payment. | 49 * @param card The autofill card that can be used for payment. |
45 * @param billingAddress The billing address for the card. | 50 * @param billingAddress The billing address for the card. |
46 */ | 51 */ |
47 public AutofillPaymentInstrument(Context context, WebContents webContents, C
reditCard card, | 52 public AutofillPaymentInstrument(Context context, WebContents webContents, C
reditCard card, |
(...skipping 12 matching lines...) Expand all Loading... |
60 | 65 |
61 @Override | 66 @Override |
62 public String getInstrumentMethodName() { | 67 public String getInstrumentMethodName() { |
63 return mCard.getBasicCardPaymentType(); | 68 return mCard.getBasicCardPaymentType(); |
64 } | 69 } |
65 | 70 |
66 @Override | 71 @Override |
67 public void getInstrumentDetails(String unusedMerchantName, String unusedOri
gin, | 72 public void getInstrumentDetails(String unusedMerchantName, String unusedOri
gin, |
68 PaymentItem unusedTotal, List<PaymentItem> unusedCart, JSONObject un
usedDetails, | 73 PaymentItem unusedTotal, List<PaymentItem> unusedCart, JSONObject un
usedDetails, |
69 InstrumentDetailsCallback callback) { | 74 InstrumentDetailsCallback callback) { |
| 75 // The billing address should never be null for a credit card at this po
int. |
| 76 assert mBillingAddress != null; |
70 assert mIsComplete; | 77 assert mIsComplete; |
71 assert mCallback == null; | 78 assert mCallback == null; |
72 mCallback = callback; | 79 mCallback = callback; |
| 80 |
| 81 mIsWaitingForBillingNormalization = true; |
| 82 mIsWaitingForFullCardDetails = true; |
| 83 |
| 84 // Start the billing address normalization. |
| 85 PersonalDataManager.getInstance().normalizeAddress( |
| 86 mBillingAddress.getGUID(), AutofillAddress.getCountryCode(mBilli
ngAddress), this); |
| 87 |
| 88 // Start to get the full card details. |
73 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this)
; | 89 PersonalDataManager.getInstance().getFullCard(mWebContents, mCard, this)
; |
74 } | 90 } |
75 | 91 |
76 @Override | 92 @Override |
77 public void onFullCardDetails(CreditCard card, String cvc) { | 93 public void onFullCardDetails(CreditCard updatedCard, String cvc) { |
| 94 // Keep the cvc for after the normalization. |
| 95 mSecurityCode = cvc; |
| 96 |
| 97 // Update the card's expiration date. |
| 98 mCard.setMonth(updatedCard.getMonth()); |
| 99 mCard.setYear(updatedCard.getYear()); |
| 100 |
| 101 mIsWaitingForFullCardDetails = false; |
| 102 |
| 103 // Show the loading UI while the address gets normalized. |
| 104 mCallback.loadingInstrumentDetails(); |
| 105 |
| 106 // Wait for the billing address normalization before sending the instrum
ent details. |
| 107 if (mIsWaitingForBillingNormalization) { |
| 108 // If the normalization is not completed yet, Start a timer to cance
l it if it takes too |
| 109 // long. |
| 110 new Handler().postDelayed(new Runnable() { |
| 111 @Override |
| 112 public void run() { |
| 113 onAddressNormalized(null); |
| 114 } |
| 115 }, PersonalDataManager.getInstance().getNormalizationTimeoutMS()); |
| 116 |
| 117 return; |
| 118 } else { |
| 119 sendIntrumentDetails(); |
| 120 } |
| 121 } |
| 122 |
| 123 @Override |
| 124 public void onAddressNormalized(AutofillProfile profile) { |
| 125 if (!mIsWaitingForBillingNormalization) return; |
| 126 mIsWaitingForBillingNormalization = false; |
| 127 |
| 128 // If the normalization finished first, use the normalized address. |
| 129 if (profile != null) mBillingAddress = profile; |
| 130 |
| 131 // Wait for the full card details before sending the instrument details. |
| 132 if (!mIsWaitingForFullCardDetails) sendIntrumentDetails(); |
| 133 } |
| 134 |
| 135 /** |
| 136 * Stringify the card details and send the resulting string and the method n
ame to the |
| 137 * registered callback. |
| 138 */ |
| 139 private void sendIntrumentDetails() { |
78 StringWriter stringWriter = new StringWriter(); | 140 StringWriter stringWriter = new StringWriter(); |
79 JsonWriter json = new JsonWriter(stringWriter); | 141 JsonWriter json = new JsonWriter(stringWriter); |
80 try { | 142 try { |
81 json.beginObject(); | 143 json.beginObject(); |
82 | 144 |
83 json.name("cardholderName").value(card.getName()); | 145 json.name("cardholderName").value(mCard.getName()); |
84 json.name("cardNumber").value(card.getNumber()); | 146 json.name("cardNumber").value(mCard.getNumber()); |
85 json.name("expiryMonth").value(card.getMonth()); | 147 json.name("expiryMonth").value(mCard.getMonth()); |
86 json.name("expiryYear").value(card.getYear()); | 148 json.name("expiryYear").value(mCard.getYear()); |
87 json.name("cardSecurityCode").value(cvc); | 149 json.name("cardSecurityCode").value(mSecurityCode); |
88 | 150 |
89 json.name("billingAddress").beginObject(); | 151 json.name("billingAddress").beginObject(); |
90 | 152 |
91 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC
ode())); | 153 json.name("country").value(ensureNotNull(mBillingAddress.getCountryC
ode())); |
92 json.name("region").value(ensureNotNull(mBillingAddress.getRegion())
); | 154 json.name("region").value(ensureNotNull(mBillingAddress.getRegion())
); |
93 json.name("city").value(ensureNotNull(mBillingAddress.getLocality())
); | 155 json.name("city").value(ensureNotNull(mBillingAddress.getLocality())
); |
94 json.name("dependentLocality") | 156 json.name("dependentLocality") |
95 .value(ensureNotNull(mBillingAddress.getDependentLocality())
); | 157 .value(ensureNotNull(mBillingAddress.getDependentLocality())
); |
96 | 158 |
97 json.name("addressLine").beginArray(); | 159 json.name("addressLine").beginArray(); |
(...skipping 12 matching lines...) Expand all Loading... |
110 json.name("organization").value(ensureNotNull(mBillingAddress.getCom
panyName())); | 172 json.name("organization").value(ensureNotNull(mBillingAddress.getCom
panyName())); |
111 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa
me())); | 173 json.name("recipient").value(ensureNotNull(mBillingAddress.getFullNa
me())); |
112 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe
r())); | 174 json.name("phone").value(ensureNotNull(mBillingAddress.getPhoneNumbe
r())); |
113 | 175 |
114 json.endObject(); | 176 json.endObject(); |
115 | 177 |
116 json.endObject(); | 178 json.endObject(); |
117 } catch (IOException e) { | 179 } catch (IOException e) { |
118 onFullCardError(); | 180 onFullCardError(); |
119 return; | 181 return; |
| 182 } finally { |
| 183 mSecurityCode = ""; |
120 } | 184 } |
121 | 185 |
122 mCallback.onInstrumentDetailsReady(card.getBasicCardPaymentType(), strin
gWriter.toString()); | 186 mCallback.onInstrumentDetailsReady( |
| 187 mCard.getBasicCardPaymentType(), stringWriter.toString()); |
123 } | 188 } |
124 | 189 |
125 private static String ensureNotNull(@Nullable String value) { | 190 private static String ensureNotNull(@Nullable String value) { |
126 return value == null ? "" : value; | 191 return value == null ? "" : value; |
127 } | 192 } |
128 | 193 |
129 @Override | 194 @Override |
130 public void onFullCardError() { | 195 public void onFullCardError() { |
131 mCallback.onInstrumentDetailsError(); | 196 mCallback.onInstrumentDetailsError(); |
132 mCallback = null; | 197 mCallback = null; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 mIsComplete = true; | 230 mIsComplete = true; |
166 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber()
, card.getName(), | 231 updateIdentifierLabelsAndIcon(card.getGUID(), card.getObfuscatedNumber()
, card.getName(), |
167 null, ApiCompatibilityUtils.getDrawable( | 232 null, ApiCompatibilityUtils.getDrawable( |
168 mContext.getResources(), card.getIssuerIconDrawabl
eId())); | 233 mContext.getResources(), card.getIssuerIconDrawabl
eId())); |
169 } | 234 } |
170 | 235 |
171 /** @return The credit card represented by this payment instrument. */ | 236 /** @return The credit card represented by this payment instrument. */ |
172 public CreditCard getCard() { | 237 public CreditCard getCard() { |
173 return mCard; | 238 return mCard; |
174 } | 239 } |
175 } | 240 |
| 241 /** @return The billing address associated with this credit card. */ |
| 242 public AutofillProfile getBillingAddress() { |
| 243 return mBillingAddress; |
| 244 } |
| 245 } |
OLD | NEW |