This commit is contained in:
richboo111
2022-07-29 15:23:21 +08:00
parent 4bbf80c7a4
commit e11b4f4611
8 changed files with 97 additions and 42 deletions

View File

@@ -3,6 +3,7 @@ package cms
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/jxstore/event"
"github.com/astaxie/beego/client/orm"
"strings"
"time"
@@ -129,12 +130,13 @@ var (
//余额支付 微信补差值
func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int, vendorPayType, appID string) (*financial.WxPayParam, string, error) {
var (
db = dao.GetDB()
//wxPayParam *financial.WxPayParam
db = dao.GetDB()
txDB orm.TxOrmer
)
//获取订单信息
globals.SugarLogger.Debug("begin get order+info")
globals.SugarLogger.Debug("begin get order_info")
orderInfo, err := dao.GetOrderByID(db, orderID)
//tempPrice := orderInfo.PayPrice
if err != nil {
return nil, "获取订单信息失败", err
}
@@ -145,54 +147,49 @@ func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int
if err != nil {
return nil, "获取用户会员账户余额失败", err
}
txDB, _ := dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db, txDB)
panic(r)
}
}()
if orderInfo.Status == NotPay {
globals.SugarLogger.Debug("进入账单未支付")
globals.SugarLogger.Debug("user_bill.balance==================", userBill.AccountBalance)
if userBill.AccountBalance > 0 && restPrice == 0 {
//余额 全款支付
// (3)使用余额且 余额大于支付金额
if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && restPrice == 0 {
//余额>0
globals.SugarLogger.Debug("进入余额支付部分")
if userBill.AccountBalance > orderInfo.PayPrice && userBill.AccountBalance-orderInfo.PayPrice > 0 {
if err = financial.AddExpendUpdateAccount(txDB, userBill, model.BillTypePayByAccountBalance, orderInfo.PayPrice, 0); err != nil {
dao.Rollback(db, txDB)
return nil, "使用余额支付失败:", err
}
orderInfo.Status = AlreadyPay
//修改订单状态
orderInfo.Status = model.OrderStatusSuccessPay
orderInfo.PayMethod = 1 //1-余额支付2-微信支付
}
}
if restPrice != 0 {
//需支付部分
//restPrice := orderInfo.PayPrice - userBill.AccountBalance
globals.SugarLogger.Debug("进入混合支付部分")
orderInfo.PayPrice = restPrice
globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice)
//修改余额为0
if err := dao.UpdateUserBill(orderInfo.UserID, 0); err != nil {
globals.SugarLogger.Debug("修改余额失败")
dao.Rollback(db, txDB)
return nil, "余额修改失败:", err
if restPrice > 0 {
//1用户不使用余额或者余额=0 即直接微信支付
//if orderInfo.PayPrice == restPrice || userBill.AccountBalance == 0 {
// WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID)
// //orderInfo.PayMethod = 2 //微信支付方式
// if err != nil {
// globals.SugarLogger.Debug("err=================", err)
// return nil, "微信支付失败:", err
// }
// return WxPayParam, "", err
//}
//2用户使用余额剩余微信支付
if userBill.AccountBalance+restPrice != orderInfo.PayPrice {
return nil, "支付金额错误,请重新计算", err
}
//增加用户账单
globals.SugarLogger.Debug("开始创建用户账单")
globals.SugarLogger.Debug("微信需支付", orderInfo.PayPrice)
if err = financial.AddBillExpend(txDB, userBill.BillID, model.BillTypePayByAccountBalance, orderInfo.PayPrice, 0); err != nil {
globals.SugarLogger.Debug("创建账单失败")
dao.Rollback(db, txDB)
return nil, "创建账单失败", err
if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice {
globals.SugarLogger.Debug("进入混合支付部分")
orderInfo.PayMethod = 5 //混合支付
orderInfo.PayPrice = restPrice
globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice)
}
WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID)
if err != nil {
dao.Rollback(db, txDB)
globals.SugarLogger.Debug("err=================", err)
return nil, "微信支付失败:", err
}
dao.Commit(db, txDB)
//orderInfo.PayPrice = tempPrice //存储原价
return WxPayParam, "", err
}
}