我如何为我的条纹集成编写lambda功能?

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

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!'));
there的支付流量。

-Stripe的付款处理流程

挑战:由于我将内容移至NetLify(这是无服务器CDN),因此我需要使用lambda函数与Stripe API进行交互,而不是其示例中的Express Server实现。 NETLIFY在此处使用

Serverless函数描述了此处的平台。 我能够经营这个愚蠢的小东西。但是我知道如果我想...

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}) }; }

我知道我在这里展示自己的技能水平,但是正如我父亲所说的那样:羞耻比痛苦更好。 问题:/saks有人可以帮助我理解如何考虑这一点吗?
我不知道这是否会有所帮助,但这是我的
库存储库。
任何帮助都将不可思议。而且,如果你们中的任何一个有备用的周期,并且会考虑在Zoom会议上指导我。我绝对会付钱给你。
感谢!

约翰

我绝对可以帮助您开始使用无服务器 /条纹。起初有多个API和库令人困惑,因此希望这对其他人做同样的事情会很有用。

Stripe API有多种做类似事情的方法,但这是您可以使用Netlify Lambdas(或任何类似的解决方案)在静态站点上安全地接受信用卡付款的基本流程。 我会假设您正在使用Stripe的elements信用卡表格。

基本原理是我们需要执行使用“服务器”一侧(即在lambda中)上使用条纹的操作,而我们可以在客户端上进行的其余操作。

安装条纹软件包 - react

遵循React软件包的说明,以创建基本付款表格:

Https://github.com/stripe/react-stripe-js#using-hooks,

reactjs aws-lambda stripe-payments gatsby netlify
1个回答
19
投票

secret

现在我们要写一个提交处理程序

用lambda和

npm install stripe @stripe/react-stripe-js @stripe/stripe-js
服务器侧安全制作a

import 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);
  • 确认前端的付款,并使用用户的信用卡详细信息
    使用JavaScriptAPI
  • lambda

改变您的lambda的身体如下:

paymentIntent
在这里,我们使用您的Stripe Secret密钥初始化Stripe库,我们使用仅数量和货币创建一个最小的PaymayIntent,然后将其退还给客户。
frontend
修理您的条纹和元素,如链接的示例中的加载,并具有基本的信用卡表格,您只需要编辑提交处理程序。
首先致电您的lambda:

stripe
  • 现在,您可以使用PaymayIntent来确认用户输入的卡详细信息。 (
      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
    • 方法来照顾的。我们还提供用户的电子邮件,以便他们的收据可以发送给他们。 next步骤
    • 这是通过条纹接受付款的非常基本的无服务器实现。您可以查看不同的API来设置客户并使用其ID进行付款,设置订阅等等。
    自然,您还需要处理我忽略的许多错误和各种错误。
  • versions是所有条纹库的最新版本,如2021-06-12

将仍然安全

@rubie
。任何人都无法通过API或前端修改价格吗?只是一个乞er,对这种方法感到好奇

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