| 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" |
| 11 #include "components/autofill/core/browser/personal_data_manager.h" | 11 #include "components/autofill/core/browser/personal_data_manager.h" |
| 12 #import "components/autofill/ios/browser/credit_card_util.h" | 12 #import "components/autofill/ios/browser/credit_card_util.h" |
| 13 #include "components/strings/grit/components_strings.h" | 13 #include "components/strings/grit/components_strings.h" |
| 14 #include "ios/chrome/browser/application_context.h" | 14 #include "ios/chrome/browser/application_context.h" |
| 15 #include "ios/chrome/browser/payments/payment_request.h" | 15 #include "ios/chrome/browser/payments/payment_request.h" |
| 16 #import "ios/chrome/browser/payments/payment_request_util.h" | 16 #import "ios/chrome/browser/payments/payment_request_util.h" |
| 17 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h" | 17 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h" |
| 18 #import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h" | |
| 19 #import "ios/chrome/browser/ui/payments/cells/accepted_payment_methods_item.h" | 18 #import "ios/chrome/browser/ui/payments/cells/accepted_payment_methods_item.h" |
| 20 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h" | 19 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h" |
| 21 #import "ios/chrome/browser/ui/payments/payment_request_edit_consumer.h" | 20 #import "ios/chrome/browser/ui/payments/payment_request_edit_consumer.h" |
| 22 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h" | 21 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h" |
| 23 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 22 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 25 | 24 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) | 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." | 26 #error "This file requires ARC support." |
| 28 #endif | 27 #endif |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 using ::AutofillUITypeFromAutofillType; | |
| 32 using ::autofill::data_util::GetIssuerNetworkForBasicCardIssuerNetwork; | 30 using ::autofill::data_util::GetIssuerNetworkForBasicCardIssuerNetwork; |
| 33 using ::autofill::data_util::GetPaymentRequestData; | 31 using ::autofill::data_util::GetPaymentRequestData; |
| 34 using ::payment_request_util::GetBillingAddressLabelFromAutofillProfile; | 32 using ::payment_request_util::GetBillingAddressLabelFromAutofillProfile; |
| 35 } // namespace | 33 } // namespace |
| 36 | 34 |
| 37 @interface CreditCardEditViewControllerMediator () | 35 @interface CreditCardEditViewControllerMediator () |
| 38 | 36 |
| 39 // The PaymentRequest object owning an instance of web::PaymentRequest as | 37 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 40 // provided by the page invoking the Payment Request API. This is a weak | 38 // provided by the page invoking the Payment Request API. This is a weak |
| 41 // pointer and should outlive this class. | 39 // pointer and should outlive this class. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 66 return self; | 64 return self; |
| 67 } | 65 } |
| 68 | 66 |
| 69 #pragma mark - Setters | 67 #pragma mark - Setters |
| 70 | 68 |
| 71 - (void)setConsumer:(id<PaymentRequestEditConsumer>)consumer { | 69 - (void)setConsumer:(id<PaymentRequestEditConsumer>)consumer { |
| 72 _consumer = consumer; | 70 _consumer = consumer; |
| 73 [self.consumer setEditorFields:[self createEditorFields]]; | 71 [self.consumer setEditorFields:[self createEditorFields]]; |
| 74 } | 72 } |
| 75 | 73 |
| 76 #pragma mark - CreditCardEditViewControllerDataSource | 74 #pragma mark - PaymentRequestEditViewControllerDataSource |
| 77 | 75 |
| 78 - (CollectionViewItem*)headerItem { | 76 - (CollectionViewItem*)headerItem { |
| 79 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) { | 77 if (_creditCard && !autofill::IsCreditCardLocal(*_creditCard)) { |
| 80 // Return an item that identifies the server card being edited. | 78 // Return an item that identifies the server card being edited. |
| 81 PaymentMethodItem* cardSummaryItem = [[PaymentMethodItem alloc] init]; | 79 PaymentMethodItem* cardSummaryItem = [[PaymentMethodItem alloc] init]; |
| 82 cardSummaryItem.methodID = | 80 cardSummaryItem.methodID = |
| 83 base::SysUTF16ToNSString(_creditCard->NetworkAndLastFourDigits()); | 81 base::SysUTF16ToNSString(_creditCard->NetworkAndLastFourDigits()); |
| 84 cardSummaryItem.methodDetail = base::SysUTF16ToNSString( | 82 cardSummaryItem.methodDetail = base::SysUTF16ToNSString( |
| 85 _creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); | 83 _creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); |
| 86 const int issuerNetworkIconID = | 84 const int issuerNetworkIconID = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 111 l10n_util::GetNSString(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL); | 109 l10n_util::GetNSString(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL); |
| 112 acceptedMethodsItem.methodTypeIcons = issuerNetworkIcons; | 110 acceptedMethodsItem.methodTypeIcons = issuerNetworkIcons; |
| 113 return acceptedMethodsItem; | 111 return acceptedMethodsItem; |
| 114 } | 112 } |
| 115 | 113 |
| 116 - (BOOL)shouldHideBackgroundForHeaderItem { | 114 - (BOOL)shouldHideBackgroundForHeaderItem { |
| 117 // YES if the header item displays the accepted payment method type icons. | 115 // YES if the header item displays the accepted payment method type icons. |
| 118 return !_creditCard || autofill::IsCreditCardLocal(*_creditCard); | 116 return !_creditCard || autofill::IsCreditCardLocal(*_creditCard); |
| 119 } | 117 } |
| 120 | 118 |
| 121 - (UIImage*)cardTypeIconFromCardNumber:(NSString*)cardNumber { | 119 - (UIImage*)iconIdentifyingEditorField:(EditorField*)field { |
| 120 // Early return if the field is not the credit card number field. |
| 121 if (field.autofillUIType != AutofillUITypeCreditCardNumber) |
| 122 return nil; |
| 123 |
| 122 const char* issuerNetwork = autofill::CreditCard::GetCardNetwork( | 124 const char* issuerNetwork = autofill::CreditCard::GetCardNetwork( |
| 123 base::SysNSStringToUTF16(cardNumber)); | 125 base::SysNSStringToUTF16(field.value)); |
| 124 // This should not happen in Payment Request. | 126 // This should not happen in Payment Request. |
| 125 if (issuerNetwork == autofill::kGenericCard) | 127 if (issuerNetwork == autofill::kGenericCard) |
| 126 return nil; | 128 return nil; |
| 127 | 129 |
| 128 // Return the card issuer network icon. | 130 // Return the card issuer network icon. |
| 129 int resourceID = autofill::data_util::GetPaymentRequestData(issuerNetwork) | 131 int resourceID = autofill::data_util::GetPaymentRequestData(issuerNetwork) |
| 130 .icon_resource_id; | 132 .icon_resource_id; |
| 131 return NativeImage(resourceID); | 133 return NativeImage(resourceID); |
| 132 } | 134 } |
| 133 | 135 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 NSString* creditCardExpMonth = | 181 NSString* creditCardExpMonth = |
| 180 _creditCard | 182 _creditCard |
| 181 ? [NSString stringWithFormat:@"%02d", _creditCard->expiration_month()] | 183 ? [NSString stringWithFormat:@"%02d", _creditCard->expiration_month()] |
| 182 : nil; | 184 : nil; |
| 183 | 185 |
| 184 NSString* creditCardExpYear = | 186 NSString* creditCardExpYear = |
| 185 _creditCard | 187 _creditCard |
| 186 ? [NSString stringWithFormat:@"%04d", _creditCard->expiration_year()] | 188 ? [NSString stringWithFormat:@"%04d", _creditCard->expiration_year()] |
| 187 : nil; | 189 : nil; |
| 188 | 190 |
| 189 NSMutableArray* editorFields = [[NSMutableArray alloc] init]; | 191 return @[ |
| 190 [editorFields addObjectsFromArray:@[ | |
| 191 [[EditorField alloc] | 192 [[EditorField alloc] |
| 192 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 193 initWithAutofillUIType:AutofillUITypeCreditCardNumber |
| 193 autofill::CREDIT_CARD_NUMBER) | |
| 194 fieldType:EditorFieldTypeTextField | 194 fieldType:EditorFieldTypeTextField |
| 195 label:l10n_util::GetNSString(IDS_PAYMENTS_CARD_NUMBER) | 195 label:l10n_util::GetNSString(IDS_PAYMENTS_CARD_NUMBER) |
| 196 value:creditCardNumber | 196 value:creditCardNumber |
| 197 required:YES], | 197 required:YES], |
| 198 [[EditorField alloc] | 198 [[EditorField alloc] |
| 199 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 199 initWithAutofillUIType:AutofillUITypeCreditCardHolderFullName |
| 200 autofill::CREDIT_CARD_NAME_FULL) | |
| 201 fieldType:EditorFieldTypeTextField | 200 fieldType:EditorFieldTypeTextField |
| 202 label:l10n_util::GetNSString(IDS_PAYMENTS_NAME_ON_CARD) | 201 label:l10n_util::GetNSString(IDS_PAYMENTS_NAME_ON_CARD) |
| 203 value:creditCardName | 202 value:creditCardName |
| 204 required:YES], | 203 required:YES], |
| 205 [[EditorField alloc] | 204 [[EditorField alloc] |
| 206 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 205 initWithAutofillUIType:AutofillUITypeCreditCardExpMonth |
| 207 autofill::CREDIT_CARD_EXP_MONTH) | |
| 208 fieldType:EditorFieldTypeTextField | 206 fieldType:EditorFieldTypeTextField |
| 209 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_MONTH) | 207 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_MONTH) |
| 210 value:creditCardExpMonth | 208 value:creditCardExpMonth |
| 211 required:YES], | 209 required:YES], |
| 212 [[EditorField alloc] | 210 [[EditorField alloc] |
| 213 initWithAutofillUIType:AutofillUITypeFromAutofillType( | 211 initWithAutofillUIType:AutofillUITypeCreditCardExpYear |
| 214 autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) | |
| 215 fieldType:EditorFieldTypeTextField | 212 fieldType:EditorFieldTypeTextField |
| 216 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_YEAR) | 213 label:l10n_util::GetNSString(IDS_PAYMENTS_EXP_YEAR) |
| 217 value:creditCardExpYear | 214 value:creditCardExpYear |
| 218 required:YES] | 215 required:YES], |
| 219 ]]; | 216 billingAddressEditorField, |
| 220 // The billing address field goes at the end. | 217 [[EditorField alloc] |
| 221 [editorFields addObject:billingAddressEditorField]; | 218 initWithAutofillUIType:AutofillUITypeCreditCardSaveToChrome |
| 222 return editorFields; | 219 fieldType:EditorFieldTypeSwitch |
| 220 label:l10n_util::GetNSString( |
| 221 IDS_PAYMENTS_SAVE_CARD_TO_DEVICE_CHECKBOX) |
| 222 value:@"YES" |
| 223 required:YES], |
| 224 ]; |
| 223 } | 225 } |
| 224 | 226 |
| 225 @end | 227 @end |
| OLD | NEW |