关于node.js的stripe-webhook的问题



我刚看了一个youtube,用stripe API、react和node.js创建了一个支付系统。我不知道webhook是什么,也不知道这个stirpe API的用途。这段代码已经检查了错误,但为什么webhook是必要的,它是如何工作的?我看到了一堆node.js的stripe-webhook示例,但仍然无法理解。我是的超级初学者

这是我的代码

React(index.html(

import React, {useState} from 'react';
import logo from './logo.svg';
import './App.css';
import StripeCheckout from "react-stripe-checkout"
function App() {
const [product, setstate] = useState({
name: "React from FB",
price: 10,
productBy:"facebook"
})
const makePayment = token => {
const body = {
token, 
product
}
const headers = {
"Content-Type": "application/json"
}
return fetch('http://localhost:8282/payment', {
method: "POST",
headers,
body: JSON.stringify(body)
}).then(response =>{
console.log("RESPONSE", response)
const{status} = response;
console.log("STATUS", status)
})
.catch(error => {
console.log(error);
})
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />

<a
className="App-link"
href="#"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
<StripeCheckout
stripeKey="KEY"
token={makePayment}
name="Buy React"
amount = {product.price * 100}>
<button className="btn-large pink"> buy react is just {product.price}$</button>
</StripeCheckout>
</header>
</div>
);
}
export default App;

Node.js(index.js(

const cors = require('cors');
const express = require('express');
const stripe = require('stripe')("KEY");
const uuid = require("uuid");
const app = express();

//middleware
app.use(express.json());
app.use(cors());

//routes
app.get("/", (req,res)=>{
res.send("It works!!!")
});
app.post("/payment", (req,res)=>{
const{product, token} = req.body;
console.log("PRODUCT", product);
console.log("PRICE", product.price);
const idempontencyKey = uuid();
return stripe.customers.create({
email: token.email,
source: token.id
}).then(customer => {
stripe.charges.create({
amount: product.price * 100,
currency: 'usd',
customer: customer.id,
receipt_email: token.email,
description: product.name,
shipping: {
name: token.card.name,
address: {
country: token.card.address_country
}
}
}, {idempontencyKey})
})
.then(result => res.status(200).json(result))
.catch(err => console.log(err));
})
//listen
app.listen(8282, () =>{
console.log("Listening at PORT 8282");
});

这似乎是对什么是webhook的一个不错的解释:https://zapier.com/blog/what-are-webhooks/

不过,基本上:当你向Stripe的API发出请求时,你是在询问信息,webhook为Stripe提供了一种方法,可以在信息可用时立即将信息直接发送到你的应用程序。

有不同的方法可以将Stripe集成到您的应用程序中。

仅客户端处理

Stripe允许您集成Checkout机制,您只需要实现客户端代码即可执行支付。

以下是仅使用客户端代码的一次性支付的参考:https://stripe.com/docs/payments/checkout/client

客户端和服务器端

但有时也会出现客户端和服务器需要在支付过程之前/期间相互通信的情况(https://stripe.com/docs/payments/integration-builder)。例如,如果您不使用Stripe管理库存,则需要在服务器上使用一些自己的逻辑。一个原因是,除了PayPal等其他支付方式外,你只想将Stripe作为信用卡的支付网关。

Webhooks

在这两种情况下都可以使用webhook。webhook允许Stripe与后端服务器通信,通知您成功付款、失败付款、客户更新、订单、账单等信息。通过定义webhook URL,您可以指定要从Stripe接收的事件。然后,您可以使用事件更新数据库中的指定数据。

我认为一个主要想法是避免费率限制,因为如果没有webhook,您的后端服务器可能需要在指定的时间间隔或触发时询问Stripe API,让您发送指定产品或客户的最新信息。有了API的响应,您就可以更新自己的数据库了。

让我们考虑一下这样的情况,即您在Stripe Dashboard中更改产品,然后您需要尽快用触发器或其他东西"手动"更新自己的后端,以向网站访问者显示最新更改。还有Stripe Billing,它可以让您集成经常性付款。在这种情况下,需要向Stripe API发出更多请求,以使您自己的数据库与实际数据保持一致。

尽管如此,一个网钩是非常奇特的。为什么?从您的后端到Stripe API的点击量将大大减少,因为Stripe将在您的Stripe仪表板内通知您定价、产品、客户信息的每一项更改。有了这些信息,在我看来,你可以更干净地自动化你的后端,因为你唯一的工作就是检查哪些事件被发送到你的服务器,以及你如何提取指定的信息来满足你自己的需要。

以下是指定的Stripe事件https://stripe.com/docs/api/events.

同样,这确实取决于您的用例。