【问题标题】:Question for stripe-webhooks with node.js带有 node.js 的条带 webhook 的问题
【发布时间】:2021-01-17 16:44:39
【问题描述】:

我刚刚观看了一个 youtube,它使用 Stripe API、react 和 node.js 创建了一个支付系统。我不确定 webhook 是什么以及这个 Stirpe api 的目的。这段代码已经检查了错误,但为什么需要 webhook 以及它是如何工作的?我看到了一堆 node.js 条纹 webhook 示例,但仍然无法理解。我是超级初学者

这是我的代码

反应(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");
});

【问题讨论】:

    标签: node.js reactjs stripe-payments webhooks


    【解决方案1】:

    这似乎是对 webhook 的一个很好的解释:https://zapier.com/blog/what-are-webhooks/

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

    【讨论】:

      【解决方案2】:

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

      仅客户端处理

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

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

      客户端和服务器端

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

      网络钩子

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

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

      让我们考虑一下您在 Stripe Dashboard 中更改产品的情况,然后您需要尽快“手动”更新您自己的后端,使用触发器或其他方式向您的访问者显示最新更改网站。还有 Stripe Billing,可让您整合定期付款。在这种情况下,需要对 Stripe API 提出更多请求,以使您自己的数据库与实际数据保持一致。

      对于这一切,webhook 非常漂亮。为什么?您需要从后端到 Stripe API 的点击次数要少得多,因为 Stripe 会在您的 Stripe Dashboard 内通知您对定价、产品、客户信息所做的每一项更改。在我看来,有了这些信息,您可以自动化后端更清洁,因为您唯一的工作就是检查哪些事件被发送到您的服务器,以及如何根据自己的需要提取指定的信息。

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

      这真的取决于你的用例。

      【讨论】:

        猜你喜欢
        • 2016-09-17
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 2020-09-30
        • 2018-02-14
        • 1970-01-01
        • 2012-08-31
        • 2020-03-22
        相关资源
        最近更新 更多