Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: ios/chrome/browser/ui/payments/payment_request_mediator.mm

Issue 2924663003: [Payment Request] Moves PaymentRequestViewController to :payments_ui (Closed)
Patch Set: Addressed comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Foundation/Foundation.h>
6
5 #include "ios/chrome/browser/ui/payments/payment_request_mediator.h" 7 #include "ios/chrome/browser/ui/payments/payment_request_mediator.h"
6 8
7 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_data_util.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/credit_card.h"
14 #include "components/autofill/core/browser/field_types.h"
15 #include "components/payments/core/currency_formatter.h"
16 #include "components/payments/core/strings_util.h"
8 #include "components/signin/core/browser/signin_manager.h" 17 #include "components/signin/core/browser/signin_manager.h"
18 #include "components/strings/grit/components_strings.h"
19 #include "ios/chrome/browser/payments/payment_request.h"
20 #include "ios/chrome/browser/payments/payment_request_util.h"
9 #include "ios/chrome/browser/signin/signin_manager_factory.h" 21 #include "ios/chrome/browser/signin/signin_manager_factory.h"
10 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h"
11 @implementation PaymentRequestMediator { 23 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h"
12 ios::ChromeBrowserState* _browserState; 24 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h"
13 } 25 #import "ios/chrome/browser/ui/payments/cells/autofill_profile_item.h"
14 26 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h"
15 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { 27 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h"
28 #import "ios/chrome/browser/ui/payments/cells/price_item.h"
29 #include "ios/chrome/browser/ui/uikit_ui_util.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32
33 #if !defined(__has_feature) || !__has_feature(objc_arc)
34 #error "This file requires ARC support."
35 #endif
36
37 namespace {
38 // String used as the "URL" to take the user to the settings page for card and
39 // address options. Needs to be URL-like; otherwise, the link will not appear
40 // as a link in the UI (see setLabelLinkURL: in CollectionViewFooterCell).
41 const char kSettingsURL[] = "settings://card-and-address";
42
43 using ::payments::GetShippingOptionSectionString;
44 using ::payment_request_util::GetEmailLabelFromAutofillProfile;
45 using ::payment_request_util::GetNameLabelFromAutofillProfile;
46 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile;
47 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile;
48 using ::payment_request_util::GetShippingSectionTitle;
49 } // namespace
50
51 @interface PaymentRequestMediator ()
52
53 @property(nonatomic, assign) ios::ChromeBrowserState* browserState;
54
55 // The PaymentRequest object owning an instance of web::PaymentRequest as
56 // provided by the page invoking the Payment Request API. This is a weak
57 // pointer and should outlive this class.
58 @property(nonatomic, assign) PaymentRequest* paymentRequest;
59
60 @end
61
62 @implementation PaymentRequestMediator
63
64 @synthesize totalValueChanged = _totalValueChanged;
65 @synthesize browserState = _browserState;
66 @synthesize paymentRequest = _paymentRequest;
67
68 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
69 paymentRequest:(PaymentRequest*)paymentRequest {
16 DCHECK(browserState); 70 DCHECK(browserState);
17 self = [super init]; 71 self = [super init];
18 if (self) { 72 if (self) {
19 _browserState = browserState; 73 _browserState = browserState;
74 _paymentRequest = paymentRequest;
20 } 75 }
21 return self; 76 return self;
22 } 77 }
23 78
79 #pragma mark - PaymentRequestViewControllerDataSource
80
81 - (BOOL)canPay {
82 return self.paymentRequest->selected_credit_card() != nullptr &&
83 (self.paymentRequest->selected_shipping_option() != nullptr ||
84 ![self requestShipping]) &&
85 (self.paymentRequest->selected_shipping_profile() != nullptr ||
86 ![self requestShipping]) &&
87 (self.paymentRequest->selected_contact_profile() != nullptr ||
88 ![self requestContactInfo]);
89 }
90
91 - (BOOL)canShip {
92 return !self.paymentRequest->shipping_options().empty() &&
93 self.paymentRequest->selected_shipping_profile() != nullptr;
94 }
95
96 - (BOOL)hasPaymentItems {
97 return !self.paymentRequest->payment_details().display_items.empty();
98 }
99
100 - (BOOL)requestShipping {
101 return self.paymentRequest->request_shipping();
102 }
103
104 - (BOOL)requestContactInfo {
105 return self.paymentRequest->request_payer_name() ||
106 self.paymentRequest->request_payer_email() ||
107 self.paymentRequest->request_payer_phone();
108 }
109
110 - (CollectionViewItem*)paymentSummaryItem {
111 PriceItem* item = [[PriceItem alloc] init];
112 item.item = base::SysUTF16ToNSString(
113 self.paymentRequest->payment_details().total.label);
114 payments::CurrencyFormatter* currencyFormatter =
115 self.paymentRequest->GetOrCreateCurrencyFormatter();
116 item.price = SysUTF16ToNSString(l10n_util::GetStringFUTF16(
117 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SHEET_TOTAL_FORMAT,
118 base::UTF8ToUTF16(currencyFormatter->formatted_currency_code()),
119 currencyFormatter->Format(base::UTF16ToASCII(
120 self.paymentRequest->payment_details().total.amount.value))));
121 item.notification = self.totalValueChanged
122 ? l10n_util::GetNSString(IDS_PAYMENTS_UPDATED_LABEL)
123 : nil;
124 self.totalValueChanged = NO;
125 if ([self hasPaymentItems]) {
126 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
127 }
128 return item;
129 }
130
131 - (CollectionViewItem*)shippingSectionHeaderItem {
132 PaymentsTextItem* item = [[PaymentsTextItem alloc] init];
133 item.text = GetShippingSectionTitle(self.paymentRequest->shipping_type());
134 return item;
135 }
136
137 - (CollectionViewItem*)shippingAddressItem {
138 const autofill::AutofillProfile* profile =
139 self.paymentRequest->selected_shipping_profile();
140 if (profile) {
141 AutofillProfileItem* item = [[AutofillProfileItem alloc] init];
142 item.name = GetNameLabelFromAutofillProfile(*profile);
143 item.address = GetShippingAddressLabelFromAutofillProfile(*profile);
144 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*profile);
145 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
146 return item;
147 }
148
149 CollectionViewDetailItem* item = [[CollectionViewDetailItem alloc] init];
150 item.text = SysUTF16ToNSString(
151 GetShippingAddressSectionString(self.paymentRequest->shipping_type()));
152 if (self.paymentRequest->shipping_profiles().empty()) {
153 item.detailText = [l10n_util::GetNSString(IDS_ADD)
154 uppercaseStringWithLocale:[NSLocale currentLocale]];
155 } else {
156 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
157 }
158 return item;
159 }
160
161 - (CollectionViewItem*)shippingOptionItem {
162 const web::PaymentShippingOption* option =
163 self.paymentRequest->selected_shipping_option();
164 if (option) {
165 PaymentsTextItem* item = [[PaymentsTextItem alloc] init];
166 item.text = base::SysUTF16ToNSString(option->label);
167 payments::CurrencyFormatter* currencyFormatter =
168 self.paymentRequest->GetOrCreateCurrencyFormatter();
169 item.detailText = SysUTF16ToNSString(
170 currencyFormatter->Format(base::UTF16ToASCII(option->amount.value)));
171 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
172 return item;
173 }
174
175 CollectionViewDetailItem* item = [[CollectionViewDetailItem alloc] init];
176 item.text = base::SysUTF16ToNSString(
177 GetShippingOptionSectionString(self.paymentRequest->shipping_type()));
178 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
179 return item;
180 }
181
182 - (CollectionViewItem*)paymentMethodSectionHeaderItem {
183 if (!self.paymentRequest->selected_credit_card())
184 return nil;
185 PaymentsTextItem* item = [[PaymentsTextItem alloc] init];
186 item.text =
187 l10n_util::GetNSString(IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME);
188 return item;
189 }
190
191 - (CollectionViewItem*)paymentMethodItem {
192 const autofill::CreditCard* creditCard =
193 self.paymentRequest->selected_credit_card();
194 if (creditCard) {
195 PaymentMethodItem* item = [[PaymentMethodItem alloc] init];
196 item.methodID =
197 base::SysUTF16ToNSString(creditCard->NetworkAndLastFourDigits());
198 item.methodDetail = base::SysUTF16ToNSString(
199 creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL));
200 int issuerNetworkIconID =
201 autofill::data_util::GetPaymentRequestData(creditCard->network())
202 .icon_resource_id;
203 item.methodTypeIcon = NativeImage(issuerNetworkIconID);
204 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
205 return item;
206 }
207
208 CollectionViewDetailItem* item = [[CollectionViewDetailItem alloc] init];
209 item.text =
210 l10n_util::GetNSString(IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME);
211 if (self.paymentRequest->credit_cards().empty()) {
212 item.detailText = [l10n_util::GetNSString(IDS_ADD)
213 uppercaseStringWithLocale:[NSLocale currentLocale]];
214 } else {
215 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
216 }
217 return item;
218 }
219
220 - (CollectionViewItem*)contactInfoSectionHeaderItem {
221 if (!self.paymentRequest->selected_contact_profile())
222 return nil;
223 PaymentsTextItem* item = [[PaymentsTextItem alloc] init];
224 item.text = l10n_util::GetNSString(IDS_PAYMENTS_CONTACT_DETAILS_LABEL);
225 return item;
226 }
227
228 - (CollectionViewItem*)contactInfoItem {
229 const autofill::AutofillProfile* profile =
230 self.paymentRequest->selected_contact_profile();
231 if (profile) {
232 AutofillProfileItem* item = [[AutofillProfileItem alloc] init];
233 item.name = GetNameLabelFromAutofillProfile(*profile);
234 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*profile);
235 item.email = GetEmailLabelFromAutofillProfile(*profile);
236 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
237 return item;
238 }
239
240 CollectionViewDetailItem* item = [[CollectionViewDetailItem alloc] init];
241 item.text = l10n_util::GetNSString(IDS_PAYMENTS_CONTACT_DETAILS_LABEL);
242 if (self.paymentRequest->contact_profiles().empty()) {
243 item.detailText = [l10n_util::GetNSString(IDS_ADD)
244 uppercaseStringWithLocale:[NSLocale currentLocale]];
245 } else {
246 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator;
247 }
248 return item;
249 }
250
251 - (CollectionViewFooterItem*)footerItem {
252 CollectionViewFooterItem* item = [[CollectionViewFooterItem alloc] init];
253
254 // TODO(crbug.com/602666): Find out if the first payment has completed.
255 BOOL firstPaymentCompleted = YES;
256 if (!firstPaymentCompleted) {
257 item.text = l10n_util::GetNSString(IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS);
258 } else if ([[self authenticatedAccountName] length]) {
259 const base::string16 accountName =
260 base::SysNSStringToUTF16([self authenticatedAccountName]);
261 const std::string formattedString = l10n_util::GetStringFUTF8(
262 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN, accountName);
263 item.text = base::SysUTF8ToNSString(formattedString);
264 } else {
265 item.text = l10n_util::GetNSString(
266 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT);
267 }
268 item.linkURL = GURL(kSettingsURL);
269 return item;
270 }
271
272 #pragma mark - Helper methods
273
274 // Returns the authenticated account name, or nil if user is not authenticated.
24 - (NSString*)authenticatedAccountName { 275 - (NSString*)authenticatedAccountName {
25 const SigninManager* signinManager = 276 const SigninManager* signinManager =
26 ios::SigninManagerFactory::GetForBrowserStateIfExists(_browserState); 277 ios::SigninManagerFactory::GetForBrowserStateIfExists(self.browserState);
27 if (signinManager && signinManager->IsAuthenticated()) { 278 if (signinManager && signinManager->IsAuthenticated()) {
28 return base::SysUTF8ToNSString( 279 return base::SysUTF8ToNSString(
29 signinManager->GetAuthenticatedAccountInfo().email); 280 signinManager->GetAuthenticatedAccountInfo().email);
30 } 281 }
31 return nil; 282 return nil;
32 } 283 }
33 284
34 @end 285 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698