ios 如何在SwiftUI中集成Razorpay而无需故事板

wfypjpf4  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(103)

我正在研究在Xcode中将Razorpay结账功能与iOS集成,并在https://razorpay.com/docs/payment-gateway/ios-integration/standard/上找到了官方文档。该文档有助于将Razorpay与UIViewController集成。我正在构建的iOS应用程序没有使用故事板,并且是严格的SwiftUI。我已经研究了在SwiftUI中合并UIViewController的多种方法,这完全可以使用UIViewRepresentable,但代码结构使用

struct ComponentName: UIViewRepresentable{}

字符串
但是Razorpay SDK for iOS希望将RazorpayPaymentCompletionProtocol实现为类而不是结构。如何在严格的SwiftUI应用程序中使用它?

hmtdttj4

hmtdttj41#

您可以使用协调器来管理视图控制器,该协调器将为RazorpayPaymentCompletionProtocol
范例:

struct ComponentName: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> CheckoutViewController {
        .init()
    }

    func updateUIViewController(_ uiViewController: CheckoutViewController, context: Context) { }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, RazorpayPaymentCompletionProtocol {
        let parent: ComponentName
        
        typealias Razorpay = RazorpayCheckout
        var razorpay: RazorpayCheckout!
        
        init(_ parent: ComponentName) {
            self.parent = parent
            
            RazorpayCheckout.initWithKey(razorpayTestKey, andDelegate: self)
        }
        
        func onPaymentError(_ code: Int32, description str: String) {
              print("error: ", code, str)
           //   self.presentAlert(withTitle: "Alert", message: str)
            // parent.alert with message
          }

          func onPaymentSuccess(_ payment_id: String) {
              print("success: ", payment_id)
           //   self.presentAlert(withTitle: "Success", message: "Payment Succeeded")
          }
    }
}

class CheckoutViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //  self.showPaymentForm()
    }
}

字符串

fnvucqvd

fnvucqvd2#

在Swiftui项目中集成razorpay的更好版本:

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)
    }
}

字符串

相关问题