diff --git a/common/model/staff_pay_log.go b/common/model/staff_pay_log.go index c88766c..ed9825f 100644 --- a/common/model/staff_pay_log.go +++ b/common/model/staff_pay_log.go @@ -3,6 +3,7 @@ package model type StaffPayLog struct { Id int64 StaffId int64 + OutTradeNo string PayType string Amount int64 Title string diff --git a/common/model/staff_salary.go b/common/model/staff_salary.go index 4d5ef8e..b0bd48c 100644 --- a/common/model/staff_salary.go +++ b/common/model/staff_salary.go @@ -73,6 +73,10 @@ func (s *StaffSalary) GetBillUrl() string { return billUrl } +func (s *StaffSalary) GetOutTradeNo() string { + return fmt.Sprintf("SALARY_%d_%s_%d", s.CorpId, s.Month, s.UserId) +} + type StaffSalarySort []*StaffSalary func (s StaffSalarySort) Len() int { diff --git a/server/controller/payment.go b/server/controller/payment.go index 3ce484d..0e225b7 100644 --- a/server/controller/payment.go +++ b/server/controller/payment.go @@ -51,7 +51,7 @@ func (q *Payment) Pay(ctx *gin.Context) { session.CheckDBError(err) session.CheckNilError(user, fmt.Sprintf("用户[%s]不存在", req.Username)) - if err = CommonService.NewPay().Pay(corp, user, req.Title, req.PayType, req.Amount); err != nil { + if err = CommonService.NewPay().Pay(corp, user, req.Title, req.PayType, req.Amount, ""); err != nil { panic(config.ErrInternal.New().Append(err)) } ctx.JSON(http.StatusOK, session.NewRspOk()) diff --git a/server/service/staff_salary.go b/server/service/staff_salary.go index dd344d4..d9d818a 100644 --- a/server/service/staff_salary.go +++ b/server/service/staff_salary.go @@ -161,7 +161,7 @@ func (s *StaffSalary) Pay(sess *session.AdminSession, req *api.PaySalaryReq) { salary.Status = model.StaffSalaryStatusPayed dao.NewStaffSalaryDao().Update(salary) - if err = CommonService.NewPay().Pay(corp, user, title, req.PayType, int64(salary.GetRealSalary()*100)); err != nil { + if err = CommonService.NewPay().Pay(corp, user, title, req.PayType, int64(salary.GetRealSalary()*100), salary.GetOutTradeNo()); err != nil { panic(config.ErrInternal.New().Append(err)) } } diff --git a/server/service/staff_user.go b/server/service/staff_user.go index 6f26017..f18b4e7 100644 --- a/server/service/staff_user.go +++ b/server/service/staff_user.go @@ -210,7 +210,7 @@ func (s *StaffUser) Pay(sess *session.AdminSession, req *api.StaffPayReq) { if req.Title == "" { req.Title = "测试" } - if err = CommonService.NewPay().Pay(corp, user, req.Title, req.PayType, cast.ToInt64(req.Amount)); err != nil { + if err = CommonService.NewPay().Pay(corp, user, req.Title, req.PayType, cast.ToInt64(req.Amount), ""); err != nil { panic(config.ErrInternal.New().Append(err)) } } diff --git a/service/pay.go b/service/pay.go index f2d9e0f..59e910b 100644 --- a/service/pay.go +++ b/service/pay.go @@ -12,8 +12,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/smartwalle/alipay/v3" "github.com/smbrave/goutil" - "github.com/spf13/cast" "strings" + "time" ) type Pay struct { @@ -23,7 +23,10 @@ func NewPay() *Pay { return &Pay{} } -func (p *Pay) Pay(corp *model.Corp, user *model.StaffUser, title, payType string, amount int64) error { +func (p *Pay) Pay(corp *model.Corp, user *model.StaffUser, title, payType string, amount int64, outTradeNo string) error { + if outTradeNo == "" { + outTradeNo = fmt.Sprintf("%d_%s_%s", corp.Id, time.Now().Format("200601020304"), user.Id) + } var err error realPayType := "" payee := user.GetPayee() @@ -31,10 +34,10 @@ func (p *Pay) Pay(corp *model.Corp, user *model.StaffUser, title, payType string if payee.AlipayUid == "" { panic("请绑定支付宝账号") } - err = p.payAlipay(corp, user, title, amount) + err = p.payAlipay(corp, user, title, amount, outTradeNo) realPayType = "alipay" } else { - err = p.payWeixin(corp, user, title, amount) + err = p.payWeixin(corp, user, title, amount, outTradeNo) realPayType = "weixin" } @@ -62,13 +65,13 @@ func (p *Pay) Pay(corp *model.Corp, user *model.StaffUser, title, payType string return nil } -func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, title string, amount int64) error { +func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, title string, amount int64, outTradeNo string) error { corpConfig := corp.GetConfig() userPayee := user.GetPayee() cli := config.GetAliPayClient(goutil.If(corpConfig.PayChannel != "", corpConfig.PayChannel, "batiao")) var payReq alipay.FundTransUniTransfer payReq.Remark = title - payReq.OutBizNo = cast.ToString(goutil.GetBigID(0, 0)) + payReq.OutBizNo = outTradeNo payReq.TransAmount = goutil.FormatMoney(amount) payReq.ProductCode = "TRANS_ACCOUNT_NO_PWD" payReq.BizScene = "DIRECT_TRANSFER" @@ -89,7 +92,7 @@ func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, title string, a return nil } -func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, title string, amount int64) error { +func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, title string, amount int64, outTradeNo string) error { cfg := corp.GetConfig() @@ -107,7 +110,7 @@ func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, title string, a // 支付费用 var payReq qyweixin.PayReq - payReq.BillNo = cast.ToString(goutil.GetBigID(0, 0)) + payReq.BillNo = outTradeNo payReq.Title = title payReq.Openid = openid payReq.TotalAmount = amount diff --git a/worker/staff.go b/worker/staff.go index 263ac09..0bdc36e 100644 --- a/worker/staff.go +++ b/worker/staff.go @@ -131,7 +131,11 @@ func (s *Staff) PayStaffSalary(corpId int64, month string) { message = append(message, fmt.Sprintf(`查看明细`, staffSalary.GetBillUrl())) if staffUser.Username == "jiangyong" { - err = service.NewPay().Pay(corp, staffUser, fmt.Sprintf("[%s]工资", staffSalary.Month), model.StaffSalaryPaymentAlipay, int64(100*staffSalary.GetRealSalary())) + err = service.NewPay().Pay(corp, staffUser, + fmt.Sprintf("[%s]工资", staffSalary.Month), + model.StaffSalaryPaymentAlipay, + int64(100*staffSalary.GetRealSalary()), + staffSalary.GetOutTradeNo()) if err == nil { staffSalary.Status = model.StaffSalaryStatusPayed message = append(message, "发放成功")