OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "ios/chrome/browser/ui/payments/payment_request_coordinator.h" | 5 #import "ios/chrome/browser/ui/payments/payment_request_coordinator.h" |
6 | 6 |
7 #include <unordered_set> | 7 #include <unordered_set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "components/autofill/core/browser/autofill_client.h" | 13 #include "components/autofill/core/browser/autofill_client.h" |
14 #include "components/autofill/core/browser/autofill_data_util.h" | 14 #include "components/autofill/core/browser/autofill_data_util.h" |
15 #include "components/autofill/core/browser/autofill_manager.h" | 15 #include "components/autofill/core/browser/autofill_manager.h" |
16 #include "components/autofill/core/browser/autofill_profile.h" | 16 #include "components/autofill/core/browser/autofill_profile.h" |
17 #include "components/autofill/core/browser/credit_card.h" | 17 #include "components/autofill/core/browser/credit_card.h" |
18 #include "components/autofill/core/browser/field_types.h" | 18 #include "components/autofill/core/browser/field_types.h" |
19 #include "components/autofill/core/browser/payments/full_card_request.h" | 19 #include "components/autofill/core/browser/payments/full_card_request.h" |
20 #include "components/autofill/core/browser/personal_data_manager.h" | 20 #include "components/autofill/core/browser/personal_data_manager.h" |
21 #include "components/autofill/core/browser/ui/card_unmask_prompt_controller_impl
.h" | 21 #include "components/autofill/core/browser/ui/card_unmask_prompt_controller_impl
.h" |
22 #include "components/payments/core/payment_address.h" | 22 #include "components/payments/core/payment_address.h" |
23 #include "components/payments/core/payment_request_data_util.h" | 23 #include "components/payments/core/payment_request_data_util.h" |
24 #include "components/signin/core/browser/signin_manager.h" | |
25 #include "components/strings/grit/components_strings.h" | 24 #include "components/strings/grit/components_strings.h" |
26 #include "ios/chrome/browser/application_context.h" | 25 #include "ios/chrome/browser/application_context.h" |
27 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 26 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
28 #include "ios/chrome/browser/payments/payment_request.h" | 27 #include "ios/chrome/browser/payments/payment_request.h" |
29 #include "ios/chrome/browser/payments/payment_request_util.h" | 28 #include "ios/chrome/browser/payments/payment_request_util.h" |
30 #include "ios/chrome/browser/signin/signin_manager_factory.h" | |
31 #include "ios/chrome/browser/ui/autofill/card_unmask_prompt_view_bridge.h" | 29 #include "ios/chrome/browser/ui/autofill/card_unmask_prompt_view_bridge.h" |
| 30 #include "ios/chrome/browser/ui/payments/payment_request_mediator.h" |
32 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
33 | 32 |
34 #if !defined(__has_feature) || !__has_feature(objc_arc) | 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
35 #error "This file requires ARC support." | 34 #error "This file requires ARC support." |
36 #endif | 35 #endif |
37 | 36 |
38 namespace { | 37 namespace { |
39 using ::payments::data_util::GetBasicCardResponseFromAutofillCreditCard; | 38 using ::payments::data_util::GetBasicCardResponseFromAutofillCreditCard; |
40 using ::payments::data_util::GetPaymentAddressFromAutofillProfile; | 39 using ::payments::data_util::GetPaymentAddressFromAutofillProfile; |
41 } // namespace | 40 } // namespace |
42 | 41 |
43 // The unmask prompt UI for Payment Request. | 42 // The unmask prompt UI for Payment Request. |
44 class PRCardUnmaskPromptViewBridge | 43 class PRCardUnmaskPromptViewBridge |
45 : public autofill::CardUnmaskPromptViewBridge { | 44 : public autofill::CardUnmaskPromptViewBridge { |
46 public: | 45 public: |
47 explicit PRCardUnmaskPromptViewBridge( | 46 PRCardUnmaskPromptViewBridge(autofill::CardUnmaskPromptController* controller, |
48 autofill::CardUnmaskPromptController* controller, | 47 UIViewController* base_view_controller) |
49 UIViewController* base_view_controller) | |
50 : autofill::CardUnmaskPromptViewBridge(controller), | 48 : autofill::CardUnmaskPromptViewBridge(controller), |
51 base_view_controller_(base_view_controller) {} | 49 base_view_controller_(base_view_controller) {} |
52 | 50 |
53 // autofill::CardUnmaskPromptView: | 51 // autofill::CardUnmaskPromptView: |
54 void Show() override { | 52 void Show() override { |
55 view_controller_.reset( | 53 view_controller_.reset( |
56 [[CardUnmaskPromptViewController alloc] initWithBridge:this]); | 54 [[CardUnmaskPromptViewController alloc] initWithBridge:this]); |
57 [view_controller_ setModalPresentationStyle:UIModalPresentationFormSheet]; | 55 [view_controller_ setModalPresentationStyle:UIModalPresentationFormSheet]; |
58 [view_controller_ | 56 [view_controller_ |
59 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; | 57 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; |
60 [base_view_controller_ presentViewController:view_controller_ | 58 [base_view_controller_ presentViewController:view_controller_ |
61 animated:YES | 59 animated:YES |
62 completion:nil]; | 60 completion:nil]; |
63 }; | 61 }; |
64 | 62 |
65 private: | 63 private: |
66 __weak UIViewController* base_view_controller_; | 64 __weak UIViewController* base_view_controller_; |
67 DISALLOW_COPY_AND_ASSIGN(PRCardUnmaskPromptViewBridge); | 65 DISALLOW_COPY_AND_ASSIGN(PRCardUnmaskPromptViewBridge); |
68 }; | 66 }; |
69 | 67 |
70 // Receives the full credit card details. Also displays the unmask prompt UI. | 68 // Receives the full credit card details. Also displays the unmask prompt UI. |
71 class FullCardRequester | 69 class FullCardRequester |
72 : public autofill::payments::FullCardRequest::ResultDelegate, | 70 : public autofill::payments::FullCardRequest::ResultDelegate, |
73 public autofill::payments::FullCardRequest::UIDelegate, | 71 public autofill::payments::FullCardRequest::UIDelegate, |
74 public base::SupportsWeakPtr<FullCardRequester> { | 72 public base::SupportsWeakPtr<FullCardRequester> { |
75 public: | 73 public: |
76 explicit FullCardRequester(PaymentRequestCoordinator* owner, | 74 FullCardRequester(PaymentRequestCoordinator* owner, |
77 UIViewController* base_view_controller, | 75 UIViewController* base_view_controller, |
78 ios::ChromeBrowserState* browser_state) | 76 ios::ChromeBrowserState* browser_state) |
79 : owner_(owner), | 77 : owner_(owner), |
80 base_view_controller_(base_view_controller), | 78 base_view_controller_(base_view_controller), |
81 unmask_controller_(browser_state->GetPrefs(), | 79 unmask_controller_(browser_state->GetPrefs(), |
82 browser_state->IsOffTheRecord()) {} | 80 browser_state->IsOffTheRecord()) {} |
83 | 81 |
84 void GetFullCard(autofill::CreditCard* card, | 82 void GetFullCard(autofill::CreditCard* card, |
85 autofill::AutofillManager* autofill_manager) { | 83 autofill::AutofillManager* autofill_manager) { |
86 DCHECK(card); | 84 DCHECK(card); |
87 DCHECK(autofill_manager); | 85 DCHECK(autofill_manager); |
88 autofill_manager->GetOrCreateFullCardRequest()->GetFullCard( | 86 autofill_manager->GetOrCreateFullCardRequest()->GetFullCard( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 130 |
133 @implementation PaymentRequestCoordinator { | 131 @implementation PaymentRequestCoordinator { |
134 UINavigationController* _navigationController; | 132 UINavigationController* _navigationController; |
135 PaymentRequestViewController* _viewController; | 133 PaymentRequestViewController* _viewController; |
136 PaymentRequestErrorCoordinator* _errorCoordinator; | 134 PaymentRequestErrorCoordinator* _errorCoordinator; |
137 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator; | 135 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator; |
138 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator; | 136 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator; |
139 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator; | 137 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator; |
140 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator; | 138 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator; |
141 | 139 |
| 140 PaymentRequestMediator* _mediator; |
| 141 |
142 // Receiver of the full credit card details. Also displays the unmask prompt | 142 // Receiver of the full credit card details. Also displays the unmask prompt |
143 // UI. | 143 // UI. |
144 std::unique_ptr<FullCardRequester> _fullCardRequester; | 144 std::unique_ptr<FullCardRequester> _fullCardRequester; |
145 | 145 |
146 // The selected shipping address, pending approval from the page. | 146 // The selected shipping address, pending approval from the page. |
147 autofill::AutofillProfile* _pendingShippingAddress; | 147 autofill::AutofillProfile* _pendingShippingAddress; |
148 } | 148 } |
149 | 149 |
150 @synthesize paymentRequest = _paymentRequest; | 150 @synthesize paymentRequest = _paymentRequest; |
151 @synthesize autofillManager = _autofillManager; | 151 @synthesize autofillManager = _autofillManager; |
152 @synthesize browserState = _browserState; | 152 @synthesize browserState = _browserState; |
153 @synthesize pageFavicon = _pageFavicon; | 153 @synthesize pageFavicon = _pageFavicon; |
154 @synthesize pageTitle = _pageTitle; | 154 @synthesize pageTitle = _pageTitle; |
155 @synthesize pageHost = _pageHost; | 155 @synthesize pageHost = _pageHost; |
156 @synthesize delegate = _delegate; | 156 @synthesize delegate = _delegate; |
157 | 157 |
158 - (void)start { | 158 - (void)start { |
| 159 _mediator = |
| 160 [[PaymentRequestMediator alloc] initWithBrowserState:_browserState]; |
| 161 |
159 _viewController = [[PaymentRequestViewController alloc] | 162 _viewController = [[PaymentRequestViewController alloc] |
160 initWithPaymentRequest:_paymentRequest]; | 163 initWithPaymentRequest:_paymentRequest]; |
161 [_viewController setPageFavicon:_pageFavicon]; | 164 [_viewController setPageFavicon:_pageFavicon]; |
162 [_viewController setPageTitle:_pageTitle]; | 165 [_viewController setPageTitle:_pageTitle]; |
163 [_viewController setPageHost:_pageHost]; | 166 [_viewController setPageHost:_pageHost]; |
164 [_viewController setDelegate:self]; | 167 [_viewController setDelegate:self]; |
165 DCHECK(_browserState); | 168 [_viewController setDataSource:_mediator]; |
166 const SigninManager* signinManager = | |
167 ios::SigninManagerFactory::GetForBrowserStateIfExists(_browserState); | |
168 if (signinManager && signinManager->IsAuthenticated()) { | |
169 NSString* accountName = base::SysUTF8ToNSString( | |
170 signinManager->GetAuthenticatedAccountInfo().email); | |
171 [_viewController setAuthenticatedAccountName:accountName]; | |
172 } | |
173 [_viewController loadModel]; | 169 [_viewController loadModel]; |
174 | 170 |
175 _navigationController = [[UINavigationController alloc] | 171 _navigationController = [[UINavigationController alloc] |
176 initWithRootViewController:_viewController]; | 172 initWithRootViewController:_viewController]; |
177 [_navigationController | 173 [_navigationController |
178 setModalPresentationStyle:UIModalPresentationFormSheet]; | 174 setModalPresentationStyle:UIModalPresentationFormSheet]; |
179 [_navigationController | 175 [_navigationController |
180 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; | 176 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; |
181 [_navigationController setNavigationBarHidden:YES]; | 177 [_navigationController setNavigationBarHidden:YES]; |
182 | 178 |
(...skipping 23 matching lines...) Expand all Loading... |
206 - (void)sendPaymentResponse { | 202 - (void)sendPaymentResponse { |
207 DCHECK(_paymentRequest->selected_credit_card()); | 203 DCHECK(_paymentRequest->selected_credit_card()); |
208 autofill::CreditCard* card = _paymentRequest->selected_credit_card(); | 204 autofill::CreditCard* card = _paymentRequest->selected_credit_card(); |
209 _fullCardRequester = base::MakeUnique<FullCardRequester>( | 205 _fullCardRequester = base::MakeUnique<FullCardRequester>( |
210 self, _navigationController, _browserState); | 206 self, _navigationController, _browserState); |
211 _fullCardRequester->GetFullCard(card, _autofillManager); | 207 _fullCardRequester->GetFullCard(card, _autofillManager); |
212 } | 208 } |
213 | 209 |
214 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card | 210 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card |
215 CVC:(const base::string16&)cvc { | 211 CVC:(const base::string16&)cvc { |
216 web::PaymentResponse paymentResponse; | |
217 | |
218 // If the merchant specified the card network as part of the "basic-card" | |
219 // payment method, return "basic-card" as the method_name. Otherwise, return | |
220 // the name of the network directly. | |
221 std::string issuer_network = | |
222 autofill::data_util::GetPaymentRequestData(card.network()) | |
223 .basic_card_issuer_network; | |
224 paymentResponse.method_name = | |
225 _paymentRequest->basic_card_specified_networks().find(issuer_network) != | |
226 _paymentRequest->basic_card_specified_networks().end() | |
227 ? base::ASCIIToUTF16("basic-card") | |
228 : base::ASCIIToUTF16(issuer_network); | |
229 | |
230 // Get the billing address | |
231 autofill::AutofillProfile billingAddress; | |
232 | |
233 // TODO(crbug.com/714768): Make sure the billing address is set and valid | |
234 // before getting here. Once the bug is addressed, there will be no need to | |
235 // copy the address, *billing_address_ptr can be used to get the basic card | |
236 // response. | |
237 if (!card.billing_address_id().empty()) { | |
238 autofill::AutofillProfile* billingAddressPtr = | |
239 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( | |
240 card.billing_address_id(), _paymentRequest->billing_profiles()); | |
241 if (billingAddressPtr) | |
242 billingAddress = *billingAddressPtr; | |
243 } | |
244 | |
245 paymentResponse.details = GetBasicCardResponseFromAutofillCreditCard( | |
246 card, cvc, billingAddress, | |
247 GetApplicationContext()->GetApplicationLocale()); | |
248 | |
249 if (_paymentRequest->request_shipping()) { | |
250 autofill::AutofillProfile* shippingAddress = | |
251 _paymentRequest->selected_shipping_profile(); | |
252 // TODO(crbug.com/602666): User should get here only if they have selected | |
253 // a shipping address. | |
254 DCHECK(shippingAddress); | |
255 paymentResponse.shipping_address = GetPaymentAddressFromAutofillProfile( | |
256 *shippingAddress, GetApplicationContext()->GetApplicationLocale()); | |
257 | |
258 web::PaymentShippingOption* shippingOption = | |
259 _paymentRequest->selected_shipping_option(); | |
260 DCHECK(shippingOption); | |
261 paymentResponse.shipping_option = shippingOption->id; | |
262 } | |
263 | |
264 if (_paymentRequest->request_payer_name()) { | |
265 autofill::AutofillProfile* contactInfo = | |
266 _paymentRequest->selected_contact_profile(); | |
267 // TODO(crbug.com/602666): User should get here only if they have selected | |
268 // a contact info. | |
269 DCHECK(contactInfo); | |
270 paymentResponse.payer_name = | |
271 contactInfo->GetInfo(autofill::AutofillType(autofill::NAME_FULL), | |
272 GetApplicationContext()->GetApplicationLocale()); | |
273 } | |
274 | |
275 if (_paymentRequest->request_payer_email()) { | |
276 autofill::AutofillProfile* contactInfo = | |
277 _paymentRequest->selected_contact_profile(); | |
278 // TODO(crbug.com/602666): User should get here only if they have selected | |
279 // a contact info. | |
280 DCHECK(contactInfo); | |
281 paymentResponse.payer_email = | |
282 contactInfo->GetRawInfo(autofill::EMAIL_ADDRESS); | |
283 } | |
284 | |
285 if (_paymentRequest->request_payer_phone()) { | |
286 autofill::AutofillProfile* contactInfo = | |
287 _paymentRequest->selected_contact_profile(); | |
288 // TODO(crbug.com/602666): User should get here only if they have selected | |
289 // a contact info. | |
290 DCHECK(contactInfo); | |
291 paymentResponse.payer_phone = | |
292 contactInfo->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER); | |
293 } | |
294 | |
295 _viewController.view.userInteractionEnabled = NO; | 212 _viewController.view.userInteractionEnabled = NO; |
296 [_viewController setPending:YES]; | 213 [_viewController setPending:YES]; |
297 [_viewController loadModel]; | 214 [_viewController loadModel]; |
298 [[_viewController collectionView] reloadData]; | 215 [[_viewController collectionView] reloadData]; |
299 | 216 |
300 [_delegate paymentRequestCoordinator:self | 217 [_delegate paymentRequestCoordinator:self |
301 didConfirmWithPaymentResponse:paymentResponse]; | 218 didCompletePaymentRequest:_paymentRequest |
| 219 card:card |
| 220 cvc:cvc]; |
302 } | 221 } |
303 | 222 |
304 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails { | 223 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails { |
305 BOOL totalValueChanged = | 224 BOOL totalValueChanged = |
306 (_paymentRequest->payment_details().total != paymentDetails.total); | 225 (_paymentRequest->payment_details().total != paymentDetails.total); |
307 _paymentRequest->set_payment_details(paymentDetails); | 226 _paymentRequest->set_payment_details(paymentDetails); |
308 | 227 |
309 if (_paymentRequest->shipping_options().empty()) { | 228 if (_paymentRequest->shipping_options().empty()) { |
310 // Display error in the shipping address/option selection view. | 229 // Display error in the shipping address/option selection view. |
311 if (_shippingAddressSelectionCoordinator) { | 230 if (_shippingAddressSelectionCoordinator) { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 - (void)paymentMethodSelectionCoordinatorDidReturn: | 420 - (void)paymentMethodSelectionCoordinatorDidReturn: |
502 (PaymentMethodSelectionCoordinator*)coordinator { | 421 (PaymentMethodSelectionCoordinator*)coordinator { |
503 // Clear the 'Updated' label on the payment summary item, if there is one. | 422 // Clear the 'Updated' label on the payment summary item, if there is one. |
504 [_viewController updatePaymentSummaryWithTotalValueChanged:NO]; | 423 [_viewController updatePaymentSummaryWithTotalValueChanged:NO]; |
505 | 424 |
506 [_methodSelectionCoordinator stop]; | 425 [_methodSelectionCoordinator stop]; |
507 _methodSelectionCoordinator = nil; | 426 _methodSelectionCoordinator = nil; |
508 } | 427 } |
509 | 428 |
510 @end | 429 @end |
OLD | NEW |