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.app.Activity; | 7 import android.app.Activity; |
8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
9 import android.os.Handler; | 9 import android.os.Handler; |
10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 disconnectFromClientWithDebugMessage("PaymentRequest.show() called m
ore than once."); | 148 disconnectFromClientWithDebugMessage("PaymentRequest.show() called m
ore than once."); |
149 return; | 149 return; |
150 } | 150 } |
151 | 151 |
152 mSupportedMethods = getValidatedSupportedMethods(supportedMethods); | 152 mSupportedMethods = getValidatedSupportedMethods(supportedMethods); |
153 if (mSupportedMethods == null) { | 153 if (mSupportedMethods == null) { |
154 disconnectFromClientWithDebugMessage("Invalid payment methods"); | 154 disconnectFromClientWithDebugMessage("Invalid payment methods"); |
155 return; | 155 return; |
156 } | 156 } |
157 | 157 |
158 mLineItems = getValidatedLineItems(details); | 158 if (!setLineItemsAndShippingOptionsOrDisconnectFromClient(details)) retu
rn; |
159 if (mLineItems == null) { | |
160 disconnectFromClientWithDebugMessage("Invalid line items"); | |
161 return; | |
162 } | |
163 mPaymentItems = Arrays.asList(details.items); | |
164 | |
165 mShippingOptions = | |
166 getValidatedShippingOptions(details.items[0].amount.currencyCode
, details); | |
167 if (mShippingOptions == null) { | |
168 disconnectFromClientWithDebugMessage("Invalid shipping options"); | |
169 return; | |
170 } | |
171 | 159 |
172 // If the merchant requests shipping and does not provide shipping optio
ns here, then the | 160 // If the merchant requests shipping and does not provide shipping optio
ns here, then the |
173 // merchant needs the shipping address to calculate shipping price and a
vailability. | 161 // merchant needs the shipping address to calculate shipping price and a
vailability. |
174 boolean requestShipping = options != null && options.requestShipping; | 162 boolean requestShipping = options != null && options.requestShipping; |
175 mMerchantNeedsShippingAddress = requestShipping && mShippingOptions.isEm
pty(); | 163 mMerchantNeedsShippingAddress = requestShipping && mShippingOptions.isEm
pty(); |
176 | 164 |
177 mData = getValidatedData(mSupportedMethods, stringifiedData); | 165 mData = getValidatedData(mSupportedMethods, stringifiedData); |
178 if (mData == null) { | 166 if (mData == null) { |
179 disconnectFromClientWithDebugMessage("Invalid payment method specifi
c data"); | 167 disconnectFromClientWithDebugMessage("Invalid payment method specifi
c data"); |
180 return; | 168 return; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 204 } |
217 } | 205 } |
218 | 206 |
219 if (!isGettingInstruments) mPaymentMethods = new SectionInformation(); | 207 if (!isGettingInstruments) mPaymentMethods = new SectionInformation(); |
220 | 208 |
221 mUI = PaymentRequestUI.show(mContext, this, requestShipping, mMerchantNa
me, mOrigin); | 209 mUI = PaymentRequestUI.show(mContext, this, requestShipping, mMerchantNa
me, mOrigin); |
222 if (mFavicon != null) mUI.setTitleBitmap(mFavicon); | 210 if (mFavicon != null) mUI.setTitleBitmap(mFavicon); |
223 mFavicon = null; | 211 mFavicon = null; |
224 } | 212 } |
225 | 213 |
| 214 /** |
| 215 * Called by merchant to update the shipping options and line items after th
e user has selected |
| 216 * their shipping address or shipping option. |
| 217 */ |
| 218 @Override |
| 219 public void updateWith(PaymentDetails details) { |
| 220 if (mClient == null) return; |
| 221 |
| 222 if (mUI == null) { |
| 223 disconnectFromClientWithDebugMessage( |
| 224 "PaymentRequestUpdateEvent.updateWith() called without Payme
ntRequest.show()"); |
| 225 return; |
| 226 } |
| 227 |
| 228 if (!setLineItemsAndShippingOptionsOrDisconnectFromClient(details)) retu
rn; |
| 229 |
| 230 // Empty shipping options means the merchant cannot ship to the user's s
elected shipping |
| 231 // address. |
| 232 if (mShippingOptions.isEmpty() && !mMerchantNeedsShippingAddress) { |
| 233 disconnectFromClientWithDebugMessage("Merchant indicates inablity to
ship although " |
| 234 + "originally indicated that can ship anywhere"); |
| 235 } |
| 236 |
| 237 mUI.updateLineItems(mLineItems); |
| 238 mUI.updateShippingOptions(mShippingOptions); |
| 239 } |
| 240 |
| 241 private boolean setLineItemsAndShippingOptionsOrDisconnectFromClient(Payment
Details details) { |
| 242 mLineItems = getValidatedLineItems(details); |
| 243 if (mLineItems == null) { |
| 244 disconnectFromClientWithDebugMessage("Invalid line items"); |
| 245 return false; |
| 246 } |
| 247 mPaymentItems = Arrays.asList(details.items); |
| 248 |
| 249 mShippingOptions = |
| 250 getValidatedShippingOptions(details.items[0].amount.currencyCode
, details); |
| 251 if (mShippingOptions == null) { |
| 252 disconnectFromClientWithDebugMessage("Invalid shipping options"); |
| 253 return false; |
| 254 } |
| 255 |
| 256 return true; |
| 257 } |
| 258 |
226 private HashSet<String> getValidatedSupportedMethods(String[] methods) { | 259 private HashSet<String> getValidatedSupportedMethods(String[] methods) { |
227 // Payment methods are required. | 260 // Payment methods are required. |
228 if (methods == null || methods.length == 0) return null; | 261 if (methods == null || methods.length == 0) return null; |
229 | 262 |
230 HashSet<String> result = new HashSet<>(); | 263 HashSet<String> result = new HashSet<>(); |
231 for (int i = 0; i < methods.length; i++) { | 264 for (int i = 0; i < methods.length; i++) { |
232 // Payment methods should be non-empty. | 265 // Payment methods should be non-empty. |
233 if (TextUtils.isEmpty(methods[i])) return null; | 266 if (TextUtils.isEmpty(methods[i])) return null; |
234 result.add(methods[i]); | 267 result.add(methods[i]); |
235 } | 268 } |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 if (mPaymentMethods != null) { | 592 if (mPaymentMethods != null) { |
560 for (int i = 0; i < mPaymentMethods.getSize(); i++) { | 593 for (int i = 0; i < mPaymentMethods.getSize(); i++) { |
561 PaymentOption option = mPaymentMethods.getItem(i); | 594 PaymentOption option = mPaymentMethods.getItem(i); |
562 assert option instanceof PaymentInstrument; | 595 assert option instanceof PaymentInstrument; |
563 ((PaymentInstrument) option).dismiss(); | 596 ((PaymentInstrument) option).dismiss(); |
564 } | 597 } |
565 mPaymentMethods = null; | 598 mPaymentMethods = null; |
566 } | 599 } |
567 } | 600 } |
568 } | 601 } |
OLD | NEW |