简体   繁体   English

如何从 webhook 重定向到 URL

[英]How to redirect to URL from webhook

I have developed a Go app.我开发了一个 Go 应用程序。 A webhook has been integrated.已集成 webhook。 I have been successful in getting the payload (JSON format) from the webhook.我已成功从 webhook 获取有效负载(JSON 格式)。 However, I can't seem to do a redirect.但是,我似乎无法进行重定向。

Step 1: User make payment at page /live.第 1 步:用户在 /live 页面付款。

Step 2: Upon successful payment, my payment processing company send a POST request to my webhook (/paymentsucess).第 2 步:支付成功后,我的支付处理公司向我的 webhook (/paymentssucess) 发送一个 POST 请求。 payment function is called.付款 function 被调用。 In payment(), I was able to decode the JSON data that was POST to /paymentsucess and fmt.Println accordingly.在 payment() 中,我能够解码 JSON 数据,这些数据是 POST 到 /paymentsucess 和 fmt.Println 的。

http.HandleFunc("/paymentsuccess", payment)

Now in Step 3, I want to redirect the user (which is still seeing /live) to a page (/thankyou) on successful payment.现在在第 3 步中,我想将用户(仍然看到 /live)重定向到成功付款的页面(/thankyou)。 In the JSON posted to the webhook, there is a payment status field.在发布到 webhook 的 JSON 中,有一个支付状态字段。 I was able to do a if condition to check that this field has value "success".我能够做一个 if 条件来检查该字段是否具有“成功”值。 In such case, fmt.Println("redirecting to thank you").在这种情况下,fmt.Println("redirecting toThank you")。 This is all good.这一切都很好。 Then in the next line (inside payment() function), I added the below然后在下一行(在 payment() 函数中),我添加了以下内容

http.Redirect(w, r, "/thankyou", http.StatusSeeOther)

I was expecting the current client page (which is still at /live) to redirect to /thankyou.我期待当前的客户端页面(仍然在 /live)重定向到 /thankyou。 But this is not happening.但这并没有发生。

How should one handle webhook redirects?应该如何处理 webhook 重定向?

UPDATE When the webhook post successfully to payment(), I receive the below response and request更新当 webhook 成功发布到 payment() 时,我收到以下响应和请求

response回复

&{0xc00029c1e0 0xc0002a0700 0xc000386300 0x1120e20 false false false false 0 {0 0} 0xc000386200 {0xc0003a67e0 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0] 0xc0003481c0 0}

request要求

&{POST /paymentsuccess HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip] Content-Length:[3162] Content-Type:[application/json] User-Agent:[Omise/2019-05-29] X-Datadog-Parent-Id:[6096593905335487199] X-Datadog-Sampling-Priority:[1] X-Datadog-Trace-Id:[8016780258334486542] X-Forwarded-For:[52.74.199.175] X-Forwarded-Proto:[https]] 0xc000386300 <nil> 3162 [] false d4e7c3fca000.ngrok.io:443 map[] map[] <nil> map[] [::1]:60999 /paymentsuccess <nil> <nil> <nil> 0xc000386340}

UPDATE 2 Thanks to @gipsy I as able to implement the steps he suggested.更新 2感谢@gipsy,我能够实施他建议的步骤。 Right up to Step 4. I have a goroutine who poll query mysql database every two seconds to check for payment status.一直到第 4 步。我有一个 goroutine,每两秒轮询一次查询 mysql 数据库以检查付款状态。 Once so, I was able to fmt.Println accordingly.一旦这样,我就可以相应地 fmt.Println 了。 So that confirms that I can confirm payment.这样可以确认我可以确认付款。 BUT... I have trouble redirecting the current client page (user is at /live)...但是...我无法重定向当前客户端页面(用户在 /live)...

func startPolling1(ShopName, SourceID, Status string) (string, bool) {
    for {
        time.Sleep(2 * time.Second)
        Amount, Result := queryDBForPaymentSuccess(ShopName, SourceID, Status)
        if Result == true {
            fmt.Println("found result")
            fmt.Println("no more polling")

            //HOW DO I DO A REDIRECT HERE? http.Redirect won't work because of error "superfluous response.WriteHeader call"

            break
        }
    }
    return Amount, Result
}

func livemode(res http.ResponseWriter, req *http.Request) {
if !alreadyLoggedIn(req) {
        http.Redirect(res, req, "/", http.StatusSeeOther)
        return
    }
    Shop = getUser(res, req)

    if req.Method == http.MethodPost {
       //Some codes here
       go startPolling1(Shop.Shopname, Charge.Source.ID, "successful")
       //Some codes here
     }
   }
 tpl.ExecuteTemplate(res, "golive.html", data1)
}

First of all the web-hook request coming to your server is totally independent from the request you get from your user.首先,到达您的服务器的 web-hook 请求完全独立于您从用户那里获得的请求。 So a redirect you try in the web-hook handle is not going to work所以你在 web-hook 句柄中尝试的重定向是行不通的

I think there could be many way to handle this.我认为可以有很多方法来处理这个问题。 One way could be:一种方法可能是:

  1. Let the user submit the payment at /live( which you are doing)让用户在 /live( 你正在做的) 提交付款
  2. As part of completing the payment set status for that payment request in your db, say pending.作为在您的数据库中完成该付款请求的付款集状态的一部分,例如挂起。
  3. When the web-hook arrives update the status to completed.当 web-hook 到达时,将状态更新为已完成。
  4. Keep polling for the payment status from /live and when it see completed redirect user to wherever you want.继续从 /live 轮询付款状态,并在看到完成时将用户重定向到您想要的任何地方。
  5. You could use an ajax call to your server for the status check.您可以使用 ajax 调用您的服务器进行状态检查。

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

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