Integrate Razorpay with SwiftUI
Are you building an iOS app using SwiftUI and need to accept payments through Razorpay? Razorpay is one of the most developer-friendly and secure payment gateways in India, supporting UPI, credit/debit cards, and wallets.
In this guide, you’ll learn how to integrate Razorpay into a SwiftUI app using a native SDK, handle callbacks, and offer a smooth payment experience — step-by-step.
🚀 Why Razorpay?
Razorpay provides:
- A powerful native iOS SDK
- Quick integration
- Support for multiple payment modes
- Secure encryption and PCI-DSS compliance
🧱 Prerequisites
Before starting:
- Xcode 15 or above
- iOS 13+ target
- SwiftUI-based app
- Razorpay merchant account
1. 🔧 Install Razorpay iOS SDK
You can integrate Razorpay using Swift Package Manager:
✅ Add Razorpay via Swift Package Manager
- Open your Xcode project.
- Go to
File > Add Packages
. - Enter the following URL:
https://github.com/razorpay/razorpay-pod.git
- Choose the version and add
Razorpay
to your target.
2. 🧠Create a SwiftUI Payment View
Razorpay SDK is written in UIKit, so we’ll need to bridge it with SwiftUI using UIViewControllerRepresentable
.
Step 1: Import Razorpay
import Razorpay
Step 2: Create a Razorpay Manager Class
class RazorpayManager: NSObject, RazorpayPaymentCompletionProtocolWithData {
private var razorpay: RazorpayCheckout?
var onSuccess: (() -> Void)?
var onFailure: ((String) -> Void)?
override init() {
super.init()
razorpay = RazorpayCheckout.initWithKey("YOUR_KEY_ID", andDelegateWithData: self)
}
func startPayment() {
let options: [String:Any] = [
"amount": "5000", // Amount in paise (₹50.00)
"currency": "INR",
"description": "Test Purchase",
"prefill": [
"contact": "9999999999",
"email": "test@example.com"
],
"theme": [
"color": "#528FF0"
]
]
if let rzp = razorpay, let vc = UIApplication.shared.windows.first?.rootViewController {
rzp.open(options, displayController: vc)
}
}
func onPaymentSuccess(_ payment_id: String, andData response: [AnyHashable : Any]) {
print("Payment Success: \(payment_id)")
onSuccess?()
}
func onPaymentError(_ code: Int32, description str: String, andData response: [AnyHashable : Any]?) {
print("Payment Failed: \(str)")
onFailure?(str)
}
}
Step 3: SwiftUI Integration
struct PaymentButton: View {
@State private var paymentManager = RazorpayManager()
var body: some View {
Button(action: {
paymentManager.startPayment()
}) {
Text("Pay ₹50")
.padding()
.frame(maxWidth: .infinity)
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
.onAppear {
paymentManager.onSuccess = {
print("Handle Success UI here")
}
paymentManager.onFailure = { error in
print("Handle Failure UI here: \(error)")
}
}
}
}
🛡️ Secure Your Payment Flow
Never expose sensitive data or create orders on the client. Always use Razorpay’s backend API to generate an order ID from your server and pass it to the SDK via options:
let options: [String: Any] = [
"amount": "5000",
"currency": "INR",
"order_id": "order_DBJOWzybf0sJbb",
...
]
🧪 Test Before You Go Live
Use Razorpay’s test credentials and sandbox mode to test all flows.
✅ Conclusion
By integrating Razorpay with your SwiftUI app, you give users a seamless way to pay while maintaining security and a native experience. While Razorpay is based on UIKit, bridging it into SwiftUI is straightforward and scalable.
If you enjoy my content, consider supporting me! ☕ [Buy me a coffee](https://buymeacoffee.com/dhavaljasoliya)
Comments
Post a Comment