修改bug

main
elf 11 months ago
parent 59a9da1f7b
commit 6828767657

@ -53,9 +53,14 @@ func Pay(c *gin.Context) (utils.Data, error) {
if err != nil { if err != nil {
return nil, errors.NewBusinessError("请求支付宝失败:" + err.Error()) return nil, errors.NewBusinessError("请求支付宝失败:" + err.Error())
} }
fmt.Println(result.String()) 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{ data := utils.Data{
"pay_url": result.String(), "pay_url": global.Config.Server.Domain + "/payment/" + order.Token
} }
return data, nil 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 { func postToSdk(url string, data map[string]interface{}) string {
content, err := json.Marshal(data) content, err := json.Marshal(data)
if err != nil { if err != nil {
@ -187,3 +202,22 @@ func Return(c *gin.Context) {
c.Redirect(http.StatusFound, order.ReturnUrl) 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)
}

@ -17,8 +17,10 @@ func main() {
r.GET("/ping", utils.BuildJsonHandler(api.Index)) r.GET("/ping", utils.BuildJsonHandler(api.Index))
r.GET("/error", utils.BuildJsonHandler(api.Error)) r.GET("/error", utils.BuildJsonHandler(api.Error))
r.POST("/pay", utils.BuildJsonHandler(api.Pay)) r.POST("/pay", utils.BuildJsonHandler(api.Pay))
r.POST("/payment/:token", utils.BuildHandler(api.Payment))
r.POST("/notify", utils.BuildHandler(api.Notify)) r.POST("/notify", utils.BuildHandler(api.Notify))
r.GET("/return/:order_no", utils.BuildHandler(api.Return)) 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 err := r.Run() // listen and serve on 0.0.0.0:8080
panic(err) panic(err)
} }

@ -21,6 +21,7 @@ type Order struct {
NotifyUrl string NotifyUrl string
ReturnUrl string ReturnUrl string
Subject string Subject string
Token string
PromoteId int PromoteId int
PromoteAccount string PromoteAccount string
PayTime *time.Time PayTime *time.Time

@ -0,0 +1,5 @@
package request
type PaymentRequest struct {
Token string `url:"token"`
}

@ -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))
}
Loading…
Cancel
Save