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_mediator.h" | 5 #import "ios/chrome/browser/ui/payments/credit_card_edit_mediator.h" |
6 | 6 |
7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
8 #include "components/autofill/core/browser/autofill_data_util.h" | 8 #include "components/autofill/core/browser/autofill_data_util.h" |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
10 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 @synthesize creditCard = _creditCard; | 58 @synthesize creditCard = _creditCard; |
59 @synthesize editorFields = _editorFields; | 59 @synthesize editorFields = _editorFields; |
60 | 60 |
61 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest | 61 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest |
62 creditCard:(autofill::CreditCard*)creditCard { | 62 creditCard:(autofill::CreditCard*)creditCard { |
63 self = [super init]; | 63 self = [super init]; |
64 if (self) { | 64 if (self) { |
65 _paymentRequest = paymentRequest; | 65 _paymentRequest = paymentRequest; |
66 _creditCard = creditCard; | 66 _creditCard = creditCard; |
67 _editorFields = [self createEditorFields]; | 67 _editorFields = [self createEditorFields]; |
68 _state = _creditCard ? EditViewControllerStateEdit | |
69 : EditViewControllerStateCreate; | |
68 } | 70 } |
69 return self; | 71 return self; |
70 } | 72 } |
71 | 73 |
72 - (CollectionViewItem*)serverCardSummaryItem { | 74 - (CollectionViewItem*)headerItem { |
73 // Return nil if creating or editing a local card. | 75 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) { |
74 if (!_creditCard || autofill::IsCreditCardLocal(*_creditCard)) | 76 // Return an item that identifies the server card being edited. |
75 return nil; | 77 PaymentMethodItem* cardSummaryItem = [[PaymentMethodItem alloc] init]; |
78 cardSummaryItem.methodID = | |
79 base::SysUTF16ToNSString(_creditCard->NetworkAndLastFourDigits()); | |
80 cardSummaryItem.methodDetail = base::SysUTF16ToNSString( | |
81 _creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); | |
82 const int issuerNetworkIconID = | |
83 autofill::data_util::GetPaymentRequestData(_creditCard->network()) | |
84 .icon_resource_id; | |
85 cardSummaryItem.methodTypeIcon = NativeImage(issuerNetworkIconID); | |
86 return cardSummaryItem; | |
87 } else { | |
macourteau
2017/05/11 18:59:39
nit: get rid of the else {}, since the 'if' always
Moe
2017/05/11 19:39:38
Done. Not sure If I understand the comment in pare
macourteau
2017/05/11 19:58:23
Never mind that comment, didn't match the braces c
| |
88 // Return an item that displays a list of payment method type icons for the | |
89 // accepted payment methods. | |
90 NSMutableArray* issuerNetworkIcons = [NSMutableArray array]; | |
91 for (const auto& supportedNetwork : | |
92 _paymentRequest->supported_card_networks()) { | |
93 const std::string issuerNetwork = | |
94 GetIssuerNetworkForBasicCardIssuerNetwork(supportedNetwork); | |
95 const autofill::data_util::PaymentRequestData& cardData = | |
96 GetPaymentRequestData(issuerNetwork); | |
97 UIImage* issuerNetworkIcon = | |
98 ResizeImage(NativeImage(cardData.icon_resource_id), | |
99 CGSizeMake(kCardIssuerNetworkIconDimension, | |
100 kCardIssuerNetworkIconDimension), | |
101 ProjectionMode::kAspectFillNoClipping); | |
102 issuerNetworkIcon.accessibilityLabel = | |
103 l10n_util::GetNSString(cardData.a11y_label_resource_id); | |
104 [issuerNetworkIcons addObject:issuerNetworkIcon]; | |
105 } | |
76 | 106 |
77 PaymentMethodItem* cardSummaryItem = [[PaymentMethodItem alloc] init]; | 107 AcceptedPaymentMethodsItem* acceptedMethodsItem = |
78 cardSummaryItem.methodID = | 108 [[AcceptedPaymentMethodsItem alloc] init]; |
79 base::SysUTF16ToNSString(_creditCard->NetworkAndLastFourDigits()); | 109 acceptedMethodsItem.message = |
80 cardSummaryItem.methodDetail = base::SysUTF16ToNSString( | 110 l10n_util::GetNSString(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL); |
81 _creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); | 111 acceptedMethodsItem.methodTypeIcons = issuerNetworkIcons; |
82 const int issuerNetworkIconID = | 112 return acceptedMethodsItem; |
83 autofill::data_util::GetPaymentRequestData(_creditCard->network()) | 113 } |
84 .icon_resource_id; | |
85 cardSummaryItem.methodTypeIcon = NativeImage(issuerNetworkIconID); | |
86 return cardSummaryItem; | |
87 } | 114 } |
88 | 115 |
89 - (CollectionViewItem*)acceptedPaymentMethodsItem { | 116 - (BOOL)shouldHideBackgroundForHeaderItem { |
90 // Return nil if a server card is being edited. | 117 // YES if the header item displays the accepted payment method type icons. |
91 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) | 118 return !_creditCard || autofill::IsCreditCardLocal(*_creditCard); |
92 return nil; | |
93 | |
94 NSMutableArray* issuerNetworkIcons = [NSMutableArray array]; | |
95 for (const auto& supportedNetwork : | |
96 _paymentRequest->supported_card_networks()) { | |
97 const std::string issuerNetwork = | |
98 GetIssuerNetworkForBasicCardIssuerNetwork(supportedNetwork); | |
99 const autofill::data_util::PaymentRequestData& cardData = | |
100 GetPaymentRequestData(issuerNetwork); | |
101 UIImage* issuerNetworkIcon = | |
102 ResizeImage(NativeImage(cardData.icon_resource_id), | |
103 CGSizeMake(kCardIssuerNetworkIconDimension, | |
104 kCardIssuerNetworkIconDimension), | |
105 ProjectionMode::kAspectFillNoClipping); | |
106 issuerNetworkIcon.accessibilityLabel = | |
107 l10n_util::GetNSString(cardData.a11y_label_resource_id); | |
108 [issuerNetworkIcons addObject:issuerNetworkIcon]; | |
109 } | |
110 | |
111 AcceptedPaymentMethodsItem* acceptedMethodsItem = | |
112 [[AcceptedPaymentMethodsItem alloc] init]; | |
113 acceptedMethodsItem.message = | |
114 l10n_util::GetNSString(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL); | |
115 acceptedMethodsItem.methodTypeIcons = issuerNetworkIcons; | |
116 return acceptedMethodsItem; | |
117 } | |
118 | |
119 - (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID { | |
120 DCHECK(profileGUID); | |
121 autofill::AutofillProfile* profile = | |
122 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( | |
123 base::SysNSStringToUTF8(profileGUID), | |
124 _paymentRequest->billing_profiles()); | |
125 DCHECK(profile); | |
126 return GetBillingAddressLabelFromAutofillProfile(*profile); | |
127 } | 119 } |
128 | 120 |
129 - (UIImage*)cardTypeIconFromCardNumber:(NSString*)cardNumber { | 121 - (UIImage*)cardTypeIconFromCardNumber:(NSString*)cardNumber { |
130 const char* issuerNetwork = autofill::CreditCard::GetCardNetwork( | 122 const char* issuerNetwork = autofill::CreditCard::GetCardNetwork( |
131 base::SysNSStringToUTF16(cardNumber)); | 123 base::SysNSStringToUTF16(cardNumber)); |
132 // This should not happen in Payment Request. | 124 // This should not happen in Payment Request. |
133 if (issuerNetwork == autofill::kGenericCard) | 125 if (issuerNetwork == autofill::kGenericCard) |
134 return nil; | 126 return nil; |
135 | 127 |
136 // Resize and return the card issuer network icon. | 128 // Resize and return the card issuer network icon. |
137 int resourceID = autofill::data_util::GetPaymentRequestData(issuerNetwork) | 129 int resourceID = autofill::data_util::GetPaymentRequestData(issuerNetwork) |
138 .icon_resource_id; | 130 .icon_resource_id; |
139 CGFloat dimension = kCardIssuerNetworkIconDimension; | 131 CGFloat dimension = kCardIssuerNetworkIconDimension; |
140 return ResizeImage(NativeImage(resourceID), CGSizeMake(dimension, dimension), | 132 return ResizeImage(NativeImage(resourceID), CGSizeMake(dimension, dimension), |
141 ProjectionMode::kAspectFillNoClipping); | 133 ProjectionMode::kAspectFillNoClipping); |
142 } | 134 } |
143 | 135 |
144 - (NSArray<EditorField*>*)editorFields { | 136 - (NSArray<EditorField*>*)editorFields { |
145 return _editorFields; | 137 return _editorFields; |
146 } | 138 } |
147 | 139 |
148 #pragma mark - Helper methods | 140 #pragma mark - Helper methods |
149 | 141 |
142 // Returns the billing address label from an autofill profile with the given | |
143 // guid. Returns nil if the profile does not have an address. | |
144 - (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID { | |
145 DCHECK(profileGUID); | |
146 autofill::AutofillProfile* profile = | |
147 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( | |
148 base::SysNSStringToUTF8(profileGUID), | |
149 _paymentRequest->billing_profiles()); | |
150 DCHECK(profile); | |
151 return GetBillingAddressLabelFromAutofillProfile(*profile); | |
152 } | |
153 | |
150 - (NSArray<EditorField*>*)createEditorFields { | 154 - (NSArray<EditorField*>*)createEditorFields { |
151 // Server credit cards are not editable. | 155 NSString* billingAddressGUID = |
156 _creditCard && !_creditCard->billing_address_id().empty() | |
157 ? base::SysUTF8ToNSString(_creditCard->billing_address_id()) | |
158 : nil; | |
159 NSString* billingAddressLabel = | |
160 billingAddressGUID | |
161 ? [self billingAddressLabelForProfileWithGUID:billingAddressGUID] | |
162 : nil; | |
163 EditorField* billingAddressEditorField = [[EditorField alloc] | |
164 initWithAutofillUIType:AutofillUITypeCreditCardBillingAddress | |
165 fieldType:EditorFieldTypeSelector | |
166 label:l10n_util::GetNSString( | |
167 IDS_PAYMENTS_BILLING_ADDRESS) | |
168 value:billingAddressGUID | |
169 required:YES]; | |
170 [billingAddressEditorField setDisplayValue:billingAddressLabel]; | |
171 | |
172 // Only the billing address of server credit cards can be edited. | |
macourteau
2017/05/11 18:59:39
nit: this comment had me stumped for a while, mayb
Moe
2017/05/11 19:39:38
Done.
| |
152 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) | 173 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) |
153 return @[]; | 174 return @[ billingAddressEditorField ]; |
154 | 175 |
155 NSString* creditCardNumber = | 176 NSString* creditCardNumber = |
156 _creditCard ? base::SysUTF16ToNSString(_creditCard->number()) : nil; | 177 _creditCard ? base::SysUTF16ToNSString(_creditCard->number()) : nil; |
157 | 178 |
158 NSString* creditCardName = | 179 NSString* creditCardName = |
159 _creditCard | 180 _creditCard |
160 ? autofill::GetCreditCardName( | 181 ? autofill::GetCreditCardName( |
161 *_creditCard, GetApplicationContext()->GetApplicationLocale()) | 182 *_creditCard, GetApplicationContext()->GetApplicationLocale()) |
162 : nil; | 183 : nil; |
163 | 184 |
164 NSString* creditCardExpMonth = | 185 NSString* creditCardExpMonth = |
165 _creditCard | 186 _creditCard |
166 ? [NSString stringWithFormat:@"%02d", _creditCard->expiration_month()] | 187 ? [NSString stringWithFormat:@"%02d", _creditCard->expiration_month()] |
167 : nil; | 188 : nil; |
168 | 189 |
169 NSString* creditCardExpYear = | 190 NSString* creditCardExpYear = |
170 _creditCard | 191 _creditCard |
171 ? [NSString stringWithFormat:@"%04d", _creditCard->expiration_year()] | 192 ? [NSString stringWithFormat:@"%04d", _creditCard->expiration_year()] |
172 : nil; | 193 : nil; |
173 | 194 |
174 return @[ | 195 NSMutableArray* editorFields = [[NSMutableArray alloc] init]; |
196 [editorFields addObjectsFromArray:@[ | |
175 [[EditorField alloc] | 197 [[EditorField alloc] |
176 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 198 initWithAutofillUIType:AutofillUITypeFromAutofillType( |
177 autofill::CREDIT_CARD_NUMBER) | 199 autofill::CREDIT_CARD_NUMBER) |
200 fieldType:EditorFieldTypeTextField | |
178 label:l10n_util::GetNSString(IDS_PAYMENTS_CARD_NUMBER) | 201 label:l10n_util::GetNSString(IDS_PAYMENTS_CARD_NUMBER) |
179 value:creditCardNumber | 202 value:creditCardNumber |
180 required:YES], | 203 required:YES], |
181 [[EditorField alloc] | 204 [[EditorField alloc] |
182 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 205 initWithAutofillUIType:AutofillUITypeFromAutofillType( |
183 autofill::CREDIT_CARD_NAME_FULL) | 206 autofill::CREDIT_CARD_NAME_FULL) |
207 fieldType:EditorFieldTypeTextField | |
184 label:l10n_util::GetNSString(IDS_PAYMENTS_NAME_ON_CARD) | 208 label:l10n_util::GetNSString(IDS_PAYMENTS_NAME_ON_CARD) |
185 value:creditCardName | 209 value:creditCardName |
186 required:YES], | 210 required:YES], |
187 [[EditorField alloc] | 211 [[EditorField alloc] |
188 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 212 initWithAutofillUIType:AutofillUITypeFromAutofillType( |
189 autofill::CREDIT_CARD_EXP_MONTH) | 213 autofill::CREDIT_CARD_EXP_MONTH) |
214 fieldType:EditorFieldTypeTextField | |
190 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_MONTH) | 215 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_MONTH) |
191 value:creditCardExpMonth | 216 value:creditCardExpMonth |
192 required:YES], | 217 required:YES], |
193 [[EditorField alloc] | 218 [[EditorField alloc] |
194 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 219 initWithAutofillUIType:AutofillUITypeFromAutofillType( |
195 autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) | 220 autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) |
221 fieldType:EditorFieldTypeTextField | |
196 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_YEAR) | 222 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_YEAR) |
197 value:creditCardExpYear | 223 value:creditCardExpYear |
198 required:YES] | 224 required:YES] |
199 ]; | 225 ]]; |
226 // The billing address field goes at the end. | |
227 [editorFields addObject:billingAddressEditorField]; | |
228 return editorFields; | |
200 } | 229 } |
201 | 230 |
202 @end | 231 @end |
OLD | NEW |