This commit is contained in:
richboo111
2022-08-01 14:17:11 +08:00
parent 07ae683b22
commit 577823a804
8 changed files with 141 additions and 59 deletions

View File

@@ -3,7 +3,6 @@ package cms
import ( import (
"fmt" "fmt"
"git.rosy.net.cn/jx-callback/business/jxstore/event" "git.rosy.net.cn/jx-callback/business/jxstore/event"
"github.com/astaxie/beego/client/orm"
"strings" "strings"
"time" "time"
@@ -81,7 +80,7 @@ func CreateOrder(ctx *jxcontext.Context, type1, orderType int, way string, price
return order.OrderID, errCode, err 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 ( var (
db = dao.GetDB() db = dao.GetDB()
order = &model.Order{ order = &model.Order{
@@ -93,6 +92,17 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app
VendorPayType: vendorPayType, VendorPayType: vendorPayType,
} }
) )
//支付金额<原金额
if payPrice < order.PayPrice {
order = &model.Order{
PayPrice: payPrice,
PayMethod: 5, //混合支付
}
} else {
order = &model.Order{
PayMethod: 2, //微信支付
}
}
globals.SugarLogger.Debugf("pay begin……") globals.SugarLogger.Debugf("pay begin……")
err = dao.GetEntity(db, order, "OrderID") err = dao.GetEntity(db, order, "OrderID")
if order.ID == 0 { if order.ID == 0 {
@@ -124,20 +134,21 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, app
} }
var ( var (
NotPay = 2 //订单待支付 NotPay = 2 //订单待支付
AlreadyPay = 1 //订单已支付 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 ( var (
db = dao.GetDB() db = dao.GetDB()
txDB orm.TxOrmer //txDB orm.TxOrmer
) )
//获取订单信息 //获取订单信息
globals.SugarLogger.Debug("begin get order_info") globals.SugarLogger.Debug("begin get order_info")
orderInfo, err := dao.GetOrderByID(db, orderID) orderInfo, err := dao.GetOrderByID(db, orderID)
//tempPrice := orderInfo.PayPrice
if err != nil { if err != nil {
return nil, "获取订单信息失败", err return nil, "获取订单信息失败", err
} }
@@ -152,46 +163,57 @@ func PayByBalance(ctx *jxcontext.Context, orderID string, restPrice, payType int
globals.SugarLogger.Debug("进入账单未支付") globals.SugarLogger.Debug("进入账单未支付")
globals.SugarLogger.Debug("user_bill.balance==================", userBill.AccountBalance) globals.SugarLogger.Debug("user_bill.balance==================", userBill.AccountBalance)
// (3)使用余额且 余额大于支付金额 // (3)使用余额且 余额大于支付金额
if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && restPrice == 0 { if userBill.AccountBalance > 0 && userBill.AccountBalance > orderInfo.PayPrice && isChoose == NotChoose {
//余额>0
globals.SugarLogger.Debug("进入余额支付部分") globals.SugarLogger.Debug("进入余额支付部分")
if userBill.AccountBalance > orderInfo.PayPrice && userBill.AccountBalance-orderInfo.PayPrice > 0 { var order = &model.Order{
if err = financial.AddExpendUpdateAccount(txDB, userBill, model.BillTypePayByAccountBalance, orderInfo.PayPrice, 0); err != nil { PayPrice: orderInfo.PayPrice,
return nil, "使用余额支付失败:", err PayMethod: 1, //余额支付
}
txDB, _ := dao.Begin(db)
defer func() {
if r := recover(); r != nil {
panic(r)
} }
//修改订单状态 dao.Commit(db, txDB)
orderInfo.Status = model.OrderStatusSuccessPay }()
orderInfo.PayMethod = 1 //1-余额支付2-微信支付 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 即直接微信支付 //1用户不使用余额或者余额=0 即直接微信支付
//if orderInfo.PayPrice == restPrice || userBill.AccountBalance == 0 { if userBill.AccountBalance == 0 {
// WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID) WxPayParam, err := Pay(ctx, orderInfo.OrderID, payType, vendorPayType, appID, orderInfo.PayPrice)
// //orderInfo.PayMethod = 2 //微信支付方式 if err != nil {
// if err != nil { globals.SugarLogger.Debug("err=================", err)
// globals.SugarLogger.Debug("err=================", err) return nil, "微信支付失败:", err
// return nil, "微信支付失败:", err }
// } return WxPayParam, "", err
// return WxPayParam, "", err
//}
//2用户使用余额剩余微信支付
if userBill.AccountBalance+restPrice != orderInfo.PayPrice {
return nil, "支付金额错误,请重新计算", err
} }
//2用户使用余额剩余微信支付
totalPrice := orderInfo.PayPrice //订单原价
if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice { if userBill.AccountBalance > 0 && userBill.AccountBalance < orderInfo.PayPrice {
globals.SugarLogger.Debug("进入混合支付部分") globals.SugarLogger.Debug("进入混合支付部分")
orderInfo.PayMethod = 5 //混合支付 //orderInfo.PayMethod = 5 //混合支付状态
orderInfo.PayPrice = restPrice needPay := totalPrice - userBill.AccountBalance //需支付金额
globals.SugarLogger.Debug("orderInfo.PayPrice=================", orderInfo.PayPrice) 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 return nil, "", err

View File

@@ -11,12 +11,12 @@ import (
"time" "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{ mixPayInfo := &model.MixPay{
OrderID: orderID, OrderID: orderID,
BalancePrice: balancePrice, BalancePrice: balancePrice,
TotalPrice: totalPrice, TotalPrice: totalPrice,
Method: method, Status: status,
WxPrice: totalPrice - balancePrice, WxPrice: totalPrice - balancePrice,
} }
dao.WrapAddIDCULEntity(mixPayInfo, jxcontext.AdminCtx.GetUserName()) 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) 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) { 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) return dao.GetUserBillDetail(dao.GetDB(), userID, utils.Str2Time(fromTime), utils.Str2Time(toTime), pageSize, offset)
} }

View File

@@ -311,6 +311,14 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
return err 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) dao.Commit(db, txdb)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess { if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
switch order.OrderType { switch order.OrderType {

View File

@@ -16,6 +16,7 @@ const (
BillTypePayByAccountBalance = 25 //余额支付 BillTypePayByAccountBalance = 25 //余额支付
BillTypePayByMixPay1 = 26 //混合支付中的 余额部分状态码 BillTypePayByMixPay1 = 26 //混合支付中的 余额部分状态码
BillTypePayByMixPay2 = 27 //混合支付中的 微信部分状态码 BillTypePayByMixPay2 = 27 //混合支付中的 微信部分状态码
BillMixPayRefund1 = 28 //混合支付 余额部分退款
BillTypeQuitGroup = 30 //退群 BillTypeQuitGroup = 30 //退群
BillTypeJdWaybillOverWeight = 40 //京东物流超重扣款 BillTypeJdWaybillOverWeight = 40 //京东物流超重扣款
@@ -51,7 +52,7 @@ type MixPay struct {
BalancePrice int `orm:"column(balance_price)" json:"balance_price"` //余额支付部分 BalancePrice int `orm:"column(balance_price)" json:"balance_price"` //余额支付部分
TotalPrice int `orm:"column(total_price)" json:"total_price"` //订单总额 TotalPrice int `orm:"column(total_price)" json:"total_price"` //订单总额
WxPrice int `orm:"column(wx_price)" json:"wx_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 { func (v *MixPay) TableIndex() [][]string {

View File

@@ -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}...) _, err := ExecuteSQL(GetDB(), `UPDATE user_bill SET account_balance = ? WHERE user_id = ? `, []interface{}{money, userId}...)
return err 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
}

View File

@@ -6,12 +6,14 @@ import (
bida "git.rosy.net.cn/baseapi/platformapi/q_bida" bida "git.rosy.net.cn/baseapi/platformapi/q_bida"
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi" "git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
"git.rosy.net.cn/baseapi/utils" "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"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "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"
"git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api" "git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego/client/orm"
"time" "time"
) )
@@ -203,6 +205,7 @@ func CancelWayOrder(ctx *jxcontext.Context, userId string, param *bida.CancelOrd
SuccessCode = 0 SuccessCode = 0
tmp_orderNo = param.OrderNo tmp_orderNo = param.OrderNo
db = dao.GetDB() db = dao.GetDB()
txDB orm.TxOrmer
) )
// 查询订单 // 查询订单
order, err := dao.GetUserVendorOrder(db, userId, param.OrderNo) 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 { if err != nil {
return FailCode, err return FailCode, err
} }
if orderWay.PayMethod == 1 && orderWay.Status == 110 { // 余额支付且已支付 if orderWay.Status == 110 {
// 支付方式为余额支付则需要修改order/userVendorOrder修改订单状态给用户账户价钱生成一个价钱数据 if orderWay.PayMethod == 1 { // 余额支付
} else if orderWay.PayMethod == 2 && orderWay.Status == 110 { // 微信支付且已支付 if err := financial.AddBillIncome(txDB, 0, model.BillTypePayByMixPay1, orderWay.PayPrice, 0); err != nil {
// 微信支付原路退款,发起退款申请 return FailCode, err
res, err := RefundOrderByTL(ctx, orderWay, order, order.OtherWayBill, int(order.ChannelFee*100), "申请退款") }
if len(res.VendorRefundID) > 0 { order.OrderStatus = model.OrderStatusCancel
return SuccessCode, err if _, err := dao.UpdateEntityTx(txDB, order, "OrderStatus"); err != nil {
} else { return FailCode, err
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 return SuccessCode, nil
} }

View File

@@ -20,12 +20,13 @@ type OrderController struct {
// @Param vendorPayType formData string true "平台支付类型" // @Param vendorPayType formData string true "平台支付类型"
// @Param orderType formData string true "订单类型member会员express快递recharge充值" // @Param orderType formData string true "订单类型member会员express快递recharge充值"
// @Param appId formData string true "appId" // @Param appId formData string true "appId"
// @Param isChoose formData int true "-1:未选中余额抵消 1余额抵消"
// @Success 200 {object} controllers.CallResult // @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult
// @router /Pay [post] // @router /Pay [post]
func (c *OrderController) Pay() { func (c *OrderController) Pay() {
c.callPay(func(params *tOrderPayParams) (retVal interface{}, errCode string, err error) { 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 return retVal, "", err
}) })
} }
@@ -38,13 +39,13 @@ func (c *OrderController) Pay() {
// @Param vendorPayType formData string true "平台支付类型" // @Param vendorPayType formData string true "平台支付类型"
// @Param orderType formData string true "订单类型member会员express快递recharge充值" // @Param orderType formData string true "订单类型member会员express快递recharge充值"
// @Param appId formData string true "appId" // @Param appId formData string true "appId"
// @Param restPrice formData int true "需要微信支付的价格" // @Param isChoose formData int true "-1:未选中余额抵消 1余额抵消"
// @Success 200 {object} controllers.CallResult // @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult
// @router /PayByBalance [post] // @router /PayByBalance [post]
func (c *OrderController) PayByBalance() { func (c *OrderController) PayByBalance() {
c.callPayByBalance(func(params *tOrderPayByBalanceParams) (retVal interface{}, errMsg string, err error) { 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 return retVal, "", err
}) })
} }

View File

@@ -134,7 +134,7 @@ func (c *QBiDaExpressController) CreateWayOrder() {
}) })
} }
// CancelWayVendorOrder 取消运单 // pay 取消运单
// @Title Q必达 // @Title Q必达
// @Description 取消运单 // @Description 取消运单
// @Param token header string true "管理员token" // @Param token header string true "管理员token"