PTForm
PTForm is a SwiftUI view that serves as a container for Pay Theory payment fields. It ensures that all Pay Theory fields have access to the necessary data and environment objects required for proper functioning.
Overview
PTForm is designed to wrap around your custom views that contain Pay Theory payment fields. It provides the necessary context for these fields to interact with the Pay Theory SDK.
Key Features
- Provides environment objects required by Pay Theory fields
- Manages app lifecycle changes for proper SDK interaction
- Ensures consistent behavior across all wrapped Pay Theory components
Usage
To use PTForm, you need to:
- Initialize a
PayTheoryobject - Wrap your view containing Pay Theory fields with
PTForm - Pass the
PayTheoryobject as an environment object toPTForm
Example
struct ContentView: View {
let payTheory = PayTheory(apiKey: "your-api-key") { error in
print("Error: \(error.error)")
}
var body: some View {
PTForm {
VStack {
// Your custom views
PTCardNumber()
PTExp()
PTCvv()
// More Pay Theory fields as needed
}
}
.environmentObject(payTheory)
}
}
Important Notes
-
Environment Object: The
PTFormmust be provided with aPayTheoryobject as an environment object. This object contains the necessary context and methods for the Pay Theory SDK to function correctly. -
Wrapping Fields: All Pay Theory fields (such as
PTCardNumber,PTExp,PTCvv, etc.) must be descendants of thePTFormview in the view hierarchy. This ensures they have access to the required environment objects and context. -
Ancestor View: While the Pay Theory fields must be descendants of
PTForm, they don't need to be direct children. You can have intermediate views betweenPTFormand the actual fields:
PTForm {
VStack {
Text("Payment Information")
HStack {
VStack {
PTCardNumber()
PTExp()
}
PTCvv()
}
}
}
.environmentObject(payTheory)
- App Lifecycle:
PTFormmanages app lifecycle changes, ensuring proper interaction with the Pay Theory SDK as the app moves between active, inactive, and background states.