247 lines
7.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"github.com/gin-gonic/gin"
"hepa-calc-api/api/dao"
"hepa-calc-api/api/responses"
"hepa-calc-api/api/service"
"hepa-calc-api/extend/weChat"
"hepa-calc-api/global"
"hepa-calc-api/utils"
"net/http"
"time"
)
// CallBack 回调
type CallBack struct{}
// WxPaySingle 微信支付回调-单项
func (r *CallBack) WxPaySingle(c *gin.Context) {
notifyReq, transaction, err := weChat.ParseNotify(c)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 记录日志
utils.LogJsonErr("微信支付回调-单项", notifyReq)
utils.LogJsonErr("微信支付回调-单项", transaction)
if transaction == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少订单数据"})
return
}
if transaction.OutTradeNo == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少外部订单号"})
return
}
// 查询订单
orderSingleDao := dao.OrderSingleDao{}
maps := make(map[string]interface{})
maps["order_no"] = transaction.OutTradeNo
orderSingle, err := orderSingleDao.GetOrderSingle(maps)
if err != nil || orderSingle == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "无订单数据"})
return
}
// 验证订单状态-订单状态1:待支付 2:已完成 3:已取消)
if orderSingle.OrderStatus != 1 {
message := "订单状态:" + utils.OrderSingleStatusToString(orderSingle.OrderStatus) + " 无需处理"
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": message})
return
}
// 处理支付状态
if transaction.TradeState == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少支付状态"})
return
}
// 处理支付状态
wxPayResult, err := weChat.HandlePayStatus(transaction)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": err})
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 修改订单
orderSingleData := make(map[string]interface{})
if wxPayResult.OrderStatus != nil {
orderSingleData["order_status"] = &wxPayResult.OrderStatus
}
orderSingleData["pay_status"] = &wxPayResult.PayStatus
if wxPayResult.PayTime != nil {
orderSingleData["pay_time"] = &wxPayResult.PayTime
}
orderSingleData["escrow_trade_no"] = transaction.TransactionId
orderSingleData["updated_at"] = time.Now().Format("2006-01-02 15:04:05")
err = orderSingleDao.EditOrderSingleById(tx, orderSingle.OrderId, orderSingleData)
if err != nil {
tx.Rollback()
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": err})
return
}
// 增加单项支付次数
questionService := service.QuestionService{}
res, err := questionService.AddQuestionPayCount(tx, orderSingle.QuestionId)
if err != nil || res == false {
tx.Rollback()
responses.FailWithMessage("内部错误", c)
return
}
// 增加用户单项支付次数
userService := service.UserService{}
res, err = userService.AddUserSingleSubmitCount(tx, orderSingle.UserId)
if err != nil || res == false {
tx.Rollback()
responses.FailWithMessage("内部错误", c)
return
}
tx.Commit()
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "OK"})
}
// WxPayMember 微信支付回调-会员
func (r *CallBack) WxPayMember(c *gin.Context) {
notifyReq, transaction, err := weChat.ParseNotify(c)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 记录日志
utils.LogJsonErr("微信支付回调-会员", notifyReq)
utils.LogJsonErr("微信支付回调-会员", transaction)
if transaction == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少订单数据"})
return
}
if transaction.OutTradeNo == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少外部订单号"})
return
}
// 查询订单
orderMemberDao := dao.OrderMemberDao{}
maps := make(map[string]interface{})
maps["order_no"] = transaction.OutTradeNo
orderMember, err := orderMemberDao.GetOrderMember(maps)
if err != nil || orderMember == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "无订单数据"})
return
}
// 验证订单状态-订单状态1:待支付 2:已完成 3:已取消)
if orderMember.OrderStatus != 1 {
message := "订单状态:" + utils.OrderSingleStatusToString(orderMember.OrderStatus) + " 无需处理"
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": message})
return
}
// 处理支付状态
if transaction.TradeState == nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "缺少支付状态"})
return
}
// 处理支付状态
wxPayResult, err := weChat.HandlePayStatus(transaction)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": err})
return
}
// 获取用户数据
userDao := dao.UserDao{}
user, err := userDao.GetUserById(orderMember.UserId)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": "ERROR", "message": "用户数据错误"})
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 修改订单
orderSingleData := make(map[string]interface{})
if wxPayResult.OrderStatus != nil {
orderSingleData["order_status"] = &wxPayResult.OrderStatus
}
orderSingleData["pay_status"] = &wxPayResult.PayStatus
if wxPayResult.PayTime != nil {
orderSingleData["pay_time"] = &wxPayResult.PayTime
}
orderSingleData["escrow_trade_no"] = transaction.TransactionId
orderSingleData["updated_at"] = time.Now().Format("2006-01-02 15:04:05")
err = orderMemberDao.EditOrderMemberById(tx, orderMember.OrderId, orderSingleData)
if err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": err})
return
}
// 处理未支付单项订单
if *wxPayResult.OrderStatus == 2 {
orderSingleService := service.OrderSingleService{}
res, err := orderSingleService.CompleteUnPayOrderSingle(tx, orderMember.UserId)
if err != nil || res == false {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": err})
return
}
}
// 处理用户会员过期时间
if *wxPayResult.OrderStatus == 2 {
// 获取订单配置
systemMemberDao := dao.SystemMemberDao{}
systemMember, err := systemMemberDao.GetSystemMemberById(orderMember.SystemMemberId)
if err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "会员配置错误"})
return
}
userService := service.UserService{}
res := userService.AddUserMemberValidDate(tx, user, int(systemMember.MemberDays))
if res == false {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "增加用户会员到期时间失败"})
return
}
}
// 增加用户单项提交次数
userService := service.UserService{}
res, err := userService.AddUserMemberBuyCount(tx, user.UserId)
if err != nil || res == false {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "增加用户会员到期时间失败"})
return
}
tx.Commit()
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "OK"})
}