add 充值到余额订单

This commit is contained in:
richboo111
2022-08-08 14:04:42 +08:00
parent b393bd17b8
commit 9476a98b69
4 changed files with 85 additions and 117 deletions

View File

@@ -105,9 +105,7 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app
VendorPayType: vendorPayType, VendorPayType: vendorPayType,
} }
) )
globals.SugarLogger.Debug("payPrice before1111=========================", payPrice)
//支付金额<原金额 //支付金额<原金额
globals.SugarLogger.Debug("order.PayPrice=========================", order.PayPrice)
if payPrice < order.PayPrice { if payPrice < order.PayPrice {
temp_PayPrice = payPrice temp_PayPrice = payPrice
temp_PayMethod = 5 temp_PayMethod = 5
@@ -115,39 +113,21 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app
temp_PayPrice = orderInfo.PayPrice temp_PayPrice = orderInfo.PayPrice
temp_PayMethod = 2 temp_PayMethod = 2
} }
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 { if _, err := dao.SetOrderStatus(txDB, temp_PayPrice, temp_PayMethod, orderInfo.Status, orderID); err != nil {
return nil, err 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……") globals.SugarLogger.Debugf("pay begin……")
err = dao.GetEntity(db, order, "OrderID") err = dao.GetEntity(db, order, "OrderID")
if order.OrderID == "" { if order.OrderID == "" {
return result, fmt.Errorf("未找到此订单!") return result, fmt.Errorf("未找到此订单!")
} }
//info, err := dao.GetOrderByID(db, orderID)
//if err != nil {
// return nil, err
//}
globals.SugarLogger.Debug("Order再次从数据库获得order详情", order.PayPrice, order.PayMethod)
//globals.SugarLogger.Debug("Order再次从数据库获得order详情", info.PayPrice, info.PayMethod)
payHandler.Order = order payHandler.Order = order
//payHandler.Order = info
globals.SugarLogger.Debug("payHandler.Order再次从数据库获得order详情", payHandler.Order.PayPrice, payHandler.Order.PayMethod)
//如果用户没有对应账单信息就给他生成一条 //如果用户没有对应账单信息就给他生成一条
// 给用户创建一个银行卡账户 // 给用户创建一个银行卡账户
globals.SugarLogger.Debug("create bill begin……") globals.SugarLogger.Debug("create bill begin……")
userBill, err := dao.GetUserBill(db, order.UserID, "") userBill, err := dao.GetUserBill(db, order.UserID, "")
if userBill == nil { if userBill == nil {
globals.SugarLogger.Debug("order.UserIDuserBill=======================", order.UserID, userBill)
err = financial.AddUserBill(txDB, jxutils.GenBillID(), order.UserID) err = financial.AddUserBill(txDB, jxutils.GenBillID(), order.UserID)
} }
err = payHandler.CreatePay(txDB, appId) err = payHandler.CreatePay(txDB, appId)
@@ -167,7 +147,6 @@ var (
func PayByBalance(ctx *jxcontext.Context, orderID string, isChoose, 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 ( var (
db = dao.GetDB() db = dao.GetDB()
//txDB orm.TxOrmer
) )
//获取订单信息 //获取订单信息
globals.SugarLogger.Debug("begin get order_info") globals.SugarLogger.Debug("begin get order_info")
@@ -177,106 +156,91 @@ func PayByBalance(ctx *jxcontext.Context, orderID string, isChoose, payType int,
} }
globals.SugarLogger.Debug("orderInfo.OrderType===============", orderInfo.OrderType) globals.SugarLogger.Debug("orderInfo.OrderType===============", orderInfo.OrderType)
//获取用户 会员账户信息 //获取用户 会员账户信息
globals.SugarLogger.Debug("开始获取会员信息")
userBill, err := dao.GetUserBill(db, orderInfo.UserID, "") userBill, err := dao.GetUserBill(db, orderInfo.UserID, "")
if err != nil { if err != nil {
return nil, "获取用户会员账户余额失败", err return nil, "获取用户会员账户余额失败", err
} }
if orderInfo.Status == NotPay { if orderInfo.Status == NotPay {
globals.SugarLogger.Debug("进入账单未支付") //需要充值余额支付的方式//todo 后续添加
globals.SugarLogger.Debug("user_bill.balance==================", userBill.AccountBalance) //if orderInfo.OrderType == 6 {
//
//}
// (3)使用余额且 余额大于支付金额 // (3)使用余额且 余额大于支付金额
if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && isChoose == Choose { //快递混合支付
globals.SugarLogger.Debug("进入余额支付部分") if orderInfo.OrderType == 3 {
txDB, _ := dao.Begin(db) if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && isChoose == Choose {
defer func() { globals.SugarLogger.Debug("进入余额支付部分")
if r := recover(); r != nil { txDB, _ := dao.Begin(db)
panic(r) defer func() {
if r := recover(); r != nil {
panic(r)
}
}()
//增加账单 余额减去相应金额
money := userBill.AccountBalance - orderInfo.PayPrice
if err = dao.UpdateUserBill(userBill.UserID, money); err != nil {
return nil, "余额支付失败", err
} }
}() //更新订单状态
//增加账单 余额减去相应金额 orderInfo.PayMethod = 1
globals.SugarLogger.Debug("增加账单 余额减去相应金额") orderInfo.Status = 110
money := userBill.AccountBalance - orderInfo.PayPrice if _, err := dao.UpdateEntityTx(txDB, orderInfo, "PayMethod"); err != nil {
if err = dao.UpdateUserBill(userBill.UserID, money); err != nil { dao.Rollback(db, txDB)
return nil, "余额支付失败", err return nil, "更新order.PayMethod状态失败", err
} }
//更新订单状态 if _, err := dao.UpdateEntityTx(txDB, orderInfo, "Status"); err != nil {
globals.SugarLogger.Debug("更新order状态") dao.Rollback(db, txDB)
//temp_method := 1 return nil, "更新order.Status状态失败", err
//temp_status := 110 }
orderInfo.PayMethod = 1 //todo 后续需增加其他订单类型
orderInfo.Status = 110 //(1)更新快递 订单状态
if _, err := dao.UpdateEntityTx(txDB, orderInfo, "PayMethod"); err != nil { temp_vendor_status := 4
dao.Rollback(db, txDB) if _, err := dao.SetUserVendorOrderStatus(txDB, orderInfo.OrderID, temp_vendor_status); err != nil {
return nil, "更新order.PayMethod状态失败", err dao.Rollback(db, txDB)
} return nil, "更新user_vendor_order状态失败", err
if _, err := dao.UpdateEntityTx(txDB, orderInfo, "Status"); err != nil { }
dao.Rollback(db, txDB) dao.Commit(db, txDB)
return nil, "更新order.Status状态失败", err //再次从数据库获取order、userOrder
} orderNew, err := dao.GetOrderByID(db, orderID)
//if _, err := dao.SetOrderStatus(txDB, orderInfo.PayPrice, 1, 110, orderID); err != nil { if err != nil {
// dao.Rollback(db, txDB) return nil, "获取orderNew失败", err
// return nil, "更新order状态失败", err }
//} userOrder, err := dao.GetUserVendorOrder(db, orderNew.UserID, orderNew.OrderID)
//todo 后续需增加其他订单类型 if err != nil {
//(1)更新快递 订单状态 return nil, "获取userOrder失败", err
globals.SugarLogger.Debug("更新UserVendorOrder状态") }
temp_vendor_status := 4 //快递单 同步到qbd
if _, err := dao.SetUserVendorOrderStatus(txDB, orderInfo.OrderID, temp_vendor_status); err != nil { if orderNew.Status == 110 && userOrder.OrderStatus == 4 {
dao.Rollback(db, txDB) if err := q_bida.CreateOrder2QBiDa(userOrder, orderInfo.OrderID); err != nil {
return nil, "更新user_vendor_order状态失败", err return nil, "", err
} }
dao.Commit(db, txDB) } else {
//再次从数据库获取orderuserOrder return nil, "order/user_vendor_order更新状态出错", nil
orderNew, err := dao.GetOrderByID(db, orderID)
if err != nil {
return nil, "获取orderNew失败", err
}
globals.SugarLogger.Debug("再次获取order数据进行检验", orderNew.PayMethod, orderNew.PayPrice)
userOrder, err := dao.GetUserVendorOrder(db, orderNew.UserID, orderNew.OrderID)
if err != nil {
return nil, "获取userOrder失败", err
}
globals.SugarLogger.Debug("再次获取user_vendor_order数据进行检验", userOrder.OrderStatus)
//快递单 同步到qbd
globals.SugarLogger.Debug("输出一下", orderInfo.Status, userOrder.OrderStatus)
if orderNew.Status == 110 && userOrder.OrderStatus == 4 {
globals.SugarLogger.Debug("进入qbd创建订单流程")
if err := q_bida.CreateOrder2QBiDa(userOrder, orderInfo.OrderID); err != nil {
return nil, "", err
} }
} else {
return nil, "order/user_vendor_order更新状态出错", nil
}
}
if isChoose == Choose {
//1用户不使用余额或者余额=0 即直接微信支付
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用户使用余额剩余微信支付 if isChoose == Choose {
totalPrice := orderInfo.PayPrice //订单原价 //1用户不使用余额或者余额=0 即直接微信支付
if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice { if userBill.AccountBalance == 0 {
globals.SugarLogger.Debug("进入混合支付部分") WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, orderInfo.PayPrice)
//orderInfo.PayMethod = 5 //混合支付状态 if err != nil {
needPay := totalPrice - userBill.AccountBalance //需支付金额 return nil, "微信支付失败:", err
globals.SugarLogger.Debug("needPay=================", needPay) }
globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice) return WxPayParam, "", err
globals.SugarLogger.Debug("orderInfo.OrderID=================", orderInfo.OrderID) }
WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, needPay) //2用户使用余额剩余微信支付
if err != nil { totalPrice := orderInfo.PayPrice //订单原价
globals.SugarLogger.Debug("err=================", err) if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice {
return nil, "微信支付失败:", err needPay := totalPrice - userBill.AccountBalance //需支付金额
WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, needPay)
if err != nil {
return nil, "微信支付失败:", err
}
return WxPayParam, "", err
} }
return WxPayParam, "", err
} }
} }
} }
return nil, "", err return nil, "", err
} }

