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