Stripe Webhook Node.js Firebase 函数

问题描述 投票:0回答:1

查看为什么我在 Stripe 仪表板中获得 200 成功,但在 Firebase 函数日志中,我看到此函数被调用两次,一次是成功 200,第二次是 400 错误。我添加了 Node.js 代码、快速调用的调用函数以及 Firebase 日志的图片。我已经测试过我是否在 swift 中调用这个函数两次,但我没有。

Node.js stripe Webhook 调用

  exports.events = functions.https.onRequest(async (request, response) => {
  const endpointSecret = 'whsec_qYR81nTsb0uyACJL92TIx9dct6nHNLqg'
  const stripe = require('stripe')(functions.config().stripe.token);
  const payloadData = request.rawBody;
  const webhookStripeSignatureHeader = request.headers['stripe-signature'];
  let event = request.body;

  if (endpointSecret) {
    // Get the signature sent by Stripe
    const signature = request.headers['stripe-signature'];
    try {
      event = stripe.webhooks.constructEvent(payloadData, webhookStripeSignatureHeader, endpointSecret);
    } catch (err) {
      console.log(`⚠️  Webhook signature verification failed.`, err.message);
      return response.sendStatus(400);
    }
  }

  // Handle the event
  switch (event.type) {
    case 'payment_intent.succeeded':
      const paymentIntent = event.data.object;
      console.log(`PaymentIntent for ${paymentIntent.amount} was successful!`);
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data.object;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`);
  }

  // Return a 200 response to acknowledge receipt of the event
  response.send();

});

我如何在 Swift 中调用

var functions = Functions.functions()
                functions.httpsCallable("events").call() { result, error in
                    
                    print(result)
                    print("printing the functions call result")
                    debugPrint(error)
                    print("debug printing the error")
                    
                    if let error = error as NSError? {
                        if error.domain == FunctionsErrorDomain {
                          let code = FunctionsErrorCode(rawValue: error.code)
                          let message = error.localizedDescription
                          let details = error.userInfo[FunctionsErrorDetailsKey]
                            print(code)
                            print(message)
                            print(details)
                        }
                        // ...
                      }
                    
                    if let error = error as NSError? {
                        print(error)
                        print("printing the error in stripeWebhook call")
                        debugPrint(error)
                    }
                    
                    if let data = result?.data as? [String: Any] {
                        print(data)
                        
                    }
                }

我的 Firebase 函数日志

enter image description here

javascript node.js swift firebase google-cloud-functions
1个回答
0
投票

您将可调用函数普通 HTTP 函数混淆了。 您的应用尝试使用 Firebase 客户端 SDK 调用可调用类型函数,但您的函数被声明为普通 HTTP 类型函数。 他们这样彼此不兼容。

如果您想使用 Firebase 客户端 SDK 调用可调用函数,则需要使用

onCall
来定义它,而不是
onRequest
。 或者,如果您想继续使用 HTTP 函数,则需要使用普通 HTTP 客户端而不是 Firebase SDK 来调用它。 可调用函数和 HTTP 函数具有不同的 API。 请参阅:Callable Cloud Functions 与 HTTP 函数相比如何?

© www.soinside.com 2019 - 2024. All rights reserved.