bill job
This commit is contained in:
27
business/jxstore/financial/bill.go
Normal file
27
business/jxstore/financial/bill.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package financial
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func AddBillIncome(db *dao.DaoDB, billID int64, billType, incomePrice int) (err error) {
|
||||
billIncome := &model.BillIncome{
|
||||
BillID: billID,
|
||||
Type: billType,
|
||||
IncomePrice: incomePrice,
|
||||
}
|
||||
dao.WrapAddIDCULEntity(billIncome, jxcontext.AdminCtx.GetUserName())
|
||||
return dao.CreateEntity(db, billIncome)
|
||||
}
|
||||
|
||||
func AddBillExpend(db *dao.DaoDB, billID int64, billType, expendPrice int) (err error) {
|
||||
billExpend := &model.BillExpend{
|
||||
BillID: billID,
|
||||
Type: billType,
|
||||
ExpendPrice: expendPrice,
|
||||
}
|
||||
dao.WrapAddIDCULEntity(billExpend, jxcontext.AdminCtx.GetUserName())
|
||||
return dao.CreateEntity(db, billExpend)
|
||||
}
|
||||
@@ -17,21 +17,17 @@ import (
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
|
||||
var (
|
||||
IPayHandler PayHandlerInterface
|
||||
)
|
||||
|
||||
func (p *PayHandler) CreatePay(order *model.Order, payType int, vendorPayType string) (err error) {
|
||||
switch payType {
|
||||
func (p *PayHandler) CreatePay() (err error) {
|
||||
switch p.PayType {
|
||||
case model.PayTypeTL:
|
||||
param := &tonglianpayapi.CreateUnitorderOrderParam{
|
||||
Trxamt: int(order.PayPrice),
|
||||
Trxamt: int(p.Order.PayPrice),
|
||||
NotifyUrl: globals.TLPayNotifyURL,
|
||||
Reqsn: utils.Int64ToStr(order.OrderID),
|
||||
PayType: vendorPayType,
|
||||
Reqsn: utils.Int64ToStr(p.Order.OrderID),
|
||||
PayType: p.VendorPayType,
|
||||
}
|
||||
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
|
||||
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
|
||||
if p.VendorPayType == tonglianpayapi.PayTypeWxXcx {
|
||||
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
|
||||
param.Acct = authInfo.GetAuthID()
|
||||
}
|
||||
}
|
||||
@@ -39,66 +35,17 @@ func (p *PayHandler) CreatePay(order *model.Order, payType int, vendorPayType st
|
||||
if err == nil {
|
||||
var result2 tonglianpayapi.PayInfo
|
||||
json.Unmarshal([]byte(result.PayInfo), &result2)
|
||||
order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
||||
order.TransactionID = result.TrxID
|
||||
order.OriginalData = utils.LimitUTF8StringLen(result.PayInfo, 3200)
|
||||
|
||||
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
||||
p.Order.TransactionID = result.TrxID
|
||||
// p.Order.OriginalData = utils.LimitUTF8StringLen(result.PayInfo, 3200)
|
||||
_, err = dao.UpdateEntity(dao.GetDB(), p.Order, "PrepayID", "TransactionID", "OriginalData")
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("支付方式:%d当前不支持", payType)
|
||||
err = fmt.Errorf("支付方式:%d当前不支持", p.PayType)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Pay4OrderByTL(ctx *jxcontext.Context, order *model.GoodsOrder, payType int, vendorPayType string) (orderPay *model.OrderPay, err error) {
|
||||
payCreatedAt := time.Now()
|
||||
param := &tonglianpayapi.CreateUnitorderOrderParam{
|
||||
Trxamt: int(order.ActualPayPrice),
|
||||
NotifyUrl: globals.TLPayNotifyURL,
|
||||
Reqsn: order.VendorOrderID,
|
||||
PayType: vendorPayType,
|
||||
}
|
||||
//暂时做兼容处理
|
||||
if vendorPayType == "JSAPI" {
|
||||
param.PayType = tonglianpayapi.PayTypeWxXcx
|
||||
}
|
||||
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
|
||||
if authInfo, err := ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini {
|
||||
param.Acct = authInfo.GetAuthID()
|
||||
}
|
||||
}
|
||||
if vendorPayType == tonglianpayapi.PayTypeH5 {
|
||||
param2 := &tonglianpayapi.CreateH5UnitorderOrderParam{
|
||||
Trxamt: int(order.ActualPayPrice),
|
||||
NotifyUrl: globals.TLPayNotifyURL,
|
||||
Body: "京西菜市",
|
||||
Charset: "UTF-8",
|
||||
}
|
||||
err = api.TLpayAPI.CreateH5UnitorderOrder(param2)
|
||||
} else {
|
||||
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
|
||||
if err == nil {
|
||||
var result2 tonglianpayapi.PayInfo
|
||||
json.Unmarshal([]byte(result.PayInfo), &result2)
|
||||
prePayID := result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
||||
orderPay = &model.OrderPay{
|
||||
PayOrderID: param.Reqsn,
|
||||
PayType: payType,
|
||||
VendorPayType: vendorPayType,
|
||||
TransactionID: result.TrxID,
|
||||
VendorOrderID: order.VendorOrderID,
|
||||
VendorID: order.VendorID,
|
||||
Status: 0,
|
||||
PayCreatedAt: payCreatedAt,
|
||||
PrepayID: prePayID,
|
||||
CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200),
|
||||
TotalFee: int(order.ActualPayPrice),
|
||||
}
|
||||
}
|
||||
}
|
||||
return orderPay, err
|
||||
}
|
||||
|
||||
func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) {
|
||||
globals.SugarLogger.Debugf("OnTLPayCallback msg:%s", utils.Format4Output(call, true))
|
||||
switch call.TrxCode {
|
||||
@@ -111,30 +58,28 @@ func OnTLPayCallback(call *tonglianpayapi.CallBackResult) (err error) {
|
||||
}
|
||||
|
||||
func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
|
||||
orderPay := &model.OrderPay{
|
||||
PayOrderID: call.CusorderID,
|
||||
// PayType: model.PayTypeTL,
|
||||
order := &model.Order{
|
||||
OrderID: utils.Str2Int64(call.CusorderID),
|
||||
}
|
||||
orderPay.DeletedAt = utils.DefaultTimeValue
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetEntity(db, orderPay, "PayOrderID", "DeletedAt"); err == nil {
|
||||
if orderPay.Status != 0 {
|
||||
if err = dao.GetEntity(db, order, "OrderID"); err == nil {
|
||||
if order.Status != model.OrderStatusWait4Pay {
|
||||
globals.SugarLogger.Debugf("already pay msg:%s, err:%v", utils.Format4Output(call, true), err)
|
||||
return err
|
||||
}
|
||||
loc, _ := time.LoadLocation("Local")
|
||||
t1, _ := time.ParseInLocation("20060102150405", call.PayTime, loc)
|
||||
orderPay.PayFinishedAt = utils.Time2Pointer(t1)
|
||||
// orderPay.TransactionID = call.ChnlTrxID
|
||||
orderPay.OriginalData = utils.Format4Output(call, true)
|
||||
order.PayFinishedAt = utils.Time2Pointer(t1)
|
||||
// order.TransactionID = call.ChnlTrxID
|
||||
order.OriginalData = utils.Format4Output(call, true)
|
||||
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
|
||||
orderPay.Status = model.PayStatusYes
|
||||
order.Status = model.OrderStatusFinished
|
||||
} else {
|
||||
orderPay.Status = model.PayStatusFailed
|
||||
order.Status = model.OrderStatusCanceled
|
||||
}
|
||||
dao.UpdateEntity(db, orderPay)
|
||||
dao.UpdateEntity(db, order)
|
||||
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
|
||||
// err = OnPayFinished(orderPay)
|
||||
err = OnPayFinished(order)
|
||||
}
|
||||
} else {
|
||||
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)
|
||||
|
||||
@@ -1,13 +1,64 @@
|
||||
package financial
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type PayHandler struct {
|
||||
PayType int `json:"-"` //支付方式
|
||||
PayType int `json:"-"` //支付方式
|
||||
Ctx *jxcontext.Context
|
||||
Order *model.Order
|
||||
VendorPayType string
|
||||
}
|
||||
|
||||
type PayHandlerInterface interface {
|
||||
CreatePay(order *model.Order, payType int, vendorPayType string) (err error)
|
||||
CreatePay() (err error)
|
||||
}
|
||||
|
||||
func OnPayFinished(order *model.Order) (err error) {
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
billID int64
|
||||
)
|
||||
globals.SugarLogger.Debugf("OnPayFinished begin modify account order: %v", utils.Format4Output(order, false))
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
//如果用户没有对应账单信息就给他生成一条
|
||||
userBill, err := dao.GetUserBill(db, order.UserID, "")
|
||||
if userBill == nil {
|
||||
globals.SugarLogger.Debugf("OnPayFinished 未找到该用户的账单 order: %v", utils.Format4Output(order, false))
|
||||
return fmt.Errorf("未找到该用户的账单!%v", order.UserID)
|
||||
}
|
||||
//根据订单类型来操作账户
|
||||
switch order.Type {
|
||||
case model.OrderTypeDeposit:
|
||||
//如果是发布任务的保证金
|
||||
//1、账户收入表增加一条保证金记录
|
||||
if err = AddBillIncome(db, billID, order.Type, order.PayPrice); err != nil {
|
||||
dao.Rollback(db)
|
||||
}
|
||||
//2、账户表保证金总额增加相应值
|
||||
userBill.DepositBalance += order.PayPrice
|
||||
if _, err = dao.UpdateEntity(db, userBill, "DepositBalance"); err != nil {
|
||||
dao.Rollback(db)
|
||||
}
|
||||
default:
|
||||
globals.SugarLogger.Debugf("OnPayFinished 暂不支持此订单类型 order: %v", utils.Format4Output(order, false))
|
||||
return fmt.Errorf("暂不支持此订单类型!")
|
||||
}
|
||||
dao.Commit(db)
|
||||
globals.SugarLogger.Debugf("OnPayFinished end modify account ...")
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user