繁体   English   中英

如何从 webhook 重定向到 URL

[英]How to redirect to URL from webhook

我开发了一个 Go 应用程序。 已集成 webhook。 我已成功从 webhook 获取有效负载(JSON 格式)。 但是,我似乎无法进行重定向。

第 1 步:用户在 /live 页面付款。

第 2 步:支付成功后,我的支付处理公司向我的 webhook (/paymentssucess) 发送一个 POST 请求。 付款 function 被调用。 在 payment() 中,我能够解码 JSON 数据,这些数据是 POST 到 /paymentsucess 和 fmt.Println 的。

http.HandleFunc("/paymentsuccess", payment)

现在在第 3 步中,我想将用户(仍然看到 /live)重定向到成功付款的页面(/thankyou)。 在发布到 webhook 的 JSON 中,有一个支付状态字段。 我能够做一个 if 条件来检查该字段是否具有“成功”值。 在这种情况下,fmt.Println("redirecting toThank you")。 这一切都很好。 然后在下一行(在 payment() 函数中),我添加了以下内容

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

我期待当前的客户端页面(仍然在 /live)重定向到 /thankyou。 但这并没有发生。

应该如何处理 webhook 重定向?

更新当 webhook 成功发布到 payment() 时,我收到以下响应和请求

回复

&{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}

要求

&{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}

更新 2感谢@gipsy,我能够实施他建议的步骤。 一直到第 4 步。我有一个 goroutine,每两秒轮询一次查询 mysql 数据库以检查付款状态。 一旦这样,我就可以相应地 fmt.Println 了。 这样可以确认我可以确认付款。 但是...我无法重定向当前客户端页面(用户在 /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)
}

首先,到达您的服务器的 web-hook 请求完全独立于您从用户那里获得的请求。 所以你在 web-hook 句柄中尝试的重定向是行不通的

我认为可以有很多方法来处理这个问题。 一种方法可能是:

  1. 让用户在 /live( 你正在做的) 提交付款
  2. 作为在您的数据库中完成该付款请求的付款集状态的一部分,例如挂起。
  3. 当 web-hook 到达时,将状态更新为已完成。
  4. 继续从 /live 轮询付款状态,并在看到完成时将用户重定向到您想要的任何地方。
  5. 您可以使用 ajax 调用您的服务器进行状态检查。

暂无
暂无

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

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