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( |
89 *card, autofill::AutofillClient::UNMASK_FOR_PAYMENT_REQUEST, | 87 *card, autofill::AutofillClient::UNMASK_FOR_PAYMENT_REQUEST, |
90 AsWeakPtr(), AsWeakPtr()); | 88 AsWeakPtr(), AsWeakPtr()); |
91 } | 89 } |
92 | 90 |
93 // payments::FullCardRequest::ResultDelegate: | 91 // payments::FullCardRequest::ResultDelegate: |
94 void OnFullCardRequestSucceeded(const autofill::CreditCard& card, | 92 void OnFullCardRequestSucceeded( |
95 const base::string16& cvc) override { | 93 const autofill::CreditCard& card, |
96 [owner_ fullCardRequestDidSucceedWithCard:card CVC:cvc]; | 94 const base::string16& verificationCode) override { |
| 95 [owner_ fullCardRequestDidSucceedWithCard:card |
| 96 verificationCode:verificationCode]; |
97 } | 97 } |
98 | 98 |
99 // payments::FullCardRequest::ResultDelegate: | 99 // payments::FullCardRequest::ResultDelegate: |
100 void OnFullCardRequestFailed() override { | 100 void OnFullCardRequestFailed() override { |
101 // No action is required here. PRCardUnmaskPromptViewBridge manages its own | 101 // No action is required here. PRCardUnmaskPromptViewBridge manages its own |
102 // life cycle. When the prompt is explicitly dismissed via tapping the close | 102 // life cycle. When the prompt is explicitly dismissed via tapping the close |
103 // button (either in presence or absence of an error), the unmask prompt | 103 // button (either in presence or absence of an error), the unmask prompt |
104 // dialog pops itself and the user is back to the Payment Request UI. | 104 // dialog pops itself and the user is back to the Payment Request UI. |
105 } | 105 } |
106 | 106 |
(...skipping 25 matching lines...) Expand all Loading... |
132 | 132 |
133 @implementation PaymentRequestCoordinator { | 133 @implementation PaymentRequestCoordinator { |
134 UINavigationController* _navigationController; | 134 UINavigationController* _navigationController; |
135 PaymentRequestViewController* _viewController; | 135 PaymentRequestViewController* _viewController; |
136 PaymentRequestErrorCoordinator* _errorCoordinator; | 136 PaymentRequestErrorCoordinator* _errorCoordinator; |
137 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator; | 137 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator; |
138 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator; | 138 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator; |
139 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator; | 139 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator; |
140 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator; | 140 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator; |
141 | 141 |
| 142 PaymentRequestMediator* _mediator; |
| 143 |
142 // Receiver of the full credit card details. Also displays the unmask prompt | 144 // Receiver of the full credit card details. Also displays the unmask prompt |
143 // UI. | 145 // UI. |
144 std::unique_ptr<FullCardRequester> _fullCardRequester; | 146 std::unique_ptr<FullCardRequester> _fullCardRequester; |
145 | 147 |
146 // The selected shipping address, pending approval from the page. | 148 // The selected shipping address, pending approval from the page. |
147 autofill::AutofillProfile* _pendingShippingAddress; | 149 autofill::AutofillProfile* _pendingShippingAddress; |
148 } | 150 } |
149 | 151 |
150 @synthesize paymentRequest = _paymentRequest; | 152 @synthesize paymentRequest = _paymentRequest; |
151 @synthesize autofillManager = _autofillManager; | 153 @synthesize autofillManager = _autofillManager; |
152 @synthesize browserState = _browserState; | 154 @synthesize browserState = _browserState; |
153 @synthesize pageFavicon = _pageFavicon; | 155 @synthesize pageFavicon = _pageFavicon; |
154 @synthesize pageTitle = _pageTitle; | 156 @synthesize pageTitle = _pageTitle; |
155 @synthesize pageHost = _pageHost; | 157 @synthesize pageHost = _pageHost; |
156 @synthesize delegate = _delegate; | 158 @synthesize delegate = _delegate; |
157 | 159 |
158 - (void)start { | 160 - (void)start { |
| 161 _mediator = |
| 162 [[PaymentRequestMediator alloc] initWithBrowserState:_browserState]; |
| 163 |
159 _viewController = [[PaymentRequestViewController alloc] | 164 _viewController = [[PaymentRequestViewController alloc] |
160 initWithPaymentRequest:_paymentRequest]; | 165 initWithPaymentRequest:_paymentRequest]; |
161 [_viewController setPageFavicon:_pageFavicon]; | 166 [_viewController setPageFavicon:_pageFavicon]; |
162 [_viewController setPageTitle:_pageTitle]; | 167 [_viewController setPageTitle:_pageTitle]; |
163 [_viewController setPageHost:_pageHost]; | 168 [_viewController setPageHost:_pageHost]; |
164 [_viewController setDelegate:self]; | 169 [_viewController setDelegate:self]; |
165 DCHECK(_browserState); | 170 [_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]; | 171 [_viewController loadModel]; |
174 | 172 |
175 _navigationController = [[UINavigationController alloc] | 173 _navigationController = [[UINavigationController alloc] |
176 initWithRootViewController:_viewController]; | 174 initWithRootViewController:_viewController]; |
177 [_navigationController | 175 [_navigationController |
178 setModalPresentationStyle:UIModalPresentationFormSheet]; | 176 setModalPresentationStyle:UIModalPresentationFormSheet]; |
179 [_navigationController | 177 [_navigationController |
180 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; | 178 setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; |
181 [_navigationController setNavigationBarHidden:YES]; | 179 [_navigationController setNavigationBarHidden:YES]; |
182 | 180 |
(...skipping 22 matching lines...) Expand all Loading... |
205 | 203 |
206 - (void)sendPaymentResponse { | 204 - (void)sendPaymentResponse { |
207 DCHECK(_paymentRequest->selected_credit_card()); | 205 DCHECK(_paymentRequest->selected_credit_card()); |
208 autofill::CreditCard* card = _paymentRequest->selected_credit_card(); | 206 autofill::CreditCard* card = _paymentRequest->selected_credit_card(); |
209 _fullCardRequester = base::MakeUnique<FullCardRequester>( | 207 _fullCardRequester = base::MakeUnique<FullCardRequester>( |
210 self, _navigationController, _browserState); | 208 self, _navigationController, _browserState); |
211 _fullCardRequester->GetFullCard(card, _autofillManager); | 209 _fullCardRequester->GetFullCard(card, _autofillManager); |
212 } | 210 } |
213 | 211 |
214 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card | 212 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card |
215 CVC:(const base::string16&)cvc { | 213 verificationCode: |
216 web::PaymentResponse paymentResponse; | 214 (const base::string16&)verificationCode { |
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; | 215 _viewController.view.userInteractionEnabled = NO; |
296 [_viewController setPending:YES]; | 216 [_viewController setPending:YES]; |
297 [_viewController loadModel]; | 217 [_viewController loadModel]; |
298 [[_viewController collectionView] reloadData]; | 218 [[_viewController collectionView] reloadData]; |
299 | 219 |
300 [_delegate paymentRequestCoordinator:self | 220 [_delegate paymentRequestCoordinator:self |
301 didConfirmWithPaymentResponse:paymentResponse]; | 221 didCompletePaymentRequest:_paymentRequest |
| 222 card:card |
| 223 verificationCode:verificationCode]; |
302 } | 224 } |
303 | 225 |
304 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails { | 226 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails { |
305 BOOL totalValueChanged = | 227 BOOL totalValueChanged = |
306 (_paymentRequest->payment_details().total != paymentDetails.total); | 228 (_paymentRequest->payment_details().total != paymentDetails.total); |
307 _paymentRequest->set_payment_details(paymentDetails); | 229 _paymentRequest->set_payment_details(paymentDetails); |
308 | 230 |
309 if (_paymentRequest->shipping_options().empty()) { | 231 if (_paymentRequest->shipping_options().empty()) { |
310 // Display error in the shipping address/option selection view. | 232 // Display error in the shipping address/option selection view. |
311 if (_shippingAddressSelectionCoordinator) { | 233 if (_shippingAddressSelectionCoordinator) { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 - (void)paymentMethodSelectionCoordinatorDidReturn: | 423 - (void)paymentMethodSelectionCoordinatorDidReturn: |
502 (PaymentMethodSelectionCoordinator*)coordinator { | 424 (PaymentMethodSelectionCoordinator*)coordinator { |
503 // Clear the 'Updated' label on the payment summary item, if there is one. | 425 // Clear the 'Updated' label on the payment summary item, if there is one. |
504 [_viewController updatePaymentSummaryWithTotalValueChanged:NO]; | 426 [_viewController updatePaymentSummaryWithTotalValueChanged:NO]; |
505 | 427 |
506 [_methodSelectionCoordinator stop]; | 428 [_methodSelectionCoordinator stop]; |
507 _methodSelectionCoordinator = nil; | 429 _methodSelectionCoordinator = nil; |
508 } | 430 } |
509 | 431 |
510 @end | 432 @end |
OLD | NEW |