简体   繁体   English

通过扫描二维码完成支付后如何将react app重定向到支付成功页面?

[英]How to redirect react app to payment success page after completing payment by scanning a QR code?

I have a PERN (Postgres, Express, React, Node) e-commerce app.我有一个PRN (Postgres、Express、React、Node)电子商务应用程序。 I want to notify a user that the payment is successful by redirecting to a payment successful page after a user scans QR code to pay.我想在用户扫描二维码支付后,通过重定向到支付成功页面来通知用户支付成功。

However, I'm not sure how do I achieve this.但是,我不确定如何实现这一目标。

Currently, after the user completed a payment, I will receive a REST's POST request for payment confirmation from a bank API.目前,在用户完成支付后,我会收到来自银行 API 的POST支付确认请求。 . . However, I'm also using GraphQL and I think that Graphql's Subscription could be what I'm trying to achieve.但是,我也在使用 GraphQL,我认为Graphql 的Subscription可能是我想要实现的。

Does anyone know how am I supposed to achieve this?有谁知道我应该如何实现这一目标?

  1. Are there anyways to send a request from REST to Graphql on my own server?无论如何要在我自己的服务器上从 REST 向 Graphql 发送请求吗?
  2. Or are there any better ways to do this?或者有没有更好的方法来做到这一点?

Just like what is done in this video:就像在这个视频中所做的一样:

Do I understand correctly that you are looking for a way to notify your client that the payment was successful?我是否正确理解您正在寻找一种方法来通知您的客户付款成功?

I assume you know how to handle redirect logic so here are the ways you can notify your web-app from the server.我假设你知道如何处理重定向逻辑,所以这里是你可以从服务器通知你的 web 应用程序的方法。

1. Regular HTTP request 1. 常规 HTTP 请求

The client sends a regular HTTP-request to your server and you withhold the response until you received the 'payment completed' request from the bank API.客户端向您的服务器发送一个常规的 HTTP 请求,您保留响应,直到您收到来自银行 API 的“付款完成”请求。 If your request times out before the bank API confirms the payment - you simply send the same request again.如果您的请求在银行 API 确认付款之前超时 - 您只需再次发送相同的请求。 This pattern is also known as Long Polling这种模式也称为长轮询

2. Server-Sent Events 2. 服务器发送事件

Your client can open a channel to your server that allow your server to send Events to your client (one-way).您的客户端可以打开一个到您的服务器的通道,允许您的服务器向您的客户端发送事件(单向)。 It is fairly easy to implement and a solid way to handle one-way communication.它相当容易实现,也是一种处理单向通信的可靠方法。 Check out the MDN documentation .查看 MDN 文档

3. Web-SocketsMDN 3. Web-SocketsMDN

You are probably familiar with these since they are the de-facto standard nowadays.您可能对这些很熟悉,因为它们是当今事实上的标准。 Similar to Server-Sent Events but they allow two-way communication.类似于服务器发送的事件,但它们允许双向通信。

Most libraries that handle client-server communication implement a combination of these technologies in order to provide a fallback solution.大多数处理客户端-服务器通信的库都实现了这些技术的组合,以提供回退解决方案。 I would recommend to stick to one of these libraries as they handle many edge-cases that are otherwise work-intensive to cover.我建议坚持使用其中一个库,因为它们处理许多需要大量工作的边缘案例。

As you pointed out, if you are already using GraphQL you can simply use its subscriptions api .正如您所指出的,如果您已经在使用 GraphQL ,则可以简单地使用其订阅 api It uses WebSockets with a fallback to long-polling underneath.它使用 WebSockets 并回退到底层的长轮询。

Other popular options include socket.io or graphql-sse .其他流行的选项包括socket.iographql-sse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM