OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/payments/payment_request_coordinator.h" |
| 6 |
| 7 #include <unordered_set> |
| 8 #include <vector> |
| 9 |
| 10 #import "base/ios/weak_nsobject.h" |
| 11 #include "base/mac/objc_property_releaser.h" |
| 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/browser/autofill_data_util.h" |
| 15 #include "components/autofill/core/browser/autofill_profile.h" |
| 16 #include "components/autofill/core/browser/credit_card.h" |
| 17 #include "components/autofill/core/browser/field_types.h" |
| 18 #include "components/autofill/core/browser/personal_data_manager.h" |
| 19 #include "ios/chrome/browser/application_context.h" |
| 20 |
| 21 @interface PaymentRequestCoordinator () { |
| 22 autofill::PersonalDataManager* _personalDataManager; // weak |
| 23 base::WeakNSProtocol<id<PaymentRequestCoordinatorDelegate>> _delegate; |
| 24 base::scoped_nsobject<UINavigationController> _navigationController; |
| 25 base::scoped_nsobject<PaymentRequestViewController> _viewController; |
| 26 base::scoped_nsobject<PaymentItemsDisplayCoordinator> |
| 27 _itemsDisplayCoordinator; |
| 28 base::scoped_nsobject<ShippingAddressSelectionCoordinator> |
| 29 _shippingAddressSelectionCoordinator; |
| 30 base::scoped_nsobject<PaymentMethodSelectionCoordinator> |
| 31 _methodSelectionCoordinator; |
| 32 |
| 33 base::mac::ObjCPropertyReleaser _propertyReleaser_PaymentRequestCoordinator; |
| 34 } |
| 35 |
| 36 // Returns the credit cards available from |_personalDataManager| that match |
| 37 // a supported type specified in |_paymentRequest|. |
| 38 - (std::vector<autofill::CreditCard*>)supportedMethods; |
| 39 |
| 40 @end |
| 41 |
| 42 @implementation PaymentRequestCoordinator |
| 43 |
| 44 @synthesize paymentRequest = _paymentRequest; |
| 45 @synthesize pageFavicon = _pageFavicon; |
| 46 @synthesize pageTitle = _pageTitle; |
| 47 @synthesize pageHost = _pageHost; |
| 48 @synthesize selectedShippingAddress = _selectedShippingAddress; |
| 49 @synthesize selectedPaymentMethod = _selectedPaymentMethod; |
| 50 |
| 51 - (instancetype)initWithBaseViewController:(UIViewController*)baseViewController |
| 52 personalDataManager: |
| 53 (autofill::PersonalDataManager*)personalDataManager { |
| 54 if ((self = [super initWithBaseViewController:baseViewController])) { |
| 55 _propertyReleaser_PaymentRequestCoordinator.Init( |
| 56 self, [PaymentRequestCoordinator class]); |
| 57 _personalDataManager = personalDataManager; |
| 58 } |
| 59 return self; |
| 60 } |
| 61 |
| 62 - (id<PaymentRequestCoordinatorDelegate>)delegate { |
| 63 return _delegate.get(); |
| 64 } |
| 65 |
| 66 - (void)setDelegate:(id<PaymentRequestCoordinatorDelegate>)delegate { |
| 67 _delegate.reset(delegate); |
| 68 } |
| 69 |
| 70 - (void)start { |
| 71 const std::vector<autofill::AutofillProfile*> addresses = |
| 72 _personalDataManager->GetProfilesToSuggest(); |
| 73 if (addresses.size() > 0) |
| 74 _selectedShippingAddress = addresses[0]; |
| 75 |
| 76 const std::vector<autofill::CreditCard*> cards = [self supportedMethods]; |
| 77 if (cards.size() > 0) |
| 78 _selectedPaymentMethod = cards[0]; |
| 79 |
| 80 _viewController.reset([[PaymentRequestViewController alloc] init]); |
| 81 [_viewController setPaymentRequest:_paymentRequest]; |
| 82 [_viewController setPageFavicon:_pageFavicon]; |
| 83 [_viewController setPageTitle:_pageTitle]; |
| 84 [_viewController setPageHost:_pageHost]; |
| 85 [_viewController setSelectedShippingAddress:_selectedShippingAddress]; |
| 86 [_viewController setSelectedPaymentMethod:_selectedPaymentMethod]; |
| 87 [_viewController setDelegate:self]; |
| 88 [_viewController loadModel]; |
| 89 |
| 90 _navigationController.reset([[UINavigationController alloc] |
| 91 initWithRootViewController:_viewController]); |
| 92 [_navigationController setNavigationBarHidden:YES]; |
| 93 |
| 94 [[self baseViewController] presentViewController:_navigationController |
| 95 animated:YES |
| 96 completion:nil]; |
| 97 } |
| 98 |
| 99 - (void)stop { |
| 100 [_navigationController dismissViewControllerAnimated:YES completion:nil]; |
| 101 _itemsDisplayCoordinator.reset(); |
| 102 _shippingAddressSelectionCoordinator.reset(); |
| 103 _methodSelectionCoordinator.reset(); |
| 104 _navigationController.reset(); |
| 105 _viewController.reset(); |
| 106 } |
| 107 |
| 108 - (std::vector<autofill::CreditCard*>)supportedMethods { |
| 109 std::vector<autofill::CreditCard*> supported_methods; |
| 110 |
| 111 std::unordered_set<base::string16> supported_method_types; |
| 112 for (web::PaymentMethodData method_data : _paymentRequest.method_data) { |
| 113 for (base::string16 supported_method : method_data.supported_methods) |
| 114 supported_method_types.insert(supported_method); |
| 115 } |
| 116 |
| 117 for (autofill::CreditCard* card : |
| 118 _personalDataManager->GetCreditCardsToSuggest()) { |
| 119 const std::string spec_card_type = |
| 120 autofill::data_util::GetPaymentRequestData(card->type()) |
| 121 .basic_card_payment_type; |
| 122 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != |
| 123 supported_method_types.end()) |
| 124 supported_methods.push_back(card); |
| 125 } |
| 126 |
| 127 return supported_methods; |
| 128 } |
| 129 |
| 130 - (void)sendPaymentResponse { |
| 131 DCHECK(_selectedPaymentMethod); |
| 132 |
| 133 // TODO(crbug.com/602666): Unmask if this is a server card and/or ask the user |
| 134 // for CVC here. |
| 135 // TODO(crbug.com/602666): Record the use of this card with the |
| 136 // PersonalDataManager. |
| 137 web::PaymentResponse paymentResponse; |
| 138 paymentResponse.details.cardholder_name = |
| 139 _selectedPaymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL); |
| 140 paymentResponse.details.card_number = |
| 141 _selectedPaymentMethod->GetRawInfo(autofill::CREDIT_CARD_NUMBER); |
| 142 paymentResponse.details.expiry_month = |
| 143 _selectedPaymentMethod->GetRawInfo(autofill::CREDIT_CARD_EXP_MONTH); |
| 144 paymentResponse.details.expiry_year = _selectedPaymentMethod->GetRawInfo( |
| 145 autofill::CREDIT_CARD_EXP_2_DIGIT_YEAR); |
| 146 paymentResponse.details.card_security_code = |
| 147 _selectedPaymentMethod->GetRawInfo( |
| 148 autofill::CREDIT_CARD_VERIFICATION_CODE); |
| 149 if (!_selectedPaymentMethod->billing_address_id().empty()) { |
| 150 autofill::AutofillProfile* address = _personalDataManager->GetProfileByGUID( |
| 151 _selectedPaymentMethod->billing_address_id()); |
| 152 paymentResponse.details.billing_address.country = |
| 153 address->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY); |
| 154 paymentResponse.details.billing_address.address_line.push_back( |
| 155 address->GetRawInfo(autofill::ADDRESS_HOME_LINE1)); |
| 156 paymentResponse.details.billing_address.address_line.push_back( |
| 157 address->GetRawInfo(autofill::ADDRESS_HOME_LINE2)); |
| 158 paymentResponse.details.billing_address.address_line.push_back( |
| 159 address->GetRawInfo(autofill::ADDRESS_HOME_LINE3)); |
| 160 paymentResponse.details.billing_address.region = |
| 161 address->GetRawInfo(autofill::ADDRESS_HOME_STATE); |
| 162 paymentResponse.details.billing_address.city = |
| 163 address->GetRawInfo(autofill::ADDRESS_HOME_CITY); |
| 164 paymentResponse.details.billing_address.dependent_locality = |
| 165 address->GetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY); |
| 166 paymentResponse.details.billing_address.postal_code = |
| 167 address->GetRawInfo(autofill::ADDRESS_HOME_ZIP); |
| 168 paymentResponse.details.billing_address.sorting_code = |
| 169 address->GetRawInfo(autofill::ADDRESS_HOME_SORTING_CODE); |
| 170 paymentResponse.details.billing_address.language_code = |
| 171 base::UTF8ToUTF16(address->language_code()); |
| 172 paymentResponse.details.billing_address.organization = |
| 173 address->GetRawInfo(autofill::COMPANY_NAME); |
| 174 paymentResponse.details.billing_address.recipient = |
| 175 address->GetInfo(autofill::AutofillType(autofill::NAME_FULL), |
| 176 GetApplicationContext()->GetApplicationLocale()); |
| 177 paymentResponse.details.billing_address.phone = |
| 178 address->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER); |
| 179 } |
| 180 |
| 181 [_delegate paymentRequestCoordinatorDidConfirm:paymentResponse]; |
| 182 } |
| 183 |
| 184 #pragma mark - PaymentRequestViewControllerDelegate |
| 185 |
| 186 - (void)paymentRequestViewControllerDidCancel { |
| 187 [_delegate paymentRequestCoordinatorDidCancel]; |
| 188 } |
| 189 |
| 190 - (void)paymentRequestViewControllerDidConfirm { |
| 191 [self sendPaymentResponse]; |
| 192 } |
| 193 |
| 194 - (void)paymentRequestViewControllerDisplayPaymentItems { |
| 195 _itemsDisplayCoordinator.reset([[PaymentItemsDisplayCoordinator alloc] |
| 196 initWithBaseViewController:_viewController]); |
| 197 [_itemsDisplayCoordinator setTotal:_paymentRequest.details.total]; |
| 198 [_itemsDisplayCoordinator |
| 199 setPaymentItems:_paymentRequest.details.display_items]; |
| 200 [_itemsDisplayCoordinator |
| 201 setPayButtonEnabled:(_selectedPaymentMethod != nil)]; |
| 202 [_itemsDisplayCoordinator setDelegate:self]; |
| 203 |
| 204 [_itemsDisplayCoordinator start]; |
| 205 } |
| 206 |
| 207 - (void)paymentRequestViewControllerSelectShippingAddress { |
| 208 _shippingAddressSelectionCoordinator.reset( |
| 209 [[ShippingAddressSelectionCoordinator alloc] |
| 210 initWithBaseViewController:_viewController]); |
| 211 const std::vector<autofill::AutofillProfile*> addresses = |
| 212 _personalDataManager->GetProfilesToSuggest(); |
| 213 [_shippingAddressSelectionCoordinator setShippingAddresses:addresses]; |
| 214 [_shippingAddressSelectionCoordinator |
| 215 setSelectedShippingAddress:_selectedShippingAddress]; |
| 216 [_shippingAddressSelectionCoordinator setDelegate:self]; |
| 217 |
| 218 [_shippingAddressSelectionCoordinator start]; |
| 219 } |
| 220 |
| 221 - (void)paymentRequestViewControllerSelectPaymentMethod { |
| 222 _methodSelectionCoordinator.reset([[PaymentMethodSelectionCoordinator alloc] |
| 223 initWithBaseViewController:_viewController]); |
| 224 [_methodSelectionCoordinator setPaymentMethods:[self supportedMethods]]; |
| 225 [_methodSelectionCoordinator setSelectedPaymentMethod:_selectedPaymentMethod]; |
| 226 [_methodSelectionCoordinator setDelegate:self]; |
| 227 |
| 228 [_methodSelectionCoordinator start]; |
| 229 } |
| 230 |
| 231 #pragma mark - PaymentItemsDisplayCoordinatorDelegate |
| 232 |
| 233 - (void)paymentItemsDisplayCoordinatorDidReturn: |
| 234 (PaymentItemsDisplayCoordinator*)coordinator { |
| 235 [_itemsDisplayCoordinator stop]; |
| 236 _itemsDisplayCoordinator.reset(); |
| 237 } |
| 238 |
| 239 - (void)paymentItemsDisplayCoordinatorDidConfirm: |
| 240 (PaymentItemsDisplayCoordinator*)coordinator { |
| 241 [self sendPaymentResponse]; |
| 242 } |
| 243 |
| 244 #pragma mark - ShippingAddressSelectionCoordinatorDelegate |
| 245 |
| 246 - (void)shippingAddressSelectionCoordinator: |
| 247 (ShippingAddressSelectionCoordinator*)coordinator |
| 248 selectedShippingAddress: |
| 249 (autofill::AutofillProfile*)shippingAddress { |
| 250 _selectedShippingAddress = shippingAddress; |
| 251 [_viewController updateSelectedShippingAddress:shippingAddress]; |
| 252 |
| 253 [_shippingAddressSelectionCoordinator stop]; |
| 254 _shippingAddressSelectionCoordinator.reset(); |
| 255 } |
| 256 |
| 257 - (void)shippingAddressSelectionCoordinatorDidReturn: |
| 258 (ShippingAddressSelectionCoordinator*)coordinator { |
| 259 [_shippingAddressSelectionCoordinator stop]; |
| 260 _shippingAddressSelectionCoordinator.reset(); |
| 261 } |
| 262 |
| 263 #pragma mark - PaymentMethodSelectionCoordinatorDelegate |
| 264 |
| 265 - (void)paymentMethodSelectionCoordinator: |
| 266 (PaymentMethodSelectionCoordinator*)coordinator |
| 267 selectedPaymentMethod:(autofill::CreditCard*)creditCard { |
| 268 _selectedPaymentMethod = creditCard; |
| 269 |
| 270 [_viewController setSelectedPaymentMethod:creditCard]; |
| 271 [_viewController loadModel]; |
| 272 [[_viewController collectionView] reloadData]; |
| 273 |
| 274 [_methodSelectionCoordinator stop]; |
| 275 _methodSelectionCoordinator.reset(); |
| 276 } |
| 277 |
| 278 - (void)paymentMethodSelectionCoordinatorDidReturn: |
| 279 (PaymentMethodSelectionCoordinator*)coordinator { |
| 280 [_methodSelectionCoordinator stop]; |
| 281 _methodSelectionCoordinator.reset(); |
| 282 } |
| 283 |
| 284 @end |
OLD | NEW |