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