您可以使用协调器来管理视图控制器,并且该协调器将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()
}
}