Skip to main content
Version: Lab (preview)

PayTheory Class

The PayTheory class is the core component of the PayTheory iOS SDK. It manages payment processing, handles various payment methods, and provides access to transaction-related information.

Class Declaration​

public class PayTheory: ObservableObject, WebSocketProtocol

The PayTheory class conforms to ObservableObject, allowing it to be used with SwiftUI's state management system, and WebSocketProtocol for handling real-time communication with PayTheory services.

Initializer​

The PayTheory class is initialized with the following function signature:

public init(amount: Int? = nil, apiKey: String, devMode: Bool = false, attestationEnvironment: AttestationEnvironment = .local, attestationKey: String? = nil, errorHandler: @escaping (PTError) -> Void)

Here's a detailed description of each parameter:

ParameterTypeDescription
amountInt?An optional integer representing the transaction amount in cents. If provided, it will be used to calculate fees for Service Fee mode. Defaults to nil.
apiKeyStringA string containing the API key for authentication with PayTheory services. It should be in the format '{partner}-{paytheorystage}-{UUID}'.
devModeBoolA boolean flag indicating whether to run in development mode. When true it allows you to skip the app attestation process and run in the simulator. This cannot be turned on in production.
attestationEnvironmentAttestationEnvironmentThe environment to use for attestation. Defaults to .local. If you are running from Xcode you will use .local. If you are deploying to testflight or the app store you will use .production.
attestationKeyString?A string containing the attestation key for the app. This is used if you want to manage the app attestation key in your own code. If not provided, the SDK will manage it for you.
errorHandler@escaping (PTError) -> VoidA closure that handles any errors that might occur during initialization and other background actions. It takes a PTError parameter and doesn't return a value.
Important

You must set attestationEnvironment to .production before deploying your app to TestFlight or the App Store. If you leave it set to .local, app attestation will fail in these environments and your app will not be able to process payments.

Example Usage​

let payTheory = PayTheory(
amount: 1000, // $10.00
apiKey: "partner-stage-1234567890abcdef",
devMode: true
) { error in
print("Error occurred: \(error.error)")
}

Public Variables​

The PayTheory class provides several public variables for accessing payment-related information and states:

Payment Method States​

@Published public private(set) var card: CardState
@Published public private(set) var cash: CashState
@Published public private(set) var ach: ACHState

These variables provide access to the current state of different payment methods. Each state object contains validation status and entered details for the respective payment method.

More details on these states can be found here:

Service Fees​

@Published public private(set) var cardServiceFee: Int?
@Published public private(set) var bankServiceFee: Int?

These variables hold the calculated service fees (in cents) for card and bank (ACH) transactions. They will be nil if no fee applies or if the fee hasn't been calculated yet.

Ready State​

@Published public private(set) var isReady: Bool

This boolean indicates whether the PayTheory instance has completed its initialization and is ready to process payments. It should be checked before attempting to use the instance for payment operations.

Private Setters

The private(set) modifier on these published properties means they can be read from outside the class, but only modified within the class itself. This ensures that the SDK maintains control over these states while still allowing your app to observe them.

Usage in SwiftUI​

To use the PayTheory class in a SwiftUI view, you typically initialize it as a state object or environment object:

struct ContentView: View {
@StateObject private var payTheory = PayTheory(
apiKey: "your-api-key-here"
) { error in
print("PayTheory Error: \(error.error)")
}

var body: some View {
PTForm {
// Your payment form contents
}
.environmentObject(payTheory)
}
}

By using @StateObject and passing the payTheory instance as an environment object, you ensure that all child views have access to the same PayTheory instance and can react to its state changes.

For more details on the specific functions provided by the PayTheory class and how to use the various state classes, please refer to the detailed API documentation.