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

Convert BraintreeDemoCardTokenizationViewController to Swift #1095

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import UIKit
import BraintreeCard

class BraintreeDemoCardTokenizationViewController: BraintreeDemoPaymentButtonBaseViewController {

let cardFormView = BTCardFormView()
let autofillButton = UIButton(type: .system)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 7d02e20


override func viewDidLoad() {
super.viewDidLoad()
createSubviews()
layoutConstraints()
}

override func createPaymentButton() -> UIView! {
let submitButton = UIButton(type: .system)
submitButton.setTitle("Submit", for: .normal)
submitButton.setTitleColor(.blue, for: .normal)
submitButton.setTitleColor(.lightGray, for: .highlighted)
submitButton.setTitleColor(.lightGray, for: .disabled)
submitButton.addTarget(self, action: #selector(tappedSubmit), for: .touchUpInside)
submitButton.translatesAutoresizingMaskIntoConstraints = false

return submitButton
}

@objc func tappedSubmit() {
progressBlock("Tokenizing card details!")

let cardClient = BTCardClient(apiClient: apiClient)
let card = newCard()

setFieldsEnabled(false)
cardClient.tokenize(card) { nonce, error in
self.setFieldsEnabled(true)

guard let nonce else {
self.progressBlock(error?.localizedDescription)
return
}

self.completionBlock(nonce)
}
}

@objc func tappedAutofill() {
cardFormView.cardNumberTextField.text = "4111111111111111"
cardFormView.cvvTextField.text = "123"
cardFormView.expirationTextField.text = generateFutureDate()
}

private func newCard() -> BTCard {
let card = BTCard()

if let cardNumber = cardFormView.cardNumber {
card.number = cardNumber
}

if let expirationYear = cardFormView.expirationYear {
card.expirationYear = expirationYear
}

if let expirationMonth = cardFormView.expirationMonth {
card.expirationMonth = expirationMonth
}

if let cvv = cardFormView.cvv {
card.cvv = cvv
}

return card
}

private func setFieldsEnabled(_ isEnabled: Bool) {
cardFormView.cardNumberTextField.isEnabled = isEnabled
cardFormView.expirationTextField.isEnabled = isEnabled
cardFormView.cvvTextField.isEnabled = isEnabled
autofillButton.isEnabled = isEnabled
}

private func generateFutureDate() -> String {
let monthString = "12"

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yy"

let futureYear = Calendar.current.date(byAdding: .year, value: 3, to: Date())!
let yearString = dateFormatter.string(from: futureYear)

return "\(monthString)/\(yearString)"
}

private func createSubviews() {
cardFormView.translatesAutoresizingMaskIntoConstraints = false
cardFormView.hidePhoneNumberField = true
cardFormView.hidePostalCodeField = true
setFieldsEnabled(true)

autofillButton.setTitle("Autofill", for: .normal)
autofillButton.setTitleColor(.blue, for: .normal)
autofillButton.addTarget(self, action: #selector(tappedAutofill), for: .touchUpInside)
autofillButton.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(cardFormView)
view.addSubview(autofillButton)
}

private func layoutConstraints() {
NSLayoutConstraint.activate([
cardFormView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
cardFormView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
cardFormView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
cardFormView.heightAnchor.constraint(equalToConstant: 200),

autofillButton.topAnchor.constraint(equalTo: cardFormView.bottomAnchor, constant: 10),
autofillButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
autofillButton.heightAnchor.constraint(equalToConstant: 30)
])
}
}
10 changes: 4 additions & 6 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
A0988FE624DB44B20095EEEE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F5824DB44B10095EEEE /* main.m */; };
A0988FE824DB44B20095EEEE /* BraintreeDemoBTDataCollectorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F5D24DB44B10095EEEE /* BraintreeDemoBTDataCollectorViewController.m */; };
A0988FEA24DB44B20095EEEE /* BraintreeDemoIdealViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F6424DB44B10095EEEE /* BraintreeDemoIdealViewController.m */; };
A0988FEE24DB44B20095EEEE /* BraintreeDemoCardTokenizationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F6E24DB44B10095EEEE /* BraintreeDemoCardTokenizationViewController.m */; };
A0988FF024DB44B20095EEEE /* BraintreeDemoApplePayPassKitViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F7424DB44B10095EEEE /* BraintreeDemoApplePayPassKitViewController.m */; };
A0988FF224DB44B20095EEEE /* BraintreeDemoThreeDSecurePaymentFlowViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F7A24DB44B10095EEEE /* BraintreeDemoThreeDSecurePaymentFlowViewController.m */; };
A0988FF424DB44B20095EEEE /* BraintreeDemoAmexViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F7F24DB44B10095EEEE /* BraintreeDemoAmexViewController.m */; };
Expand All @@ -77,6 +76,7 @@
BE9BD75A28872E4D00022983 /* BraintreeSEPADirectDebit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = BE9BD75828872E4D00022983 /* BraintreeSEPADirectDebit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
BEAAAD052970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEAAAD042970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift */; };
BEE2E4BC290043D100C03FDD /* PayPalCheckout in Frameworks */ = {isa = PBXBuildFile; productRef = BEE2E4BB290043D100C03FDD /* PayPalCheckout */; };
BEE9304B2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEE9304A2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -181,8 +181,6 @@
A0988F5E24DB44B10095EEEE /* BraintreeDemoBTDataCollectorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BraintreeDemoBTDataCollectorViewController.h; sourceTree = "<group>"; };
A0988F6324DB44B10095EEEE /* BraintreeDemoIdealViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BraintreeDemoIdealViewController.h; sourceTree = "<group>"; };
A0988F6424DB44B10095EEEE /* BraintreeDemoIdealViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BraintreeDemoIdealViewController.m; sourceTree = "<group>"; };
A0988F6C24DB44B10095EEEE /* BraintreeDemoCardTokenizationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BraintreeDemoCardTokenizationViewController.h; sourceTree = "<group>"; };
A0988F6E24DB44B10095EEEE /* BraintreeDemoCardTokenizationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BraintreeDemoCardTokenizationViewController.m; sourceTree = "<group>"; };
A0988F7324DB44B10095EEEE /* BraintreeDemoApplePayPassKitViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BraintreeDemoApplePayPassKitViewController.h; sourceTree = "<group>"; };
A0988F7424DB44B10095EEEE /* BraintreeDemoApplePayPassKitViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BraintreeDemoApplePayPassKitViewController.m; sourceTree = "<group>"; };
A0988F7924DB44B10095EEEE /* BraintreeDemoThreeDSecurePaymentFlowViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BraintreeDemoThreeDSecurePaymentFlowViewController.h; sourceTree = "<group>"; };
Expand All @@ -206,6 +204,7 @@
BE777AC327D9370400487D23 /* BraintreeDemoSEPADirectDebitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BraintreeDemoSEPADirectDebitViewController.swift; sourceTree = "<group>"; };
BE9BD75828872E4D00022983 /* BraintreeSEPADirectDebit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = BraintreeSEPADirectDebit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BEAAAD042970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTSEPADirectDebitTestHelper.swift; sourceTree = "<group>"; };
BEE9304A2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BraintreeDemoCardTokenizationViewController.swift; sourceTree = "<group>"; };
C8E5BAD1DA81AAD310B19786 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -436,8 +435,7 @@
A0988F6B24DB44B10095EEEE /* Credit Card Tokenization */ = {
isa = PBXGroup;
children = (
A0988F6C24DB44B10095EEEE /* BraintreeDemoCardTokenizationViewController.h */,
A0988F6E24DB44B10095EEEE /* BraintreeDemoCardTokenizationViewController.m */,
BEE9304A2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift */,
);
path = "Credit Card Tokenization";
sourceTree = "<group>";
Expand Down Expand Up @@ -807,9 +805,9 @@
A0988FF424DB44B20095EEEE /* BraintreeDemoAmexViewController.m in Sources */,
A0988FE624DB44B20095EEEE /* main.m in Sources */,
A0988F9724DB44B20095EEEE /* BraintreeDemoContainmentViewController.m in Sources */,
BEE9304B2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift in Sources */,
A0988FEA24DB44B20095EEEE /* BraintreeDemoIdealViewController.m in Sources */,
BE4F788927EE2B6D00FF4C0E /* BraintreeDemoPayPalVaultViewController.swift in Sources */,
A0988FEE24DB44B20095EEEE /* BraintreeDemoCardTokenizationViewController.m in Sources */,
42D47A152554679D0012BAF1 /* BraintreeDemoSceneDelegate.m in Sources */,
A0988FE824DB44B20095EEEE /* BraintreeDemoBTDataCollectorViewController.m in Sources */,
A0988FF224DB44B20095EEEE /* BraintreeDemoThreeDSecurePaymentFlowViewController.m in Sources */,
Expand Down