Skip to content

Commit 9126807

Browse files
Fix /initialize_client response (stripe#2084)
* fix init client expected card * remove unused code * update test and example app
1 parent ad3a44d commit 9126807

File tree

10 files changed

+77
-45
lines changed

10 files changed

+77
-45
lines changed

Example/CardImageVerification Example/CardImageVerification Example/View Controllers/VerificationCardInputViewController.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ extension VerificationCardInputViewController: UITextFieldDelegate {
6565

6666
@objc
6767
func textDidChange() {
68-
if iinTextField.text!.count == 6 && lastFourTextField.text!.count == 4 {
69-
continueButton.updateButtonState(isLoading: false)
68+
if lastFourTextField.text!.count == 4 {
7069
view.endEditing(true)
71-
} else {
72-
continueButton.updateButtonState(isLoading: true)
7370
}
7471
}
7572

Example/CardImageVerification Example/CardImageVerification Example/View Controllers/VerificationExplanationViewController.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ private extension VerificationExplanationViewController {
8181
/// Disable button until card image verification sheet is set
8282
updateButtonState(isLoading: true)
8383

84-
let requestJson = [
85-
"expected_card[iin]": expectedCardViewModel?.iin ?? "424242",
86-
"expected_card[last4]": expectedCardViewModel?.last4 ?? "4242"
87-
]
84+
/// Add expected card fields when available
85+
var requestJson: [String: Any] = [:]
86+
if let last4 = expectedCardViewModel?.last4, !last4.isEmpty {
87+
requestJson["expected_card[last4]"] = last4
88+
}
8889

90+
if let iin = expectedCardViewModel?.iin, !iin.isEmpty {
91+
requestJson["expected_card[iin]"] = iin
92+
}
93+
8994
/// Make request to our verification endpoint
9095
APIClient.jsonRequest(
9196
url: URLHelper.cardSet.verifyURL,

StripeCardScan/StripeCardScan/Source/CardVerify/Api/CardImageVerificationDetailsResponse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Foundation
99
@_spi(STP) import StripeCore
1010

1111
struct CardImageVerificationExpectedCard: Decodable {
12-
let last4: String
13-
let issuer: String
12+
let last4: String?
13+
let issuer: String?
1414
}
1515

1616
struct CardImageVerificationImageSettings: Decodable {
@@ -42,7 +42,7 @@ struct CardImageVerificationAcceptedImageConfigs: Decodable {
4242
}
4343

4444
struct CardImageVerificationDetailsResponse: Decodable {
45-
let expectedCard: CardImageVerificationExpectedCard?
45+
let expectedCard: CardImageVerificationExpectedCard
4646
let acceptedImageConfigs: CardImageVerificationAcceptedImageConfigs?
4747
}
4848

StripeCardScan/StripeCardScan/Source/CardVerify/Api/STPAPIClient+CardImageVerification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension STPAPIClient {
1313
func fetchCardImageVerificationDetails(
1414
cardImageVerificationSecret: String,
1515
cardImageVerificationId: String
16-
) -> Promise<CardImageVerificationDetailsResponse?> {
16+
) -> Promise<CardImageVerificationDetailsResponse> {
1717
let parameters: [String: Any] = ["client_secret": cardImageVerificationSecret]
1818
let endpoint = APIEndpoints.fetchCardImageVerificationDetails(id: cardImageVerificationId)
1919
return self.post(resource: endpoint, parameters: parameters)

StripeCardScan/StripeCardScan/Source/CardVerify/Card Image Verification/CardImageVerificationController.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CardImageVerificationController {
3131
}
3232

3333
func present(
34-
with expectedCard: CardImageVerificationExpectedCard?,
34+
with expectedCard: CardImageVerificationExpectedCard,
3535
and acceptedImageConfigs: CardImageVerificationAcceptedImageConfigs?,
3636
from presentingViewController: UIViewController,
3737
animated: Bool = true
@@ -50,12 +50,13 @@ class CardImageVerificationController {
5050
}
5151

5252
// TODO(jaimepark): Create controller that has configurable view and handles coordination / business logic
53-
if let expectedCard = expectedCard {
53+
if let expectedCardLast4 = expectedCard.last4 {
5454
/// Create the view controller for card-set-verification with expected card's last4 and issuer
5555
let vc = VerifyCardViewController(
56-
expectedCard: expectedCard,
5756
acceptedImageConfigs: acceptedImageConfigs,
58-
configuration: configuration
57+
configuration: configuration,
58+
expectedCardLast4: expectedCardLast4,
59+
expectedCardIssuer: expectedCard.issuer
5960
)
6061
vc.verifyDelegate = self
6162
presentingViewController.present(vc, animated: animated)

StripeCardScan/StripeCardScan/Source/CardVerify/Card Image Verification/CardImageVerificationSheet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ final public class CardImageVerificationSheet {
7777

7878
/// Present the verify view controller
7979
cardImageVerificationController.present(
80-
with: response?.expectedCard,
81-
and: response?.acceptedImageConfigs,
80+
with: response.expectedCard,
81+
and: response.acceptedImageConfigs,
8282
from: presentingViewController,
8383
animated: animated
8484
)
@@ -102,7 +102,7 @@ extension CardImageVerificationSheet {
102102
fileprivate func load(
103103
civId: String,
104104
civSecret: String,
105-
completion: @escaping ((Result<CardImageVerificationDetailsResponse?, Error>) -> Void)
105+
completion: @escaping ((Result<CardImageVerificationDetailsResponse, Error>) -> Void)
106106
) {
107107
/// Clear the scan analytics manager since it is the beginning of a new session
108108
ScanAnalyticsManager.shared.reset()

StripeCardScan/StripeCardScan/Source/CardVerify/VerifyCardViewController.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class VerifyCardViewController: SimpleScanViewController {
4242
static var torchButton: UIButton?
4343

4444
// configuration
45-
private let expectedCard: CardImageVerificationExpectedCard
45+
private let expectedCardLast4: String
46+
private let expectedCardIssuer: String?
4647
private let acceptedImageConfigs: CardImageVerificationAcceptedImageConfigs?
4748
private let configuration: CardImageVerificationSheet.Configuration
4849

@@ -63,13 +64,15 @@ class VerifyCardViewController: SimpleScanViewController {
6364
var userId: String?
6465

6566
init(
66-
expectedCard: CardImageVerificationExpectedCard,
6767
acceptedImageConfigs: CardImageVerificationAcceptedImageConfigs?,
68-
configuration: CardImageVerificationSheet.Configuration
68+
configuration: CardImageVerificationSheet.Configuration,
69+
expectedCardLast4: String,
70+
expectedCardIssuer: String?
6971
) {
70-
self.expectedCard = expectedCard
7172
self.acceptedImageConfigs = acceptedImageConfigs
7273
self.configuration = configuration
74+
self.expectedCardLast4 = expectedCardLast4
75+
self.expectedCardIssuer = expectedCardIssuer
7376
self.mainLoopDurationTask = TrackableTask()
7477

7578
super.init()
@@ -80,7 +83,7 @@ class VerifyCardViewController: SimpleScanViewController {
8083
override func viewDidLoad() {
8184
// setup our ML so that we use the UX model + OCR in the main loop
8285
let fraudData = CardVerifyFraudData(
83-
last4: expectedCard.last4,
86+
last4: expectedCardLast4,
8487
acceptedImageConfigs: acceptedImageConfigs
8588
)
8689
if debugRetainCompletionLoopImages {
@@ -95,7 +98,7 @@ class VerifyCardViewController: SimpleScanViewController {
9598
override func createOcrMainLoop() -> OcrMainLoop? {
9699
var uxAndOcrMainLoop = UxAndOcrMainLoop(
97100
stateMachine: CardVerifyStateMachine(
98-
requiredLastFour: expectedCard.last4,
101+
requiredLastFour: expectedCardLast4,
99102
requiredBin: nil,
100103
strictModeFramesCount: configuration.strictModeFrames
101104
)
@@ -104,7 +107,7 @@ class VerifyCardViewController: SimpleScanViewController {
104107
if #available(iOS 13.0, *), scanPerformancePriority == .accurate {
105108
uxAndOcrMainLoop = UxAndOcrMainLoop(
106109
stateMachine: CardVerifyAccurateStateMachine(
107-
requiredLastFour: expectedCard.last4,
110+
requiredLastFour: expectedCardLast4,
108111
requiredBin: nil,
109112
maxNameExpiryDurationSeconds: maxErrorCorrectionDuration,
110113
strictModeFramesCount: configuration.strictModeFrames
@@ -140,19 +143,14 @@ class VerifyCardViewController: SimpleScanViewController {
140143
///TODO(jaimepark): Update text ui with viewmodel
141144
//let network = bin.map { CreditCardUtils.determineCardNetwork(cardNumber: $0) }
142145
//var text = "\(network.map { $0.toString() } ?? cardNetwork?.toString() ?? "")"
143-
var text = ""
144-
text.append(contentsOf: " •••• \(expectedCard.last4)")
146+
let text = "\(expectedCardIssuer ?? "") •••• \(expectedCardLast4)"
145147

146148
cardDescriptionText.textColor = .white
147149
cardDescriptionText.textAlignment = .center
148150
cardDescriptionText.numberOfLines = 2
151+
cardDescriptionText.text = text
152+
cardDescriptionText.isHidden = false
149153

150-
if !text.isEmpty {
151-
cardDescriptionText.text = text
152-
cardDescriptionText.isHidden = false
153-
} else {
154-
cardDescriptionText.isHidden = true
155-
}
156154
}
157155

158156
// MARK: -Autolayout constraints
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
{
2+
"accepted_image_configs": {
3+
"default_settings": {
4+
"compression_ratio": 0.6,
5+
"image_count": 5,
6+
"image_size": [
7+
1080,
8+
1920
9+
]
10+
},
11+
"format_settings": {
12+
"heic": {
13+
"compression_ratio": 0.5
14+
},
15+
"jpeg": {},
16+
"webp": {
17+
"image_count": 3
18+
}
19+
},
20+
"preferred_formats": [
21+
"heic",
22+
"webp",
23+
"jpeg"
24+
]
25+
},
26+
"expected_card": {
27+
"issuer": null,
28+
"last4": null
29+
},
30+
"use_api_version": 2
231
}

StripeCardScan/StripeCardScanTests/Unit/API Bindings/STPAPIClient+CardImageVerificationTest.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,26 @@ class STPAPIClient_CardImageVerificationTest: APIStubbedTestCase {
6060
promise.observe { result in
6161
switch result {
6262
case .success(let response):
63-
XCTAssertEqual(response?.expectedCard?.last4, "4242")
64-
XCTAssertEqual(response?.expectedCard?.issuer, "Visa")
63+
XCTAssertEqual(response.expectedCard.last4, "4242")
64+
XCTAssertEqual(response.expectedCard.issuer, "Visa")
6565

66-
XCTAssertNotNil(response?.acceptedImageConfigs)
66+
XCTAssertNotNil(response.acceptedImageConfigs)
6767

6868
XCTAssertEqual(
69-
response?.acceptedImageConfigs?.preferredFormats,
69+
response.acceptedImageConfigs?.preferredFormats,
7070
[.heic, .webp, .jpeg]
7171
)
7272

7373
XCTAssertEqual(
74-
response?.acceptedImageConfigs?.defaultSettings?.compressionRatio,
74+
response.acceptedImageConfigs?.defaultSettings?.compressionRatio,
7575
0.8
7676
)
7777
XCTAssertEqual(
78-
response?.acceptedImageConfigs?.defaultSettings?.imageSize,
78+
response.acceptedImageConfigs?.defaultSettings?.imageSize,
7979
[1080.0, 1920.0]
8080
)
8181

82-
if let formatSettings = response?.acceptedImageConfigs?.formatSettings {
82+
if let formatSettings = response.acceptedImageConfigs?.formatSettings {
8383
let webpSettings = formatSettings[.webp]
8484
XCTAssertEqual(webpSettings??.compressionRatio, 0.7)
8585
XCTAssertEqual(webpSettings??.imageSize, [2160.0, 1920.0])
@@ -145,7 +145,8 @@ class STPAPIClient_CardImageVerificationTest: APIStubbedTestCase {
145145
promise.observe { result in
146146
switch result {
147147
case .success(let response):
148-
XCTAssertNil(response?.expectedCard)
148+
XCTAssertNil(response.expectedCard.last4)
149+
XCTAssertNil(response.expectedCard.issuer)
149150
case .failure(let error):
150151
XCTFail("Request returned error \(error)")
151152
}

StripeCardScan/StripeCardScanTests/Unit/CardImageVerificationControllerTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import XCTest
1414
@testable import StripeCardScan
1515

1616
class CardImageVerificationControllerTests: APIStubbedTestCase {
17+
private let expectedCard = CardImageVerificationExpectedCard(last4: "1234", issuer: nil)
1718
private var result: CardImageVerificationSheetResult?
1819
private var resultExp: XCTestExpectation!
1920
private var verifyFramesRequestExp: XCTestExpectation!
@@ -54,7 +55,7 @@ class CardImageVerificationControllerTests: APIStubbedTestCase {
5455
stubUploadScanStats()
5556

5657
/// Invoke a `VerifyCardAddViewController` being created by not passing an expected card
57-
verificationSheetController.present(with: nil, and: nil, from: baseViewController)
58+
verificationSheetController.present(with: expectedCard, and: nil, from: baseViewController)
5859
verificationSheetController.verifyViewControllerDidCancel(
5960
baseViewController,
6061
with: .back
@@ -73,7 +74,7 @@ class CardImageVerificationControllerTests: APIStubbedTestCase {
7374
stubUploadScanStats()
7475

7576
/// Invoke a `VerifyCardAddViewController` being created by not passing an expected card
76-
verificationSheetController.present(with: nil, and: nil, from: baseViewController)
77+
verificationSheetController.present(with: expectedCard, and: nil, from: baseViewController)
7778
verificationSheetController.verifyViewControllerDidCancel(
7879
baseViewController,
7980
with: .closed
@@ -93,7 +94,7 @@ class CardImageVerificationControllerTests: APIStubbedTestCase {
9394
stubUploadScanStats()
9495

9596
/// Invoke a `VerifyCardAddViewController` being created by not passing an expected card
96-
verificationSheetController.present(with: nil, and: nil, from: baseViewController)
97+
verificationSheetController.present(with: expectedCard, and: nil, from: baseViewController)
9798

9899
/// Mock the event where the scanning is complete and the verification frames data is passed back to be submitted for completion
99100
verificationSheetController.verifyViewControllerDidFinish(

0 commit comments

Comments
 (0)