diff --git a/business/jxstore/cms/order.go b/business/jxstore/cms/order.go index 7ffe7ff8d..b91599b19 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" + "git.rosy.net.cn/jx-callback/business/q_bida" "github.com/astaxie/beego/client/orm" "strings" "time" @@ -81,11 +82,22 @@ 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 ( + temp_PayPrice int + temp_PayMethod int + txDB orm.TxOrmer + db = dao.GetDB() + ) + orderInfo, err := dao.GetOrderByID(dao.GetDB(), orderID) + if err != nil { + return nil, err + } var ( - db = dao.GetDB() order = &model.Order{ - OrderID: orderID, + OrderID: orderID, + PayPrice: orderInfo.PayPrice, + UserID: orderInfo.UserID, } payHandler = &financial.PayHandler{ PayType: payType, @@ -93,51 +105,71 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app VendorPayType: vendorPayType, } ) - globals.SugarLogger.Debugf("pay begin……") - err = dao.GetEntity(db, order, "OrderID") - if order.ID == 0 { - return result, fmt.Errorf("未找到此订单!") + globals.SugarLogger.Debug("payPrice before1111=========================", payPrice) + //支付金额<原金额 + globals.SugarLogger.Debug("order.PayPrice=========================", order.PayPrice) + if payPrice < order.PayPrice { + temp_PayPrice = payPrice + temp_PayMethod = 5 + } else { + temp_PayPrice = orderInfo.PayPrice + temp_PayMethod = 2 } - payHandler.Order = order - globals.SugarLogger.Debug("order.payprice==================", payHandler.Order.PayPrice) - //如果用户没有对应账单信息就给他生成一条 - txDB, _ := dao.Begin(db) - defer func() { - if r := recover(); r != nil { - dao.Rollback(db, txDB) - panic(r) - } - }() + globals.SugarLogger.Debug("检验初始数据情况", order.OrderID, order.PayMethod) + globals.SugarLogger.Debug("检验初始数据情况", order.PayPrice) + //微信支付实际金额更新到数据库 + if _, err := dao.SetOrderStatus(txDB, temp_PayPrice, temp_PayMethod, orderInfo.Status, orderID); err != nil { + return nil, err + } + //if _, err := dao.UpdateEntityTx(txDB, order); err != nil { + // return nil, err + //} + globals.SugarLogger.Debug("经过更新操作后的数据after=========================", order.OrderID, order.PayPrice, order.PayMethod) + globals.SugarLogger.Debug("经过更新操作后的数据after=========================", order.PayPrice) + globals.SugarLogger.Debugf("pay begin……") + //err = dao.GetEntity(db, order, "OrderID") + //if order.OrderID == "" { + // return result, fmt.Errorf("未找到此订单!") + //} + info, err := dao.GetOrderByID(db, orderID) + if err != nil { + return nil, err + } + globals.SugarLogger.Debug("Order再次从数据库获得order详情", info.PayPrice, info.PayMethod) + payHandler.Order = info + + globals.SugarLogger.Debug("payHandler.Order再次从数据库获得order详情", payHandler.Order.PayPrice, payHandler.Order.PayMethod) + //如果用户没有对应账单信息就给他生成一条 // 给用户创建一个银行卡账户 globals.SugarLogger.Debug("create bill begin……") userBill, err := dao.GetUserBill(db, order.UserID, "") if userBill == nil { + globals.SugarLogger.Debug("order.UserID,userBill=======================", order.UserID, userBill) err = financial.AddUserBill(txDB, jxutils.GenBillID(), order.UserID) } err = payHandler.CreatePay(txDB, appId) - globals.SugarLogger.Debug("errrrrr=========================", err) - dao.Commit(db, txDB) - globals.SugarLogger.Debug("the last step of this program,return err……", err) + globals.SugarLogger.Debug("the last step o f this program,return err……", err) globals.SugarLogger.Debugf("result : %v", utils.Format4Output(payHandler.WxPayParam, false)) return payHandler.WxPayParam, err } 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 } @@ -148,50 +180,87 @@ func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int if err != nil { return nil, "获取用户会员账户余额失败", err } + if orderInfo.Status == NotPay { 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 == Choose { 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 + txDB, _ := dao.Begin(db) + defer func() { + if r := recover(); r != nil { + panic(r) } - //修改订单状态 - orderInfo.Status = model.OrderStatusSuccessPay - orderInfo.PayMethod = 1 //1-余额支付,2-微信支付 + }() + //增加账单 余额减去相应金额 + flag := -1 + globals.SugarLogger.Debug("增加账单 余额减去相应金额") + money := userBill.AccountBalance - orderInfo.PayPrice + if err = dao.UpdateUserBill(userBill.UserID, money); err != nil { + return nil, "余额支付失败", err + } else { + flag = 1 //支付成功 } + //更新订单状态 + globals.SugarLogger.Debug("更新订单状态") + temp_method := 1 + temp_status := 110 + if _, err := dao.SetOrderStatus(txDB, orderInfo.PayPrice, temp_method, temp_status, orderID); err != nil { + dao.Rollback(db, txDB) + return nil, "更新order状态失败", err + } + //todo 后续需增加其他订单类型 + //(1)更新快递 订单状态 + temp_vendor_status := 4 + if _, err := dao.SetUserVendorOrderStatus(txDB, orderInfo.OrderID, temp_vendor_status); err != nil { + dao.Rollback(db, txDB) + return nil, "更新user_vendor_order状态失败", err + } + //再次从数据库获取order、userOrder + orderNew, err := dao.GetOrderByID(db, orderID) + if err != nil { + return nil, "获取orderNew失败", err + } + userOrder, err := dao.GetUserVendorOrder(db, orderNew.UserID, orderNew.OrderID) + if err != nil { + return nil, "获取userOrder失败", err + } + //快递单 同步到qbd + if orderInfo.Status == 110 && flag == 1 { + if err := q_bida.CreateOrder2QBiDa(userOrder, orderInfo.OrderID); err != nil { + 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("needPay=================", needPay) globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice) + globals.SugarLogger.Debug("orderInfo.OrderID=================", orderInfo.OrderID) + 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/cms/order_test.go b/business/jxstore/cms/order_test.go new file mode 100644 index 000000000..e9f1ca95a --- /dev/null +++ b/business/jxstore/cms/order_test.go @@ -0,0 +1,13 @@ +package cms + +import ( + "git.rosy.net.cn/jx-callback/business/model/dao" + "testing" +) + +func Test_Set(t *testing.T) { + if _, err := dao.SetOrderStatus(110, 5, "165943225529BCVFdaX"); err != nil { + return + } + return +} diff --git a/business/jxstore/financial/bill.go b/business/jxstore/financial/bill.go index 6da16f798..e368bb0c6 100644 --- a/business/jxstore/financial/bill.go +++ b/business/jxstore/financial/bill.go @@ -7,16 +7,18 @@ import ( "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" "github.com/astaxie/beego/client/orm" "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()) return dao.CreateEntityTx(txDB, mixPayInfo) @@ -40,6 +42,7 @@ func AddBillExpend(txDB orm.TxOrmer, billID int64, billType, expendPrice, jobID ExpendPrice: expendPrice, JobID: jobID, } + globals.SugarLogger.Debug("进入AddBillExpend") dao.WrapAddIDCULEntity(billExpend, jxcontext.AdminCtx.GetUserName()) return dao.CreateEntityTx(txDB, billExpend) } @@ -61,17 +64,13 @@ 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) } func AddExpendUpdateAccount(txDB orm.TxOrmer, userBill *model.UserBill, billType, price, jobID int) (err error) { //1、账户支出增加一条记录 + globals.SugarLogger.Debug("进入AddExpendUpdateAccount") err = AddBillExpend(txDB, userBill.BillID, billType, price, jobID) if err != nil { return err diff --git a/business/jxstore/financial/financial.go b/business/jxstore/financial/financial.go index b5709f61a..87fb8192b 100644 --- a/business/jxstore/financial/financial.go +++ b/business/jxstore/financial/financial.go @@ -245,6 +245,7 @@ func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) { } func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { + globals.SugarLogger.Debug("进入微信回调============================") order := &model.Order{ OrderID: call.CusorderID, } @@ -259,7 +260,7 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { order.PayFinishedAt = t1 order.OriginalData = utils.Format4Output(call, true) payStatus := 0 - order.PayMethod = 2 // 通联微信支付 + //order.PayMethod = 2 // 通联微信支付 if call.TrxStatus == tonglianpayapi.TrxStatusSuccess { order.Status = model.OrderStatusFinished payStatus = model.OrderStatusSuccessPay @@ -272,21 +273,25 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { if (order.OrderType == 2 || order.OrderType == 5) && call.TrxStatus == tonglianpayapi.TrxStatusSuccess { return OnWXPayFinished(order) } else if order.OrderType == 3 && call.TrxStatus == tonglianpayapi.TrxStatusSuccess { + globals.SugarLogger.Debug("得到微信回调结果,快递流程开始") txdb, _ := dao.Begin(db) defer func() { if r := recover(); r != nil { panic(r) } }() + globals.SugarLogger.Debug("更新order") if _, err := dao.UpdateEntityTx(txdb, order); err != nil { dao.Rollback(db, txdb) return err } + globals.SugarLogger.Debug("获取UserVendorOrder") userOrder := model.UserVendorOrder{LocalWayBill: order.OrderID} if err := dao.GetEntity(db, &userOrder, "LocalWayBill"); err != nil { dao.Rollback(db, txdb) return err } + globals.SugarLogger.Debug("更新UserVendorOrder") userOrder.OrderStatus = payStatus if _, err := dao.UpdateEntityTx(txdb, &userOrder, "OrderStatus"); err != nil { dao.Rollback(db, txdb) @@ -294,17 +299,22 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { } //1-余额,2-微信,5-混合 if order.PayMethod == 5 { + globals.SugarLogger.Debug("进入混合支付回调流程") 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 { + globals.SugarLogger.Debug("totalPrice=================", totalPrice) + globals.SugarLogger.Debug("进入增加mixpay账单") + if err := AddMixPay(txdb, order.OrderID, userBill.AccountBalance, totalPrice, 1); err != nil { + globals.SugarLogger.Debug("增加mixpay账单失败") dao.Rollback(db, txdb) return err } //余额清空 + globals.SugarLogger.Debug("开始清空余额") if err := dao.UpdateUserBill(order.UserID, 0); err != nil { globals.SugarLogger.Debug("修改余额失败") dao.Rollback(db, txdb) @@ -312,6 +322,8 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) { } } dao.Commit(db, txdb) + globals.SugarLogger.Debug("call.TrxStatus======================", call.TrxStatus) + globals.SugarLogger.Debug("order.OrderType======================", order.OrderType) if call.TrxStatus == tonglianpayapi.TrxStatusSuccess { switch order.OrderType { case model.PayType4Express: diff --git a/business/model/bill.go b/business/model/bill.go index 07500c322..4aa175d2c 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"` //订单状态 -1:退款/1:正常 } func (v *MixPay) TableIndex() [][]string { diff --git a/business/model/dao/dao_bill.go b/business/model/dao/dao_bill.go index c7f980e35..4c9086750 100644 --- a/business/model/dao/dao_bill.go +++ b/business/model/dao/dao_bill.go @@ -1,6 +1,7 @@ package dao import ( + "git.rosy.net.cn/jx-callback/globals" "time" "git.rosy.net.cn/baseapi/utils" @@ -135,3 +136,42 @@ 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 { + ID int `json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdateAt time.Time `json:"update_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) (details *model.MixPay, err error) { + //var info []*MixPayDetail + info := make([]*model.MixPay, 0) + var sqlParams []interface{} + sql := "" + if orderID != "" { + sql += "SELECT * FROM mix_pay WHERE order_id = ?" + sqlParams = append(sqlParams, orderID) + } + err = GetRows(GetDB(), &info, sql, sqlParams...) + if err != nil { + globals.SugarLogger.Debug("er===========", err) + return nil, err + } + return info[0], err + //if err == nil { + // details = &MixPayDetail{ + // OrderID: info[0].OrderID, + // BalancePrice: info[0].BalancePrice, + // TotalPrice: info[0].TotalPrice, + // WxPrice: info[0].WxPrice, + // Status: info[0].Status, + // } + //} +} diff --git a/business/model/dao/dao_order.go b/business/model/dao/dao_order.go index e57fa28ec..f974b8de4 100644 --- a/business/model/dao/dao_order.go +++ b/business/model/dao/dao_order.go @@ -1,6 +1,7 @@ package dao import ( + "github.com/astaxie/beego/client/orm" "time" "git.rosy.net.cn/baseapi/utils" @@ -668,35 +669,20 @@ func GetUnionOrdersPage(db *DaoDB, vendorIDs, statuss []int, beginTime, endTime } return page, err } -func SetOrderStatus(db *DaoDB, orderID string, status int) (msg string, err error) { - sqlParams := []interface{}{} - sql := `UPDATE order o SET o.status = ? ` - if status > 0 { - sqlParams = append(sqlParams, status) - } - if orderID != "" { - sql += `WHERE o.order_id = ?` - sqlParams = append(sqlParams, orderID) - } - orderInfos := &model.Order{} - if err := GetRow(db, orderInfos, sql, sqlParams); err != nil { - return "更新order状态失败", err - } - return "更新order状态成功", err -} -func SetUserVendorOrderStatus(db *DaoDB, localWayBillID string, status int) (msg string, err error) { - sqlParams := []interface{}{} - sql := `UPDATE user_vendor_order a SET a.order_status = ?` - if status > 0 { - sqlParams = append(sqlParams, status) - } - if localWayBillID != "" { - sql += `WHERE a.local_way_bill = ?` - sqlParams = append(sqlParams, localWayBillID) - } - Infos := &model.UserVendorOrder{} - if err := GetRow(db, Infos, sql, sqlParams); err != nil { - return "更新UserVendorOrder状态失败", err + +//更新user_vendor_order 支付状态 +func SetUserVendorOrderStatus(tx orm.TxOrmer, localWayBillID string, status int) (msg string, err error) { + if _, err := ExecuteSQL(GetDB(), "UPDATE `user_vendor_order` SET order_status = ? WHERE local_way_bill = ? ", []interface{}{status, localWayBillID}...); err != nil { + return "", err } return "更新UserVendorOrder状态成功", err } + +//更新order 价格和状态 +func SetOrderStatus(tx orm.TxOrmer, payPrice, payMethod, status int, orderID string) (string, error) { + + if _, err := ExecuteSQL(GetDB(), "UPDATE `order` SET pay_price = ?,pay_method= ?,status=? WHERE order_id = ? ", []interface{}{payPrice, payMethod, status, orderID}...); err != nil { + return "", err + } + return "更新Order状态成功", nil +} diff --git a/business/q_bida/q_bida_server.go b/business/q_bida/q_bida_server.go index 2f5334f70..9a3dd2322 100644 --- a/business/q_bida/q_bida_server.go +++ b/business/q_bida/q_bida_server.go @@ -203,6 +203,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) @@ -222,7 +223,6 @@ func CancelWayOrder(ctx *jxcontext.Context, userId string, param *bida.CancelOrd panic(r) } }() - order.OrderStatus = model.OrderStatusCancel if _, err := dao.UpdateEntityTx(tx, order, "OrderStatus"); err != nil { tx.Rollback() @@ -248,17 +248,76 @@ 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 + globals.SugarLogger.Debug("回调获取order详情", orderWay) + userBill, err := dao.GetUserBill(db, orderWay.UserID, "") + if err != nil { + return FailCode, err + } + globals.SugarLogger.Debug("回调获取user_bill详情", userBill) + + if orderWay.Status == 110 { + if orderWay.PayMethod == 1 { // 余额支付 + txDB, _ := dao.Begin(db) + defer func() { + if r := recover(); r != nil { + panic(r) + } + dao.Commit(db, txDB) + }() + //余额增加金额 + userBill.AccountBalance += orderWay.PayPrice + if _, err := dao.UpdateEntityTx(txDB, userBill, "AccountBalance"); err != nil { + dao.Rollback(db, txDB) + return FailCode, err + } + //用户运单状态更新 + order.OrderStatus = model.OrderStatusCancel + if _, err := dao.UpdateEntityTx(txDB, order, "OrderStatus"); err != nil { + dao.Rollback(db, txDB) + return FailCode, err + } + } + 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 { //混合支付 + globals.SugarLogger.Debug("回调进入混合支付流程") + //余额增加相应金额 + globals.SugarLogger.Debug("回调输出订单id", orderWay.OrderID) + mixPayInfo, err := dao.GetMixPayDetail(orderWay.OrderID) + if err != nil { + return FailCode, err + } + userBill.AccountBalance += mixPayInfo.BalancePrice + globals.SugarLogger.Debug("回调输出userBill.AccountBalance", userBill.AccountBalance) + if _, err := dao.UpdateEntity(db, userBill, "AccountBalance"); err != nil { + return FailCode, err + } + //更新mixPay 状态 + mixPayInfo.Status = -1 //model.BillMixPayRefund1 //退款状态 + if _, err := dao.UpdateEntity(db, mixPayInfo, "Status"); err != nil { + return FailCode, err + } + + globals.SugarLogger.Debug("回调进入微信退款") + globals.SugarLogger.Debug("orderWay==============", orderWay) + globals.SugarLogger.Debug("order==============", order) + globals.SugarLogger.Debug("order.OtherWayBill==============", order.OtherWayBill) + 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 } @@ -405,12 +464,15 @@ func CreateOrder2QBiDa(order *model.UserVendorOrder, orderId string) error { // RefundOrderByTL 发起取消订单 func RefundOrderByTL(ctx *jxcontext.Context, orderPay *model.Order, order *model.UserVendorOrder, refundID string, refundFee int, refundDesc string) (orderPayRefund *model.OrderPayRefund, err error) { + globals.SugarLogger.Debug("进入取消订单流程") result, err := api.TLpayAPI.PayRefund(&tonglianpayapi.PayRefundParam{ Trxamt: refundFee, Reqsn: utils.GetUUID(), Remark: refundDesc, OldTrxID: orderPay.TransactionID, }) + globals.SugarLogger.Debug("result===============", result) + globals.SugarLogger.Debug("errerrerr===============", err) if err == nil { orderPayRefund = &model.OrderPayRefund{ RefundID: refundID, @@ -424,12 +486,14 @@ func RefundOrderByTL(ctx *jxcontext.Context, orderPay *model.Order, order *model dao.WrapAddIDCULDEntity(orderPayRefund, ctx.GetUserName()) db := dao.GetDB() if result.TrxStatus == tonglianpayapi.TrxStatusSuccess { + globals.SugarLogger.Debug("dfjiaojfiaojfa") orderPayRefund.Status = model.RefundStatusYes } else { + globals.SugarLogger.Debug("gedgsedgresgwagwg") orderPayRefund.Status = model.RefundStatusFailed } orderPayRefund.OriginalData = utils.Format4Output(result, true) - + globals.SugarLogger.Debug("saferhgtrhew", orderPayRefund.OriginalData) tx, _ := dao.Begin(db) defer func() { if r := recover(); r != nil { @@ -447,11 +511,13 @@ func RefundOrderByTL(ctx *jxcontext.Context, orderPay *model.Order, order *model dao.Rollback(db, tx) return nil, err } + globals.SugarLogger.Debug("rtryjyttyejrthg") orderPay.Status = model.OrderStatusCancel if _, err := dao.UpdateEntityTx(tx, orderPay); err != nil { dao.Rollback(db, tx) return nil, err } + globals.SugarLogger.Debug("ferbdebere") dao.Commit(db, tx) } 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..e16c7b240 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" @@ -159,7 +159,7 @@ func (c *QBiDaExpressController) CancelWayVendorOrder() { }) } -// QueryUserOrderList 获取用户订单列表QueryUserOrderList +// pay 获取用户订单列表QueryUserOrderList // @Title Q必达 // @Description 获取用户订单列表 // @Param token header string true "管理员token" diff --git a/globals/beegodb/beegodb.go b/globals/beegodb/beegodb.go index e1b90eb16..070e3c7e0 100644 --- a/globals/beegodb/beegodb.go +++ b/globals/beegodb/beegodb.go @@ -9,6 +9,8 @@ import ( func Init() { // set default database + orm.RegisterDataBase("default", "mysql", beego.AppConfig.String("dbConnectStr")) + //orm.RegisterDataBase("jxd_dev_0", "mysql", "root:WebServer@1@tcp(test1.jxc4.com:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true") if beego.BConfig.RunMode == "rsm" { orm.RegisterDataBase("default", "mysql", beego.AppConfig.String("dbConnectStr")) //用户 @@ -50,13 +52,11 @@ func Init() { orm.RegisterModel(&model.OrderPayRefund{}) // 退款表 orm.RegisterModel(&model.UserVendorOrder{}) // 物流订单 orm.RegisterModel(&model.WithdrawalRecord{}) // 提现申请 - } else { - orm.RegisterDataBase("default", "mysql", "root:WebServer@1@tcp(127.0.0.1:3306)/api?charset=utf8mb4&loc=Local&parseTime=true") - //打印机 - orm.RegisterModel(&model.PrintMsg{}) - orm.RegisterModel(&model.Printer{}) } + //打印机 + orm.RegisterModel(&model.PrintMsg{}) + orm.RegisterModel(&model.Printer{}) // create table orm.RunSyncdb("default", false, true) }