【问题标题】:fetch. Stripe integration works on a localhost and stop working after uploaded to a firebase拿来。 Stripe 集成在本地主机上工作,并在上传到 Firebase 后停止工作
【发布时间】:2023-03-16 01:17:01
【问题描述】:

尝试使用 firebase 平台集成 Stripe。在 lolalhost(firebase 模拟器)上测试使用卡付款。上传到服务器后,得到500

作为示例使用此代码(Web 上的客户端,使用 node.js 的服务器) https://github.com/stripe-samples/accept-a-card-payment/tree/master/using-webhooks

客户:

var orderData = {
  items: [{ id: "photo-subscription" }],
  currency: "usd"
};

fetch("/create-payment-intent", {
  method: "POST",
  headers: {
  "Content-Type": "application/json"
},
  body: JSON.stringify(orderData)
})
.then(function(result) {
  console.log(result);
  return result.json();
})
.then(function(data) {
  console.log(data);
  return setupElements(data);
})
.then(function({ stripe, card, clientSecret }) {
  // Handle form submission.
  var form = document.getElementById("payment-form");
  form.addEventListener("submit", function(event) {
    event.preventDefault();
    // Initiate payment when the submit button is clicked
    pay(stripe, card, clientSecret);
  });
});

服务器(云功能)

app.post("/create-payment-intent", async (req, res) => {

  const { items, currency } = req.body;
  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: 1400,
    currency: currency,
    payment_method_types: ['card'],
  });

  // Send publishable key and PaymentIntent details to client
  res.send({
    publishableKey: 'pk_test_...',
    clientSecret: paymentIntent.client_secret
  });
});

在本地主机上响应:

Response {type: "basic", url: "http://localhost:5000/jobs/create-payment-intent", redirected: false, status: 200, ok: true, …}

{publishableKey: "pk_test_...", clientSecret: "pi_1Fij2FH..."}

服务器上的响应:

POST https://pd...b.firebaseapp.com/create-payment-intent 500

Response {type: "basic", url: "https://pd...b.firebaseapp.com/create-payment-intent", redirected: false, status: 500, ok: false, …}

Uncaught (in promise) SyntaxError: Unexpected token E in JSON at position 0

云功能日志:

Error: An error occurred with our connection to Stripe.
at /srv/functions/node_modules/stripe/lib/StripeResource.js:212:9
at ClientRequest.req.on 
(/srv/functions/node_modules/stripe/lib/StripeResource.js:467:67)
at ClientRequest.emit (events.js:189:13)
at ClientRequest.EventEmitter.emit (domain.js:441:20)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:189:13)
at TLSSocket.EventEmitter.emit (domain.js:441:20)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
Function execution took 346 ms, finished with status: 'crash'

【问题讨论】:

    标签: node.js firebase stripe-payments


    【解决方案1】:

    这已经晚了,但我相信您必须升级到 Firebase Blaze 计划。 Firebase 不允许在免费计划中向非 Google 服务发出 API 请求。

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      相关资源
      最近更新 更多