From 68287676570cce5432f7ab07f95b7898434e9390 Mon Sep 17 00:00:00 2001 From: elf <360197197@qq.com> Date: Tue, 12 Dec 2023 01:25:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/index.go | 36 +++++++++++++++++++++++++++++++++++- main.go | 2 ++ model/order.go | 1 + request/PaymentRequest.go | 5 +++++ utils/str.go | 12 ++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 request/PaymentRequest.go create mode 100644 utils/str.go diff --git a/api/index.go b/api/index.go index c128ece..35e83f3 100644 --- a/api/index.go +++ b/api/index.go @@ -53,9 +53,14 @@ func Pay(c *gin.Context) (utils.Data, error) { if err != nil { return nil, errors.NewBusinessError("请求支付宝失败:" + err.Error()) } + fmt.Println(result.String()) + order.PayUrl = result.String() + order.Token = utils.Md5(order.OrderNo + strconv.Itoa(order.UserId) + strconv.FormatInt(time.Now().Unix(), 10)) + global.DB.Save(order) + data := utils.Data{ - "pay_url": result.String(), + "pay_url": global.Config.Server.Domain + "/payment/" + order.Token } return data, nil } @@ -136,6 +141,16 @@ func Notify(c *gin.Context) { } } +func TestPost(c *gin.Context) (utils.Data, error) { + notifyData := make(map[string]interface{}) + notifyData["trade_no"] = "112233" + notifyData["out_trade_no"] = "2222222" + notifyData["trade_status"] = "1" + notifyData["total_amount"] = "11.11" + notifyResult := postToSdk("https://api.jianghuifa.cn/callback.php/Notify/kd_callback", notifyData) + return utils.Data{"hello": notifyResult}, nil +} + func postToSdk(url string, data map[string]interface{}) string { content, err := json.Marshal(data) if err != nil { @@ -187,3 +202,22 @@ func Return(c *gin.Context) { c.Redirect(http.StatusFound, order.ReturnUrl) } + +func Payment(c *gin.Context) { + var req request.PaymentRequest + // 将路由参数绑定到结构体中 + if err := c.ShouldBindUri(&req); err != nil { + c.Writer.WriteHeader(http.StatusOK) + c.Writer.Write([]byte("参数错误")) + return + } + order := model.Order{} + tx := global.DB.Where("token = ?", req.Token).First(&order) + if tx.Error != nil { + c.Writer.WriteHeader(http.StatusOK) + c.Writer.Write([]byte("订单不存在")) + return + } + + c.Redirect(http.StatusFound, order.PayUrl) +} diff --git a/main.go b/main.go index 167dccd..2cf7a91 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,10 @@ func main() { r.GET("/ping", utils.BuildJsonHandler(api.Index)) r.GET("/error", utils.BuildJsonHandler(api.Error)) r.POST("/pay", utils.BuildJsonHandler(api.Pay)) + r.POST("/payment/:token", utils.BuildHandler(api.Payment)) r.POST("/notify", utils.BuildHandler(api.Notify)) r.GET("/return/:order_no", utils.BuildHandler(api.Return)) + r.GET("/test-post", utils.BuildJsonHandler(api.TestPost)) err := r.Run() // listen and serve on 0.0.0.0:8080 panic(err) } diff --git a/model/order.go b/model/order.go index 361de3e..c6b1005 100644 --- a/model/order.go +++ b/model/order.go @@ -21,6 +21,7 @@ type Order struct { NotifyUrl string ReturnUrl string Subject string + Token string PromoteId int PromoteAccount string PayTime *time.Time diff --git a/request/PaymentRequest.go b/request/PaymentRequest.go new file mode 100644 index 0000000..b9af2d2 --- /dev/null +++ b/request/PaymentRequest.go @@ -0,0 +1,5 @@ +package request + +type PaymentRequest struct { + Token string `url:"token"` +} diff --git a/utils/str.go b/utils/str.go new file mode 100644 index 0000000..497b522 --- /dev/null +++ b/utils/str.go @@ -0,0 +1,12 @@ +package utils + +import ( + "crypto/md5" + "io" +) + +func Md5(str string) string { + h := md5.New() + _, _ = io.WriteString(h, str) + return string(h.Sum(nil)) +}