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

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

Issue 2844783002: [Payment Request] Adds address normalization on iOS. (Closed)
Patch Set: Adds missing dependency. Created 3 years, 7 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
« no previous file with comments | « ios/chrome/browser/ui/payments/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/address_normalizer_impl.h"
22 #include "components/payments/core/payment_address.h" 23 #include "components/payments/core/payment_address.h"
23 #include "components/payments/core/payment_request_data_util.h" 24 #include "components/payments/core/payment_request_data_util.h"
24 #include "components/signin/core/browser/signin_manager.h" 25 #include "components/signin/core/browser/signin_manager.h"
25 #include "components/strings/grit/components_strings.h" 26 #include "components/strings/grit/components_strings.h"
26 #include "ios/chrome/browser/application_context.h" 27 #include "ios/chrome/browser/application_context.h"
28 #include "ios/chrome/browser/autofill/validation_rules_storage_factory.h"
27 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 29 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
28 #include "ios/chrome/browser/payments/payment_request.h" 30 #include "ios/chrome/browser/payments/payment_request.h"
29 #include "ios/chrome/browser/payments/payment_request_util.h" 31 #include "ios/chrome/browser/payments/payment_request_util.h"
30 #include "ios/chrome/browser/signin/signin_manager_factory.h" 32 #include "ios/chrome/browser/signin/signin_manager_factory.h"
31 #include "ios/chrome/browser/ui/autofill/card_unmask_prompt_view_bridge.h" 33 #include "ios/chrome/browser/ui/autofill/card_unmask_prompt_view_bridge.h"
34 #include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
35 #include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
32 #include "ui/base/l10n/l10n_util.h" 36 #include "ui/base/l10n/l10n_util.h"
33 37
34 #if !defined(__has_feature) || !__has_feature(objc_arc) 38 #if !defined(__has_feature) || !__has_feature(objc_arc)
35 #error "This file requires ARC support." 39 #error "This file requires ARC support."
36 #endif 40 #endif
37 41
38 namespace { 42 namespace {
39 using ::payments::data_util::GetBasicCardResponseFromAutofillCreditCard; 43 using ::payments::data_util::GetBasicCardResponseFromAutofillCreditCard;
40 using ::payments::data_util::GetPaymentAddressFromAutofillProfile; 44 using ::payments::data_util::GetPaymentAddressFromAutofillProfile;
41 } // namespace 45
46 constexpr int kAddressNormalizationTimeoutSeconds = 5;
47
48 // Receives notifications from the AddressNormalizer, and forwards the details
49 // to a callback. This is needed as there is currently no way to know, when
50 // the |payments::AddressNormalizer::Delegate| methods are called, which address
51 // has been normalized (shipping or contact). The callback block can keep track
52 // of which address this delegate was created for.
Moe 2017/04/28 09:04:44 nit: please update the comment to reflect "... the
macourteau 2017/05/01 15:56:22 Done.
53 class AddressNormalizerDelegate : public payments::AddressNormalizer::Delegate {
54 public:
55 // Block to be called with the profile information, once normalized.
56 typedef void (^Callback)(const autofill::AutofillProfile&);
57
58 AddressNormalizerDelegate() {}
59 ~AddressNormalizerDelegate() override {}
60
61 void SetCallback(Callback callback) { callback_ = callback; }
62
63 // payments::AddressNormalizer::Delegate:
64 void OnAddressNormalized(
65 const autofill::AutofillProfile& normalizedProfile) override {
66 DCHECK(callback_);
67 callback_(normalizedProfile);
68 }
69
70 // payments::AddressNormalizer::Delegate:
71 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override {
72 // Since the phone number is formatted in either case, this profile should
73 // be used.
74 DCHECK(callback_);
75 callback_(profile);
76 }
77
78 private:
79 Callback callback_ = nullptr;
80
81 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerDelegate);
82 };
42 83
43 // The unmask prompt UI for Payment Request. 84 // The unmask prompt UI for Payment Request.
44 class PRCardUnmaskPromptViewBridge 85 class PRCardUnmaskPromptViewBridge
45 : public autofill::CardUnmaskPromptViewBridge { 86 : public autofill::CardUnmaskPromptViewBridge {
46 public: 87 public:
47 explicit PRCardUnmaskPromptViewBridge( 88 PRCardUnmaskPromptViewBridge(autofill::CardUnmaskPromptController* controller,
48 autofill::CardUnmaskPromptController* controller, 89 UIViewController* base_view_controller)
49 UIViewController* base_view_controller)
50 : autofill::CardUnmaskPromptViewBridge(controller), 90 : autofill::CardUnmaskPromptViewBridge(controller),
51 base_view_controller_(base_view_controller) {} 91 base_view_controller_(base_view_controller) {}
52 92
53 // autofill::CardUnmaskPromptView: 93 // autofill::CardUnmaskPromptView:
54 void Show() override { 94 void Show() override {
55 view_controller_.reset( 95 view_controller_.reset(
56 [[CardUnmaskPromptViewController alloc] initWithBridge:this]); 96 [[CardUnmaskPromptViewController alloc] initWithBridge:this]);
57 [base_view_controller_ presentViewController:view_controller_ 97 [base_view_controller_ presentViewController:view_controller_
58 animated:YES 98 animated:YES
59 completion:nil]; 99 completion:nil];
60 }; 100 };
61 101
62 private: 102 private:
63 __weak UIViewController* base_view_controller_; 103 __weak UIViewController* base_view_controller_;
64 DISALLOW_COPY_AND_ASSIGN(PRCardUnmaskPromptViewBridge); 104 DISALLOW_COPY_AND_ASSIGN(PRCardUnmaskPromptViewBridge);
65 }; 105 };
66 106
67 // Receives the full credit card details. Also displays the unmask prompt UI. 107 // Receives the full credit card details. Also displays the unmask prompt UI.
68 class FullCardRequester 108 class FullCardRequester
69 : public autofill::payments::FullCardRequest::ResultDelegate, 109 : public autofill::payments::FullCardRequest::ResultDelegate,
70 public autofill::payments::FullCardRequest::UIDelegate, 110 public autofill::payments::FullCardRequest::UIDelegate,
71 public base::SupportsWeakPtr<FullCardRequester> { 111 public base::SupportsWeakPtr<FullCardRequester> {
72 public: 112 public:
73 explicit FullCardRequester(PaymentRequestCoordinator* owner, 113 FullCardRequester(PaymentRequestCoordinator* owner,
74 UIViewController* base_view_controller, 114 UIViewController* base_view_controller,
75 ios::ChromeBrowserState* browser_state) 115 ios::ChromeBrowserState* browser_state)
76 : owner_(owner), 116 : owner_(owner),
77 base_view_controller_(base_view_controller), 117 base_view_controller_(base_view_controller),
78 unmask_controller_(browser_state->GetPrefs(), 118 unmask_controller_(browser_state->GetPrefs(),
79 browser_state->IsOffTheRecord()) {} 119 browser_state->IsOffTheRecord()) {}
80 120
81 void GetFullCard(autofill::CreditCard* card, 121 void GetFullCard(autofill::CreditCard* card,
82 autofill::AutofillManager* autofill_manager) { 122 autofill::AutofillManager* autofill_manager) {
83 DCHECK(card); 123 DCHECK(card);
84 DCHECK(autofill_manager); 124 DCHECK(autofill_manager);
85 autofill_manager->GetOrCreateFullCardRequest()->GetFullCard( 125 autofill_manager->GetOrCreateFullCardRequest()->GetFullCard(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 160 }
121 161
122 private: 162 private:
123 __weak PaymentRequestCoordinator* owner_; 163 __weak PaymentRequestCoordinator* owner_;
124 __weak UIViewController* base_view_controller_; 164 __weak UIViewController* base_view_controller_;
125 autofill::CardUnmaskPromptControllerImpl unmask_controller_; 165 autofill::CardUnmaskPromptControllerImpl unmask_controller_;
126 166
127 DISALLOW_COPY_AND_ASSIGN(FullCardRequester); 167 DISALLOW_COPY_AND_ASSIGN(FullCardRequester);
128 }; 168 };
129 169
170 } // namespace
171
172 struct FullCard {
Moe 2017/04/28 09:04:44 nit: please move to anonymous namespace.
macourteau 2017/05/01 15:56:22 Done.
173 autofill::CreditCard creditCard;
174 base::string16 cvc;
175 autofill::AutofillProfile billingAddress;
176 };
177
130 @implementation PaymentRequestCoordinator { 178 @implementation PaymentRequestCoordinator {
131 UINavigationController* _navigationController; 179 UINavigationController* _navigationController;
132 PaymentRequestViewController* _viewController; 180 PaymentRequestViewController* _viewController;
133 PaymentRequestErrorCoordinator* _errorCoordinator; 181 PaymentRequestErrorCoordinator* _errorCoordinator;
134 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator; 182 PaymentItemsDisplayCoordinator* _itemsDisplayCoordinator;
135 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator; 183 ShippingAddressSelectionCoordinator* _shippingAddressSelectionCoordinator;
136 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator; 184 ShippingOptionSelectionCoordinator* _shippingOptionSelectionCoordinator;
137 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator; 185 PaymentMethodSelectionCoordinator* _methodSelectionCoordinator;
138 186
139 // Receiver of the full credit card details. Also displays the unmask prompt 187 // Receiver of the full credit card details. Also displays the unmask prompt
140 // UI. 188 // UI.
141 std::unique_ptr<FullCardRequester> _fullCardRequester; 189 std::unique_ptr<FullCardRequester> _fullCardRequester;
142 190
143 // The selected shipping address, pending approval from the page. 191 // The selected shipping address, pending approval from the page.
144 autofill::AutofillProfile* _pendingShippingAddress; 192 autofill::AutofillProfile* _pendingShippingAddress;
193
194 // The address normalizer, to normalize contact & shipping addresses. Also,
195 // delegates to handle callbacks for normalization of each address.
196 std::unique_ptr<payments::AddressNormalizer> _addressNormalizer;
197 AddressNormalizerDelegate _contactAddressNormalizerDelegate;
198 AddressNormalizerDelegate _shippingAddressNormalizerDelegate;
199 AddressNormalizerDelegate _billingAddressNormalizerDelegate;
200
201 // Member variables to keep track of which operations are still pending.
202 BOOL _contactAddressNormalizationPending;
203 BOOL _shippingAddressNormalizationPending;
204 BOOL _fullCardRequestPending;
205 BOOL _billingAddressNormalizationPending;
206
207 // Storage for the full card.
208 FullCard _fullCard;
145 } 209 }
146 210
147 @synthesize paymentRequest = _paymentRequest; 211 @synthesize paymentRequest = _paymentRequest;
148 @synthesize autofillManager = _autofillManager; 212 @synthesize autofillManager = _autofillManager;
149 @synthesize browserState = _browserState; 213 @synthesize browserState = _browserState;
150 @synthesize pageFavicon = _pageFavicon; 214 @synthesize pageFavicon = _pageFavicon;
151 @synthesize pageTitle = _pageTitle; 215 @synthesize pageTitle = _pageTitle;
152 @synthesize pageHost = _pageHost; 216 @synthesize pageHost = _pageHost;
153 @synthesize delegate = _delegate; 217 @synthesize delegate = _delegate;
154 218
219 - (void)initAddressNormalizer {
Moe 2017/04/28 09:04:44 nit: s/initAddressNormalizer/startAddressNormalize
macourteau 2017/05/01 15:56:22 Done.
220 autofill::PersonalDataManager* personalDataManager =
221 _paymentRequest->GetPersonalDataManager();
222
223 std::unique_ptr<i18n::addressinput::Source> addressNormalizerSource =
224 base::MakeUnique<autofill::ChromeMetadataSource>(
225 I18N_ADDRESS_VALIDATION_DATA_URL,
226 personalDataManager->GetURLRequestContextGetter());
227
228 std::unique_ptr<i18n::addressinput::Storage> addressNormalizerStorage =
229 autofill::ValidationRulesStorageFactory::CreateStorage();
230
231 _addressNormalizer.reset(new payments::AddressNormalizerImpl(
232 std::move(addressNormalizerSource), std::move(addressNormalizerStorage)));
233
234 // Kickoff the process of loading the rules (which is asynchronous) for each
235 // profile's country, to get faster address normalization later.
236 for (const autofill::AutofillProfile* profile :
237 personalDataManager->GetProfilesToSuggest()) {
238 std::string countryCode =
239 base::UTF16ToUTF8(profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
240 if (autofill::data_util::IsValidCountryCode(countryCode)) {
241 _addressNormalizer->LoadRulesForRegion(countryCode);
242 }
243 }
244 }
245
155 - (void)start { 246 - (void)start {
156 _viewController = [[PaymentRequestViewController alloc] 247 _viewController = [[PaymentRequestViewController alloc]
157 initWithPaymentRequest:_paymentRequest]; 248 initWithPaymentRequest:_paymentRequest];
158 [_viewController setPageFavicon:_pageFavicon]; 249 [_viewController setPageFavicon:_pageFavicon];
159 [_viewController setPageTitle:_pageTitle]; 250 [_viewController setPageTitle:_pageTitle];
160 [_viewController setPageHost:_pageHost]; 251 [_viewController setPageHost:_pageHost];
161 [_viewController setDelegate:self]; 252 [_viewController setDelegate:self];
162 DCHECK(_browserState); 253 DCHECK(_browserState);
163 const SigninManager* signinManager = 254 const SigninManager* signinManager =
164 ios::SigninManagerFactory::GetForBrowserStateIfExists(_browserState); 255 ios::SigninManagerFactory::GetForBrowserStateIfExists(_browserState);
165 if (signinManager && signinManager->IsAuthenticated()) { 256 if (signinManager && signinManager->IsAuthenticated()) {
166 NSString* accountName = base::SysUTF8ToNSString( 257 NSString* accountName = base::SysUTF8ToNSString(
167 signinManager->GetAuthenticatedAccountInfo().email); 258 signinManager->GetAuthenticatedAccountInfo().email);
168 [_viewController setAuthenticatedAccountName:accountName]; 259 [_viewController setAuthenticatedAccountName:accountName];
169 } 260 }
170 [_viewController loadModel]; 261 [_viewController loadModel];
171 262
172 _navigationController = [[UINavigationController alloc] 263 _navigationController = [[UINavigationController alloc]
173 initWithRootViewController:_viewController]; 264 initWithRootViewController:_viewController];
174 [_navigationController setNavigationBarHidden:YES]; 265 [_navigationController setNavigationBarHidden:YES];
175 266
176 [[self baseViewController] presentViewController:_navigationController 267 [[self baseViewController] presentViewController:_navigationController
177 animated:YES 268 animated:YES
178 completion:nil]; 269 completion:nil];
270
271 [self initAddressNormalizer];
179 } 272 }
180 273
181 - (void)stop { 274 - (void)stop {
182 [[_navigationController presentingViewController] 275 [[_navigationController presentingViewController]
183 dismissViewControllerAnimated:YES 276 dismissViewControllerAnimated:YES
184 completion:nil]; 277 completion:nil];
185 [_itemsDisplayCoordinator stop]; 278 [_itemsDisplayCoordinator stop];
186 _itemsDisplayCoordinator = nil; 279 _itemsDisplayCoordinator = nil;
187 [_shippingAddressSelectionCoordinator stop]; 280 [_shippingAddressSelectionCoordinator stop];
188 _shippingAddressSelectionCoordinator = nil; 281 _shippingAddressSelectionCoordinator = nil;
189 [_shippingOptionSelectionCoordinator stop]; 282 [_shippingOptionSelectionCoordinator stop];
190 _shippingOptionSelectionCoordinator = nil; 283 _shippingOptionSelectionCoordinator = nil;
191 [_methodSelectionCoordinator stop]; 284 [_methodSelectionCoordinator stop];
192 _methodSelectionCoordinator = nil; 285 _methodSelectionCoordinator = nil;
193 [_errorCoordinator stop]; 286 [_errorCoordinator stop];
194 _errorCoordinator = nil; 287 _errorCoordinator = nil;
195 _viewController = nil; 288 _viewController = nil;
196 _navigationController = nil; 289 _navigationController = nil;
197 } 290 }
198 291
292 - (void)normalizeProfile:(autofill::AutofillProfile*)profileToNormalize
293 delegate:(AddressNormalizerDelegate*)delegate
294 flag:(BOOL*)normalizationRequestPendingFlag {
Moe 2017/04/28 09:04:44 nit: this function has side effects. Please declar
macourteau 2017/05/01 15:56:22 Done.
295 DCHECK(normalizationRequestPendingFlag);
296
297 __weak PaymentRequestCoordinator* weakSelf = self;
298
299 delegate->SetCallback(^(const autofill::AutofillProfile& normalizedProfile) {
300 // Check that PaymentRequestCoordinator still exists as |profileToNormalize|
301 // and |normalizationRequestPendingFlag| are member variables of that
302 // object.
303 PaymentRequestCoordinator* strongSelf = weakSelf;
304 if (!strongSelf)
305 return;
Moe 2017/04/28 09:04:44 I think you should be able to say "if (!weakSelf)
macourteau 2017/05/01 15:56:22 Done.
306
307 *profileToNormalize = normalizedProfile;
308 *normalizationRequestPendingFlag = NO;
309 [strongSelf sendPaymentResponseIfAllAsyncCallsCompleted];
310 });
311
312 std::string country_code = base::UTF16ToUTF8(
313 profileToNormalize->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
314 if (!autofill::data_util::IsValidCountryCode(country_code)) {
315 country_code = base::SysNSStringToUTF8(
316 [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]);
317 DCHECK(autofill::data_util::IsValidCountryCode(country_code));
318 }
319 _addressNormalizer->StartAddressNormalization(
320 *profileToNormalize, country_code, kAddressNormalizationTimeoutSeconds,
321 delegate);
322 }
323
199 - (void)sendPaymentResponse { 324 - (void)sendPaymentResponse {
Moe 2017/04/28 09:04:44 nit: s/sendPaymentResponse/requestFullCardAndNorma
macourteau 2017/05/01 15:56:22 Done.
325 // Set these before starting any potentially asynchronous calls, to ensure we
326 // wait for completion of all operations before proceeding.
327 _contactAddressNormalizationPending = YES;
328 _shippingAddressNormalizationPending = _paymentRequest->request_shipping();
329 _fullCardRequestPending = YES;
330
200 DCHECK(_paymentRequest->selected_credit_card()); 331 DCHECK(_paymentRequest->selected_credit_card());
201 autofill::CreditCard* card = _paymentRequest->selected_credit_card(); 332 autofill::CreditCard* card = _paymentRequest->selected_credit_card();
202 _fullCardRequester = base::MakeUnique<FullCardRequester>( 333 _fullCardRequester = base::MakeUnique<FullCardRequester>(
203 self, _navigationController, _browserState); 334 self, _navigationController, _browserState);
204 _fullCardRequester->GetFullCard(card, _autofillManager); 335 _fullCardRequester->GetFullCard(card, _autofillManager);
336
337 if (_paymentRequest->request_shipping()) {
338 [self normalizeProfile:_paymentRequest->selected_shipping_profile()
339 delegate:&_shippingAddressNormalizerDelegate
340 flag:&_shippingAddressNormalizationPending];
341 }
342
343 [self normalizeProfile:_paymentRequest->selected_contact_profile()
344 delegate:&_contactAddressNormalizerDelegate
345 flag:&_contactAddressNormalizationPending];
205 } 346 }
206 347
207 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card 348 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card
208 CVC:(const base::string16&)cvc { 349 CVC:(const base::string16&)cvc {
209 web::PaymentResponse paymentResponse;
210
211 // If the merchant specified the card network as part of the "basic-card"
212 // payment method, return "basic-card" as the method_name. Otherwise, return
213 // the name of the network directly.
214 std::string basic_card_type =
215 autofill::data_util::GetPaymentRequestData(card.type())
216 .basic_card_payment_type;
217 paymentResponse.method_name =
218 _paymentRequest->basic_card_specified_networks().find(basic_card_type) !=
219 _paymentRequest->basic_card_specified_networks().end()
220 ? base::ASCIIToUTF16("basic-card")
221 : base::ASCIIToUTF16(basic_card_type);
222
223 // Get the billing address
224 autofill::AutofillProfile billingAddress;
225
226 // TODO(crbug.com/714768): Make sure the billing address is set and valid 350 // TODO(crbug.com/714768): Make sure the billing address is set and valid
227 // before getting here. Once the bug is addressed, there will be no need to 351 // before getting here. Once the bug is addressed, there will be no need to
228 // copy the address, *billing_address_ptr can be used to get the basic card 352 // copy the address, *billing_address_ptr can be used to get the basic card
229 // response. 353 // response.
230 if (!card.billing_address_id().empty()) { 354 if (!card.billing_address_id().empty()) {
231 autofill::AutofillProfile* billingAddressPtr = 355 autofill::AutofillProfile* billingAddress =
232 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( 356 autofill::PersonalDataManager::GetProfileFromProfilesByGUID(
233 card.billing_address_id(), _paymentRequest->billing_profiles()); 357 card.billing_address_id(), _paymentRequest->billing_profiles());
234 if (billingAddressPtr) 358 if (billingAddress) {
235 billingAddress = *billingAddressPtr; 359 _fullCard.billingAddress = *billingAddress;
360 _billingAddressNormalizationPending = YES;
361
362 [self normalizeProfile:&_fullCard.billingAddress
363 delegate:&_billingAddressNormalizerDelegate
364 flag:&_billingAddressNormalizationPending];
365 }
236 } 366 }
237 367
238 paymentResponse.details = GetBasicCardResponseFromAutofillCreditCard( 368 _fullCard.creditCard = card;
239 card, cvc, billingAddress, 369 _fullCard.cvc = cvc;
240 GetApplicationContext()->GetApplicationLocale()); 370 _fullCardRequestPending = NO;
241
242 if (_paymentRequest->request_shipping()) {
243 autofill::AutofillProfile* shippingAddress =
244 _paymentRequest->selected_shipping_profile();
245 // TODO(crbug.com/602666): User should get here only if they have selected
246 // a shipping address.
247 DCHECK(shippingAddress);
248 paymentResponse.shipping_address = GetPaymentAddressFromAutofillProfile(
249 *shippingAddress, GetApplicationContext()->GetApplicationLocale());
250
251 web::PaymentShippingOption* shippingOption =
252 _paymentRequest->selected_shipping_option();
253 DCHECK(shippingOption);
254 paymentResponse.shipping_option = shippingOption->id;
255 }
256
257 if (_paymentRequest->request_payer_name()) {
258 autofill::AutofillProfile* contactInfo =
259 _paymentRequest->selected_contact_profile();
260 // TODO(crbug.com/602666): User should get here only if they have selected
261 // a contact info.
262 DCHECK(contactInfo);
263 paymentResponse.payer_name =
264 contactInfo->GetInfo(autofill::AutofillType(autofill::NAME_FULL),
265 GetApplicationContext()->GetApplicationLocale());
266 }
267
268 if (_paymentRequest->request_payer_email()) {
269 autofill::AutofillProfile* contactInfo =
270 _paymentRequest->selected_contact_profile();
271 // TODO(crbug.com/602666): User should get here only if they have selected
272 // a contact info.
273 DCHECK(contactInfo);
274 paymentResponse.payer_email =
275 contactInfo->GetRawInfo(autofill::EMAIL_ADDRESS);
276 }
277
278 if (_paymentRequest->request_payer_phone()) {
279 autofill::AutofillProfile* contactInfo =
280 _paymentRequest->selected_contact_profile();
281 // TODO(crbug.com/602666): User should get here only if they have selected
282 // a contact info.
283 DCHECK(contactInfo);
284 paymentResponse.payer_phone =
285 contactInfo->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER);
286 }
287 371
288 _viewController.view.userInteractionEnabled = NO; 372 _viewController.view.userInteractionEnabled = NO;
289 [_viewController setPending:YES]; 373 [_viewController setPending:YES];
290 [_viewController loadModel]; 374 [_viewController loadModel];
291 [[_viewController collectionView] reloadData]; 375 [[_viewController collectionView] reloadData];
292 376
293 [_delegate paymentRequestCoordinator:self 377 [self sendPaymentResponseIfAllAsyncCallsCompleted];
294 didConfirmWithPaymentResponse:paymentResponse]; 378 }
379
380 - (void)sendPaymentResponseIfAllAsyncCallsCompleted {
381 if (_contactAddressNormalizationPending == NO &&
382 _shippingAddressNormalizationPending == NO &&
383 _billingAddressNormalizationPending == NO &&
384 _fullCardRequestPending == NO) {
385 web::PaymentResponse paymentResponse;
386
387 // If the merchant specified the card network as part of the "basic-card"
388 // payment method, return "basic-card" as the method_name. Otherwise, return
389 // the name of the network directly.
390 std::string basic_card_type =
391 autofill::data_util::GetPaymentRequestData(_fullCard.creditCard.type())
392 .basic_card_payment_type;
393 paymentResponse.method_name =
394 _paymentRequest->basic_card_specified_networks().find(
395 basic_card_type) !=
396 _paymentRequest->basic_card_specified_networks().end()
397 ? base::ASCIIToUTF16("basic-card")
398 : base::ASCIIToUTF16(basic_card_type);
399
400 paymentResponse.details = GetBasicCardResponseFromAutofillCreditCard(
401 _fullCard.creditCard, _fullCard.cvc, _fullCard.billingAddress,
402 GetApplicationContext()->GetApplicationLocale());
403
404 if (_paymentRequest->request_shipping()) {
405 autofill::AutofillProfile* shippingAddress =
406 _paymentRequest->selected_shipping_profile();
407 // TODO(crbug.com/602666): User should get here only if they have selected
408 // a shipping address.
409 DCHECK(shippingAddress);
410 paymentResponse.shipping_address = GetPaymentAddressFromAutofillProfile(
411 *shippingAddress, GetApplicationContext()->GetApplicationLocale());
412
413 web::PaymentShippingOption* shippingOption =
414 _paymentRequest->selected_shipping_option();
415 DCHECK(shippingOption);
416 paymentResponse.shipping_option = shippingOption->id;
417 }
418
419 if (_paymentRequest->request_payer_name()) {
420 autofill::AutofillProfile* contactInfo =
421 _paymentRequest->selected_contact_profile();
422 // TODO(crbug.com/602666): User should get here only if they have selected
423 // a contact info.
424 DCHECK(contactInfo);
425 paymentResponse.payer_name =
426 contactInfo->GetInfo(autofill::AutofillType(autofill::NAME_FULL),
427 GetApplicationContext()->GetApplicationLocale());
428 }
429
430 if (_paymentRequest->request_payer_email()) {
431 autofill::AutofillProfile* contactInfo =
432 _paymentRequest->selected_contact_profile();
433 // TODO(crbug.com/602666): User should get here only if they have selected
434 // a contact info.
435 DCHECK(contactInfo);
436 paymentResponse.payer_email =
437 contactInfo->GetRawInfo(autofill::EMAIL_ADDRESS);
438 }
439
440 if (_paymentRequest->request_payer_phone()) {
441 autofill::AutofillProfile* contactInfo =
442 _paymentRequest->selected_contact_profile();
443 // TODO(crbug.com/602666): User should get here only if they have selected
444 // a contact info.
445 DCHECK(contactInfo);
446 paymentResponse.payer_phone =
447 contactInfo->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER);
448 }
449
450 [_delegate paymentRequestCoordinator:self
451 didConfirmWithPaymentResponse:paymentResponse];
452 }
295 } 453 }
296 454
297 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails { 455 - (void)updatePaymentDetails:(web::PaymentDetails)paymentDetails {
298 BOOL totalValueChanged = 456 BOOL totalValueChanged =
299 (_paymentRequest->payment_details().total != paymentDetails.total); 457 (_paymentRequest->payment_details().total != paymentDetails.total);
300 _paymentRequest->set_payment_details(paymentDetails); 458 _paymentRequest->set_payment_details(paymentDetails);
301 459
302 if (_paymentRequest->shipping_options().empty()) { 460 if (_paymentRequest->shipping_options().empty()) {
303 // Display error in the shipping address/option selection view. 461 // Display error in the shipping address/option selection view.
304 if (_shippingAddressSelectionCoordinator) { 462 if (_shippingAddressSelectionCoordinator) {
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 - (void)paymentMethodSelectionCoordinatorDidReturn: 652 - (void)paymentMethodSelectionCoordinatorDidReturn:
495 (PaymentMethodSelectionCoordinator*)coordinator { 653 (PaymentMethodSelectionCoordinator*)coordinator {
496 // Clear the 'Updated' label on the payment summary item, if there is one. 654 // Clear the 'Updated' label on the payment summary item, if there is one.
497 [_viewController updatePaymentSummaryWithTotalValueChanged:NO]; 655 [_viewController updatePaymentSummaryWithTotalValueChanged:NO];
498 656
499 [_methodSelectionCoordinator stop]; 657 [_methodSelectionCoordinator stop];
500 _methodSelectionCoordinator = nil; 658 _methodSelectionCoordinator = nil;
501 } 659 }
502 660
503 @end 661 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/payments/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698