Parcourir la source

支付回调,最大佣金

k.zhang il y a 3 mois
Parent
commit
a89aab03ad

+ 2 - 3
apis/shanghu/base.go

@@ -88,9 +88,8 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 		activeV1.POST("/active/config/whxy", UpdateActiveConfigWHXY)      //二维码坐标
 		activeV1.POST("/active/qr", GetClientActiveQR)                    //获取二维码
 		activeV1.POST("/active/unified/order", GroupByUnifiedOrder)       //拼团购买
-		//activeV1.POST("")                                                 //支付回调
-		//
-		//activeV1.POST("")                                                 //拼团购买
+		activeV1.POST("/active/pay/callback", ActivePayCallBack)          //支付回调
+		//邀请次数
 		//待核销金额
 		//
 

+ 7 - 0
apis/shanghu/client.active.draw.num.go

@@ -0,0 +1,7 @@
+package shanghu
+
+import "github.com/gin-gonic/gin"
+
+func InvitedDrawNum(c *gin.Context) {
+
+}

+ 81 - 0
apis/shanghu/merchant.active.config.go

@@ -16,6 +16,7 @@ import (
 	"github.com/go-pay/gopay/wechat"
 	"github.com/shopspring/decimal"
 	"math/rand"
+	"net/http"
 	"strconv"
 	"time"
 )
