Skip to content

iOS / Swift

Add the Sanwo Swift package via Swift Package Manager:

  1. In Xcode, go to File > Add Package Dependencies…
  2. Enter the repository URL: https://github.com/Sanwohq/ios
  3. Add the products you need:
Product Description
Sanwo Core SDK
SanwoPaystack Paystack provider
SanwoFlutterwave Flutterwave provider
SanwoRazorpay Razorpay provider
SanwoMonnify Monnify provider
SanwoInterswitch Interswitch provider
  • iOS 15+
  • Swift 5.9+
import Sanwo
import SanwoPaystack
let sanwo = Sanwo(
provider: paystackProvider,
publicKey: "pk_test_xxxxxxxxxxxx"
)
let options = CheckoutOptions(
amount: 500000, // 5,000.00 NGN in kobo
currency: "NGN",
customer: CheckoutCustomer(email: "customer@example.com")
)
Property Type Required Description
amount Int Yes Amount in minor units (kobo, cents, paisa)
currency String Yes ISO 4217 currency code
customer CheckoutCustomer Yes Customer with at least an email
reference String? No Your unique transaction reference
channels [String]? No Allowed payment channels
metadata [String: Any]? No Arbitrary metadata
import SwiftUI
import Sanwo
import SanwoPaystack
struct CheckoutView: View {
let sanwo = Sanwo(
provider: paystackProvider,
publicKey: "pk_test_xxxxxxxxxxxx"
)
@State private var showCheckout = false
@State private var message = ""
let options = CheckoutOptions(
amount: 500000,
currency: "NGN",
customer: CheckoutCustomer(email: "customer@example.com")
)
var body: some View {
VStack(spacing: 20) {
Button("Pay ₦5,000") {
showCheckout = true
}
if !message.isEmpty {
Text(message)
}
}
.sanwoCheckout(
isPresented: $showCheckout,
sanwo: sanwo,
options: options
) { result in
switch result {
case .successful(let data):
message = "Paid! Ref: \(data.reference)"
case .cancelled:
message = "Cancelled"
case .failed:
message = "Payment failed"
}
}
}
}
enum CheckoutResult {
case successful(SuccessData)
case cancelled
case failed
}
struct SuccessData {
let reference: String
let providerResponse: Any?
}

Subscribe to lifecycle events. Calls are chainable:

sanwo
.on(.loaded) { event in
print("Checkout UI loaded")
}
.on(.success) { event in
print("Payment succeeded: \(event.reference ?? "")")
}
.on(.cancelled) { event in
print("User cancelled")
}
Event Description
.started Checkout initiated
.opened Checkout UI presented
.loaded Provider UI finished loading
.success Payment successful
.cancelled User dismissed checkout
.failed Payment failed
.closed Checkout UI dismissed
import SwiftUI
import Sanwo
import SanwoPaystack
@main
struct MyStoreApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
let sanwo = Sanwo(
provider: paystackProvider,
publicKey: "pk_test_xxxxxxxxxxxx"
)
@State private var showCheckout = false
@State private var resultMessage: String?
var body: some View {
NavigationStack {
VStack(spacing: 24) {
Text("Premium Plan")
.font(.title)
Text("₦5,000 / month")
.font(.headline)
.foregroundStyle(.secondary)
Button("Subscribe") {
showCheckout = true
}
.buttonStyle(.borderedProminent)
if let resultMessage {
Text(resultMessage)
.foregroundStyle(
resultMessage.contains("Paid") ? .green : .red
)
}
}
.navigationTitle("My Store")
.sanwoCheckout(
isPresented: $showCheckout,
sanwo: sanwo,
options: CheckoutOptions(
amount: 500000,
currency: "NGN",
customer: CheckoutCustomer(email: "customer@example.com"),
reference: "order_\(Int(Date().timeIntervalSince1970))"
)
) { result in
switch result {
case .successful(let data):
resultMessage = "Paid! Ref: \(data.reference)"
case .cancelled:
resultMessage = "Payment cancelled"
case .failed:
resultMessage = "Payment failed"
}
}
}
}
}