blob: f947000cef0e5be7ebaffa8b991887c8bf937b30 [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"
krb7f6421b2016-11-18 17:46:2116#include "mojo/public/cpp/bindings/binding.h"
17
anthonyvd045303a2017-01-17 22:19:1918namespace autofill {
tmartino68c0a272017-01-19 17:44:0819class AutofillProfile;
anthonyvd045303a2017-01-17 22:19:1920class CreditCard;
rouslan908248c2017-02-27 21:30:2421class PersonalDataManager;
anthonyvd045303a2017-01-17 22:19:1922}
23
krb7f6421b2016-11-18 17:46:2124namespace content {
25class WebContents;
26}
27
28namespace payments {
29
rouslan908248c2017-02-27 21:30:2430class CurrencyFormatter;
mathpf709499d2017-01-09 20:48:3631class PaymentRequestWebContentsManager;
32
33class PaymentRequest : payments::mojom::PaymentRequest {
krb7f6421b2016-11-18 17:46:2134 public:
mathpd4be8de82017-03-01 00:51:4835 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
mathpf709499d2017-01-09 20:48:3645 PaymentRequest(
krb7f6421b2016-11-18 17:46:2146 content::WebContents* web_contents,
mathpf709499d2017-01-09 20:48:3647 std::unique_ptr<PaymentRequestDelegate> delegate,
48 PaymentRequestWebContentsManager* manager,
krb7f6421b2016-11-18 17:46:2149 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request);
mathpf709499d2017-01-09 20:48:3650 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2151
52 // payments::mojom::PaymentRequest "stub"
53 void Init(payments::mojom::PaymentRequestClientPtr client,
mathpc2d07f962017-02-17 18:33:5154 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
krb7f6421b2016-11-18 17:46:2155 payments::mojom::PaymentDetailsPtr details,
56 payments::mojom::PaymentOptionsPtr options) override;
sanjoy.pala1f17e82016-12-15 03:39:1257 void Show() override;
krb7f6421b2016-11-18 17:46:2158 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {}
mathpf4bc50e2017-01-24 05:17:5059 void Abort() override;
krb7f6421b2016-11-18 17:46:2160 void Complete(payments::mojom::PaymentComplete result) override {}
rob.buise3da0af2016-12-02 23:36:5761 void CanMakePayment() override {}
krb7f6421b2016-11-18 17:46:2162
mathpf4bc50e2017-01-24 05:17:5063 // 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();
mathp6758be032017-01-13 04:49:5073
mathpd4be8de82017-03-01 00:51:4874 // Called when the user clicks on the "Pay" button.
75 void Pay();
76
77 void AddObserver(Observer* observer);
78 void RemoveObserver(Observer* observer);
79
mathp6758be032017-01-13 04:49:5080 // 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.bang42764542017-01-24 14:42:5686 const std::string& currency_system,
mathp6758be032017-01-13 04:49:5087 const std::string& locale_name);
88
anthonyvdb0233f22017-03-06 20:34:4789 // 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
tmartino36405622017-01-26 16:23:0397 // 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.
mathpd4be8de82017-03-01 00:51:48101 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 }
tmartino36405622017-01-26 16:23:03110
mathpd4be8de82017-03-01 00:51:48111 // Gets the Autofill Profile representing the shipping address or contact
tmartino36405622017-01-26 16:23:03112 // information currently selected for this PaymentRequest flow. Can return
113 // null.
114 autofill::AutofillProfile* selected_shipping_profile() const {
115 return selected_shipping_profile_;
116 }
tmartino36405622017-01-26 16:23:03117 autofill::AutofillProfile* selected_contact_profile() const {
118 return selected_contact_profile_;
119 }
anthonyvd045303a2017-01-17 22:19:19120 // 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.
anthonyvd75bc4662017-02-22 22:04:43123 autofill::CreditCard* selected_credit_card() { return selected_credit_card_; }
krb7f6421b2016-11-18 17:46:21124
mathpd4be8de82017-03-01 00:51:48125 // 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);
anthonyvd4b014c7e2017-02-27 17:08:28130
mathpd4cfd8f2017-02-09 21:16:53131 autofill::PersonalDataManager* personal_data_manager() {
132 return delegate_->GetPersonalDataManager();
133 }
134
anthonyvd045303a2017-01-17 22:19:19135 payments::mojom::PaymentDetails* details() { return details_.get(); }
mathpc2d07f962017-02-17 18:33:51136 const std::vector<std::string>& supported_card_networks() {
137 return supported_card_networks_;
138 }
anthonyvd3d7f9722016-12-07 18:43:54139 content::WebContents* web_contents() { return web_contents_; }
140
mathpabf66752017-03-01 22:16:44141 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
mathpd4be8de82017-03-01 00:51:48146 bool is_ready_to_pay() { return is_ready_to_pay_; }
147
krb7f6421b2016-11-18 17:46:21148 private:
tmartino36405622017-01-26 16:23:03149 // 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
anthonyvd75bc4662017-02-22 22:04:43153 // Sets the default values for the selected Shipping and Contact profiles, as
154 // well as the selected Credit Card.
tmartino36405622017-01-26 16:23:03155 void SetDefaultProfileSelections();
156
mathpc2d07f962017-02-17 18:33:51157 // Validates the |method_data| and fills |supported_card_networks_|.
158 void PopulateValidatedMethodData(
159 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data);
160
mathpd4be8de82017-03-01 00:51:48161 // 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
krb7f6421b2016-11-18 17:46:21175 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:36176 std::unique_ptr<PaymentRequestDelegate> delegate_;
177 // |manager_| owns this PaymentRequest.
178 PaymentRequestWebContentsManager* manager_;
krb7f6421b2016-11-18 17:46:21179 mojo::Binding<payments::mojom::PaymentRequest> binding_;
anthonyvd3d7f9722016-12-07 18:43:54180 payments::mojom::PaymentRequestClientPtr client_;
anthonyvddd172712017-01-06 15:21:24181 payments::mojom::PaymentDetailsPtr details_;
mathpd4be8de82017-03-01 00:51:48182 payments::mojom::PaymentOptionsPtr options_;
mathp6758be032017-01-13 04:49:50183 std::unique_ptr<CurrencyFormatter> currency_formatter_;
mathpc2d07f962017-02-17 18:33:51184 // A set of supported basic card networks.
185 std::vector<std::string> supported_card_networks_;
mathpd4be8de82017-03-01 00:51:48186 bool is_ready_to_pay_;
187
188 base::ObserverList<Observer> observers_;
tmartino36405622017-01-26 16:23:03189
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_;
anthonyvd75bc4662017-02-22 22:04:43198 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
199 std::vector<autofill::CreditCard*> credit_cards_;
200 autofill::CreditCard* selected_credit_card_;
anthonyvd3d7f9722016-12-07 18:43:54201
mathpf709499d2017-01-09 20:48:36202 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21203};
204
205} // namespace payments
206
rouslan908248c2017-02-27 21:30:24207#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_