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 20 matching lines...) Expand all Loading... |
31 import org.chromium.content_public.browser.WebContents; | 31 import org.chromium.content_public.browser.WebContents; |
32 import org.chromium.payments.mojom.PaymentCurrencyAmount; | 32 import org.chromium.payments.mojom.PaymentCurrencyAmount; |
33 import org.chromium.payments.mojom.PaymentDetailsModifier; | 33 import org.chromium.payments.mojom.PaymentDetailsModifier; |
34 import org.chromium.payments.mojom.PaymentItem; | 34 import org.chromium.payments.mojom.PaymentItem; |
35 import org.chromium.payments.mojom.PaymentMethodData; | 35 import org.chromium.payments.mojom.PaymentMethodData; |
36 import org.chromium.ui.base.WindowAndroid; | 36 import org.chromium.ui.base.WindowAndroid; |
37 | 37 |
38 import java.io.IOException; | 38 import java.io.IOException; |
39 import java.io.StringWriter; | 39 import java.io.StringWriter; |
40 import java.util.ArrayList; | 40 import java.util.ArrayList; |
| 41 import java.util.Collection; |
41 import java.util.Collections; | 42 import java.util.Collections; |
42 import java.util.HashSet; | 43 import java.util.HashSet; |
43 import java.util.List; | 44 import java.util.List; |
44 import java.util.Map; | 45 import java.util.Map; |
45 import java.util.Set; | 46 import java.util.Set; |
46 | 47 |
47 import javax.annotation.Nullable; | 48 import javax.annotation.Nullable; |
48 | 49 |
49 /** | 50 /** |
50 * The point of interaction with a locally installed 3rd party native Android pa
yment app. | 51 * The point of interaction with a locally installed 3rd party native Android pa
yment app. |
(...skipping 18 matching lines...) Expand all Loading... |
69 private static final String EXTRA_DEPRECATED_ID = "id"; | 70 private static final String EXTRA_DEPRECATED_ID = "id"; |
70 private static final String EXTRA_DEPRECATED_IFRAME_ORIGIN = "iframeOrigin"; | 71 private static final String EXTRA_DEPRECATED_IFRAME_ORIGIN = "iframeOrigin"; |
71 private static final String EXTRA_DEPRECATED_METHOD_NAME = "methodName"; | 72 private static final String EXTRA_DEPRECATED_METHOD_NAME = "methodName"; |
72 private static final String EXTRA_DEPRECATED_ORIGIN = "origin"; | 73 private static final String EXTRA_DEPRECATED_ORIGIN = "origin"; |
73 | 74 |
74 // Freshest parameters sent to the payment app. | 75 // Freshest parameters sent to the payment app. |
75 private static final String EXTRA_CERTIFICATE = "certificate"; | 76 private static final String EXTRA_CERTIFICATE = "certificate"; |
76 private static final String EXTRA_MERCHANT_NAME = "merchantName"; | 77 private static final String EXTRA_MERCHANT_NAME = "merchantName"; |
77 private static final String EXTRA_METHOD_DATA = "methodData"; | 78 private static final String EXTRA_METHOD_DATA = "methodData"; |
78 private static final String EXTRA_METHOD_NAMES = "methodNames"; | 79 private static final String EXTRA_METHOD_NAMES = "methodNames"; |
| 80 private static final String EXTRA_MODIFIERS = "modifiers"; |
79 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId"; | 81 private static final String EXTRA_PAYMENT_REQUEST_ID = "paymentRequestId"; |
80 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr
igin"; | 82 private static final String EXTRA_PAYMENT_REQUEST_ORIGIN = "paymentRequestOr
igin"; |
81 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer
tificateChain"; | 83 private static final String EXTRA_TOP_LEVEL_CERTIFICATE_CHAIN = "topLevelCer
tificateChain"; |
82 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin"; | 84 private static final String EXTRA_TOP_LEVEL_ORIGIN = "topLevelOrigin"; |
83 private static final String EXTRA_TOTAL = "total"; | 85 private static final String EXTRA_TOTAL = "total"; |
84 | 86 |
85 // Response from the payment app. | 87 // Response from the payment app. |
86 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = "
instrumentDetails"; | 88 private static final String EXTRA_DEPRECATED_RESPONSE_INSTRUMENT_DETAILS = "
instrumentDetails"; |
87 private static final String EXTRA_RESPONSE_DETAILS = "details"; | 89 private static final String EXTRA_RESPONSE_DETAILS = "details"; |
88 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName"; | 90 private static final String EXTRA_RESPONSE_METHOD_NAME = "methodName"; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData
Map.keySet())); | 371 extras.putStringArrayList(EXTRA_METHOD_NAMES, new ArrayList<>(methodData
Map.keySet())); |
370 | 372 |
371 Bundle methodDataBundle = new Bundle(); | 373 Bundle methodDataBundle = new Bundle(); |
372 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent
rySet()) { | 374 for (Map.Entry<String, PaymentMethodData> methodData : methodDataMap.ent
rySet()) { |
373 methodDataBundle.putString(methodData.getKey(), | 375 methodDataBundle.putString(methodData.getKey(), |
374 methodData.getValue() == null ? EMPTY_JSON_DATA | 376 methodData.getValue() == null ? EMPTY_JSON_DATA |
375 : methodData.getValue().string
ifiedData); | 377 : methodData.getValue().string
ifiedData); |
376 } | 378 } |
377 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle); | 379 extras.putParcelable(EXTRA_METHOD_DATA, methodDataBundle); |
378 | 380 |
| 381 if (modifiers != null) { |
| 382 extras.putString(EXTRA_MODIFIERS, serializeModifiers(modifiers.value
s())); |
| 383 } |
| 384 |
379 String serializedTotalAmount = serializeTotalAmount(total.amount); | 385 String serializedTotalAmount = serializeTotalAmount(total.amount); |
380 extras.putString(EXTRA_TOTAL, | 386 extras.putString(EXTRA_TOTAL, |
381 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota
lAmount); | 387 serializedTotalAmount == null ? EMPTY_JSON_DATA : serializedTota
lAmount); |
382 | 388 |
383 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica
teChain, | 389 return addDeprecatedExtras(id, origin, iframeOrigin, serializedCertifica
teChain, |
384 methodDataMap, methodDataBundle, total, displayItems, extras); | 390 methodDataMap, methodDataBundle, total, displayItems, extras); |
385 } | 391 } |
386 | 392 |
387 private static Bundle addDeprecatedExtras(@Nullable String id, String origin
, | 393 private static Bundle addDeprecatedExtras(@Nullable String id, String origin
, |
388 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha
in, | 394 String iframeOrigin, @Nullable Parcelable[] serializedCertificateCha
in, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 json.name("amount").beginObject(); | 496 json.name("amount").beginObject(); |
491 json.name("currency").value(item.amount.currency); | 497 json.name("currency").value(item.amount.currency); |
492 json.name("value").value(item.amount.value); | 498 json.name("value").value(item.amount.value); |
493 json.endObject(); | 499 json.endObject(); |
494 // }}} amount | 500 // }}} amount |
495 | 501 |
496 json.endObject(); | 502 json.endObject(); |
497 // }}} item | 503 // }}} item |
498 } | 504 } |
499 | 505 |
| 506 private static String serializeModifiers(Collection<PaymentDetailsModifier>
modifiers) { |
| 507 StringWriter stringWriter = new StringWriter(); |
| 508 JsonWriter json = new JsonWriter(stringWriter); |
| 509 try { |
| 510 json.beginArray(); |
| 511 for (PaymentDetailsModifier modifier : modifiers) { |
| 512 serializeModifier(modifier, json); |
| 513 } |
| 514 json.endArray(); |
| 515 } catch (IOException e) { |
| 516 return EMPTY_JSON_DATA; |
| 517 } |
| 518 return stringWriter.toString(); |
| 519 } |
| 520 |
| 521 private static void serializeModifier(PaymentDetailsModifier modifier, JsonW
riter json) |
| 522 throws IOException { |
| 523 // {{{ |
| 524 json.beginObject(); |
| 525 |
| 526 // total {{{ |
| 527 if (modifier.total != null) { |
| 528 json.name("total"); |
| 529 serializePaymentItem(modifier.total, json); |
| 530 } else { |
| 531 json.name("total").nullValue(); |
| 532 } |
| 533 // }}} total |
| 534 |
| 535 // supportedMethods {{{ |
| 536 json.name("supportedMethods").beginArray(); |
| 537 for (String method : modifier.methodData.supportedMethods) { |
| 538 json.value(method); |
| 539 } |
| 540 json.endArray(); |
| 541 // }}} supportedMethods |
| 542 |
| 543 // data {{{ |
| 544 json.name("data").value(modifier.methodData.stringifiedData); |
| 545 // }}} |
| 546 |
| 547 json.endObject(); |
| 548 // }}} |
| 549 } |
| 550 |
500 @Override | 551 @Override |
501 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d
ata) { | 552 public void onIntentCompleted(WindowAndroid window, int resultCode, Intent d
ata) { |
502 ThreadUtils.assertOnUiThread(); | 553 ThreadUtils.assertOnUiThread(); |
503 window.removeIntentCallback(this); | 554 window.removeIntentCallback(this); |
504 if (data == null || data.getExtras() == null || resultCode != Activity.R
ESULT_OK) { | 555 if (data == null || data.getExtras() == null || resultCode != Activity.R
ESULT_OK) { |
505 mInstrumentDetailsCallback.onInstrumentDetailsError(); | 556 mInstrumentDetailsCallback.onInstrumentDetailsError(); |
506 } else { | 557 } else { |
507 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS); | 558 String details = data.getExtras().getString(EXTRA_RESPONSE_DETAILS); |
508 if (details == null) { | 559 if (details == null) { |
509 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I
NSTRUMENT_DETAILS); | 560 details = data.getExtras().getString(EXTRA_DEPRECATED_RESPONSE_I
NSTRUMENT_DETAILS); |
510 } | 561 } |
511 if (details == null) { | 562 if (details == null) { |
512 details = EMPTY_JSON_DATA; | 563 details = EMPTY_JSON_DATA; |
513 } | 564 } |
514 mInstrumentDetailsCallback.onInstrumentDetailsReady( | 565 mInstrumentDetailsCallback.onInstrumentDetailsReady( |
515 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta
ils); | 566 data.getExtras().getString(EXTRA_RESPONSE_METHOD_NAME), deta
ils); |
516 } | 567 } |
517 mInstrumentDetailsCallback = null; | 568 mInstrumentDetailsCallback = null; |
518 } | 569 } |
519 | 570 |
520 @Override | 571 @Override |
521 public void dismissInstrument() {} | 572 public void dismissInstrument() {} |
522 } | 573 } |
OLD | NEW |