Skip to content

Commit

Permalink
Merge branch 'main' into convert-datacollector-viewcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxdesmarais authored Aug 29, 2023
2 parents af8a0e0 + 25774f4 commit 347b74b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 146 deletions.

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 {

private let cardFormView = BTCardFormView()
private let autofillButton = UIButton(type: .system)

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 @@ -55,7 +55,6 @@
A0988FE324DB44B20095EEEE /* Main.strings in Resources */ = {isa = PBXBuildFile; fileRef = A0988F5224DB44B10095EEEE /* Main.strings */; };
A0988FE424DB44B20095EEEE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = A0988F5424DB44B10095EEEE /* InfoPlist.strings */; };
A0988FE624DB44B20095EEEE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A0988F5824DB44B10095EEEE /* main.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 @@ -76,6 +75,7 @@
BEAAAA342A98E5E6001ECA63 /* BraintreeDemoIdealViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEAAAA332A98E5E6001ECA63 /* BraintreeDemoIdealViewController.swift */; };
BEAAAD052970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEAAAD042970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift */; };
BEE930492A98FE9200C85779 /* BraintreeDemoBTDataCollectorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEE930482A98FE9200C85779 /* BraintreeDemoBTDataCollectorViewController.swift */; };
BEE9304B2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEE9304A2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -176,8 +176,6 @@
A0988F5624DB44B10095EEEE /* Braintree-Demo-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Braintree-Demo-Prefix.pch"; sourceTree = "<group>"; };
A0988F5724DB44B10095EEEE /* Braintree-Demo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Braintree-Demo-Info.plist"; sourceTree = "<group>"; };
A0988F5824DB44B10095EEEE /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.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 @@ -203,6 +201,7 @@
BEAAAA332A98E5E6001ECA63 /* BraintreeDemoIdealViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BraintreeDemoIdealViewController.swift; sourceTree = "<group>"; };
BEAAAD042970A70D000BD296 /* BTSEPADirectDebitTestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTSEPADirectDebitTestHelper.swift; sourceTree = "<group>"; };
BEE930482A98FE9200C85779 /* BraintreeDemoBTDataCollectorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BraintreeDemoBTDataCollectorViewController.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 @@ -430,8 +429,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 @@ -800,8 +798,8 @@
A0988FF424DB44B20095EEEE /* BraintreeDemoAmexViewController.m in Sources */,
A0988FE624DB44B20095EEEE /* main.m in Sources */,
A0988F9724DB44B20095EEEE /* BraintreeDemoContainmentViewController.m in Sources */,
BEE9304B2A992F6200C85779 /* BraintreeDemoCardTokenizationViewController.swift in Sources */,
BE4F788927EE2B6D00FF4C0E /* BraintreeDemoPayPalVaultViewController.swift in Sources */,
A0988FEE24DB44B20095EEEE /* BraintreeDemoCardTokenizationViewController.m in Sources */,
42D47A152554679D0012BAF1 /* BraintreeDemoSceneDelegate.m in Sources */,
A0988FF224DB44B20095EEEE /* BraintreeDemoThreeDSecurePaymentFlowViewController.m in Sources */,
A0988F9424DB44B20095EEEE /* BraintreeDemoPaymentButtonBaseViewController.m in Sources */,
Expand Down

0 comments on commit 347b74b

Please sign in to comment.