0

我正在考虑在 Xcode 中将 Razorpay 结帐功能与 iOS 集成,并在https://razorpay.com/docs/payment-gateway/ios-integration/standard/找到了官方文档。该文档有助于将 Razorpay 与 UIViewController 集成。我正在构建的 iOS 应用程序不使用情节提要,并且严格来说是 SwiftUI。我已经研究了将 UIViewController 合并到 SwiftUI 中的多种方法,这完全可以通过 UIViewRepresentable 实现,但代码结构使用

struct ComponentName: UIViewRepresentable{}

但适用于 iOS 的 Razorpay SDK 想要实现RazorpayPaymentCompletionProtocol为类而不是结构。如何在严格的 SwiftUI 应用程序中使用它?

4

1 回答 1

0

您可以使用协调器来管理视图控制器,并且该协调器将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()
    }
}
于 2022-01-12T20:35:04.967 回答