@@ -209,6 +210,8 @@ func ActiveConfigInfo(c *gin.Context) {
 		groupBuyInfo.OriginalPrice = v.OriginalPrice
 		groupBuyInfo.RebateRate = v.RebateRate
 
+		groupBuyInfo.MaxRebatePrice = v.GroupBuyOnePrice.Mul(decimal.NewFromInt32(int32(v.RebateRate)).Div(decimal.NewFromInt32(100)))
+
 		groupBuyProject.GroupBuyID = v.ID
 
 		projectList, _, err := groupBuyProject.GetGroupBuyProjectList()
@@ -789,3 +792,81 @@ func Pay(c *gin.Context) {
 	// 获取参数
 	//
 }
+
+func ActivePayCallBack(c *gin.Context) {
+	var payLog shanghu.ActivePayCallbackLog
+	var payTrans shanghu.ClientActivePayTrans
+	wxNotify, err := wechat.ParseNotifyToBodyMap(c.Request)
+	if err != nil {
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+
+	//通知回调log
+	payLog.CallBackLog = wxNotify.JsonBody()
+	payLog.ThirdTradeNo = wxNotify.Get("transaction_id")
+	payLog.OutTradeNo = wxNotify.Get("out_trade_no")
+	payLog.CreatedAt = time.Now()
+	payLog.UpdatedAt = time.Now()
+
+	_, err = payLog.Create()
+	if err != nil {
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+
+	if wxNotify.Get("return_code") != "SUCCESS" || wxNotify.Get("result_code") != "SUCCESS" {
+		payLog.ErrLog = "微信返回错误:" + wxNotify.Get("return_code") + "--" + wxNotify.Get("result_code")
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+
+	//校验金额
+	payTrans.OutTradeNo = wxNotify.Get("out_trade_no")
+	payTransInfo, err := payTrans.GetPayTransByTradeNo()
+	if err != nil {
+		payLog.ErrLog = "查询交易信息错误:" + " err=" + err.Error()
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+	// 判断金额与支付流水是否一致
+	totalFee, err := decimal.NewFromString(wxNotify.Get("total_fee"))
+	if err != nil {
+		payLog.ErrLog = "解析总金额报错:" + "err=" + err.Error()
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+	if !totalFee.Equal(payTransInfo.Amount.Mul(decimal.NewFromInt(100))) {
+		payLog.ErrLog = "验证金额报错:total_fee=" + wxNotify.Get("total_fee") + " amount=" + payTransInfo.Amount.String()
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+	// 解析支付时间
+	timeEnd, err := time.ParseInLocation("20060102150405", wxNotify.Get("time_end"), tools.TimeLocation)
+	if err != nil {
+		payLog.ErrLog = "付款时间解析出错:err=" + err.Error()
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+
+	payTrans.ThirdTradeNo = wxNotify.Get("transaction_id")
+	payTrans.Status = 2 //支付成功
+	payTrans.PayTime = timeEnd
+	payTrans.AccountStatus = 1 //未分账
+
+	err = payTrans.UpdatePayTransByTradeNo()
+	if err != nil {
+		payLog.ErrLog = "更新支付状态失败:err=" + err.Error()
+		payLog.UpdateMerchant()
+		c.XML(http.StatusOK, failResp)
+		return
+	}
+
+	c.XML(http.StatusOK, successResp)
+
+}

+ 1 - 0
apis/shanghu/models/active.config.go

@@ -62,6 +62,7 @@ type DrawGroupBuy struct {
 	ActivityEndTime    string            `json:"activity_end_time"`     // 活动结束时间
 	ActivityStartTime  string            `json:"activity_start_time"`   // 活动开始时间
 	RebateRate         int               `json:"rebate_rate"`           // 佣金比例
+	MaxRebatePrice     decimal.Decimal   `json:"max_rebate_price"`      //最大佣金
 	GroupBuyProject    []GroupBuyProject `json:"group_buy_project"`
 }
 

+ 7 - 0
apis/shanghu/models/client.active.draw.num.go

@@ -0,0 +1,7 @@
+package models
+
+type InvitedDrawNumRequest struct {
+	OpenId         string `json:"open_id"`
+	InvitedCode    string `json:"invited_code"`
+	ActiveConfigId int64  `json:"active_config_id"`
+}

+ 48 - 0
models/shanghu/active.pay.callback.log.go

@@ -0,0 +1,48 @@
+package shanghu
+
+import (
+	orm "duoduo/database"
+	"time"
+)
+
+type ActivePayCallbackLog struct {
+	ID           int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
+	CallBackLog  string    `gorm:"column:call_back_log;type:text" json:"call_back_log"`
+	ErrLog       string    `gorm:"column:err_log;type:varchar(255)" json:"err_log"`               // 处理失败原因
+	OutTradeNo   string    `gorm:"column:out_trade_no;type:varchar(255)" json:"out_trade_no"`     // 交易id
+	ThirdTradeNo string    `gorm:"column:third_trade_no;type:varchar(255)" json:"third_trade_no"` // 微信交易id
+	CreateBy     int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`             // 创建者
+	UpdateBy     int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`             // 更新者
+	CreatedAt    time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`          // 创建时间
+	UpdatedAt    time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`          // 最后更新时间
+	DeletedAt    time.Time `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`          // 删除时间
+}
+
+func (m *ActivePayCallbackLog) TableName() string {
+	return "active_pay_callback_log"
+}
+
+func (u *ActivePayCallbackLog) Create() (ActivePayCallbackLog, error) {
+	var doc ActivePayCallbackLog
+	var err error
+
+	doc = *u
+	err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
+	if err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (m *ActivePayCallbackLog) UpdateMerchant() error {
+
+	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("third_trade_no = ? ", m.ThirdTradeNo).Updates(
+		map[string]interface{}{
+			"err_log":    m.ErrLog,
+			"updated_at": time.Now()}).Error; err != nil {
+		return err
+	}
+
+	return nil
+}

+ 27 - 0
models/shanghu/client.active.pay.trans.go

@@ -53,3 +53,30 @@ func (m *ClientActivePayTrans) Create() (ClientActivePayTrans, error) {
 
 	return doc, nil
 }
+
+func (m *ClientActivePayTrans) GetPayTransByTradeNo() (ClientActivePayTrans, error) {
+	var doc ClientActivePayTrans
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("out_trade_no = ?  ", m.OutTradeNo)
+
+	if err := table.Select("*").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (m *ClientActivePayTrans) UpdatePayTransByTradeNo() error {
+
+	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("out_trade_no = ? ", m.OutTradeNo).Updates(
+		map[string]interface{}{
+			"third_trade_no": m.ThirdTradeNo,
+			"pay_time":       m.PayTime,
+			"status":         m.Status,
+			"account_status": m.AccountStatus,
+			"updated_at":     time.Now()}).Error; err != nil {
+		return err
+	}
+	return nil
+}