Use the XPay Swift SDK to render XPay-hosted payment fields in a SwiftUI application and confirm a PaymentIntent created by your backend. The SDK supports iOS 15 and later.
Outcome
By the end of this guide, your application will render a card payment form, validate its fields, and submit the customer's payment details.
Install the SDK
In Xcode, add the following Swift package:
https://github.com/XStakCommerce/XPay-Swift-SDKFor production, select the latest released version. For staging, select the stage branch. Import the package in the view that renders XPay Element:
import XPayPaymentKitInitialize the SDK
Create a separate XPayController for every payment method displayed at the same time. A KeysConfiguration can be shared by those controllers.
The Swift SDK requires accountId, publicKey. Obtain these values from Store settings → Keys in the XPay portal.
let cardController = XPayController()
let keysConfiguration = KeysConfiguration(
accountId: "{{account_id}}",
publicKey: "{{publishable_key}}"
)Render the payment form
Render XPayPaymentForm and use onReady to enable the payment button only when all required fields contain valid input:
struct CheckoutView: View {
@State private var isReady = false
var body: some View {
VStack {
XPayPaymentForm(
keysConfiguration: keysConfiguration,
onReady: { ready in
isReady = ready
},
controller: cardController
)
Button("Pay now") {
confirmCardPayment()
}
.disabled(!isReady)
}
}
}Use XPayJazzCashPaymentForm or XPayEasyPaisaPaymentForm with its own controller when rendering the corresponding wallet form.
Confirm the payment
First, create a PaymentIntent on the merchant backend. Return its client secret to the application, then confirm through the controller associated with the selected payment form.
func confirmCardPayment() {
cardController.confirmPayment(
customerName: "Customer Name",
clientSecret: "{{pi_client_secret}}",
paymentResponse: { data in
let hasError = data["error"] as? Bool ?? true
let message = data["message"] as? String ?? "Payment could not be completed"
let status = data["status"] as? String
// Update the checkout UI with the submission result.
}
)
}The callback returns a dictionary containing error, message, and status. Use payment webhook events to reconcile the final payment outcome on the merchant backend.
Reset the form
cardController.clear()What to do next
Validate the SwiftUI checkout with XPay's test scenarios. Use the PaymentIntent lifecycle to design the app's result states and the XPay API reference for server endpoint schemas.