View File

@@ -272,8 +272,12 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
//充值会员 增加微信支付处理业务 //充值会员 增加微信支付处理业务
if (order.OrderType == 2 || order.OrderType == 5) && call.TrxStatus == tonglianpayapi.TrxStatusSuccess { if (order.OrderType == 2 || order.OrderType == 5) && call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
if err := OnWXPayFinished(order); err != nil {
return err
}
return OnWXPayFinished(order) return OnWXPayFinished(order)
} else if order.OrderType == 3 && call.TrxStatus == tonglianpayapi.TrxStatusSuccess { }
if order.OrderType == 3 && call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
globals.SugarLogger.Debug("得到微信回调结果,快递流程开始") globals.SugarLogger.Debug("得到微信回调结果,快递流程开始")
txdb, _ := dao.Begin(db) txdb, _ := dao.Begin(db)
defer func() { defer func() {
@@ -293,12 +297,6 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
dao.Rollback(db, txdb) dao.Rollback(db, txdb)
return err return err
} }
//测试order 数据库是否更新成功
order_test, err := dao.GetOrderByID(db, order.OrderID)
globals.SugarLogger.Debug("再次从数据库拿order.TransactionID", order_test.TransactionID)
globals.SugarLogger.Debug("再次从数据库拿order.Status", order_test.Status)
////////////////////////////////////
globals.SugarLogger.Debug("获取UserVendorOrder") globals.SugarLogger.Debug("获取UserVendorOrder")
userOrder := model.UserVendorOrder{LocalWayBill: order.OrderID} userOrder := model.UserVendorOrder{LocalWayBill: order.OrderID}
if err := dao.GetEntity(db, &userOrder, "LocalWayBill"); err != nil { if err := dao.GetEntity(db, &userOrder, "LocalWayBill"); err != nil {
@@ -346,6 +344,12 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
} }
return err return err
} }
//需要充值到余额方式 购买的
if order.OrderType == 6 && call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
if err := dao.UpdateUserBill(order.UserID, order.PayPrice); err != nil {
return err
}
}
} else { } else {
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err) globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)
} }

