批准 PayPal 订单但在服务器端

发布于 2025-01-16 21:47:27 字数 1733 浏览 1 评论 0原文

我有这个付款代码:

  generatePayPalButton() {
    paypal
      .Buttons({
        createOrder: async () => {
          const res = await this.sendReq.postReq("/api/paypal/create-order", { quantity: 1 })
          return res.id
        },
        onApprove: function (data: any, actions: any) {
          const capture = actions?.order?.capture().then((details: any) => {
            console.log(details)
          })
        },
        onCancel: () => {
          console.log("Canceled")
        },
        onError: (e: any) => {
          console.error(e)
        }
      })
      .render("#paypal")
  }

您可以清楚地看到,我在前端批准订单。 但我想在后端批准订单并将其保存到数据库中。

这是我的后端代码:

router.post("/api/paypal/create-order", async(req, res) => {
    const request = new paypal.orders.OrdersCreateRequest()
    const quantity = req.body.quantity
    const total = quantity * dataController.payement.pricePerNight
    request.prefer("return=representation")
    request.requestBody({
        intent: "CAPTURE",
        purchase_units: [{
            amount: {
                currency_code: "USD",
                value: total,
                breakdown: {
                    item_total: {
                        currency_code: "USD",
                        value: total,
                    },
                },
            },
        }, ],
    })

    try {
        const order = await paypalClient.execute(request)
        res.send({ id: order.result.id })
    } catch (e) {
        console.error(`Paypal error: `, e.message)
        res.send(e)
    }
})

所以在后端我只有一条路线,我在其中返回订单 ID,这样我就可以向用户收费。但我怎么能确定他付了钱呢?我可以在后台检查令牌或其他东西吗?

我正在使用: NodeJs,表达 @paypal/结账服务器-sdk 角 发送请求服务

I have this code for payement:

  generatePayPalButton() {
    paypal
      .Buttons({
        createOrder: async () => {
          const res = await this.sendReq.postReq("/api/paypal/create-order", { quantity: 1 })
          return res.id
        },
        onApprove: function (data: any, actions: any) {
          const capture = actions?.order?.capture().then((details: any) => {
            console.log(details)
          })
        },
        onCancel: () => {
          console.log("Canceled")
        },
        onError: (e: any) => {
          console.error(e)
        }
      })
      .render("#paypal")
  }

You can clearly see, that Im approving the order in the frontend.
But I want to approve the order in the backend and save it to a database.

Here is my backend code:

router.post("/api/paypal/create-order", async(req, res) => {
    const request = new paypal.orders.OrdersCreateRequest()
    const quantity = req.body.quantity
    const total = quantity * dataController.payement.pricePerNight
    request.prefer("return=representation")
    request.requestBody({
        intent: "CAPTURE",
        purchase_units: [{
            amount: {
                currency_code: "USD",
                value: total,
                breakdown: {
                    item_total: {
                        currency_code: "USD",
                        value: total,
                    },
                },
            },
        }, ],
    })

    try {
        const order = await paypalClient.execute(request)
        res.send({ id: order.result.id })
    } catch (e) {
        console.error(`Paypal error: `, e.message)
        res.send(e)
    }
})

So in the backend I just have a route, where Im giving the order id back, so I can charge the user. But how can I be sure that he paid. Can I check a token or something in the backend?

Im using:
NodeJs, express
@paypal/checkout-server-sdk
Angular
sendRequestService

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2025-01-23 21:47:27

批准步骤总是在客户端进行。付款的人表示批准,他们并不住在您的服务器内,而是通过客户端连接到服务器。

但是,批准之前(创建订单)和批准之后(捕获订单,这实际上创建交易)的步骤都是从服务器完成的。每一个都需要一个服务器路由;看起来您已经有一条创建路线。第二个捕获路由应采用id作为输入,捕获它,进行任何验证,例如在向客户端返回响应之前检查任何成功的交易是否具有正确的金额。 (错误/不成功的事务响应也应该传播回客户端,以便它可以显示适当的消息或在 INSTRUMENT_DECLINED 的情况下重新启动)

要在客户端上进行批准,请使用以下流程: https://developer.paypal.com/demo/checkout/#/pattern/server

The approval step always takes place on the client side. The person paying is giving their approval, and they don't live inside your server, they connect to it via a client.

However, the steps immediately prior to approval (create order) and after approval (capture order, which is what actually creates a transaction) are both done from a server. You'll need a server route for each of these; looks like you already have a create route. The second capture route should take an id as input, capture it, do any validation such as checking that any successful transaction was for the correct amount before returning a response to the client. (Error/unsuccessful transaction responses should also be propagated back to the client, so it can display an appropriate message or restart in the case of INSTRUMENT_DECLINED)

For approval on the client, use this flow: https://developer.paypal.com/demo/checkout/#/pattern/server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文