Skip to content

Commit

Permalink
PR-11233: we can collect card data
Browse files Browse the repository at this point in the history
  • Loading branch information
diki committed Feb 17, 2025
1 parent cc4c027 commit 4426c6d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 22 deletions.
51 changes: 48 additions & 3 deletions Payrails/Classes/Public/Views/Card/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ final class CardCollectContainer: CardContainer {
}


final class CardCollectView: UIStackView {
public class CardCollectView: UIStackView {
private let config: CardFormConfig
private let skyflow: Client
private var container: Container<ComposableContainer>?
private let tableName: String
var cardContainer: CardCollectContainer?

init(
public init(
skyflow: Client,
config: CardFormConfig,
tableName: String
Expand All @@ -44,7 +44,7 @@ final class CardCollectView: UIStackView {
guard let container = skyflow.container(
type: ContainerType.COMPOSABLE,
options: ContainerOptions(
layout: config.showNameField ? [2, 1, 2] : [2, 2],
layout: config.showNameField ? [2, 1, 2] : [1, 1, 2],
errorTextStyles: Styles(base: config.style.errorTextStyle)
)
) else {
Expand Down Expand Up @@ -106,8 +106,20 @@ final class CardCollectView: UIStackView {
placeholder: config.fieldConfig(for: .EXPIRATION_YEAR)?.placeholder ?? "YYYY",
type: .EXPIRATION_YEAR
)
let collectExpDateInput = CollectElementInput(
table: tableName,
column: "expiry_date",
inputStyles: config.fieldConfig(for: .EXPIRATION_DATE)?.style?.skyflowStyles ?? styles,
labelStyles: config.style.labelStyles,
errorTextStyles: config.style.errorStyles,
label: config.fieldConfig(for: .EXPIRATION_DATE)?.title ?? "Expiration Date",
placeholder: config.fieldConfig(for: .CVV)?.placeholder ?? "***",
type: .EXPIRATION_DATE
)

let requiredOption = CollectElementOptions(required: true)
_ = container.create(input: collectCardNumberInput, options: requiredOption)
// _ = container.create(input: collectExpDateInput, options: requiredOption)
_ = container.create(input: collectCVVInput, options: requiredOption)
if config.showNameField {
_ = container.create(input: collectNameInput, options: requiredOption)
Expand All @@ -122,6 +134,39 @@ final class CardCollectView: UIStackView {
do {
let cardForm = try container.getComposableView()
self.addArrangedSubview(cardForm)

// Add test button
let button = UIButton(type: .system)
button.setTitle("Test Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .black
button.layer.cornerRadius = 8
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
self.addArrangedSubview(button)
} catch {}
}

private class CardCollectCallback: Callback {
func onSuccess(_ responseBody: Any) {
if let response = responseBody as? [String: Any],
let records = response["records"] as? [[String: Any]],
let firstRecord = records.first,
let fields = firstRecord["fields"] as? [String: Any] {
print("Collection successful!")
print("Table:", firstRecord["table"] ?? "")
print("Fields:", fields)
} else {
print("Collection successful but unexpected response format:", responseBody)
}
}

func onFailure(_ error: Any) {
print("Collection failed!")
print("Error details:", error)
}
}

@objc private func buttonTapped() {
cardContainer?.collect(with: CardCollectCallback())
}
}
15 changes: 13 additions & 2 deletions Payrails/Classes/Public/skyflow/Elements/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import UIKit
#endif



public class TextField: SkyflowElement, Element, BaseElement {
var onBeginEditing: (() -> Void)?
var onEndEditing: (() -> Void)?
Expand Down Expand Up @@ -761,7 +760,6 @@ extension TextField {
}

extension TextField {

internal func updateInputStyle(_ style: Style? = nil) {
self.textField.translatesAutoresizingMaskIntoConstraints = false
let fallbackStyle = self.collectInput.inputStyles.base
Expand Down Expand Up @@ -850,8 +848,19 @@ extension TextField {

@objc func textFieldDidChange(_ textField: UITextField) {
isDirty = true
print("TextField before updateActualValue - secureText:", self.textField.secureText ?? "nil")
print("TextField before updateActualValue - getSecureRawText:", self.textField.getSecureRawText ?? "nil")

updateActualValue()
print("TextField after updateActualValue - actualValue:", self.actualValue)
print("TextField contextOptions.env:", self.contextOptions.env)

textFieldValueChanged()
let state = self.state as! StateforText
print("TextField state values - isEmpty:", state.isEmpty)
print("TextField state values - value:", state.value ?? "nil")
print("TextField state values - isValid:", state.isValid)

onChangeHandler?((self.state as! StateforText).getStateForListener())
if self.fieldType == .CARD_NUMBER {
let t = self.textField.secureText!.replacingOccurrences(of: "-", with: "").replacingOccurrences(of: " ", with: "")
Expand All @@ -874,8 +883,10 @@ extension TextField {
func updateActualValue() {
if self.fieldType == .CARD_NUMBER {
self.actualValue = textField.getSecureRawText ?? ""
print("actual value", self.actualValue)
} else {
self.actualValue = textField.secureText ?? ""
print("actual value", self.actualValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,4 @@ public struct CollectElementInput {
self.validations = validations
}

@available(*, deprecated, message: "altText param is deprecated")
public init(table: String = "", column: String = "",
inputStyles: Styles? = Styles(), labelStyles: Styles? = Styles(), errorTextStyles: Styles? = Styles(), iconStyles: Styles? = Styles(), label: String? = "",
placeholder: String? = "", altText: String? = "", type: ElementType?, validations: ValidationSet=ValidationSet()) {
self.table = table
self.column = column
self.inputStyles = inputStyles!
self.labelStyles = labelStyles!
self.errorTextStyles = errorTextStyles!
self.iconStyles = iconStyles!
self.label = label!
self.placeholder = placeholder!
self.type = type
self.validations = validations
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,31 @@ internal class StateforText: State

init(tf: TextField) {
super.init(columnName: tf.columnName, isRequired: tf.isRequired)
print("StateforText init - columnName:", tf.columnName)
print("StateforText init - isRequired:", tf.isRequired)

validationError = tf.validate()
print("StateforText init - validationError:", validationError)

isDefaultRuleFailed = validationError.count != 0
let customError = tf.validateCustomRules()
isCustomRuleFailed = customError.count != 0
isValid = !(isDefaultRuleFailed || isCustomRuleFailed)
print("StateforText init - isValid:", isValid)

print("StateforText init - getSecureRawText:", tf.textField.getSecureRawText ?? "nil")
isEmpty = (tf.textField.getSecureRawText?.count == 0)
print("StateforText init - isEmpty:", isEmpty)

isDirty = tf.isDirty
inputLength = tf.textField.getSecureRawText?.count ?? 0
elementType = tf.collectInput.type
isFocused = tf.hasFocus
selectedCardScheme = tf.selectedCardBrand

print("StateforText init - actualValue:", tf.actualValue)
print("StateforText init - contextOptions.env:", tf.contextOptions.env)

if tf.contextOptions.env == .DEV {
value = tf.actualValue
} else {
Expand All @@ -61,6 +75,7 @@ internal class StateforText: State
}
}
}
print("StateforText init - final value:", value ?? "nil")

if validationError.count == 0 {
validationError = customError
Expand All @@ -78,6 +93,7 @@ internal class StateforText: State
result["validationError"] = validationError
result["isCustomRuleFailed"] = isCustomRuleFailed
result["isDefaultRuleFailed"] = isDefaultRuleFailed
result["value"] = value

return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ComposableContainer: ContainerProtocol {}
public extension Container {

func create(input: CollectElementInput, options: CollectElementOptions? = CollectElementOptions()) -> TextField where T: ComposableContainer {
print("Creating element")
var tempContextOptions = self.skyflow.contextOptions
tempContextOptions.interface = .COMPOSABLE_CONTAINER

Expand Down Expand Up @@ -240,17 +241,46 @@ public extension Container {
var currentRecord: [String: Any] = [:]
var fields: [String: Any] = [:]

for element in self.elements {
print("DEBUG: ---- Starting Collection ----")
print("DEBUG: Number of elements:", self.elements.count)

for (index, element) in self.elements.enumerated() {
print("\nDEBUG: Processing Element \(index + 1):")
print("DEBUG: Column Name:", element.columnName ?? "nil")
print("DEBUG: Field Type:", element.fieldType)

let state = element.getState()
print("DEBUG: Raw State:", state)

// Print each state property
print("DEBUG: State Properties:")
print("- isRequired:", state["isRequired"] as? Bool ?? "nil")
print("- isEmpty:", state["isEmpty"] as? Bool ?? "nil")
print("- isDirty:", state["isDirty"] as? Bool ?? "nil")
print("- isValid:", state["isValid"] as? Bool ?? "nil")
print("- value:", state["value"] ?? "nil")

// Try to get value
if let value = state["value"] as? String {
print("DEBUG: Found value:", value)
fields[element.columnName] = value
} else {
print("DEBUG: No value found in state")

// Try to get value directly from element
print("DEBUG: Actual Value:", element.actualValue)
print("DEBUG: Get Output:", element.getOutput() ?? "nil")
print("DEBUG: Get Value:", element.getValue())
}
}

print("\nDEBUG: ---- Collection Results ----")
print("DEBUG: Table:", self.elements.first?.collectInput.table ?? "nil")
print("DEBUG: Fields:", fields)

currentRecord["table"] = self.elements.first?.collectInput.table
currentRecord["fields"] = fields
records.append(currentRecord)


let result = ["records": records]
Log.info(message: .COLLECT_SUBMIT_SUCCESS, contextOptions: tempContextOptions)
Expand Down

0 comments on commit 4426c6d

Please sign in to comment.