Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extending STPCardFormView with CardScanner functionality #4103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions Example/UI Examples/UI Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 55;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -14,6 +14,7 @@
3B902170875FA481C136290D /* StripePayments.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D7D4CC026D81D7E26EDA474B /* StripePayments.framework */; };
3BCB2193E00E9B8766E93AC9 /* Stripe.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 753BA90BA35AA6FBDAA07FBE /* Stripe.framework */; };
3D7A0D5F4391825F36AA42E7 /* StripeCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F16D8E3EDF1E3CCBF5FE6F36 /* StripeCore.framework */; };
5216B23B2CAEBB57007F271A /* CardFormScannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5216B23A2CAEBB57007F271A /* CardFormScannerViewController.swift */; };
546290D05670988D27885299 /* StripeUICore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = A7973A50BA0AE6BE3911FCE8 /* StripeUICore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
566876172C33FCC7ACB66754 /* MockCustomerContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 931AF426917A2801C4205F00 /* MockCustomerContext.swift */; };
5B4F3271CE029F9AAB2ABC7F /* StripePayments.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D7D4CC026D81D7E26EDA474B /* StripePayments.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -65,6 +66,7 @@
33045CD268B7E20221AEF345 /* UIExamples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UIExamples.app; sourceTree = BUILT_PRODUCTS_DIR; };
3E29FB4EC02BA5C29D8A7453 /* Project-Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Project-Debug.xcconfig"; sourceTree = "<group>"; };
460B319B13534E7889F7DADE /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = "<group>"; };
5216B23A2CAEBB57007F271A /* CardFormScannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardFormScannerViewController.swift; sourceTree = "<group>"; };
592779C24B55133469B657A2 /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/Localizable.strings; sourceTree = "<group>"; };
5C2F92F71B779415739E02D5 /* SwiftUICardFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUICardFormView.swift; sourceTree = "<group>"; };
69382A94399A814B97E6BF13 /* BrowseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowseViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -143,6 +145,7 @@
69382A94399A814B97E6BF13 /* BrowseViewController.swift */,
1294FDC8AE5C19800151F829 /* CardFieldViewController.swift */,
D0EE0C28786C46F7AD5678C4 /* CardFormViewController.swift */,
5216B23A2CAEBB57007F271A /* CardFormScannerViewController.swift */,
931AF426917A2801C4205F00 /* MockCustomerContext.swift */,
EFEAD84E710812643F204F1E /* PaymentMethodMessagingViewController.swift */,
5C2F92F71B779415739E02D5 /* SwiftUICardFormView.swift */,
Expand Down Expand Up @@ -240,8 +243,6 @@
60F7610314405547AB6416E8 /* Project object */ = {
isa = PBXProject;
attributes = {
TargetAttributes = {
};
};
buildConfigurationList = 6E231E366F944087F8DD657F /* Build configuration list for PBXProject "UI Examples" */;
compatibilityVersion = "Xcode 13.0";
Expand Down Expand Up @@ -288,6 +289,7 @@
buildActionMask = 2147483647;
files = (
B0542D0E99812D6D2F595CAF /* AUBECSDebitFormViewController.swift in Sources */,
5216B23B2CAEBB57007F271A /* CardFormScannerViewController.swift in Sources */,
B881DC1D937D301C7B35B86C /* AppDelegate.swift in Sources */,
A61E99A31C25F9EE54CB3465 /* BrowseViewController.swift in Sources */,
7251058865ACA98C98273875 /* CardFieldViewController.swift in Sources */,
Expand Down
60 changes: 34 additions & 26 deletions Example/UI Examples/UI Examples/Source/BrowseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,41 @@ class BrowseViewController: UITableViewController, STPAddCardViewControllerDeleg
case STPCardFormViewController
case STPCardFormViewControllerCBC
case SwiftUICardFormViewController
case STPCardFormScannerViewController
case PaymentMethodMessagingView
case ChangeTheme

var title: String {
switch self {
case .STPPaymentCardTextField: return "Card Field"
case .STPPaymentCardTextFieldWithCBC: return "Card Field (CBC)"
case .STPPaymentOptionsViewController: return "Payment Option Picker"
case .STPPaymentOptionsFPXViewController: return "Payment Option Picker (With FPX)"
case .STPShippingInfoViewController: return "Shipping Info Form"
case .STPAUBECSFormViewController: return "AU BECS Form"
case .STPCardFormViewController: return "Card Form"
case .STPCardFormViewControllerCBC: return "Card Form (CBC)"
case .SwiftUICardFormViewController: return "Card Form (SwiftUI)"
case .PaymentMethodMessagingView: return "Payment Method Messaging View"
case .ChangeTheme: return "Change Theme"
case .STPPaymentCardTextField: "Card Field"
case .STPPaymentCardTextFieldWithCBC: "Card Field (CBC)"
case .STPPaymentOptionsViewController: "Payment Option Picker"
case .STPPaymentOptionsFPXViewController: "Payment Option Picker (With FPX)"
case .STPShippingInfoViewController: "Shipping Info Form"
case .STPAUBECSFormViewController: "AU BECS Form"
case .STPCardFormViewController: "Card Form"
case .STPCardFormViewControllerCBC: "Card Form (CBC)"
case .SwiftUICardFormViewController: "Card Form (SwiftUI)"
case .STPCardFormScannerViewController: "Card Form Scanner"
case .PaymentMethodMessagingView: "Payment Method Messaging View"
case .ChangeTheme: "Change Theme"
}
}

var detail: String {
switch self {
case .STPPaymentCardTextField: return "STPPaymentCardTextField"
case .STPPaymentCardTextFieldWithCBC: return "STPPaymentCardTextField"
case .STPPaymentOptionsViewController: return "STPPaymentOptionsViewController"
case .STPPaymentOptionsFPXViewController: return "STPPaymentOptionsViewController"
case .STPShippingInfoViewController: return "STPShippingInfoViewController"
case .STPAUBECSFormViewController: return "STPAUBECSFormViewController"
case .STPCardFormViewController: return "STPCardFormViewController"
case .STPCardFormViewControllerCBC: return "STPCardFormViewController (CBC)"
case .SwiftUICardFormViewController: return "STPCardFormView.Representable"
case .PaymentMethodMessagingView: return "PaymentMethodMessagingView"
case .ChangeTheme: return ""
case .STPPaymentCardTextField: "STPPaymentCardTextField"
case .STPPaymentCardTextFieldWithCBC: "STPPaymentCardTextField"
case .STPPaymentOptionsViewController: "STPPaymentOptionsViewController"
case .STPPaymentOptionsFPXViewController: "STPPaymentOptionsViewController"
case .STPShippingInfoViewController: "STPShippingInfoViewController"
case .STPAUBECSFormViewController: "STPAUBECSFormViewController"
case .STPCardFormViewController: "STPCardFormViewController"
case .STPCardFormViewControllerCBC: "STPCardFormViewController (CBC)"
case .SwiftUICardFormViewController: "STPCardFormView.Representable"
case .STPCardFormScannerViewController: "STPCardFormScannerViewController"
case .PaymentMethodMessagingView: "PaymentMethodMessagingView"
case .ChangeTheme: ""
}
}
}
Expand Down Expand Up @@ -179,17 +182,22 @@ class BrowseViewController: UITableViewController, STPAddCardViewControllerDeleg
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .ChangeTheme:
let navigationController = UINavigationController(
rootViewController: self.themeViewController)
present(navigationController, animated: true, completion: nil)
case .SwiftUICardFormViewController:
let controller = UIHostingController(rootView: SwiftUICardFormView())
present(controller, animated: true, completion: nil)
case .STPCardFormScannerViewController:
let viewController = CardFormScannerViewController()
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.stp_theme = theme
present(navigationController, animated: true, completion: nil)
case .PaymentMethodMessagingView:
let vc = PaymentMethodMessagingViewController()
let navigationController = UINavigationController(rootViewController: vc)
present(navigationController, animated: true, completion: nil)
case .ChangeTheme:
let navigationController = UINavigationController(
rootViewController: self.themeViewController)
present(navigationController, animated: true, completion: nil)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// CardFormScannerViewController.swift
// UI Examples
//
// Copyright © 2024 Stripe. All rights reserved.
//

import Stripe
@_spi(STP) import StripePaymentsUI
@_spi(STP) import StripeUICore
import UIKit

class CardFormScannerViewController: UIViewController {
var alwaysEnableCBC: Bool = false

override func viewDidLoad() {
super.viewDidLoad()
title = "Card Form Scanner"
view.backgroundColor = .systemBackground

let handler: (Error?) -> () = { [weak self] error in
guard let error else { return }
let alertController = UIAlertController(
title: error.localizedDescription,
message: (error as NSError).localizedFailureReason,
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(
title: String.Localized.ok,
style: .cancel,
handler: nil
)
)
self?.present(alertController, animated: true)
}

let cardForm = STPCardFormScannerView(style: .standard, handleError: handler)
cardForm.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(cardForm)

NSLayoutConstraint.activate([
cardForm.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 4),
view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: cardForm.trailingAnchor, multiplier: 4),
cardForm.centerXAnchor.constraint(equalTo: view.centerXAnchor),

cardForm.topAnchor.constraint(greaterThanOrEqualToSystemSpacingBelow: view.safeAreaLayoutGuide.topAnchor, multiplier: 1),
view.safeAreaLayoutGuide.bottomAnchor.constraint(greaterThanOrEqualToSystemSpacingBelow: cardForm.bottomAnchor, multiplier: 1),
cardForm.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])

navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done, target: self, action: #selector(done))
}

@objc func done() {
dismiss(animated: true, completion: nil)
}
}
Loading