Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions Stripe/StripeiOSTests/STPCardFormViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,11 @@ class STPCardFormViewTests: XCTestCase {
}
}

func testHidingPostalUPECodeOnInit() {
NSLocale.stp_withLocale(as: NSLocale(localeIdentifier: "zh_Hans_HK")) {
let cardForm = STPCardFormView(
billingAddressCollection: .automatic,
style: .standard,
postalCodeRequirement: .upe,
prefillDetails: nil
)
XCTAssertTrue(cardForm.postalCodeField.isHidden)
}
}

func testNotHidingPostalUPECodeOnInit() {
NSLocale.stp_withLocale(as: NSLocale(localeIdentifier: "en_US")) {
let cardForm = STPCardFormView(
billingAddressCollection: .automatic,
style: .standard,
postalCodeRequirement: .upe,
prefillDetails: nil
)
XCTAssertFalse(cardForm.postalCodeField.isHidden)
}
}

func testPanLockedOnInit() {
NSLocale.stp_withLocale(as: NSLocale(localeIdentifier: "en_US")) {
let cardForm = STPCardFormView(
billingAddressCollection: .automatic,
style: .standard,
postalCodeRequirement: .upe,
prefillDetails: nil,
inputMode: .panLocked
)
Expand All @@ -127,7 +102,6 @@ class STPCardFormViewTests: XCTestCase {
let cardForm = STPCardFormView(
billingAddressCollection: .automatic,
style: .standard,
postalCodeRequirement: .upe,
prefillDetails: prefillDeatils,
inputMode: .panLocked
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import StripeCoreTestUtils
class STPPostalCodeInputTextFieldSnapshotTests: STPSnapshotTestCase {

func testEmpty() {
let field = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let field = STPPostalCodeInputTextField()
field.sizeToFit()
field.frame.size.width = 200

STPSnapshotVerifyView(field)
}

func testIncomplete() {
let field = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let field = STPPostalCodeInputTextField()
field.sizeToFit()
field.frame.size.width = 200
field.countryCode = "US"
Expand All @@ -37,7 +37,7 @@ class STPPostalCodeInputTextFieldSnapshotTests: STPSnapshotTestCase {
}

func testValidUS() {
let field = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let field = STPPostalCodeInputTextField()
field.sizeToFit()
field.frame.size.width = 200
field.countryCode = "US"
Expand All @@ -48,7 +48,7 @@ class STPPostalCodeInputTextFieldSnapshotTests: STPSnapshotTestCase {
}

func testValidUK() {
let field = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let field = STPPostalCodeInputTextField()
field.sizeToFit()
field.frame.size.width = 200
field.countryCode = "UK"
Expand All @@ -59,7 +59,7 @@ class STPPostalCodeInputTextFieldSnapshotTests: STPSnapshotTestCase {
}

func testInvalid() {
let field = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let field = STPPostalCodeInputTextField()
field.sizeToFit()
field.frame.size.width = 200
field.countryCode = "US"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import XCTest
class STPPostalCodeInputTextFieldTests: XCTestCase {

func testClearingInvalidPostalCodeAfterCountryChange() {
let postalCodeField = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let postalCodeField = STPPostalCodeInputTextField()
postalCodeField.countryCode = "UK"
postalCodeField.text = "DL12" // valid UK post code, invalid US ZIP Code

Expand All @@ -32,7 +32,7 @@ class STPPostalCodeInputTextFieldTests: XCTestCase {
}

func testPreservingValidPostalCodeAfterCountryChange() {
let postalCodeField = STPPostalCodeInputTextField(postalCodeRequirement: .standard)
let postalCodeField = STPPostalCodeInputTextField()
postalCodeField.countryCode = "US"
postalCodeField.text = "10010" // valid US and HR ZIP/postal code

Expand All @@ -47,7 +47,7 @@ class STPPostalCodeInputTextFieldTests: XCTestCase {
}

func testChangeToNonRequiredPostalCodeIsValid() {
let postalCodeField = STPPostalCodeInputTextField(postalCodeRequirement: .upe)
let postalCodeField = STPPostalCodeInputTextField()
// given that the postal code field is empty...

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import XCTest
class STPPostalCodeInputTextFieldValidatorTests: XCTestCase {

func testValidation() {
let validator = STPPostalCodeInputTextFieldValidator(postalCodeRequirement: .standard)
let validator = STPPostalCodeInputTextFieldValidator()
validator.countryCode = "US"

validator.inputValue = nil
Expand Down
24 changes: 0 additions & 24 deletions Stripe/StripeiOSTests/STPPostalCodeValidatorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,4 @@ class STPPostalCodeValidatorTest: XCTestCase {
)
}
}

func testPostalCodeIsRequiredForUPE_nil() {
XCTAssertFalse(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: nil))
}

func testPostalCodeIsRequiredForUPE_empty() {
XCTAssertFalse(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: ""))
}

func testPostalCodeIsRequiredForUPE_CA() {
XCTAssertTrue(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: "CA"))
}

func testPostalCodeIsRequiredForUPE_GB() {
XCTAssertTrue(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: "GB"))
}

func testPostalCodeIsRequiredForUPE_US() {
XCTAssertTrue(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: "CA"))
}

func testPostalCodeIsRequiredForUPE_DK() {
XCTAssertFalse(STPPostalCodeValidator.postalCodeIsRequiredForUPE(forCountryCode: "DK"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import Foundation
case cardField
}

@objc @_spi(STP) public enum STPPostalCodeRequirement: Int {
case standard
case upe
}

@_spi(STP) public class STPPostalCodeValidator: NSObject {
@_spi(STP) public class func postalCodeIsRequired(forCountryCode countryCode: String?) -> Bool {
if countryCode == nil {
Expand All @@ -33,30 +28,12 @@ import Foundation
}
}

class func postalCodeIsRequiredForUPE(forCountryCode countryCode: String?) -> Bool {
guard let countryCode = countryCode else { return false }
return self.countriesWithPostalRequiredForUPE().contains(countryCode.uppercased())
}

class func postalCodeIsRequired(
forCountryCode countryCode: String?,
with postalRequirement: STPPostalCodeRequirement
) -> Bool {
switch postalRequirement {
case .standard:
return postalCodeIsRequired(forCountryCode: countryCode)
case .upe:
return postalCodeIsRequiredForUPE(forCountryCode: countryCode)
}
}

@_spi(STP) public class func validationState(
forPostalCode postalCode: String?,
countryCode: String?,
with postalRequirement: STPPostalCodeRequirement = .standard
countryCode: String?
) -> STPCardValidationState {
let sanitizedCountryCode = countryCode?.uppercased()
if self.postalCodeIsRequired(forCountryCode: countryCode, with: postalRequirement) {
if self.postalCodeIsRequired(forCountryCode: countryCode) {
if sanitizedCountryCode == STPCountryCodeUnitedStates {
return self.validationState(forUSPostalCode: postalCode)
} else {
Expand Down Expand Up @@ -215,10 +192,6 @@ import Foundation
return formattedString
}

class func countriesWithPostalRequiredForUPE() -> [AnyHashable] {
return ["CA", "GB", "US"]
}

class func countriesWithNoPostalCodes() -> [AnyHashable]? {
return [
"AE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import UIKit
return validator.inputValue
}

public convenience init(
postalCodeRequirement: STPPostalCodeRequirement
) {
public convenience init() {
self.init(
formatter: STPPostalCodeInputTextFieldFormatter(),
validator: STPPostalCodeInputTextFieldValidator(
postalCodeRequirement: postalCodeRequirement
)
validator: STPPostalCodeInputTextFieldValidator()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ class STPPostalCodeInputTextFieldValidator: STPInputTextFieldValidator {
}
}

let postalCodeRequirement: STPPostalCodeRequirement

required init(
postalCodeRequirement: STPPostalCodeRequirement
) {
self.postalCodeRequirement = postalCodeRequirement
super.init()
}

private func updateValidationState() {

switch STPPostalCodeValidator.validationState(
forPostalCode: inputValue,
countryCode: countryCode,
with: postalCodeRequirement
countryCode: countryCode
)
{
case .valid:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class STPCardFormView: STPFormView {
let expiryField: STPCardExpiryInputTextField

let billingAddressSubForm: BillingAddressSubForm
let postalCodeRequirement: STPPostalCodeRequirement
let inputMode: STPCardNumberInputTextField.InputMode

@_spi(STP) public var countryField: STPCountryPickerInputField {
Expand All @@ -78,8 +77,7 @@ public class STPCardFormView: STPFormView {
set(
textField: postalCodeField,
isHidden: !STPPostalCodeValidator.postalCodeIsRequired(
forCountryCode: countryCode,
with: postalCodeRequirement
forCountryCode: countryCode
),
animated: window != nil
)
Expand Down Expand Up @@ -313,7 +311,6 @@ public class STPCardFormView: STPFormView {
@_spi(STP) public convenience init(
billingAddressCollection: BillingAddressCollectionLevel,
style: STPCardFormViewStyle = .standard,
postalCodeRequirement: STPPostalCodeRequirement = .standard,
prefillDetails: PrefillDetails? = nil,
inputMode: STPCardNumberInputTextField.InputMode = .standard,
cbcEnabledOverride: Bool? = nil
Expand All @@ -327,11 +324,9 @@ public class STPCardFormView: STPFormView {
cvcField: STPCardCVCInputTextField(prefillDetails: prefillDetails),
expiryField: STPCardExpiryInputTextField(prefillDetails: prefillDetails),
billingAddressSubForm: BillingAddressSubForm(
billingAddressCollection: billingAddressCollection,
postalCodeRequirement: postalCodeRequirement
billingAddressCollection: billingAddressCollection
),
style: style,
postalCodeRequirement: postalCodeRequirement,
prefillDetails: prefillDetails,
inputMode: inputMode
)
Expand All @@ -343,7 +338,6 @@ public class STPCardFormView: STPFormView {
expiryField: STPCardExpiryInputTextField,
billingAddressSubForm: BillingAddressSubForm,
style: STPCardFormViewStyle = .standard,
postalCodeRequirement: STPPostalCodeRequirement = .standard,
prefillDetails: PrefillDetails? = nil,
inputMode: STPCardNumberInputTextField.InputMode = .standard
) {
Expand All @@ -352,7 +346,6 @@ public class STPCardFormView: STPFormView {
self.expiryField = expiryField
self.billingAddressSubForm = billingAddressSubForm
self.style = style
self.postalCodeRequirement = postalCodeRequirement
self.inputMode = inputMode

if inputMode == .panLocked {
Expand Down Expand Up @@ -473,8 +466,7 @@ public class STPCardFormView: STPFormView {
let shouldFocusOnPostalCode =
countryChanged
&& STPPostalCodeValidator.postalCodeIsRequired(
forCountryCode: countryCode,
with: postalCodeRequirement
forCountryCode: countryCode
)

if shouldFocusOnPostalCode {
Expand Down Expand Up @@ -714,12 +706,9 @@ extension STPCardFormView {
}

required init(
billingAddressCollection: BillingAddressCollectionLevel,
postalCodeRequirement: STPPostalCodeRequirement
billingAddressCollection: BillingAddressCollectionLevel
) {
postalCodeField = STPPostalCodeInputTextField(
postalCodeRequirement: postalCodeRequirement
)
postalCodeField = STPPostalCodeInputTextField()

let rows: [[STPInputTextField]]
let title: String
Expand Down
Loading