krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 5 | #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
| 6 | #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 11 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 12 | #include "base/macros.h" |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 13 | #include "base/observer_list.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 14 | #include "components/payments/content/payment_request.mojom.h" |
| 15 | #include "components/payments/content/payment_request_delegate.h" |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/binding.h" |
| 17 | |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 18 | namespace autofill { |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 19 | class AutofillProfile; |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 20 | class CreditCard; |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 21 | class PersonalDataManager; |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 22 | } |
| 23 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 24 | namespace content { |
| 25 | class WebContents; |
| 26 | } |
| 27 | |
| 28 | namespace payments { |
| 29 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 30 | class CurrencyFormatter; |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 31 | class PaymentRequestWebContentsManager; |
| 32 | |
| 33 | class PaymentRequest : payments::mojom::PaymentRequest { |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 34 | public: |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 35 | class Observer { |
| 36 | public: |
| 37 | // Called when the information (payment method, address/contact info, |
| 38 | // shipping option) changes. |
| 39 | virtual void OnSelectedInformationChanged() = 0; |
| 40 | |
| 41 | protected: |
| 42 | virtual ~Observer() {} |
| 43 | }; |
| 44 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 45 | PaymentRequest( |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 46 | content::WebContents* web_contents, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 47 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 48 | PaymentRequestWebContentsManager* manager, |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 49 | mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 50 | ~PaymentRequest() override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 51 | |
| 52 | // payments::mojom::PaymentRequest "stub" |
| 53 | void Init(payments::mojom::PaymentRequestClientPtr client, |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 54 | std::vector<payments::mojom::PaymentMethodDataPtr> method_data, |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 55 | payments::mojom::PaymentDetailsPtr details, |
| 56 | payments::mojom::PaymentOptionsPtr options) override; |
sanjoy.pal | a1f17e8 | 2016-12-15 03:39:12 | [diff] [blame] | 57 | void Show() override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 58 | void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 59 | void Abort() override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 60 | void Complete(payments::mojom::PaymentComplete result) override {} |
rob.buis | e3da0af | 2016-12-02 23:36:57 | [diff] [blame] | 61 | void CanMakePayment() override {} |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 62 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 63 | // Called when the user explicitely cancelled the flow. Will send a message |
| 64 | // to the renderer which will indirectly destroy this object (through |
| 65 | // OnConnectionTerminated). |
| 66 | void UserCancelled(); |
| 67 | |
| 68 | // As a result of a browser-side error or renderer-initiated mojo channel |
| 69 | // closure (e.g. there was an error on the renderer side, or payment was |
| 70 | // successful), this method is called. It is responsible for cleaning up, |
| 71 | // such as possibly closing the dialog. |
| 72 | void OnConnectionTerminated(); |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 73 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 74 | // Called when the user clicks on the "Pay" button. |
| 75 | void Pay(); |
| 76 | |
| 77 | void AddObserver(Observer* observer); |
| 78 | void RemoveObserver(Observer* observer); |
| 79 | |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 80 | // Returns the CurrencyFormatter instance for this PaymentRequest. |
| 81 | // |locale_name| should be the result of the browser's GetApplicationLocale(). |
| 82 | // Note: Having multiple currencies per PaymentRequest is not supported; hence |
| 83 | // the CurrencyFormatter is cached here. |
| 84 | CurrencyFormatter* GetOrCreateCurrencyFormatter( |
| 85 | const std::string& currency_code, |
jinho.bang | 4276454 | 2017-01-24 14:42:56 | [diff] [blame] | 86 | const std::string& currency_system, |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 87 | const std::string& locale_name); |
| 88 | |
anthonyvd | b0233f2 | 2017-03-06 20:34:47 | [diff] [blame^] | 89 | // Uses CurrencyFormatter to format |amount| with the currency symbol for this |
| 90 | // request's currency. |
| 91 | base::string16 GetFormattedCurrencyAmount(const std::string& amount); |
| 92 | |
| 93 | // Uses CurrencyFormatter to get the formatted currency code for this |
| 94 | // request's currency. |
| 95 | std::string GetFormattedCurrencyCode(); |
| 96 | |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 97 | // Returns the appropriate Autofill Profiles for this user. On the first |
| 98 | // invocation of either getter, the profiles are fetched from the |
| 99 | // PersonalDataManager; on subsequent invocations, a cached version is |
| 100 | // returned. The profiles returned are owned by the request object. |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 101 | const std::vector<autofill::AutofillProfile*>& shipping_profiles() { |
| 102 | return shipping_profiles_; |
| 103 | } |
| 104 | const std::vector<autofill::AutofillProfile*>& contact_profiles() { |
| 105 | return contact_profiles_; |
| 106 | } |
| 107 | const std::vector<autofill::CreditCard*>& credit_cards() { |
| 108 | return credit_cards_; |
| 109 | } |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 110 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 111 | // Gets the Autofill Profile representing the shipping address or contact |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 112 | // information currently selected for this PaymentRequest flow. Can return |
| 113 | // null. |
| 114 | autofill::AutofillProfile* selected_shipping_profile() const { |
| 115 | return selected_shipping_profile_; |
| 116 | } |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 117 | autofill::AutofillProfile* selected_contact_profile() const { |
| 118 | return selected_contact_profile_; |
| 119 | } |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 120 | // Returns the currently selected credit card for this PaymentRequest flow. |
| 121 | // It's not guaranteed to be complete. Returns nullptr if there is no selected |
| 122 | // card. |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame] | 123 | autofill::CreditCard* selected_credit_card() { return selected_credit_card_; } |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 124 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 125 | // Sets the |profile| to be the selected one and will update state and notify |
| 126 | // observers. |
| 127 | void SetSelectedShippingProfile(autofill::AutofillProfile* profile); |
| 128 | void SetSelectedContactProfile(autofill::AutofillProfile* profile); |
| 129 | void SetSelectedCreditCard(autofill::CreditCard* card); |
anthonyvd | 4b014c7e | 2017-02-27 17:08:28 | [diff] [blame] | 130 | |
mathp | d4cfd8f | 2017-02-09 21:16:53 | [diff] [blame] | 131 | autofill::PersonalDataManager* personal_data_manager() { |
| 132 | return delegate_->GetPersonalDataManager(); |
| 133 | } |
| 134 | |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 135 | payments::mojom::PaymentDetails* details() { return details_.get(); } |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 136 | const std::vector<std::string>& supported_card_networks() { |
| 137 | return supported_card_networks_; |
| 138 | } |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 139 | content::WebContents* web_contents() { return web_contents_; } |
| 140 | |
mathp | abf6675 | 2017-03-01 22:16:44 | [diff] [blame] | 141 | bool request_shipping() const { return options_->request_shipping; } |
| 142 | bool request_payer_name() const { return options_->request_payer_name; } |
| 143 | bool request_payer_phone() const { return options_->request_payer_phone; } |
| 144 | bool request_payer_email() const { return options_->request_payer_email; } |
| 145 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 146 | bool is_ready_to_pay() { return is_ready_to_pay_; } |
| 147 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 148 | private: |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 149 | // Fetches the Autofill Profiles for this user from the PersonalDataManager, |
| 150 | // and stores copies of them, owned by this Request, in profile_cache_. |
| 151 | void PopulateProfileCache(); |
| 152 | |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame] | 153 | // Sets the default values for the selected Shipping and Contact profiles, as |
| 154 | // well as the selected Credit Card. |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 155 | void SetDefaultProfileSelections(); |
| 156 | |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 157 | // Validates the |method_data| and fills |supported_card_networks_|. |
| 158 | void PopulateValidatedMethodData( |
| 159 | const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data); |
| 160 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 161 | // Updates |is_ready_to_pay_| with the current state, by validating that all |
| 162 | // the required information is available and notify observers. |
| 163 | void UpdateIsReadyToPayAndNotifyObservers(); |
| 164 | |
| 165 | // Notifies all observers that selected information has changed. |
| 166 | void NotifyOnSelectedInformationChanged(); |
| 167 | |
| 168 | // Returns whether the selected data satisfies the PaymentDetails requirements |
| 169 | // (payment methods). |
| 170 | bool ArePaymentDetailsSatisfied(); |
| 171 | // Returns whether the selected data satisfies the PaymentOptions requirements |
| 172 | // (contact info, shipping address). |
| 173 | bool ArePaymentOptionsSatisfied(); |
| 174 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 175 | content::WebContents* web_contents_; |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 176 | std::unique_ptr<PaymentRequestDelegate> delegate_; |
| 177 | // |manager_| owns this PaymentRequest. |
| 178 | PaymentRequestWebContentsManager* manager_; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 179 | mojo::Binding<payments::mojom::PaymentRequest> binding_; |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 180 | payments::mojom::PaymentRequestClientPtr client_; |
anthonyvd | dd17271 | 2017-01-06 15:21:24 | [diff] [blame] | 181 | payments::mojom::PaymentDetailsPtr details_; |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 182 | payments::mojom::PaymentOptionsPtr options_; |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 183 | std::unique_ptr<CurrencyFormatter> currency_formatter_; |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 184 | // A set of supported basic card networks. |
| 185 | std::vector<std::string> supported_card_networks_; |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 186 | bool is_ready_to_pay_; |
| 187 | |
| 188 | base::ObserverList<Observer> observers_; |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 189 | |
| 190 | // Profiles may change due to (e.g.) sync events, so profiles are cached after |
| 191 | // loading and owned here. They are populated once only, and ordered by |
| 192 | // frecency. |
| 193 | std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; |
| 194 | std::vector<autofill::AutofillProfile*> shipping_profiles_; |
| 195 | std::vector<autofill::AutofillProfile*> contact_profiles_; |
| 196 | autofill::AutofillProfile* selected_shipping_profile_; |
| 197 | autofill::AutofillProfile* selected_contact_profile_; |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame] | 198 | std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; |
| 199 | std::vector<autofill::CreditCard*> credit_cards_; |
| 200 | autofill::CreditCard* selected_credit_card_; |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 201 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 202 | DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace payments |
| 206 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 207 | #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |