From e11b4f461177fa3df4fd4b325cd13a21fe08585f Mon Sep 17 00:00:00 2001 From: richboo111 Date: Fri, 29 Jul 2022 15:23:21 +0800 Subject: [PATCH] mixpay --- business/jxstore/cms/order.go | 65 ++++++++++++------------- business/jxstore/financial/bill.go | 16 ++++++ business/jxstore/financial/financial.go | 21 +++++++- business/jxstore/financial/pay.go | 11 ++++- business/model/bill.go | 18 +++++++ business/model/order.go | 2 +- controllers/order_controller.go | 4 +- globals/beegodb/beegodb.go | 2 +- 8 files changed, 97 insertions(+), 42 deletions(-) diff --git a/business/jxstore/cms/order.go b/business/jxstore/cms/order.go index 20fe70585..5c373b3c0 100644 --- a/business/jxstore/cms/order.go +++ b/business/jxstore/cms/order.go @@ -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 } } diff --git a/business/jxstore/financial/bill.go b/business/jxstore/financial/bill.go index 6f6b9cdae..6da16f798 100644 --- a/business/jxstore/financial/bill.go +++ b/business/jxstore/financial/bill.go @@ -11,6 +11,17 @@ import ( "time" ) +func AddMixPay(txDB orm.TxOrmer, orderID string, balancePrice, totalPrice, method int) error { + mixPayInfo := &model.MixPay{ + OrderID: orderID, + BalancePrice: balancePrice, + TotalPrice: totalPrice, + Method: method, + } + dao.WrapAddIDCULEntity(mixPayInfo, jxcontext.AdminCtx.GetUserName()) + return dao.CreateEntityTx(txDB, mixPayInfo) +} + func AddBillIncome(txDB orm.TxOrmer, billID int64, billType, incomePrice, jobID int) (err error) { billIncome := &model.BillIncome{ BillID: billID, @@ -50,6 +61,11 @@ func AddUserBillDb(db *dao.DaoDB, billID int64, userID string) (err error) { return dao.CreateEntity(db, userBillInsert) } +// +//func AddExpendMixPay(txDB orm.TxOrmer, userBill *model.UserBill, billType, price, jobID int) (err error) { +//err=AddMixPay() +//} + func GetUserBillDetail(ctx *jxcontext.Context, userID, fromTime, toTime string, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) { return dao.GetUserBillDetail(dao.GetDB(), userID, utils.Str2Time(fromTime), utils.Str2Time(toTime), pageSize, offset) } diff --git a/business/jxstore/financial/financial.go b/business/jxstore/financial/financial.go index cb222f4c2..6b4043109 100644 --- a/business/jxstore/financial/financial.go +++ b/business/jxstore/financial/financial.go @@ -292,13 +292,30 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { dao.Rollback(db, txdb) return err } + //1-余额,2-微信,5-混合 + if order.PayMethod == 5 { + userBill, err := dao.GetUserBill(db, order.UserID, "") + if err != nil { + return err + } + //创建混合支付账单 + totalPrice := order.PayPrice + userBill.AccountBalance + if err := AddMixPay(txdb, order.OrderID, userBill.AccountBalance, totalPrice, 5); err != nil { + dao.Rollback(db, txdb) + return err + } + //余额清空 + if err := dao.UpdateUserBill(order.UserID, 0); err != nil { + globals.SugarLogger.Debug("修改余额失败") + dao.Rollback(db, txdb) + return err + } + } dao.Commit(db, txdb) if call.TrxStatus == tonglianpayapi.TrxStatusSuccess { switch order.OrderType { case model.PayType4Express: err = q_bida.CreateOrder2QBiDa(&userOrder, order.OrderID) - case model.PayType4Member, model.PayType4Recharge: - err = OnPayFinished(order) } } return err diff --git a/business/jxstore/financial/pay.go b/business/jxstore/financial/pay.go index 05cac2921..a34da77ff 100644 --- a/business/jxstore/financial/pay.go +++ b/business/jxstore/financial/pay.go @@ -4,6 +4,7 @@ import ( "fmt" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/globals" + "github.com/astaxie/beego/client/orm" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/model" @@ -105,8 +106,9 @@ func OnCashFinished(order *model.Order) (err error) { //微信支付充值会员 func OnWXPayFinished(order *model.Order) (err error) { var ( - db = dao.GetDB() - ctx *jxcontext.Context + db = dao.GetDB() + ctx *jxcontext.Context + txDB orm.TxOrmer ) globals.SugarLogger.Debugf("OnWXPayFinished begin modify account order: %v", utils.Format4Output(order, false)) //判断是新会员充值还是老会员续费 @@ -126,6 +128,11 @@ func OnWXPayFinished(order *model.Order) (err error) { return err } } + order.Status = model.OrderStatusFinished + //更新order状态 + if _, err := dao.UpdateEntityTx(txDB, &order, "Status"); err != nil { + return err + } } else { globals.SugarLogger.Debugf("OnWXPayFinished 暂不支持此订单类型 order: %v", utils.Format4Output(order, false)) return fmt.Errorf("暂不支持此订单类型!") diff --git a/business/model/bill.go b/business/model/bill.go index 1e2860943..07500c322 100644 --- a/business/model/bill.go +++ b/business/model/bill.go @@ -43,6 +43,24 @@ var ( } ) +//混合支付详情表 +type MixPay struct { + ModelIDCUL + + OrderID string `orm:"column(order_id)" json:"order_id"` //账单ID 对应order表 + BalancePrice int `orm:"column(balance_price)" json:"balance_price"` //余额支付部分 + TotalPrice int `orm:"column(total_price)" json:"total_price"` //订单总额 + WxPrice int `orm:"column(wx_price)" json:"wx_price"` //微信支付部分 + Method int `orm:"column(method)" json:"method"` //支付方式 1-余额支付,2-微信支付,5-余额+微信混合支付 +} + +func (v *MixPay) TableIndex() [][]string { + return [][]string{ + []string{"OrderID"}, + []string{"CreatedAt"}, + } +} + //账单收入表 type BillIncome struct { ModelIDCUL diff --git a/business/model/order.go b/business/model/order.go index 9a74fb473..7ba7219f2 100644 --- a/business/model/order.go +++ b/business/model/order.go @@ -73,7 +73,7 @@ type Order struct { CityCode int `orm:"default(0)" json:"cityCode"` // 提交订单时用户所在城市 DistrictCode int `orm:"default(0)" json:"districtCode"` // 城市code Address string `orm:"size(255)" json:"address"` // 地址 - PayMethod int `orm:"size(255)" json:"address"` // 支付方式1-余额支付,2-微信支付,3-微信提现,4-支付宝提现 + PayMethod int `orm:"size(255)" json:"address"` // 支付方式1-余额支付,2-微信支付,3-微信提现,4-支付宝提现 5-余额+微信混合支付 } func (v *Order) TableUnique() [][]string { diff --git a/controllers/order_controller.go b/controllers/order_controller.go index 509c55ae3..5a00d28dc 100644 --- a/controllers/order_controller.go +++ b/controllers/order_controller.go @@ -43,8 +43,8 @@ func (c *OrderController) Pay() { // @Failure 200 {object} controllers.CallResult // @router /PayByBalance [post] func (c *OrderController) PayByBalance() { - c.callPayByBalance(func(params *tOrderPayByBalanceParams) (interface{}, string, error) { - retVal, _, err := cms.PayByBalance(params.Ctx, params.OrderID, params.RestPrice, params.PayType, params.VendorPayType, params.AppId) + c.callPayByBalance(func(params *tOrderPayByBalanceParams) (retVal interface{}, errMsg string, err error) { + retVal, _, err = cms.PayByBalance(params.Ctx, params.OrderID, params.RestPrice, params.PayType, params.VendorPayType, params.AppId) return retVal, "", err }) } diff --git a/globals/beegodb/beegodb.go b/globals/beegodb/beegodb.go index 096b3d15d..737436b28 100644 --- a/globals/beegodb/beegodb.go +++ b/globals/beegodb/beegodb.go @@ -24,7 +24,7 @@ func Init() { orm.RegisterModel(&model.UserBill{}) orm.RegisterModel(&model.BillIncome{}) orm.RegisterModel(&model.BillExpend{}) - + orm.RegisterModel(&model.MixPay{}) //混合支付 //支付订单 orm.RegisterModel(&model.Order{}) orm.RegisterModel(&model.DeliveryOrder{})