diff --git a/business/jxstore/cms/order.go b/business/jxstore/cms/order.go index 7ffe7ff8d..737734db0 100644 --- a/business/jxstore/cms/order.go +++ b/business/jxstore/cms/order.go @@ -3,7 +3,6 @@ package cms import ( "fmt" "git.rosy.net.cn/jx-callback/business/jxstore/event" - "github.com/astaxie/beego/client/orm" "strings" "time" @@ -81,7 +80,7 @@ func CreateOrder(ctx *jxcontext.Context, type1, orderType int, way string, price return order.OrderID, errCode, err } -func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, appId string) (result *financial.WxPayParam, err error) { +func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, appId string, payPrice int) (result *financial.WxPayParam, err error) { var ( db = dao.GetDB() order = &model.Order{ @@ -93,6 +92,17 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app VendorPayType: vendorPayType, } ) + //支付金额<原金额 + if payPrice < order.PayPrice { + order = &model.Order{ + PayPrice: payPrice, + PayMethod: 5, //混合支付 + } + } else { + order = &model.Order{ + PayMethod: 2, //微信支付 + } + } globals.SugarLogger.Debugf("pay begin……") err = dao.GetEntity(db, order, "OrderID") if order.ID == 0 { @@ -124,20 +134,21 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app } var ( - NotPay = 2 //订单待支付 - AlreadyPay = 1 //订单已支付 + NotPay = 2 //订单待支付 + AlreadyPay = 1 //订单已支付 + Choose = 1 //选中余额支付 + NotChoose = -1 //未选中余额支付 ) //余额支付 微信补差值 -func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int, vendorPayType, appID string) (*financial.WxPayParam, string, error) { +func PayByBalance(ctx *jxcontext.Context, orderID string, isChoose, payType int, vendorPayType, appID string) (*financial.WxPayParam, string, error) { var ( - db = dao.GetDB() - txDB orm.TxOrmer + db = dao.GetDB() + //txDB orm.TxOrmer ) //获取订单信息 globals.SugarLogger.Debug("begin get order_info") orderInfo, err := dao.GetOrderByID(db, orderID) - //tempPrice := orderInfo.PayPrice if err != nil { return nil, "获取订单信息失败", err } @@ -152,46 +163,57 @@ func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int globals.SugarLogger.Debug("进入账单未支付") globals.SugarLogger.Debug("user_bill.balance==================", userBill.AccountBalance) // (3)使用余额且 余额大于支付金额 - if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && restPrice == 0 { - //余额>0 + if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && isChoose == NotChoose { 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 { - return nil, "使用余额支付失败:", err + var order = &model.Order{ + PayPrice: orderInfo.PayPrice, + PayMethod: 1, //余额支付 + } + txDB, _ := dao.Begin(db) + defer func() { + if r := recover(); r != nil { + panic(r) } - //修改订单状态 - orderInfo.Status = model.OrderStatusSuccessPay - orderInfo.PayMethod = 1 //1-余额支付,2-微信支付 + dao.Commit(db, txDB) + }() + if err = financial.AddExpendUpdateAccount(txDB, userBill, model.BillTypePayByAccountBalance, order.PayPrice, 0); err != nil { + dao.Rollback(db, txDB) + return nil, "使用余额支付失败:", err } + //更新订单状态 + order = &model.Order{ + Status: 110, //支付成功 + } + if _, err := dao.UpdateEntityTx(txDB, order, "Status"); err != nil { + dao.Rollback(db, txDB) + return nil, "", err + } + dao.Commit(db, txDB) } - if restPrice > 0 { + if isChoose == Choose { //(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 + if userBill.AccountBalance == 0 { + WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, orderInfo.PayPrice) + if err != nil { + globals.SugarLogger.Debug("err=================", err) + return nil, "微信支付失败:", err + } + return WxPayParam, "", err } + //(2)用户使用余额,剩余微信支付 + totalPrice := orderInfo.PayPrice //订单原价 if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice { globals.SugarLogger.Debug("进入混合支付部分") - orderInfo.PayMethod = 5 //混合支付 - orderInfo.PayPrice = restPrice + //orderInfo.PayMethod = 5 //混合支付状态 + needPay := totalPrice - userBill.AccountBalance //需支付金额 globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice) + WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, needPay) + if err != nil { + globals.SugarLogger.Debug("err=================", err) + return nil, "微信支付失败:", err + } + return WxPayParam, "", err } - WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID) - if err != nil { - globals.SugarLogger.Debug("err=================", err) - return nil, "微信支付失败:", err - } - //orderInfo.PayPrice = tempPrice //存储原价 - return WxPayParam, "", err } } return nil, "", err diff --git a/business/jxstore/financial/bill.go b/business/jxstore/financial/bill.go index 30ecd1cbe..396fda19e 100644 --- a/business/jxstore/financial/bill.go +++ b/business/jxstore/financial/bill.go @@ -11,12 +11,12 @@ import ( "time" ) -func AddMixPay(txDB orm.TxOrmer, orderID string, balancePrice, totalPrice, method int) error { +func AddMixPay(txDB orm.TxOrmer, orderID string, balancePrice, totalPrice, status int) error { mixPayInfo := &model.MixPay{ OrderID: orderID, BalancePrice: balancePrice, TotalPrice: totalPrice, - Method: method, + Status: status, WxPrice: totalPrice - balancePrice, } dao.WrapAddIDCULEntity(mixPayInfo, jxcontext.AdminCtx.GetUserName()) @@ -62,11 +62,6 @@ 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 b5709f61a..49e4ef85b 100644 --- a/business/jxstore/financial/financial.go +++ b/business/jxstore/financial/financial.go @@ -311,6 +311,14 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { return err } } + if order.PayMethod == 5 || order.PayMethod == 2 { + //更新order状态 + order.Status = 110 //支付成功 + if _, err := dao.UpdateEntityTx(txdb, &order, "Status"); err != nil { + dao.Rollback(db, txdb) + return err + } + } dao.Commit(db, txdb) if call.TrxStatus == tonglianpayapi.TrxStatusSuccess { switch order.OrderType { diff --git a/business/model/bill.go b/business/model/bill.go index 07500c322..d771f2aff 100644 --- a/business/model/bill.go +++ b/business/model/bill.go @@ -16,6 +16,7 @@ const ( BillTypePayByAccountBalance = 25 //余额支付 BillTypePayByMixPay1 = 26 //混合支付中的 余额部分状态码 BillTypePayByMixPay2 = 27 //混合支付中的 微信部分状态码 + BillMixPayRefund1 = 28 //混合支付 余额部分退款 BillTypeQuitGroup = 30 //退群 BillTypeJdWaybillOverWeight = 40 //京东物流超重扣款 @@ -51,7 +52,7 @@ type MixPay struct { 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-余额+微信混合支付 + Status int `orm:"column(status)" json:"status"` //订单状态 退款/正常 } func (v *MixPay) TableIndex() [][]string { diff --git a/business/model/dao/dao_bill.go b/business/model/dao/dao_bill.go index c7f980e35..8df438809 100644 --- a/business/model/dao/dao_bill.go +++ b/business/model/dao/dao_bill.go @@ -135,3 +135,27 @@ func UpdateUserBill(userId string, money int) error { _, err := ExecuteSQL(GetDB(), `UPDATE user_bill SET account_balance = ? WHERE user_id = ? `, []interface{}{money, userId}...) return err } + +type MixPayDetail struct { + CreatedAt time.Time `json:"created_at"` + LastOperator string `json:"lastOperator"` + OrderID string `json:"order_id"` + BalancePrice int `json:"balance_price"` + TotalPrice int `json:"total_price"` + WxPrice int `json:"wx_price"` + Status int `json:"status"` +} + +//获取混合支付 余额部分信息 +func GetMixPayDetail(orderID string) (mixPayDetail []*MixPayDetail, err error) { + sql := `SELECT * FROM mix_pay WHERE deleted_at = ? ` + sqlParams := []interface{}{ + utils.DefaultTimeValue, + } + if orderID != "" { + sql += "AND order_id = ?" + sqlParams = append(sqlParams, orderID) + } + err = GetRow(GetDB(), &mixPayDetail, sql, sqlParams) + return mixPayDetail, err +} diff --git a/business/q_bida/q_bida_server.go b/business/q_bida/q_bida_server.go index 2f5334f70..d28719ae3 100644 --- a/business/q_bida/q_bida_server.go +++ b/business/q_bida/q_bida_server.go @@ -6,12 +6,14 @@ import ( bida "git.rosy.net.cn/baseapi/platformapi/q_bida" "git.rosy.net.cn/baseapi/platformapi/tonglianpayapi" "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/jxstore/financial" "git.rosy.net.cn/jx-callback/business/jxutils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" + "github.com/astaxie/beego/client/orm" "time" ) @@ -203,6 +205,7 @@ func CancelWayOrder(ctx *jxcontext.Context, userId string, param *bida.CancelOrd SuccessCode = 0 tmp_orderNo = param.OrderNo db = dao.GetDB() + txDB orm.TxOrmer ) // 查询订单 order, err := dao.GetUserVendorOrder(db, userId, param.OrderNo) @@ -248,17 +251,45 @@ func CancelWayOrder(ctx *jxcontext.Context, userId string, param *bida.CancelOrd if err != nil { return FailCode, err } - if orderWay.PayMethod == 1 && orderWay.Status == 110 { // 余额支付且已支付 - // 支付方式为余额支付,则需要修改order/userVendorOrder,修改订单状态,给用户账户价钱,生成一个价钱数据 - } else if orderWay.PayMethod == 2 && orderWay.Status == 110 { // 微信支付且已支付 - // 微信支付原路退款,发起退款申请 - res, err := RefundOrderByTL(ctx, orderWay, order, order.OtherWayBill, int(order.ChannelFee*100), "申请退款") - if len(res.VendorRefundID) > 0 { - return SuccessCode, err - } else { - return FailCode, err + if orderWay.Status == 110 { + if orderWay.PayMethod == 1 { // 余额支付 + if err := financial.AddBillIncome(txDB, 0, model.BillTypePayByMixPay1, orderWay.PayPrice, 0); err != nil { + return FailCode, err + } + order.OrderStatus = model.OrderStatusCancel + if _, err := dao.UpdateEntityTx(txDB, order, "OrderStatus"); err != nil { + return FailCode, err + } + // 支付方式为余额支付,则需要修改order/userVendorOrder,修改订单状态,给用户账户价钱,生成一个价钱数据 + } else if orderWay.PayMethod == 2 { // 微信支付 + // 微信支付原路退款,发起退款申请 + res, err := RefundOrderByTL(ctx, orderWay, order, order.OtherWayBill, int(order.ChannelFee*100), "申请退款") + if len(res.VendorRefundID) > 0 { + return SuccessCode, err + } else { + return FailCode, err + } + } + if orderWay.PayMethod == 5 { //混合支付 + //余额增加相应金额 + mixPayInfo, err := dao.GetMixPayDetail(orderWay.OrderID) + if err := financial.AddBillIncome(txDB, 0, model.BillTypePayByMixPay1, mixPayInfo[0].BalancePrice, 0); err != nil { + return FailCode, err + } + //更新mixPay 状态 + mixPayInfo[0].Status = model.BillMixPayRefund1 //退款状态 + if _, err := dao.UpdateEntityTx(txDB, &mixPayInfo, "Status"); err != nil { + return FailCode, err + } + res, err := RefundOrderByTL(ctx, orderWay, order, order.OtherWayBill, int(order.ChannelFee*100), "申请退款") + if len(res.VendorRefundID) > 0 { + return SuccessCode, err + } else { + return FailCode, err + } } } + } return SuccessCode, nil } diff --git a/controllers/order_controller.go b/controllers/order_controller.go index 5a00d28dc..1dff39e97 100644 --- a/controllers/order_controller.go +++ b/controllers/order_controller.go @@ -20,12 +20,13 @@ type OrderController struct { // @Param vendorPayType formData string true "平台支付类型" // @Param orderType formData string true "订单类型member(会员),express快递,recharge充值" // @Param appId formData string true "appId" +// @Param isChoose formData int true "-1:未选中余额抵消 1:余额抵消" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /Pay [post] func (c *OrderController) Pay() { c.callPay(func(params *tOrderPayParams) (retVal interface{}, errCode string, err error) { - retVal, err = cms.Pay(params.Ctx, params.OrderID, params.PayType, params.VendorPayType, params.AppId) + retVal, err = cms.Pay(params.Ctx, params.OrderID, params.PayType, params.VendorPayType, params.AppId, params.IsChoose) return retVal, "", err }) } @@ -38,13 +39,13 @@ func (c *OrderController) Pay() { // @Param vendorPayType formData string true "平台支付类型" // @Param orderType formData string true "订单类型member(会员),express快递,recharge充值" // @Param appId formData string true "appId" -// @Param restPrice formData int true "需要微信支付的价格" +// @Param isChoose formData int true "-1:未选中余额抵消 1:余额抵消" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /PayByBalance [post] func (c *OrderController) PayByBalance() { 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) + retVal, _, err = cms.PayByBalance(params.Ctx, params.OrderID, params.IsChoose, params.PayType, params.VendorPayType, params.AppId) return retVal, "", err }) } diff --git a/controllers/q_bida.go b/controllers/q_bida.go index 494c2bf78..3b718167b 100644 --- a/controllers/q_bida.go +++ b/controllers/q_bida.go @@ -134,7 +134,7 @@ func (c *QBiDaExpressController) CreateWayOrder() { }) } -// CancelWayVendorOrder 取消运单 +// pay 取消运单 // @Title Q必达 // @Description 取消运单 // @Param token header string true "管理员token"