View File

@@ -59,7 +59,7 @@ type Order struct {
OrderID string `orm:"column(order_id)" json:"orderID"` // 订单号 OrderID string `orm:"column(order_id)" json:"orderID"` // 订单号
UserID string `orm:"column(user_id);size(48)" json:"userID"` // 用户ID UserID string `orm:"column(user_id);size(48)" json:"userID"` // 用户ID
Type int `json:"type"` // 支付还是提现 1-支付2-提现 Type int `json:"type"` // 支付还是提现 1-支付2-提现
OrderType int `json:"orderType"` // 订单类型1-发任务2-会员月卡3-发快递4-提现5-会员年卡 OrderType int `json:"orderType"` // 订单类型1-发任务2-会员月卡3-发快递4-提现5-会员年卡,6-使用充值到余额方式的订单
Way string `json:"way"` // weixinapp ,weixinmini Way string `json:"way"` // weixinapp ,weixinmini
Status int `json:"status"` // 订单状态,待支付2已支付5支付成功110支付失败115,150取消 Status int `json:"status"` // 订单状态,待支付2已支付5支付成功110支付失败115,150取消
PayPrice int `json:"payPrice"` // 支付金额 PayPrice int `json:"payPrice"` // 支付金额

View File

@@ -70,7 +70,7 @@ func (c *OrderController) Cash() {
// @Description 创建订单 // @Description 创建订单
// @Param token header string true "认证token" // @Param token header string true "认证token"
// @Param type formData int true "支付类型/账单类型" // @Param type formData int true "支付类型/账单类型"
// @Param orderType formData int true "订单类型1为发任务2为冲会员3为发快递" // @Param orderType formData int true "订单类型1为发任务2为冲会员3为发快递6-需要充值到余额购买的方式"
// @Param way formData string true "认证方式" // @Param way formData string true "认证方式"
// @Param price formData int true "支付金额" // @Param price formData int true "支付金额"
// @Param lng formData float64 true "经纬度" // @Param lng formData float64 true "经纬度"