【问题标题】:Stripe checkout failing to load from firebase cloud function (Fixed)条纹结帐无法从 Firebase 云功能加载(已修复)
【发布时间】:2021-06-28 21:21:47
【问题描述】:

编辑:更新我如何让它在底部工作。 我有一个云函数,它在从我的 React 应用程序调用时创建一个条带结帐会话。 当它被调用时,我在调试器中收到以下错误:加载资源失败:net::ERR_FAILED

这里是云功能:

const functions = require('firebase-functions');
const express = require('express');
const app = express();
const cors = require('cors');

const stripe = require('stripe')((private key removed for this question))

app.use(cors());

exports.createCheckoutSession = functions.https.onRequest(async (req, res) => {
        const session = await stripe.checkout.sessions.create({
            payment_method_types: ['card'],
            line_items: [
                {
                    price_data: {
                        currency: 'usd',
                        product_data: {
                            name: 'T-shirt',
                        },
                        unit_amount: 2000,
                    },
                    quantity: 1,
                },
            ],
            mode: 'payment',
            success_url: 'https://example.com/success.html',
            cancel_url: 'https://example.com/cancel.html',
        });

        res.json({id: session.id});
    });

    app.listen(4242, () => console.log(`Listening on port ${4242}!`));

我相信云功能在我运行时正常工作:

curl -X POST -is "https://us-central1-project-name.cloudfunctions.net/createCheckoutSession" -d ""

我收到一条成功消息。

这是在前端调用它的地方:

    const response = await fetch("https://us-central1-project-name.cloudfunctions.net/createCheckoutSession", {
           method: "POST",
        });

        const session = await response.json();

        // When the customer clicks on the button, redirect them to Checkout.
        const result = await stripe.redirectToCheckout({
            sessionId: session.id,
        });

        if (result.error) {
            // If `redirectToCheckout` fails due to a browser or network
            // error, display the localized error message to your customer
            // using `result.error.message`.
            alert(result.error.message);
        }
    };

我在获取调用时遇到错误。

任何帮助将不胜感激。

更新: 我通过参考这篇文章设法让它工作: Access-Control-Allow-Origin not working Google Cloud Functions GCF

问题在于 CORS。我需要将这些标头添加到函数方法中:

    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Methods', 'GET, POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type');
    res.set('Access-Control-Max-Age', '3600');

【问题讨论】:

  • 您是否在 Firebase 日志中看到任何内容?您是否检查过您的 Stripe 仪表板并确保这些请求成功?
  • 我刚刚检查了 firebase 和 stripe,请求都成功了。

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


【解决方案1】:

您需要从后端创建一个称为 paymentIntent 的东西,然后将 paymentIntent Id 传递给您的前端,这是一个示例 const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const paymentIntent = await stripe.paymentIntents.create({ amount: 2000, currency: 'usd', payment_method_types: ['card'], }); 为什么要使用云使用 react 和简单节点应用程序的功能会好很多(因为您需要为 stripe 设置 webhook 以针对某些事务访问您的 api)并且也更安全,我已经以这种方式将条带集成到我的反应应用程序,它工作正常我可以分享一些代码sn-ps,身份验证和所有重要的事情都将在你的nodejs应用程序中完成,或者你使用前端的任何服务器端只会处理结果,你会能够显示错误,添加重试逻辑等 React Stripe Doc

【讨论】:

  • 据我所见,使用某种后端来处理支付逻辑更加安全。
  • 这将是后端,您会将数据发送到后端以处理前端不会显示其文档中的任何内容,因此基本上您将数据发送到后端应用价格检查等所有操作, 如果用户有保存的卡等,然后将结果发送回前端,这很整洁,如果支付失败,您可以添加一些重试逻辑等等
  • 使用这种方法,你会在什么上部署 node.js 后端?它不需要持续运行,因此需要部署在云功能上吗?
猜你喜欢
  • 2018-08-19
  • 1970-01-01
  • 2019-05-03
  • 2021-11-17
  • 2021-04-14
  • 2023-02-25
  • 2017-11-26
  • 2020-09-04
相关资源
最近更新 更多