【问题标题】:JSON could not be serialized because of error由于错误,无法序列化 JSON
【发布时间】:2023-03-10 21:59:01
【问题描述】:

我正在尝试使用 Stripe 付款来允许用户发送付款。当我的应用程序中创建了一个用户时,我可以成功创建一个条带客户,但是我找不到 ephemeralKey,我认为这就是我看到的原因,

错误,由于错误,无法序列化 JSON:无法读取数据,因为它的格式不正确。

当我创建条带客户时,我使用的代码是

exports.createStripeCustomer = functions.auth.user().onCreate((user) => {
  return stripe.customers.create({
    email: user.email,
  }).then((customer) => {
    return admin.database().ref(`/stripe_customers/${user.uid}/customer_id`).set(customer.id);
  });
});

以及我如何尝试创建 ephemeralKey

exports.createEphemeralKey = functions.https.onRequest((req, res) => {
  const stripe_version = req.body.api_version;
  const customerId = req.body.customerId
  if (!stripe_version) {
    console.log('I did not see any api version')
    res.status(400).end()
    return;
  }

  stripe.ephemeralKeys.create(
    {customer: customerId},
    {stripe_version: stripe_version}
  ).then((key) => {
     console.log("Ephemeral key: " + key)
     res.status(200).json(key)
  }).catch((err) => {
    console.log('stripe version is ' + stripe_version + " and customer id is " + customerId + " for key: " + stripe_key + " and err is " + err.message )
    res.status(500).json(err)
  });
});

和 myApiCLient 一样

   enum APIError: Error {
     case unknown

     var localizedDescription: String {
         switch self {
         case .unknown:
             return "Unknown error"
         }
     }
 }

static let sharedClient = MyAPIClient()
var baseURLString: String? = "my.functioncloudfunctions.net"
var baseURL: URL {
    if let urlString = self.baseURLString, let url = URL(string: urlString) {
        return url
    } else {
        fatalError()
    }
}

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
  let url = self.baseURL.appendingPathComponent("ephemeral_keys")
    
    let defaults = UserDefaults.standard
    let customerid = defaults.string(forKey: "customer_id")
    AF.request(url, method: .post, parameters: [
        "api_version": apiVersion, "customer_id": customerid!
        ])
        .validate(statusCode: 200..<300)
        .responseJSON { responseJSON in
            switch responseJSON.result {
            case .success(let json):
                completion(json as? [String: AnyObject], nil)
            case .failure(let error):
                completion(nil, error)
            }
    }
}

我正在尝试找出我遇到的问题,但不是很清楚,因为在我的 firebase 函数日志中,我看到的只是

这不是很清楚,我尝试将语句打印到控制台,但它们没有显示在我的函数日志中。有什么突出的地方需要改正吗??

我终于在我的条带控制台中收到一条错误消息,提示:

  {
  "error": {
    "message": "Must provide exactly one of these parameters: [:customer, :issuing_card].",
    "type": "invalid_request_error"
  }
} 

【问题讨论】:

    标签: ios node.js google-cloud-functions stripe-payments


    【解决方案1】:

    您似乎没有从您的客户端代码发送customer_id,因此很可能在您的 API 调用中使用undefined 来创建临时密钥 - 但您应该能够在您的 Stripe Dashboard 日志中确认这一点.

    【讨论】:

    • 我已经更新了我的问题底部以表示我所做的一些更改。如果我的方向正确,你能告诉我吗?
    • 和 APIClient 我已经更新了代码以表明我正在尝试通过 customer_id 客户端发送。
    • 我怀疑您没有像您认为的那样进行服务器端 API 调用; issue_card 根本不应该参与其中。
    • 我认为你的第一个答案是正确的错误是什么。我认为这是因为我在发送 customer_id 时遇到问题,因为当我尝试调试和打印时,我收到 nill。
    • 您知道获取此信息并发送它的正确方法吗?我以为“api_version”:apiVersion,“customer_id”:customerid!会完成工作,但强制打开包装会给我一个错误,说它为零
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2018-12-26
    • 2021-02-11
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多