OLD | NEW |
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 "ios/chrome/browser/ui/payments/credit_card_edit_coordinator.h" | 5 #import "ios/chrome/browser/ui/payments/credit_card_edit_coordinator.h" |
6 | 6 |
7 #include "base/guid.h" | 7 #include "base/guid.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 @synthesize paymentRequest = _paymentRequest; | 42 @synthesize paymentRequest = _paymentRequest; |
43 @synthesize delegate = _delegate; | 43 @synthesize delegate = _delegate; |
44 | 44 |
45 - (void)start { | 45 - (void)start { |
46 _viewController = [[CreditCardEditViewController alloc] init]; | 46 _viewController = [[CreditCardEditViewController alloc] init]; |
47 // TODO(crbug.com/602666): Title varies depending on the missing fields. | 47 // TODO(crbug.com/602666): Title varies depending on the missing fields. |
48 NSString* title = _creditCard | 48 NSString* title = _creditCard |
49 ? l10n_util::GetNSString(IDS_PAYMENTS_EDIT_CARD) | 49 ? l10n_util::GetNSString(IDS_PAYMENTS_EDIT_CARD) |
50 : l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD_LABEL); | 50 : l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD_LABEL); |
51 [_viewController setTitle:title]; | 51 [_viewController setTitle:title]; |
52 if (_creditCard && !_creditCard->billing_address_id().empty()) | |
53 [_viewController | |
54 setBillingAddressGUID:base::SysUTF8ToNSString( | |
55 _creditCard->billing_address_id())]; | |
56 [_viewController setDelegate:self]; | 52 [_viewController setDelegate:self]; |
57 [_viewController setValidatorDelegate:self]; | 53 [_viewController setValidatorDelegate:self]; |
58 | |
59 _mediator = [[CreditCardEditViewControllerMediator alloc] | 54 _mediator = [[CreditCardEditViewControllerMediator alloc] |
60 initWithPaymentRequest:_paymentRequest | 55 initWithPaymentRequest:_paymentRequest |
61 creditCard:_creditCard]; | 56 creditCard:_creditCard]; |
62 [_mediator setState:_creditCard ? CreditCardEditViewControllerStateEdit | |
63 : CreditCardEditViewControllerStateCreate]; | |
64 [_viewController setDataSource:_mediator]; | 57 [_viewController setDataSource:_mediator]; |
65 | |
66 [_viewController loadModel]; | 58 [_viewController loadModel]; |
67 | 59 |
68 DCHECK(self.baseViewController.navigationController); | 60 DCHECK(self.baseViewController.navigationController); |
69 [[self baseViewController].navigationController | 61 [[self baseViewController].navigationController |
70 pushViewController:_viewController | 62 pushViewController:_viewController |
71 animated:YES]; | 63 animated:YES]; |
72 } | 64 } |
73 | 65 |
74 - (void)stop { | 66 - (void)stop { |
75 [_viewController.navigationController popViewControllerAnimated:YES]; | 67 [_viewController.navigationController popViewControllerAnimated:YES]; |
76 _viewController = nil; | 68 _viewController = nil; |
77 } | 69 } |
78 | 70 |
79 #pragma mark - PaymentRequestEditViewControllerValidator | 71 #pragma mark - PaymentRequestEditViewControllerValidator |
80 | 72 |
81 - (NSString*)paymentRequestEditViewController: | 73 - (NSString*)paymentRequestEditViewController: |
82 (PaymentRequestEditViewController*)controller | 74 (PaymentRequestEditViewController*)controller |
83 validateValue:(NSString*)value | 75 validateField:(EditorField*)field { |
84 autofillUIType:(AutofillUIType)autofillUIType | 76 if (field.value.length) { |
85 required:(BOOL)required { | |
86 if (value.length) { | |
87 base::string16 errorMessage; | 77 base::string16 errorMessage; |
88 base::string16 valueString = base::SysNSStringToUTF16(value); | 78 base::string16 valueString = base::SysNSStringToUTF16(field.value); |
89 if (autofillUIType == AutofillUITypeCreditCardNumber) { | 79 if (field.autofillUIType == AutofillUITypeCreditCardNumber) { |
90 std::set<std::string> supportedCardNetworks( | 80 std::set<std::string> supportedCardNetworks( |
91 _paymentRequest->supported_card_networks().begin(), | 81 _paymentRequest->supported_card_networks().begin(), |
92 _paymentRequest->supported_card_networks().end()); | 82 _paymentRequest->supported_card_networks().end()); |
93 autofill::IsValidCreditCardNumberForBasicCardNetworks( | 83 autofill::IsValidCreditCardNumberForBasicCardNetworks( |
94 valueString, supportedCardNetworks, &errorMessage); | 84 valueString, supportedCardNetworks, &errorMessage); |
| 85 } else if (field.autofillUIType == AutofillUITypeCreditCardBillingAddress) { |
| 86 // TODO(crbug.com/602666): More validation? |
| 87 return nil; |
95 } else { | 88 } else { |
96 autofill::IsValidForType(valueString, | 89 autofill::IsValidForType( |
97 AutofillTypeFromAutofillUIType(autofillUIType), | 90 valueString, AutofillTypeFromAutofillUIType(field.autofillUIType), |
98 &errorMessage); | 91 &errorMessage); |
99 } | 92 } |
100 return !errorMessage.empty() ? base::SysUTF16ToNSString(errorMessage) : nil; | 93 return !errorMessage.empty() ? base::SysUTF16ToNSString(errorMessage) : nil; |
101 } else if (required) { | 94 } else if (field.isRequired) { |
102 return l10n_util::GetNSString( | 95 return l10n_util::GetNSString( |
103 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 96 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
104 } | 97 } |
105 return nil; | 98 return nil; |
106 } | 99 } |
107 | 100 |
108 #pragma mark - CreditCardEditViewControllerDelegate | 101 #pragma mark - CreditCardEditViewControllerDelegate |
109 | 102 |
| 103 - (void)paymentRequestEditViewController: |
| 104 (PaymentRequestEditViewController*)controller |
| 105 didSelectField:(EditorField*)field { |
| 106 if (field.autofillUIType == AutofillUITypeCreditCardBillingAddress) { |
| 107 // TODO(crbug.com/602666): Display a list of billing addresses. |
| 108 } |
| 109 } |
| 110 |
110 - (void)creditCardEditViewController:(CreditCardEditViewController*)controller | 111 - (void)creditCardEditViewController:(CreditCardEditViewController*)controller |
111 didFinishEditingFields:(NSArray<EditorField*>*)fields | 112 didFinishEditingFields:(NSArray<EditorField*>*)fields |
112 billingAddressID:(NSString*)billingAddressID | |
113 saveCreditCard:(BOOL)saveCreditCard { | 113 saveCreditCard:(BOOL)saveCreditCard { |
114 // Create an empty credit card. If a credit card is being edited, copy over | 114 // Create an empty credit card. If a credit card is being edited, copy over |
115 // the information. | 115 // the information. |
116 autofill::CreditCard creditCard("", autofill::kSettingsOrigin); | 116 autofill::CreditCard creditCard("", autofill::kSettingsOrigin); |
117 if (_creditCard) | 117 if (_creditCard) |
118 creditCard = *_creditCard; | 118 creditCard = *_creditCard; |
119 | 119 |
120 for (EditorField* field in fields) { | 120 for (EditorField* field in fields) { |
121 creditCard.SetRawInfo(AutofillTypeFromAutofillUIType(field.autofillUIType), | 121 if (field.autofillUIType == AutofillUITypeCreditCardBillingAddress) { |
122 base::SysNSStringToUTF16(field.value)); | 122 creditCard.set_billing_address_id(base::SysNSStringToUTF8(field.value)); |
| 123 } else { |
| 124 creditCard.SetRawInfo( |
| 125 AutofillTypeFromAutofillUIType(field.autofillUIType), |
| 126 base::SysNSStringToUTF16(field.value)); |
| 127 } |
123 } | 128 } |
124 | 129 |
125 creditCard.set_billing_address_id(base::SysNSStringToUTF8(billingAddressID)); | |
126 | |
127 if (!_creditCard) { | 130 if (!_creditCard) { |
128 if (saveCreditCard) { | 131 if (saveCreditCard) { |
129 // The new credit card does not yet have a valid GUID. | 132 // The new credit card does not yet have a valid GUID. |
130 creditCard.set_guid(base::GenerateGUID()); | 133 creditCard.set_guid(base::GenerateGUID()); |
131 _paymentRequest->GetPersonalDataManager()->AddCreditCard(creditCard); | 134 _paymentRequest->GetPersonalDataManager()->AddCreditCard(creditCard); |
132 } | 135 } |
133 | 136 |
134 // Add the credit card to the list of credit cards in |_paymentRequest|. | 137 // Add the credit card to the list of credit cards in |_paymentRequest|. |
135 _creditCard = _paymentRequest->AddCreditCard(creditCard); | 138 _creditCard = _paymentRequest->AddCreditCard(creditCard); |
136 } else { | 139 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
149 [_delegate creditCardEditCoordinator:self | 152 [_delegate creditCardEditCoordinator:self |
150 didFinishEditingCreditCard:_creditCard]; | 153 didFinishEditingCreditCard:_creditCard]; |
151 } | 154 } |
152 | 155 |
153 - (void)creditCardEditViewControllerDidCancel: | 156 - (void)creditCardEditViewControllerDidCancel: |
154 (CreditCardEditViewController*)controller { | 157 (CreditCardEditViewController*)controller { |
155 [_delegate creditCardEditCoordinatorDidCancel:self]; | 158 [_delegate creditCardEditCoordinatorDidCancel:self]; |
156 } | 159 } |
157 | 160 |
158 @end | 161 @end |
OLD | NEW |