blob: 08799d1275a4fa529ca0925ddabdc514c9b81a82 [file] [log] [blame]
krb7f6421b2016-11-18 17:46:211// 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
rouslan908248c2017-02-27 21:30:245#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
6#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
mathpf709499d2017-01-09 20:48:367
8#include <memory>
tmartino68c0a272017-01-19 17:44:089#include <string>
10#include <vector>
krb7f6421b2016-11-18 17:46:2111
rouslan908248c2017-02-27 21:30:2412#include "base/macros.h"
mathpd4be8de82017-03-01 00:51:4813#include "base/observer_list.h"
rouslan908248c2017-02-27 21:30:2414#include "components/payments/content/payment_request.mojom.h"
15#include "components/payments/content/payment_request_delegate.h"
mathp4b85b582017-03-08 21:07:1616#include "components/payments/core/payment_instrument.h"
krb7f6421b2016-11-18 17:46:2117#include "mojo/public/cpp/bindings/binding.h"
rouslan8fdbfb242017-03-15 01:12:4918#include "mojo/public/cpp/bindings/interface_request.h"
krb7f6421b2016-11-18 17:46:2119
anthonyvd045303a2017-01-17 22:19:1920namespace autofill {
tmartino68c0a272017-01-19 17:44:0821class AutofillProfile;
anthonyvd045303a2017-01-17 22:19:1922class CreditCard;
rouslan908248c2017-02-27 21:30:2423class PersonalDataManager;
anthonyvd045303a2017-01-17 22:19:1924}
25
krb7f6421b2016-11-18 17:46:2126namespace content {
27class WebContents;
28}
29
30namespace payments {
31
rouslan908248c2017-02-27 21:30:2432class CurrencyFormatter;
mathpf709499d2017-01-09 20:48:3633class PaymentRequestWebContentsManager;
34
mathp4b85b582017-03-08 21:07:1635class PaymentRequest : public mojom::PaymentRequest,
36 public PaymentInstrument::Delegate {
krb7f6421b2016-11-18 17:46:2137 public:
mathpd4be8de82017-03-01 00:51:4838 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
rouslan8fdbfb242017-03-15 01:12:4948 PaymentRequest(content::WebContents* web_contents,
49 std::unique_ptr<PaymentRequestDelegate> delegate,
50 PaymentRequestWebContentsManager* manager,
51 mojo::InterfaceRequest<mojom::PaymentRequest> request);
mathpf709499d2017-01-09 20:48:3652 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2153
rouslan8fdbfb242017-03-15 01:12:4954 // 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.pala1f17e82016-12-15 03:39:1259 void Show() override;
rouslan8fdbfb242017-03-15 01:12:4960 void UpdateWith(mojom::PaymentDetailsPtr details) override {}
mathpf4bc50e2017-01-24 05:17:5061 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4962 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1663 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 {}
krb7f6421b2016-11-18 17:46:2170
mathpf4bc50e2017-01-24 05:17:5071 // 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();
mathp6758be032017-01-13 04:49:5081
mathpd4be8de82017-03-01 00:51:4882 // Called when the user clicks on the "Pay" button.
83 void Pay();
84
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
87
mathp6758be032017-01-13 04:49:5088 // 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.bang42764542017-01-24 14:42:5694 const std::string& currency_system,
mathp6758be032017-01-13 04:49:5095 const std::string& locale_name);
96
anthonyvdb0233f22017-03-06 20:34:4797 // 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
tmartino36405622017-01-26 16:23:03105 // 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.
mathpd4be8de82017-03-01 00:51:48109 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 }
tmartino36405622017-01-26 16:23:03118
mathpd4be8de82017-03-01 00:51:48119 // Gets the Autofill Profile representing the shipping address or contact
tmartino36405622017-01-26 16:23:03120 // information currently selected for this PaymentRequest flow. Can return
121 // null.
122 autofill::AutofillProfile* selected_shipping_profile() const {
123 return selected_shipping_profile_;
124 }
tmartino36405622017-01-26 16:23:03125 autofill::AutofillProfile* selected_contact_profile() const {
126 return selected_contact_profile_;
127 }
anthonyvd045303a2017-01-17 22:19:19128 // 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.
anthonyvd75bc4662017-02-22 22:04:43131 autofill::CreditCard* selected_credit_card() { return selected_credit_card_; }
krb7f6421b2016-11-18 17:46:21132
anthonyvd8de8fe962017-03-09 20:55:08133 payments::mojom::PaymentShippingOption* selected_shipping_option() {
134 return selected_shipping_option_;
135 }
136
mathpd4be8de82017-03-01 00:51:48137 // 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);
anthonyvd4b014c7e2017-02-27 17:08:28142
mathpd4cfd8f2017-02-09 21:16:53143 autofill::PersonalDataManager* personal_data_manager() {
144 return delegate_->GetPersonalDataManager();
145 }
tmartino2118ab722017-03-13 16:08:45146 const std::string& locale() { return delegate_->GetApplicationLocale(); }
mathpd4cfd8f2017-02-09 21:16:53147
rouslan8fdbfb242017-03-15 01:12:49148 mojom::PaymentDetails* details() { return details_.get(); }
149 mojom::PaymentOptions* options() { return options_.get(); }
mathpc2d07f962017-02-17 18:33:51150 const std::vector<std::string>& supported_card_networks() {
151 return supported_card_networks_;
152 }
anthonyvd3d7f9722016-12-07 18:43:54153 content::WebContents* web_contents() { return web_contents_; }
154
mathpabf66752017-03-01 22:16:44155 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
mathpd4be8de82017-03-01 00:51:48160 bool is_ready_to_pay() { return is_ready_to_pay_; }
161
krb7f6421b2016-11-18 17:46:21162 private:
tmartino36405622017-01-26 16:23:03163 // 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
anthonyvd75bc4662017-02-22 22:04:43167 // Sets the default values for the selected Shipping and Contact profiles, as
168 // well as the selected Credit Card.
tmartino36405622017-01-26 16:23:03169 void SetDefaultProfileSelections();
170
mathpc2d07f962017-02-17 18:33:51171 // Validates the |method_data| and fills |supported_card_networks_|.
172 void PopulateValidatedMethodData(
rouslan8fdbfb242017-03-15 01:12:49173 const std::vector<mojom::PaymentMethodDataPtr>& method_data);
mathpc2d07f962017-02-17 18:33:51174
mathpd4be8de82017-03-01 00:51:48175 // 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
anthonyvd8de8fe962017-03-09 20:55:08189 // 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
krb7f6421b2016-11-18 17:46:21194 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:36195 std::unique_ptr<PaymentRequestDelegate> delegate_;
196 // |manager_| owns this PaymentRequest.
197 PaymentRequestWebContentsManager* manager_;
rouslan8fdbfb242017-03-15 01:12:49198 mojo::Binding<mojom::PaymentRequest> binding_;
199 mojom::PaymentRequestClientPtr client_;
200 mojom::PaymentDetailsPtr details_;
201 mojom::PaymentOptionsPtr options_;
mathp6758be032017-01-13 04:49:50202 std::unique_ptr<CurrencyFormatter> currency_formatter_;
mathpc2d07f962017-02-17 18:33:51203 // A set of supported basic card networks.
204 std::vector<std::string> supported_card_networks_;
mathpd4be8de82017-03-01 00:51:48205 bool is_ready_to_pay_;
mathp4b85b582017-03-08 21:07:16206 std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
207 mojom::PaymentResponsePtr payment_response_;
mathpd4be8de82017-03-01 00:51:48208
209 base::ObserverList<Observer> observers_;
tmartino36405622017-01-26 16:23:03210
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_;
anthonyvd75bc4662017-02-22 22:04:43219 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
220 std::vector<autofill::CreditCard*> credit_cards_;
221 autofill::CreditCard* selected_credit_card_;
anthonyvd8de8fe962017-03-09 20:55:08222 // 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_;
anthonyvd3d7f9722016-12-07 18:43:54225
mathpf709499d2017-01-09 20:48:36226 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21227};
228
229} // namespace payments
230
rouslan908248c2017-02-27 21:30:24231#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_