iOS / Swift
Installation
Section titled “Installation”Add the Sanwo Swift package via Swift Package Manager:
- In Xcode, go to File > Add Package Dependencies…
- Enter the repository URL:
https://github.com/Sanwohq/ios - 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 |
Requirements
Section titled “Requirements”- iOS 15+
- Swift 5.9+
Quick start
Section titled “Quick start”import Sanwoimport 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"))CheckoutOptions
Section titled “CheckoutOptions”| 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 |
Presenting checkout
Section titled “Presenting checkout”import SwiftUIimport Sanwoimport 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" } } }}import UIKitimport Sanwoimport SanwoPaystack
class CheckoutViewController: UIViewController { let sanwo = Sanwo( provider: paystackProvider, publicKey: "pk_test_xxxxxxxxxxxx" )
@IBAction func payTapped(_ sender: UIButton) { let options = CheckoutOptions( amount: 500000, currency: "NGN", customer: CheckoutCustomer(email: "customer@example.com") )
sanwo(from: self, options: options) { result in switch result { case .successful(let data): print("Paid! Ref: \(data.reference)") case .cancelled: print("User cancelled") case .failed: print("Payment failed") } } }}CheckoutResult
Section titled “CheckoutResult”enum CheckoutResult { case successful(SuccessData) case cancelled case failed}
struct SuccessData { let reference: String let providerResponse: Any?}Events
Section titled “Events”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") }Available events
Section titled “Available events”| 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 |
Full SwiftUI example
Section titled “Full SwiftUI example”import SwiftUIimport Sanwoimport SanwoPaystack
@mainstruct 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" } } } }}