难以从服务器 PHP 文件中检索 Stripe 中已连接账户的 URL

Struggling to retrieve the url for connected accounts in Stripe from server PHP file

提问人:Swifty 提问时间:11/13/2023 最后编辑:Swifty 更新时间:11/13/2023 访问量:106

问:

我正在使用此文档将 Connected Stripe 账户合并到我的 iOS 应用程序中。当我们调用“OURFILE.PHP“文件,它会在输出的 JSON 数据中输出 ”url”。我们正在尝试使用此 url 并将其设置为“accountURL”并在 Safari 中打开。

import UIKit
import SafariServices

let BackendAPIBaseURL: String = “OURFILE.PHP”

class ConnectOnboardViewController: UIViewController {

    // ...

    override func viewDidLoad() {
        super.viewDidLoad()

        let connectWithStripeButton = UIButton(type: .system)
        connectWithStripeButton.setTitle("Connect with Stripe", for: .normal)
        connectWithStripeButton.addTarget(self, action: #selector(didSelectConnectWithStripe), for: .touchUpInside)
        view.addSubview(connectWithStripeButton)

        // ...
    }

    @objc
    func didSelectConnectWithStripe() {
        if let url = URL(string: BackendAPIBaseURL)?.appendingPathComponent("onboard-user") {
          print(url)
            var request = URLRequest(url: url)
          request.httpMethod = "POST"
            print("hi0")
          let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
              guard let data = data,
                  let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
                  let accountURLString = json["url"] as? String,
                  let accountURL = URL(string: accountURLString)
              else {
                      // handle error
                  print("hi")
                  print(error)
                  print(response)
                  print(data)
                  
                  return
                  
              }

              let safariViewController = SFSafariViewController(url: accountURL)
              safariViewController.delegate = self

              DispatchQueue.main.async {
                  self.present(safariViewController, animated: true, completion: nil)
              }
          }
        }
    }

    // ...
}

extension ConnectOnboardViewController: SFSafariViewControllerDelegate {
    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        // the user may have closed the SFSafariViewController instance before a redirect
        // occurred. Sync with your backend to confirm the correct state
    }
}

但是,代码似乎跳过了此块

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
              guard let data = data,
                  let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
                  let accountURLString = json["url"] as? String,
                  let accountURL = URL(string: accountURLString)
              else {
                      // handle error
                  print("hi")
                  print(error)
                  print(response)
                  print(data)
                  
                  return
                  
              }

我们在不同的视图中有这个按钮:

Button {
                            self.isLoading = true
                            ConnectOnboardViewController().didSelectConnectWithStripe()
                            self.isLoading = false
                    }

我们已经为此工作了几个小时,试图进行调试,并想知道是否有人也遇到过同样的问题?

php swiftui uikit 条带支付

评论

1赞 workingdog support Ukraine 11/13/2023
你有没有试过在你声明你的后使用?task.resume()task
0赞 orakaro 11/13/2023
被召唤过吗?didSelectConnectWithStripe
0赞 Swifty 11/14/2023
@workingdogsupportUkraine 感谢您的帮助!在我重新配置json数据后,task.resume()实际上解决了这个问题(我在链接中格式不正确)。但是,现在我们收到一个错误:“[演示]尝试演示<......><...>(来自<...>)其观点不在窗口层次结构中”。现在正在调试,但您碰巧知道可能导致这种情况的原因吗?
0赞 workingdog support Ukraine 11/14/2023
如果没有一些合理数量的代码,例如,什么是[演示文稿]等......我无法调试。显示产生问题的最小可重现代码,请参阅:最小代码。显示您收到的完整错误消息以及代码的哪一行。展示一些我们可以编译和运行的极简代码。

答: 暂无答案