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 support #32

Open
Gods-of-coding opened this issue Dec 9, 2020 · 3 comments
Open

SwiftUI support #32

Gods-of-coding opened this issue Dec 9, 2020 · 3 comments

Comments

@Gods-of-coding
Copy link

Describe the feature you'd like: Swiftui integration

Suggested implementation:

Describe alternatives you've considered:

Tried using uiviwerepresentable but got no luck

Teachability, Documentation, Adoption, Migration Strategy: latest framework of ios support

@twentyone24
Copy link

Describe the feature you'd like: Swiftui integration

Suggested implementation:

Describe alternatives you've considered:

Tried using uiviwerepresentable but got no luck

Teachability, Documentation, Adoption, Migration Strategy: latest framework of ios support

Been Looking for the documentations to integrate RazerPay in swiftui, but got no luck. Looking forward to support the Apple's Latest Framework.

@vishal-jadav
Copy link

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

nileshteji commented Apr 19, 2024

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

4 participants