OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.content.ComponentName; | 8 import android.content.ComponentName; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.DialogInterface; | 10 import android.content.DialogInterface; |
(...skipping 19 matching lines...) Expand all Loading... | |
30 import org.chromium.content_public.browser.WebContents; | 30 import org.chromium.content_public.browser.WebContents; |
31 import org.chromium.payments.mojom.PaymentCurrencyAmount; | 31 import org.chromium.payments.mojom.PaymentCurrencyAmount; |
32 import org.chromium.payments.mojom.PaymentDetailsModifier; | 32 import org.chromium.payments.mojom.PaymentDetailsModifier; |
33 import org.chromium.payments.mojom.PaymentItem; | 33 import org.chromium.payments.mojom.PaymentItem; |
34 import org.chromium.payments.mojom.PaymentMethodData; | 34 import org.chromium.payments.mojom.PaymentMethodData; |
35 import org.chromium.ui.base.WindowAndroid; | 35 import org.chromium.ui.base.WindowAndroid; |
36 | 36 |
37 import java.io.IOException; | 37 import java.io.IOException; |
38 import java.io.StringWriter; | 38 import java.io.StringWriter; |
39 import java.util.ArrayList; | 39 import java.util.ArrayList; |
40 import java.util.Collection; | |
40 import java.util.Collections; | 41 import java.util.Collections; |
41 import java.util.HashSet; | 42 import java.util.HashSet; |
42 import java.util.List; | 43 import java.util.List; |
43 import java.util.Map; | 44 import java.util.Map; |
44 import java.util.Set; | 45 import java.util.Set; |
45 | 46 |
46 import javax.annotation.Nullable; | 47 import javax.annotation.Nullable; |
47 | 48 |
48 /** | 49 /** |
49 * The point of interaction with a locally installed 3rd party native Android pa yment app. | 50 * The point of interaction with a locally installed 3rd party native Android pa yment app. |
(...skipping 22 matching lines...) Expand all Loading... | |
72 | 73 |
73 // Freshest parameters sent to the payment app. | 74 // Freshest parameters sent to the payment app. |
74 private static final String EXTRA_CERTIFICATE = "certificate"; | 75 private static final String EXTRA_CERTIFICATE = "certificate"; |
75 private static final String EXTRA_MERCHANT_NAME = "merchantName"; | 76 private static final String EXTRA_MERCHANT_NAME = "merchantName"; |
76 private static final String EXTRA_METHOD_DATA = "methodData"; | 77 private static final String EXTRA_METHOD_DATA = "methodData"; |
77 private static final String EXTRA_METHOD_NAMES = "methodNames"; | 78 private static final String EXTRA_METHOD_NAMES = "methodNames"; |
78 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId"; | 79 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId"; |
79 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr igin"; | 80 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr igin"; |
80 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer tificateChain"; | 81 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer tificateChain"; |
81 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin"; | 82 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin"; |
83 private static final String EXTRA_MODIFIERS = "modifiers"; | |
gogerald1
2017/05/17 19:11:46
You can move it under EXTRA_CERTIFICATE to list in
wuandy1
2017/05/18 20:12:29
Done.
| |
82 private static final String EXTRA_TOTAL = "total"; | 84 private static final String EXTRA_TOTAL = "total"; |
83 | 85 |
84 // Response from the payment app. | 86 // Response from the payment app. |
85 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = " instrumentDetails"; | 87 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = " instrumentDetails"; |
86 private static final String EXTRA_RESPONSE_DETAILS = "details"; | 88 private static final String EXTRA_RESPONSE_DETAILS = "details"; |
87 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName"; | 89 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName"; |
88 | 90 |
89 private static final String EMPTY_JSON_DATA = "{}"; | 91 private static final String EMPTY_JSON_DATA = "{}"; |
90 | 92 |
91 private final Handler mHandler; | 93 private final Handler mHandler; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 return; | 332 return; |
331 } | 333 } |
332 | 334 |
333 mPayIntent.putExtras(buildExtras(id, merchantName, origin, iframeOrigin, certificateChain, | 335 mPayIntent.putExtras(buildExtras(id, merchantName, origin, iframeOrigin, certificateChain, |
334 methodDataMap, total, displayItems, modifiers)); | 336 methodDataMap, total, displayItems, modifiers)); |
335 if (!window.showIntent(mPayIntent, this, R.string.payments_android_app_e rror)) { | 337 if (!window.showIntent(mPayIntent, this, R.string.payments_android_app_e rror)) { |
336 notifyErrorInvokingPaymentApp(); | 338 notifyErrorInvokingPaymentApp(); |
337 } | 339 } |
338 } | 340 } |
339 | 341 |
340 private static Bundle buildExtras(@Nullable String id, @Nullable String merc hantName, | 342 private Bundle buildExtras(@Nullable String id, @Nullable String merchantNam e, String origin, |
341 String origin, String iframeOrigin, @Nullable byte[][] certificateCh ain, | 343 String iframeOrigin, @Nullable byte[][] certificateChain, |
342 Map<String, PaymentMethodData> methodDataMap, PaymentItem total, | 344 Map<String, PaymentMethodData> methodDataMap, PaymentItem total, |
343 @Nullable List<PaymentItem> displayItems, | 345 @Nullable List<PaymentItem> displayItems, |
344 @Nullable Map<String, PaymentDetailsModifier> modifiers) { | 346 @Nullable Map<String, PaymentDetailsModifier> modifiers) { |
345 Bundle extras = new Bundle(); | 347 Bundle extras = new Bundle(); |
346 | 348 |
347 if (id != null) extras.putString(EXTRA_PAYMENT_REQUEST_ID, id); | 349 if (id != null) extras.putString(EXTRA_PAYMENT_REQUEST_ID, id); |
348 | 350 |
349 if (merchantName != null) extras.putString(EXTRA_MERCHANT_NAME, merchant Name); | 351 if (merchantName != null) extras.putString(EXTRA_MERCHANT_NAME, merchant Name); |
350 | 352 |
351 extras.putString(EXTRA_TOP_LEVEL_ORIGIN, origin); | 353 extras.putString(EXTRA_TOP_LEVEL_ORIGIN, origin); |
(...skipping 10 matching lines...) Expand all Loading... | |
362 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData Map.keySet())); | 364 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData Map.keySet())); |
363 | 365 |
364 Bundle methodDataBundle = new Bundle(); | 366 Bundle methodDataBundle = new Bundle(); |
365 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent rySet()) { | 367 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent rySet()) { |
366 methodDataBundle.putString(methodData.getKey(), | 368 methodDataBundle.putString(methodData.getKey(), |
367 methodData.getValue() == null ? EMPTY_JSON_DATA | 369 methodData.getValue() == null ? EMPTY_JSON_DATA |
368 : methodData.getValue().string ifiedData); | 370 : methodData.getValue().string ifiedData); |
369 } | 371 } |
370 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle); | 372 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle); |
371 | 373 |
374 if (modifiers != null) { | |
375 extras.putString(EXTRA_MODIFIERS, buildModifierListString(modifiers. values())); | |
gogerald1
2017/05/17 19:11:47
Do not put 'null' value to EXTRA_MODIFIERS
gogerald1
2017/05/17 19:11:47
You can name it "serializeModifiers" or "serialize
wuandy1
2017/05/18 20:12:29
Done.
wuandy1
2017/05/18 20:12:29
Done.
| |
376 } | |
377 | |
372 String serializedTotalAmount = serializeTotalAmount(total.amount); | 378 String serializedTotalAmount = serializeTotalAmount(total.amount); |
373 extras.putString(EXTRA_TOTAL, | 379 extras.putString(EXTRA_TOTAL, |
374 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota lAmount); | 380 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota lAmount); |
375 | 381 |
376 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica teChain, | 382 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica teChain, |
377 methodDataMap, methodDataBundle, total, displayItems, extras); | 383 methodDataMap, methodDataBundle, total, displayItems, extras); |
378 } | 384 } |
379 | 385 |
380 private static Bundle addDeprecatedExtras(@Nullable String id, String origin , | 386 private static Bundle addDeprecatedExtras(@Nullable String id, String origin , |
381 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha in, | 387 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha in, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 json.name("amount").beginObject(); | 489 json.name("amount").beginObject(); |
484 json.name("currency").value(item.amount.currency); | 490 json.name("currency").value(item.amount.currency); |
485 json.name("value").value(item.amount.value); | 491 json.name("value").value(item.amount.value); |
486 json.endObject(); | 492 json.endObject(); |
487 // }}} amount | 493 // }}} amount |
488 | 494 |
489 json.endObject(); | 495 json.endObject(); |
490 // }}} item | 496 // }}} item |
491 } | 497 } |
492 | 498 |
499 private String buildModifierListString(Collection<PaymentDetailsModifier> mo difiers) { | |
gogerald1
2017/05/17 19:11:47
annotate @Nullable
wuandy1
2017/05/18 20:12:28
changed to return empty json instead.
gogerald1
2017/05/18 21:57:33
even better,
| |
500 StringWriter stringWriter = new StringWriter(); | |
501 JsonWriter json = new JsonWriter(stringWriter); | |
502 try { | |
503 json.beginArray(); | |
504 for (PaymentDetailsModifier modifier : modifiers) { | |
505 for (String supported : modifier.methodData.supportedMethods) { | |
506 if (mMethodNames.contains(supported)) { | |
gogerald1
2017/05/17 19:11:47
You no need to do this again here since we already
wuandy1
2017/05/18 20:12:29
Done.
| |
507 serializeModifier(modifier, json); | |
508 break; | |
509 } | |
510 } | |
511 } | |
512 json.endArray(); | |
513 } catch (IOException e) { | |
514 return null; | |
515 } | |
516 return stringWriter.toString(); | |
517 } | |
518 | |
519 private static void serializeModifier(PaymentDetailsModifier modifier, JsonW riter json) | |
520 throws IOException { | |
521 // {{{ | |
522 json.beginObject(); | |
523 | |
524 // total {{{ | |
525 json.name("total"); | |
526 serializePaymentItem(modifier.total, json); | |
gogerald1
2017/05/17 19:11:47
total could be null in modifier
wuandy1
2017/05/18 20:12:29
Done.
| |
527 // }}} total | |
528 | |
529 // supportedMethods {{{ | |
530 json.name("supportedMethods").beginArray(); | |
531 for (String method : modifier.methodData.supportedMethods) { | |
532 json.value(method); | |
533 } | |
534 json.endArray(); | |
535 // }}} supportedMethods | |
536 | |
537 json.endObject(); | |
538 // }}} | |
539 } | |
540 | |
493 @Override | 541 @Override |
494 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d ata) { | 542 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d ata) { |
495 ThreadUtils.assertOnUiThread(); | 543 ThreadUtils.assertOnUiThread(); |
496 window.removeIntentCallback(this); | 544 window.removeIntentCallback(this); |
497 if (data == null || data.getExtras() == null || resultCode != Activity.R ESULT_OK) { | 545 if (data == null || data.getExtras() == null || resultCode != Activity.R ESULT_OK) { |
498 mInstrumentDetailsCallback.onInstrumentDetailsError(); | 546 mInstrumentDetailsCallback.onInstrumentDetailsError(); |
499 } else { | 547 } else { |
500 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS); | 548 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS); |
501 if (details == null) { | 549 if (details == null) { |
502 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I NSTRUMENT_DETAILS); | 550 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I NSTRUMENT_DETAILS); |
503 } | 551 } |
504 if (details == null) { | 552 if (details == null) { |
505 details = EMPTY_JSON_DATA; | 553 details = EMPTY_JSON_DATA; |
506 } | 554 } |
507 mInstrumentDetailsCallback.onInstrumentDetailsReady( | 555 mInstrumentDetailsCallback.onInstrumentDetailsReady( |
508 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta ils); | 556 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta ils); |
509 } | 557 } |
510 mInstrumentDetailsCallback = null; | 558 mInstrumentDetailsCallback = null; |
511 } | 559 } |
512 | 560 |
513 @Override | 561 @Override |
514 public void dismissInstrument() {} | 562 public void dismissInstrument() {} |
515 } | 563 } |
OLD | NEW |