background:我使用gatsby-> netlify将销售页面迁移到无服务器CDN,并试图实现条纹自定义的支付流,因为我们想自定义我们的结帐表格。我在这里的一个登录页面上实现了条纹结帐,但这不是我们想要的。 地页
-stripe的文档非常简单,但是该文档假设一个人正在运行服务器。 以下代码是从其文档中实现的服务器实现片段。
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的付款处理流程
挑战:由于我将内容移至NetLify(这是无服务器CDN),因此我需要使用lambda函数与Stripe API进行交互,而不是其示例中的Express Server实现。 NETLIFY在此处使用
Serverless函数描述了此处的平台。 我能够经营这个愚蠢的小东西。但是我知道如果我想...
我知道我在这里展示自己的技能水平,但是正如我父亲所说的那样:羞耻比痛苦更好。 问题:/saks有人可以帮助我理解如何考虑这一点吗?
库存储库。任何帮助都将不可思议。而且,如果你们中的任何一个有备用的周期,并且会考虑在Zoom会议上指导我。我绝对会付钱给你。
约翰
我绝对可以帮助您开始使用无服务器 /条纹。起初有多个API和库令人困惑,因此希望这对其他人做同样的事情会很有用。
Stripe API有多种做类似事情的方法,但这是您可以使用Netlify Lambdas(或任何类似的解决方案)在静态站点上安全地接受信用卡付款的基本流程。 我会假设您正在使用Stripe的elements信用卡表格。
基本原理是我们需要执行使用“服务器”一侧(即在lambda中)上使用条纹的操作,而我们可以在客户端上进行的其余操作。
安装条纹软件包 - react
遵循React软件包的说明,以创建基本付款表格:
Https://github.com/stripe/react-stripe-js#using-hooks,
secret
现在我们要写一个提交处理程序
用lambda和
npm install stripe @stripe/react-stripe-js @stripe/stripe-js
服务器侧安全制作aimport React from 'react';
import ReactDOM from 'react-dom';
import {loadStripe} from '@stripe/stripe-js';
import {
CardElement,
Elements,
useStripe,
useElements,
} from '@stripe/react-stripe-js';
const CheckoutForm = () => {
const stripe = useStripe();
const elements = useElements();
// we will edit this
const handleSubmit = async (event) => {
event.preventDefault();
const {error, paymentMethod} = await stripe.createPaymentMethod({
type: 'card',
card: elements.getElement(CardElement),
});
};
return (
<form onSubmit={handleSubmit}>
<CardElement />
<button type="submit" disabled={!stripe}>
Pay
</button>
</form>
);
};
const stripePromise = loadStripe('pk_test_abc123');
const App = () => (
<Elements stripe={stripePromise}>
<CheckoutForm />
</Elements>
);
ReactDOM.render(<App />, document.body);
改变您的lambda的身体如下:
paymentIntent
在这里,我们使用您的Stripe Secret密钥初始化Stripe库,我们使用仅数量和货币创建一个最小的PaymayIntent,然后将其退还给客户。 frontend
stripe
const stripe = require("stripe")(process.env.STRIPE_SECRET);
const { amount, currency = "gbp" } = JSON.parse(event.body);
try {
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency,
});
return {
statusCode: 200, // http status code
body: JSON.stringify({
paymentIntent
}),
};
} catch (e) {
// handle errors
}
paymentIntents
。)
// handleSubmit()
const intent = await fetch("/.netlify/functions/payment-intent", {
method: "POST",
body: JSON.stringify({
amount: 500,
}),
});
const { paymentIntent } = await intent.json();
在这里,我们使用JavaScript API的方法来确认使用the the the from from from from from设置的付款。信用卡详细信息是通过paymentIntent
将仍然安全
@rubie。任何人都无法通过API或前端修改价格吗?只是一个乞er,对这种方法感到好奇