Merge branch 'rsm' of e.coding.net:rosydev/jx-callback into rsm

# Conflicts:
#	globals/beegodb/beegodb.go
This commit is contained in:
邹宗楠
2022-08-04 09:54:39 +08:00
11 changed files with 303 additions and 116 deletions

View File

@@ -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.UserIDuserBill=======================", 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

View File

@@ -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
}

View File

@@ -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

View File

@@ -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: