blob: 7630c4289234c0218c0db7713f146d5bdb16ee4f [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"
18
anthonyvd045303a2017-01-17 22:19:1919namespace autofill {
tmartino68c0a272017-01-19 17:44:0820class AutofillProfile;
anthonyvd045303a2017-01-17 22:19:1921class CreditCard;
rouslan908248c2017-02-27 21:30:2422class PersonalDataManager;
anthonyvd045303a2017-01-17 22:19:1923}
24
krb7f6421b2016-11-18 17:46:2125namespace content {
26class WebContents;
27}
28
29namespace payments {
30
rouslan908248c2017-02-27 21:30:2431class CurrencyFormatter;
mathpf709499d2017-01-09 20:48:3632class PaymentRequestWebContentsManager;
33
mathp4b85b582017-03-08 21:07:1634class PaymentRequest : public mojom::PaymentRequest,
35 public PaymentInstrument::Delegate {
krb7f6421b2016-11-18 17:46:2136 public:
mathpd4be8de82017-03-01 00:51:4837 class Observer {
38 public:
39 // Called when the information (payment method, address/contact info,
40 // shipping option) changes.
41 virtual void OnSelectedInformationChanged() = 0;
42
43 protected:
44 virtual ~Observer() {}
45 };
46
mathpf709499d2017-01-09 20:48:3647 PaymentRequest(
krb7f6421b2016-11-18 17:46:2148 content::WebContents* web_contents,
mathpf709499d2017-01-09 20:48:3649 std::unique_ptr<PaymentRequestDelegate> delegate,
50 PaymentRequestWebContentsManager* manager,
krb7f6421b2016-11-18 17:46:2151 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request);
mathpf709499d2017-01-09 20:48:3652 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2153
54 // payments::mojom::PaymentRequest "stub"
55 void Init(payments::mojom::PaymentRequestClientPtr client,
mathpc2d07f962017-02-17 18:33:5156 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
krb7f6421b2016-11-18 17:46:2157 payments::mojom::PaymentDetailsPtr details,
58 payments::mojom::PaymentOptionsPtr options) override;
sanjoy.pala1f17e82016-12-15 03:39:1259 void Show() override;
krb7f6421b2016-11-18 17:46:2160 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {}
mathpf4bc50e2017-01-24 05:17:5061 void Abort() override;
mathp4b85b582017-03-08 21:07:1662 void Complete(payments::mojom::PaymentComplete result) override;
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 {}
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
mathpd4be8de82017-03-01 00:51:48133 // Sets the |profile| to be the selected one and will update state and notify
134 // observers.
135 void SetSelectedShippingProfile(autofill::AutofillProfile* profile);
136 void SetSelectedContactProfile(autofill::AutofillProfile* profile);
137 void SetSelectedCreditCard(autofill::CreditCard* card);
anthonyvd4b014c7e2017-02-27 17:08:28138
mathpd4cfd8f2017-02-09 21:16:53139 autofill::PersonalDataManager* personal_data_manager() {
140 return delegate_->GetPersonalDataManager();
141 }
142
anthonyvd045303a2017-01-17 22:19:19143 payments::mojom::PaymentDetails* details() { return details_.get(); }
mathpc2d07f962017-02-17 18:33:51144 const std::vector<std::string>& supported_card_networks() {
145 return supported_card_networks_;
146 }
anthonyvd3d7f9722016-12-07 18:43:54147 content::WebContents* web_contents() { return web_contents_; }
148
mathpabf66752017-03-01 22:16:44149 bool request_shipping() const { return options_->request_shipping; }
150 bool request_payer_name() const { return options_->request_payer_name; }
151 bool request_payer_phone() const { return options_->request_payer_phone; }
152 bool request_payer_email() const { return options_->request_payer_email; }
153
mathpd4be8de82017-03-01 00:51:48154 bool is_ready_to_pay() { return is_ready_to_pay_; }
155
krb7f6421b2016-11-18 17:46:21156 private:
tmartino36405622017-01-26 16:23:03157 // Fetches the Autofill Profiles for this user from the PersonalDataManager,
158 // and stores copies of them, owned by this Request, in profile_cache_.
159 void PopulateProfileCache();
160
anthonyvd75bc4662017-02-22 22:04:43161 // Sets the default values for the selected Shipping and Contact profiles, as
162 // well as the selected Credit Card.
tmartino36405622017-01-26 16:23:03163 void SetDefaultProfileSelections();
164
mathpc2d07f962017-02-17 18:33:51165 // Validates the |method_data| and fills |supported_card_networks_|.
166 void PopulateValidatedMethodData(
167 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data);
168
mathpd4be8de82017-03-01 00:51:48169 // Updates |is_ready_to_pay_| with the current state, by validating that all
170 // the required information is available and notify observers.
171 void UpdateIsReadyToPayAndNotifyObservers();
172
173 // Notifies all observers that selected information has changed.
174 void NotifyOnSelectedInformationChanged();
175
176 // Returns whether the selected data satisfies the PaymentDetails requirements
177 // (payment methods).
178 bool ArePaymentDetailsSatisfied();
179 // Returns whether the selected data satisfies the PaymentOptions requirements
180 // (contact info, shipping address).
181 bool ArePaymentOptionsSatisfied();
182
krb7f6421b2016-11-18 17:46:21183 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:36184 std::unique_ptr<PaymentRequestDelegate> delegate_;
185 // |manager_| owns this PaymentRequest.
186 PaymentRequestWebContentsManager* manager_;
krb7f6421b2016-11-18 17:46:21187 mojo::Binding<payments::mojom::PaymentRequest> binding_;
anthonyvd3d7f9722016-12-07 18:43:54188 payments::mojom::PaymentRequestClientPtr client_;
anthonyvddd172712017-01-06 15:21:24189 payments::mojom::PaymentDetailsPtr details_;
mathpd4be8de82017-03-01 00:51:48190 payments::mojom::PaymentOptionsPtr options_;
mathp6758be032017-01-13 04:49:50191 std::unique_ptr<CurrencyFormatter> currency_formatter_;
mathpc2d07f962017-02-17 18:33:51192 // A set of supported basic card networks.
193 std::vector<std::string> supported_card_networks_;
mathpd4be8de82017-03-01 00:51:48194 bool is_ready_to_pay_;
mathp4b85b582017-03-08 21:07:16195 std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
196 mojom::PaymentResponsePtr payment_response_;
mathpd4be8de82017-03-01 00:51:48197
198 base::ObserverList<Observer> observers_;
tmartino36405622017-01-26 16:23:03199
200 // Profiles may change due to (e.g.) sync events, so profiles are cached after
201 // loading and owned here. They are populated once only, and ordered by
202 // frecency.
203 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_;
204 std::vector<autofill::AutofillProfile*> shipping_profiles_;
205 std::vector<autofill::AutofillProfile*> contact_profiles_;
206 autofill::AutofillProfile* selected_shipping_profile_;
207 autofill::AutofillProfile* selected_contact_profile_;
anthonyvd75bc4662017-02-22 22:04:43208 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
209 std::vector<autofill::CreditCard*> credit_cards_;
210 autofill::CreditCard* selected_credit_card_;
anthonyvd3d7f9722016-12-07 18:43:54211
mathpf709499d2017-01-09 20:48:36212 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21213};
214
215} // namespace payments
216
rouslan908248c2017-02-27 21:30:24217#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_