Open twentyone24 opened 3 years ago
Any update on this or at least a step by step guide for new SwiftUI/iOS developers?
@Nautiyalsachin @ramprasadAnand @shashankkumar
@somesh-basicx apologise for the delayed response, we are working on supporting our SDK for SwiftUI. Will post the update here soon as possible.
@ramprasadAnand Any update on SwiftUi support, We have been moving all our apps to swiftUI, But still no documentation on integration
This is the work around you can use, till we get final library update regarding swiftui support :
import Razorpay
import SwiftUI
struct RazorpayView : UIViewControllerRepresentable {
@State var razorKey : String
func makeUIViewController(context: UIViewControllerRepresentableContext<RazorpayView>) -> RazorViewController {
let controller = RazorViewController()
controller.razorKey = self.razorKey
return controller
}
func updateUIViewController(_ uiViewController: RazorViewController, context: UIViewControllerRepresentableContext<RazorpayView>) {
}
}
class RazorViewController: UIViewController {
//MARK: - INSTANCE VARIABLES
private var razorpay:RazorpayCheckout?
var razorKey = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
razorpay = RazorpayCheckout.initWithKey(razorKey, andDelegateWithData: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
let options: [String:Any] = [
"amount" : "15",
"description": "test",
"image": "https://url-to-image.jpg",
"name": "test",
"prefill": [
"contact": "9797979797",
"email": "foo@bar.com"
],
"theme": [
"color": "#F37254"
]
]
razorpay?.open(options)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
extension RazorViewController: RazorpayPaymentCompletionProtocolWithData {
func onPaymentSuccess(_ payment_id: String, andData response: [AnyHashable : Any]?) {
let alert = UIAlertController(title: "Paid", message: "Payment Success", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
func onPaymentError(_ code: Int32, description str: String, andData response: [AnyHashable : Any]?) {
let alert = UIAlertController(title: "Error", message: "\(code)\n\(str)", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
The other way around is to do something instead of having viewcontroller
struct Demo: View {
@State var razorpay:RazorpayCheckout? = nil
var body: some View {
ZStack{
Text("Demo").onTapGesture {
}
}.onAppear{
self.razorpay = RazorpayCheckout.initWithKey("", andDelegateWithData:RazorPayIntegerationMediatator())
}
}
}
class RazorPayIntegerationMediatator : RazorpayPaymentCompletionProtocolWithData{
func onPaymentError(_ code: Int32, description str: String, andData response: [AnyHashable : Any]?) {
}
func onPaymentSuccess(_ payment_id: String, andData response: [AnyHashable : Any]?) {
}
}
This way can be optimized in other ways too let me know if you found a better solution to it
SwiftUI Support
Razorpay is great at what it does, I appreciate it. But, when it comes to adopting latest framework, it has few drawbacks. With adopting RazorPay SDK to support SwiftUI, would be a new room of possibilities.
Suggested implementation:
Describe alternatives you've considered:
Wrap it in UIViewRepresntable & use it in SwiftUI, but, there's no documentation available in the RazorPay iOS Documentation.