【发布时间】:2023-04-10 21:03:01
【问题描述】:
背景:我正在使用 Gatsby -> Netlify 将我们的销售页面迁移到无服务器 CDN,并尝试实施 Stripe 自定义支付流程,因为我们想要自定义结帐表单。我在这里的一个登陆页面上实现了 Stripe Checkout,但这不是我们想要的。 Landing Page
Stripe's documentation 非常简单,但文档假设其中一个正在运行服务器。
以下代码是他们文档中的服务器实现 sn-p。
const express = require("express");
const app = express();
// This is your real test secret API key.
const stripe = require("stripe")("sk_test_51J085aDDSnMNt7v1ZO3n5fxobP6hhEhf1uC2SDmkHGIX8fCnxFiSMrxITKu06ypYUHYAMHVZ1lhc5Fqm7UoVa6dx00XvV5PZzG");
app.use(express.static("."));
app.use(express.json());
const calculateOrderAmount = items => {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
};
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd"
});
res.send({
clientSecret: paymentIntent.client_secret
});
});
app.listen(4242, () => console.log('Node server listening on port 4242!'));
这是 Stripe 的付款流程。
Stripe's Payment Processing Flow
挑战:由于我将我们的内容移动到 Netlify,它是一个无服务器 CDN,我需要使用 Lambda 函数与 Stripe API 进行交互,而不是在他们的示例中使用 express 服务器实现。
Netlify 在这里描述了在他们的平台上使用serverless functions。
我能够运行这个愚蠢的小东西。但我知道如果我想在我的 React 应用程序中请求该输出...
exports.handler = async function(event, context, callback) {
const { STRIPE_PUBLISHABLE_KEY } = process.env;
console.log("I am the Hello World Lambda function.")
return {
statusCode: 200,
body: JSON.stringify({message: STRIPE_PUBLISHABLE_KEY})
};
}
我知道我在这里展示了我的技能水平,但正如我父亲所说:耻辱胜于痛苦。
问题:/提问有人可以帮我理解如何思考这个问题吗?
我不知道这是否会有所帮助,但这是我的git repository。
任何帮助都将不胜感激。如果你们中的任何一个人有几个空闲周期,并且会考虑在一次缩放会议上指导我。我绝对会为你付出的时间。
非常感谢!
约翰
【问题讨论】:
标签: reactjs aws-lambda stripe-payments gatsby netlify