Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI iOS Support #34

Open
twentyone24 opened this issue May 21, 2021 · 6 comments
Open

SwiftUI iOS Support #34

twentyone24 opened this issue May 21, 2021 · 6 comments

Comments

@twentyone24
Copy link

twentyone24 commented May 21, 2021

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:

  • Native Support for using the declartive Framework.
  • Or a documentation helping how to wrap it in UIViewRepresntable & use it in SwiftUI, would be recommended too.

Describe alternatives you've considered:

Wrap it in UIViewRepresntable & use it in SwiftUI, but, there's no documentation available in the RazorPay iOS Documentation.

@somesh-basicx
Copy link

somesh-basicx commented Feb 9, 2022

Any update on this or at least a step by step guide for new SwiftUI/iOS developers?

@somesh-basicx
Copy link

@ramprasadAnand
Copy link

@somesh-basicx apologise for the delayed response, we are working on supporting our SDK for SwiftUI. Will post the update here soon as possible.

@Albinzr
Copy link

Albinzr commented May 31, 2022

@ramprasadAnand Any update on SwiftUi support, We have been moving all our apps to swiftUI, But still no documentation on integration

@vishal-jadav
Copy link

vishal-jadav commented Aug 1, 2023

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": "[email protected]"
            ],
            "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)
    }
}

@nileshteji
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants