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

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

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

  1. struct ComponentName: UIViewRepresentable{}

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

hmtdttj4

hmtdttj41#

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

  1. struct ComponentName: UIViewControllerRepresentable {
  2. func makeUIViewController(context: Context) -> CheckoutViewController {
  3. .init()
  4. }
  5. func updateUIViewController(_ uiViewController: CheckoutViewController, context: Context) { }
  6. func makeCoordinator() -> Coordinator {
  7. Coordinator(self)
  8. }
  9. class Coordinator: NSObject, RazorpayPaymentCompletionProtocol {
  10. let parent: ComponentName
  11. typealias Razorpay = RazorpayCheckout
  12. var razorpay: RazorpayCheckout!
  13. init(_ parent: ComponentName) {
  14. self.parent = parent
  15. RazorpayCheckout.initWithKey(razorpayTestKey, andDelegate: self)
  16. }
  17. func onPaymentError(_ code: Int32, description str: String) {
  18. print("error: ", code, str)
  19. // self.presentAlert(withTitle: "Alert", message: str)
  20. // parent.alert with message
  21. }
  22. func onPaymentSuccess(_ payment_id: String) {
  23. print("success: ", payment_id)
  24. // self.presentAlert(withTitle: "Success", message: "Payment Succeeded")
  25. }
  26. }
  27. }
  28. class CheckoutViewController: UIViewController {
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. }
  32. override func viewWillAppear(_ animated: Bool) {
  33. super.viewWillAppear(animated)
  34. // self.showPaymentForm()
  35. }
  36. }

字符串

展开查看全部
fnvucqvd

fnvucqvd2#

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

  1. import Razorpay
  2. import SwiftUI
  3. struct RazorpayView : UIViewControllerRepresentable {
  4. @State var razorKey : String
  5. func makeUIViewController(context: UIViewControllerRepresentableContext<RazorpayView>) -> RazorViewController {
  6. let controller = RazorViewController()
  7. controller.razorKey = self.razorKey
  8. return controller
  9. }
  10. func updateUIViewController(_ uiViewController: RazorViewController, context: UIViewControllerRepresentableContext<RazorpayView>) {
  11. }
  12. }
  13. class RazorViewController: UIViewController {
  14. //MARK: - INSTANCE VARIABLES
  15. private var razorpay:RazorpayCheckout?
  16. var razorKey = ""
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view, typically from a nib.
  20. razorpay = RazorpayCheckout.initWithKey(razorKey, andDelegateWithData: self)
  21. }
  22. override func viewWillAppear(_ animated: Bool) {
  23. super.viewWillAppear(animated)
  24. navigationController?.setNavigationBarHidden(true, animated: animated)
  25. let options: [String:Any] = [
  26. "amount" : "15",
  27. "description": "test",
  28. "image": "https://url-to-image.jpg",
  29. "name": "test",
  30. "prefill": [
  31. "contact": "9797979797",
  32. "email": "foo@bar.com"
  33. ],
  34. "theme": [
  35. "color": "#F37254"
  36. ]
  37. ]
  38. razorpay?.open(options)
  39. }
  40. override func viewWillDisappear(_ animated: Bool) {
  41. super.viewWillDisappear(animated)
  42. navigationController?.setNavigationBarHidden(false, animated: animated)
  43. }
  44. }
  45. extension RazorViewController: RazorpayPaymentCompletionProtocolWithData {
  46. func onPaymentSuccess(_ payment_id: String, andData response: [AnyHashable : Any]?) {
  47. let alert = UIAlertController(title: "Paid", message: "Payment Success", preferredStyle: .alert)
  48. let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
  49. alert.addAction(action)
  50. self.present(alert, animated: true, completion: nil)
  51. }
  52. func onPaymentError(_ code: Int32, description str: String, andData response: [AnyHashable : Any]?) {
  53. let alert = UIAlertController(title: "Error", message: "\(code)\n\(str)", preferredStyle: .alert)
  54. let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
  55. alert.addAction(action)
  56. self.present(alert, animated: true, completion: nil)
  57. }
  58. }

字符串

展开查看全部

相关问题