| 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 #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ | 5 #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ |
| 6 #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ | 6 #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "components/payments/content/payment_request.mojom.h" | 13 #include "components/payments/content/payment_request.mojom.h" |
| 14 #include "components/payments/core/currency_formatter.h" |
| 14 | 15 |
| 15 namespace payments { | 16 namespace payments { |
| 16 | 17 |
| 17 // The spec contains all the options that the merchant has specified about this | 18 // The spec contains all the options that the merchant has specified about this |
| 18 // Payment Request. It's a (mostly) read-only view, which can be updated in | 19 // Payment Request. It's a (mostly) read-only view, which can be updated in |
| 19 // certain occasions by the merchant (see API). | 20 // certain occasions by the merchant (see API). |
| 20 class PaymentRequestSpec { | 21 class PaymentRequestSpec { |
| 21 public: | 22 public: |
| 22 // Any class call add itself as Observer via AddObserver() and receive | 23 // Any class call add itself as Observer via AddObserver() and receive |
| 23 // notification about spec events. | 24 // notification about spec events. |
| 24 class Observer { | 25 class Observer { |
| 25 public: | 26 public: |
| 26 // Called when the provided spec (details, options, method_data) is invalid. | 27 // Called when the provided spec (details, options, method_data) is invalid. |
| 27 virtual void OnInvalidSpecProvided() = 0; | 28 virtual void OnInvalidSpecProvided() = 0; |
| 28 | 29 |
| 29 protected: | 30 protected: |
| 30 virtual ~Observer() {} | 31 virtual ~Observer() {} |
| 31 }; | 32 }; |
| 32 | 33 |
| 33 PaymentRequestSpec(mojom::PaymentOptionsPtr options, | 34 PaymentRequestSpec(mojom::PaymentOptionsPtr options, |
| 34 mojom::PaymentDetailsPtr details, | 35 mojom::PaymentDetailsPtr details, |
| 35 std::vector<mojom::PaymentMethodDataPtr> method_data, | 36 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 36 PaymentRequestSpec::Observer* observer); | 37 PaymentRequestSpec::Observer* observer, |
| 38 const std::string& app_locale); |
| 37 ~PaymentRequestSpec(); | 39 ~PaymentRequestSpec(); |
| 38 | 40 |
| 39 void AddObserver(Observer* observer); | 41 void AddObserver(Observer* observer); |
| 40 void RemoveObserver(Observer* observer); | 42 void RemoveObserver(Observer* observer); |
| 41 | 43 |
| 42 bool request_shipping() const { return options_->request_shipping; } | 44 bool request_shipping() const { return options_->request_shipping; } |
| 43 bool request_payer_name() const { return options_->request_payer_name; } | 45 bool request_payer_name() const { return options_->request_payer_name; } |
| 44 bool request_payer_phone() const { return options_->request_payer_phone; } | 46 bool request_payer_phone() const { return options_->request_payer_phone; } |
| 45 bool request_payer_email() const { return options_->request_payer_email; } | 47 bool request_payer_email() const { return options_->request_payer_email; } |
| 46 | 48 |
| 47 const std::vector<std::string>& supported_card_networks() { | 49 const std::vector<std::string>& supported_card_networks() { |
| 48 return supported_card_networks_; | 50 return supported_card_networks_; |
| 49 } | 51 } |
| 50 | 52 |
| 53 // Uses CurrencyFormatter to format |amount| with the currency symbol for this |
| 54 // request's currency. Will use currency of the "total" display item, because |
| 55 // all items are supposed to have the same currency in a given request. |
| 56 base::string16 GetFormattedCurrencyAmount(const std::string& amount); |
| 57 |
| 58 // Uses CurrencyFormatter to get the formatted currency code for this |
| 59 // request's currency. Will use currency of the "total" display item, because |
| 60 // all items are supposed to have the same currency in a given request. |
| 61 std::string GetFormattedCurrencyCode(); |
| 62 |
| 51 const mojom::PaymentDetails& details() const { return *details_.get(); } | 63 const mojom::PaymentDetails& details() const { return *details_.get(); } |
| 52 const mojom::PaymentOptions& options() const { return *options_.get(); } | 64 const mojom::PaymentOptions& options() const { return *options_.get(); } |
| 53 | 65 |
| 54 private: | 66 private: |
| 55 // Validates the |method_data| and fills |supported_card_networks_|. | 67 // Validates the |method_data| and fills |supported_card_networks_|. |
| 56 void PopulateValidatedMethodData( | 68 void PopulateValidatedMethodData( |
| 57 const std::vector<mojom::PaymentMethodDataPtr>& method_data); | 69 const std::vector<mojom::PaymentMethodDataPtr>& method_data); |
| 58 | 70 |
| 59 // Will notify all observers that the spec is invalid. | 71 // Will notify all observers that the spec is invalid. |
| 60 void NotifyOnInvalidSpecProvided(); | 72 void NotifyOnInvalidSpecProvided(); |
| 61 | 73 |
| 74 // Returns the CurrencyFormatter instance for this PaymentRequest. |
| 75 // |locale_name| should be the result of the browser's GetApplicationLocale(). |
| 76 // Note: Having multiple currencies per PaymentRequest is not supported; hence |
| 77 // the CurrencyFormatter is cached here. |
| 78 CurrencyFormatter* GetOrCreateCurrencyFormatter( |
| 79 const std::string& currency_code, |
| 80 const std::string& currency_system, |
| 81 const std::string& locale_name); |
| 82 |
| 62 mojom::PaymentOptionsPtr options_; | 83 mojom::PaymentOptionsPtr options_; |
| 63 mojom::PaymentDetailsPtr details_; | 84 mojom::PaymentDetailsPtr details_; |
| 85 const std::string app_locale_; |
| 86 std::unique_ptr<CurrencyFormatter> currency_formatter_; |
| 64 | 87 |
| 65 // A list of supported basic card networks, in order that they were specified | 88 // A list of supported basic card networks, in order that they were specified |
| 66 // by the merchant. | 89 // by the merchant. |
| 67 std::vector<std::string> supported_card_networks_; | 90 std::vector<std::string> supported_card_networks_; |
| 68 | 91 |
| 69 base::ObserverList<Observer> observers_; | 92 base::ObserverList<Observer> observers_; |
| 70 | 93 |
| 71 DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec); | 94 DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec); |
| 72 }; | 95 }; |
| 73 | 96 |
| 74 } // namespace payments | 97 } // namespace payments |
| 75 | 98 |
| 76 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ | 99 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ |
| OLD | NEW |