KhaltiCheckout is available through Swift Package Manager. To install it:
- Go to File → Add Package Dependencies
- Enter the repository URL:
https://github.com/khalti/checkout-sdk-ios.git - Select the version you want to use
- Add the
KhaltiCheckoutpackage product to your target
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/khalti/checkout-sdk-ios.git", from: "1.1.1")
]Then add the KhaltiCheckout product to your target:
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "KhaltiCheckout", package: "checkout-sdk-ios")
]
)
]- UIKit: iOS 12.0+
- SwiftUI: iOS 13.0+
- Swift 5.0+
- Xcode 12.0+
Import KhaltiCheckout in UIKit or SwiftUI files:
import KhaltiCheckoutimport UIKit
import KhaltiCheckout
class ViewController: UIViewController {
func initiatePayment() {
let config = KhaltiPayConfig(
publicKey: "your_public_key_here",
pIdx: "your_payment_idx_here",
paymentUrl: "your_payment_url_here",
environment: .TEST // Use .PROD for production
)
let khalti = Khalti(
config: config,
onPaymentResult: { result, khalti in
// Handle payment result
print("Payment Status: \(result.status ?? "Unknown")")
},
onMessage: { message, khalti in
// Handle messages/errors
print("Message: \(message.message)")
},
onReturn: { khalti in
// Handle return from payment page
print("Returned from payment page")
}
)
// Open payment page
khalti.open(viewController: self)
}
}import SwiftUI
import KhaltiCheckout
struct ContentView: View {
@State private var showKhalti = false
@State private var khalti: Khalti?
var body: some View {
VStack(spacing: 20) {
Text("Khalti Payment")
.font(.title)
Button("Pay with Khalti") {
setupKhalti()
showKhalti = true
}
.buttonStyle(.borderedProminent)
}
.padding()
.khaltiCheckoutSheet(
isPresented: $showKhalti,
khalti: khalti,
onDismiss: {
print("Payment completed or dismissed")
}
)
}
func setupKhalti() {
let config = KhaltiPayConfig(
publicKey: "your_public_key_here",
pIdx: "your_payment_idx_here",
paymentUrl: "your_payment_url_here",
environment: .TEST // Use .PROD for production
)
khalti = Khalti(
config: config,
onPaymentResult: { result, khalti in
// Handle payment result
print("Payment Status: \(result.status ?? "Unknown")")
},
onMessage: { message, khalti in
// Handle messages/errors
print("Message: \(message.message)")
},
onReturn: { khalti in
// Handle return from payment page
print("Returned from payment page")
}
)
}
}Go to the documentation to install checkout-iOS.
KhaltiCheckout is available under the MIT license. See the LICENSE file for more info.