add pay
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user