main
ljl 11 months ago
parent 5a2d4d3337
commit 5198b6e2f1

@ -41,11 +41,11 @@ func Pay(c *gin.Context) (utils.Data, error) {
var param = alipay.TradeWapPay{} var param = alipay.TradeWapPay{}
param.OutTradeNo = order.OrderNo param.OutTradeNo = order.OrderNo
param.TotalAmount = strconv.FormatFloat(order.PayAmount, 'E', -1, 64) param.TotalAmount = strconv.FormatFloat(order.PayAmount, 'f', -1, 64)
param.Subject = order.Subject param.Subject = order.Subject
// global.Config.Server.Domain token := utils.Md5(order.OrderNo + strconv.Itoa(order.UserId) + strconv.FormatInt(time.Now().Unix(), 10))
param.NotifyURL = global.Config.Server.Domain + "/notify" param.NotifyURL = global.Config.Server.Domain + "/notify"
param.ReturnURL = global.Config.Server.Domain + "/return" param.ReturnURL = global.Config.Server.Domain + "/return?token=" + token
client, err := alipay.New(global.Config.Alipay.AppId, global.Config.Alipay.PrivateKey, true) client, err := alipay.New(global.Config.Alipay.AppId, global.Config.Alipay.PrivateKey, true)
if err != nil { if err != nil {
return nil, errors.NewBusinessError("初始化支付宝失败") return nil, errors.NewBusinessError("初始化支付宝失败")
@ -57,12 +57,11 @@ func Pay(c *gin.Context) (utils.Data, error) {
fmt.Println(result.String()) fmt.Println(result.String())
order.PayUrl = result.String() order.PayUrl = result.String()
// utils.Md5(order.OrderNo + strconv.Itoa(order.UserId) + strconv.FormatInt(time.Now().Unix(), 10)) order.Token = token
order.Token = order.OrderNo global.DB.Save(&order)
global.DB.Save(order)
data := utils.Data{ data := utils.Data{
"pay_url": global.Config.Server.Domain + "/payment/" + order.OrderNo, "pay_url": global.Config.Server.Domain + "/payment?token=" + token,
} }
return data, nil return data, nil
} }
@ -111,6 +110,9 @@ func Notify(c *gin.Context) {
log.Printf("初始化支付宝失败:%v", err) log.Printf("初始化支付宝失败:%v", err)
return return
} }
client.LoadAliPayPublicKey(global.Config.Alipay.AliPublicKey)
notification, err := client.DecodeNotification(c.Request.Form) notification, err := client.DecodeNotification(c.Request.Form)
if err != nil { if err != nil {
log.Printf("解析异步通知发生错误:%v", err) log.Printf("解析异步通知发生错误:%v", err)
@ -187,39 +189,29 @@ func postToSdk(url string, data map[string]interface{}) string {
} }
func Return(c *gin.Context) { func Return(c *gin.Context) {
var req request.ReturnRequest token := c.Query("token")
// 将路由参数绑定到结构体中
if err := c.ShouldBindUri(&req); err != nil {
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte("参数错误"))
return
}
order := model.Order{} order := model.Order{}
tx := global.DB.Where("order_no = ?", req.OrderNo).First(&order) tx := global.DB.Where("token = ?", token).First(&order)
if tx.Error != nil { if tx.Error != nil {
c.Writer.WriteHeader(http.StatusOK) c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte("订单不存在")) c.Writer.Write([]byte("订单不存在"))
return return
} }
c.Redirect(http.StatusFound, order.ReturnUrl) c.Redirect(http.StatusMovedPermanently, order.ReturnUrl)
} }
func Payment(c *gin.Context) { func Payment(c *gin.Context) {
var req request.PaymentRequest token := c.Query("token")
// 将路由参数绑定到结构体中
if err := c.ShouldBindUri(&req); err != nil {
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte("参数错误"))
return
}
order := model.Order{} order := model.Order{}
tx := global.DB.Where("token = ?", req.Token).First(&order) tx := global.DB.Where("token = ?", token).First(&order)
fmt.Println(tx)
if tx.Error != nil { if tx.Error != nil {
fmt.Println(tx.Error)
c.Writer.WriteHeader(http.StatusOK) c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte("订单不存在")) c.Writer.Write([]byte("订单不存在"))
return return
} }
c.Redirect(http.StatusFound, order.PayUrl) c.Redirect(http.StatusMovedPermanently, order.PayUrl)
} }

@ -4,6 +4,7 @@ server:
alipay: alipay:
app-id: "2021004129619376" app-id: "2021004129619376"
private-key: "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQChC/5Psy+biyzdT/CkCknQKax44Ol0i6kAWVkw4BYERevISFqOHpVmlztcnYGx3LIDOeReY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/A2m6kVh9LxlJJngXldOkO2Q4VS32RufZntc9fkjiiuUxDBlFPKl5x72eiQVoBX4yM7j0gtBlwQFezsIPD0SRC/uxj75+MtjDUupAQLA4gB4zgSDmWWYAIGQlgXXAQExr5k7P1dXrXJMP9SpIYn3Uaug5ifqxanoiDCmMp/sMFasjlVBDIpLOZuKgBwcX3d2FmhRdWIWR5UVy3IbtWuqTJ8vqH8j6Y2jVFLxBA7+woYc0s7npZ0kTa0yGXFw/9LGinSGshGUCBjuAoi67cW6YvODjYFHuGqsrXZvwIDAQAB"
database: database:
host: "47.98.244.18" host: "47.98.244.18"
port: "3306" port: "3306"

@ -3,4 +3,5 @@ package config
type Alipay struct { type Alipay struct {
AppId string `yaml:"app-id"` AppId string `yaml:"app-id"`
PrivateKey string `yaml:"private-key"` PrivateKey string `yaml:"private-key"`
AliPublicKey string `yaml:"ali-public-key"`
} }

BIN
main

Binary file not shown.

@ -17,9 +17,9 @@ 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.GET("/payment", 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", utils.BuildHandler(api.Return))
r.GET("/test-post", utils.BuildJsonHandler(api.TestPost)) r.GET("/test-post", utils.BuildJsonHandler(api.TestPost))
err := r.Run(":" + global.Config.Server.Port) // listen and serve on 0.0.0.0:8080 err := r.Run(":" + global.Config.Server.Port) // listen and serve on 0.0.0.0:8080
panic(err) panic(err)

@ -2,11 +2,13 @@ package utils
import ( import (
"crypto/md5" "crypto/md5"
"io" "encoding/hex"
) )
func Md5(str string) string { func Md5(str string) string {
h := md5.New() hash := md5.New()
_, _ = io.WriteString(h, str) hash.Write([]byte(str))
return string(h.Sum(nil)) bytes := hash.Sum(nil)
md5Str := hex.EncodeToString(bytes)
return md5Str
} }

Loading…
Cancel
Save