This commit is contained in:
邹宗楠
2022-06-28 14:10:05 +08:00
parent d5fa7c2a36
commit 86f18cc35f
7 changed files with 145 additions and 41 deletions

View File

@@ -79,7 +79,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 string) (result *financial.WxPayParam, err error) {
func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType, appId string) (result *financial.WxPayParam, err error) {
var (
db = dao.GetDB()
order = &model.Order{
@@ -109,7 +109,7 @@ func Pay(ctx *jxcontext.Context, orderID string, payType int, vendorPayType stri
if userBill == nil {
err = financial.AddUserBill(txDB, jxutils.GenBillID(), order.UserID)
}
err = payHandler.CreatePay(txDB)
err = payHandler.CreatePay(txDB, appId)
dao.Commit(db, txDB)
globals.SugarLogger.Debugf("result : %v", utils.Format4Output(payHandler.WxPayParam, false))
return payHandler.WxPayParam, err

View File

@@ -34,28 +34,71 @@ var (
}
)
func (p *PayHandler) CreatePay(txDB orm.TxOrmer) (err error) {
func (p *PayHandler) CreatePay(txDB orm.TxOrmer, subAppID string) (err error) {
switch p.PayType {
case model.PayTypeTL:
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(p.Order.PayPrice),
Trxamt: p.Order.PayPrice,
NotifyUrl: globals.TLPayNotifyURL,
Reqsn: p.Order.OrderID,
PayType: p.VendorPayType,
}
//暂时做兼容处理
if p.VendorPayType == "JSAPI" {
param.PayType = tonglianpayapi.PayTypeWxXcx
}
if p.VendorPayType == tonglianpayapi.PayTypeWxXcx {
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeWxApp {
param.SubAppID = subAppID
authInfo, err := p.Ctx.GetV2AuthInfo()
// 微信小程序支付
if err == nil && authInfo.GetAuthType() == weixin.AuthTypeMini && authInfo.GetAuthTypeID() == subAppID {
param.Acct = authInfo.GetAuthID()
}
}
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
if err == nil {
var result2 tonglianpayapi.PayInfo
json.Unmarshal([]byte(result.PayInfo), &result2)
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
p.Order.TransactionID = result.TrxID
_, err = dao.UpdateEntityTx(txDB, p.Order, "PrepayID", "TransactionID")
if p.VendorPayType == tonglianpayapi.PayTypeZfbJS || p.VendorPayType == tonglianpayapi.PayTypeZfbApp {
if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil {
param.Acct = authInfo.GetAuthID()
}
if param.Acct == "" {
return fmt.Errorf("未找到用户的认证ID")
}
}
if p.VendorPayType == tonglianpayapi.PayTypeH5 {
param2 := &tonglianpayapi.CreateH5UnitorderOrderParam{
Trxamt: p.Order.PayPrice,
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)
p.Order.PrepayID = result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
p.Order.TransactionID = result.TrxID
_, err = dao.UpdateEntityTx(txDB, p.Order, "PrepayID", "TransactionID")
}
//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),
//}
}
// 暂时不支持微信直接支付
case model.PayTypeWX:
param := &wxpayapi.CreateOrderParam{
OutTradeNo: p.Order.OrderID,
@@ -213,16 +256,24 @@ func onTLpayFinished(call *tonglianpayapi.CallBackResult) (err error) {
loc, _ := time.LoadLocation("Local")
t1, _ := time.ParseInLocation("20060102150405", call.PayTime, loc)
order.PayFinishedAt = t1
// order.TransactionID = call.ChnlTrxID
order.OriginalData = utils.Format4Output(call, true)
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
order.Status = model.OrderStatusFinished
} else {
order.Status = model.OrderStatusCanceled
}
dao.UpdateEntity(db, order)
if _, err := dao.UpdateEntity(db, order); err != nil {
return err
}
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
err = OnPayFinished(order)
switch order.OrderType {
case model.PayType4Express:
case model.PayType4Member, model.PayType4Recharge:
err = OnPayFinished(order)
}
}
} else {
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)

View File

@@ -36,9 +36,7 @@ type PayHandlerInterface interface {
}
func OnPayFinished(order *model.Order) (err error) {
var (
db = dao.GetDB()
)
var db = dao.GetDB()
globals.SugarLogger.Debugf("OnPayFinished begin modify account order: %v", utils.Format4Output(order, false))
txDB, _ := dao.Begin(db)
defer func() {

View File

@@ -18,6 +18,10 @@ const (
VendorPayTypeCompanyPay = "companyPay" //企业付款
VendorPayTypeTransferAccount = "transferAccount" //手动转账
PayType4Member = 1 // 购买会员
PayType4Recharge = 2 // 充值余额
PayType4Express = 3 // 支付快递
)
const (
@@ -49,23 +53,23 @@ var (
type Order struct {
ModelIDCUL
OrderID string `orm:"column(order_id)" json:"orderID"` //订单号
UserID string `orm:"column(user_id);size(48)" json:"userID"` //用户ID
Type int `json:"type"`
OrderType int `json:"orderType"` //订单类型
OrderID string `orm:"column(order_id)" json:"orderID"` //订单号
UserID string `orm:"column(user_id);size(48)" json:"userID"` //用户ID
Type int `json:"type"` // 支付还是提现
OrderType int `json:"orderType"` // 订单类型1为发任务2为冲会员3为发快递
Way string `json:"way"` //weixinapp ,weixinmini
Status int `json:"status"` //订单状态,待支付2已支付5支付成功110支付失败115
PayPrice int `json:"payPrice"` //支付金额
TransactionID string `orm:"column(transaction_id);size(48)" json:"transactionID"` // 支付成功后支付方生成的事务ID
PayFinishedAt time.Time `orm:"type(datetime);null" json:"payFinishedAt"`
PrepayID string `orm:"column(prepay_id);size(48)" json:"prepayID"` // 下单后支付前支付方生成的事务ID
PayFinishedAt time.Time `orm:"type(datetime);null" json:"payFinishedAt"` // 支付完成时间
PrepayID string `orm:"column(prepay_id);size(48)" json:"prepayID"` // 下单后支付前支付方生成的事务ID
OriginalData string `orm:"type(text)" json:"-"`
Comment string `orm:"size(255)" json:"comment"` //备注
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
CityCode int `orm:"default(0)" json:"cityCode"` //提交订单时用户所在城市
DistrictCode int `orm:"default(0)" json:"districtCode"`
Address string `orm:"size(255)" json:"address"`
Comment string `orm:"size(255)" json:"comment"` //备注
Lng float64 `json:"lng"` // 坐标
Lat float64 `json:"lat"` // 坐标
CityCode int `orm:"default(0)" json:"cityCode"` //提交订单时用户所在城市
DistrictCode int `orm:"default(0)" json:"districtCode"` // 城市code
Address string `orm:"size(255)" json:"address"` // 地址
}
func (v *Order) TableUnique() [][]string {

View File

@@ -30,9 +30,9 @@ type UserVendorOrder struct {
Bulk float64 `orm:"column(bulk)" json:"bulk"` // 体积抛比系数
Increment float64 `orm:"column(increment)" json:"increment"` // 增值(物流)
ChannelType int `orm:"size(8);column(channel_type)" json:"channelType"` // 渠道类型1-快递2-物流3-国际物流4-整车)
OrderStatus int `orm:"size(8);column(order_status)" json:"orderType"` // 订单状态(1-待支付,2-支付失败,3-支付成功,4-取件,5-配送,6-4-取消)
OrderStatus int `orm:"size(8);column(order_status)" json:"orderType"` // 订单状态(2-待支付,3-支付失败,4-支付成功,5-取件,6-配送,25-取消)
Img string `orm:"column(img)" json:"img"` // 包裹图片
IsForward int `orm:"column(is_forward)" json:"isForward"` // 1否2是
IsForward int `orm:"column(is_forward)" json:"isForward"` // 1否2是 转寄单
}
func (*UserVendorOrder) TableUnique() [][]string {

View File

@@ -66,16 +66,16 @@ func CreateWayOrder(ctx *jxcontext.Context, param *model.MakeOrderParamReq, user
}
// 创建三方订单
otherId, err := createOtherOrder(param)
if err != nil {
return err
}
//otherId, err := createOtherOrder(param)
//if err != nil {
// return err
//}
// 第三方数据创建成功,则创建本地数据
vendorOrder := &model.UserVendorOrder{
UserId: userId,
LocalWayBill: utils.Int64ToStr(time.Now().Unix()) + userId[:4], // 当前时间秒数加用户ID前四位
OtherWayBill: otherId,
OtherWayBill: utils.Int64ToStr(time.Now().Unix()) + userId[:4],
PromiseTimeType: param.PromiseTimeType,
DeliveryType: param.DeliveryType,
Goods: param.Goods,
@@ -99,12 +99,54 @@ func CreateWayOrder(ctx *jxcontext.Context, param *model.MakeOrderParamReq, user
Bulk: param.Bulk,
Increment: param.Increment,
ChannelType: param.ChannelType,
OrderStatus: model.YES, // 创建
OrderStatus: 2, // 创建待支付
Img: param.Images,
IsForward: model.YES,
IsForward: model.YES, //
}
// 事务创建待支付运单和待支付order
db := dao.GetDB()
tdb, _ := dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db, tdb)
panic(r)
}
}()
// 添加运单表
dao.WrapAddIDCULEntity(vendorOrder, ctx.GetUserName())
return dao.CreateEntity(dao.GetDB(), vendorOrder)
if err := dao.CreateEntity(db, vendorOrder); err != nil {
dao.Rollback(db, tdb)
return err
}
// 添加待支付订单表
orderPayStatus := &model.Order{
OrderID: vendorOrder.LocalWayBill,
UserID: vendorOrder.UserId,
Type: model.OrderTypePay,
OrderType: model.PayType4Express,
Way: "",
Status: model.OrderTypeCash, // 待支付状态
PayPrice: int(vendorOrder.ChannelFee * 100),
TransactionID: "",
PayFinishedAt: time.Time{},
PrepayID: "",
OriginalData: "",
Comment: "",
Lng: 0,
Lat: 0,
CityCode: 0,
DistrictCode: 0,
Address: "",
}
if err := dao.CreateEntity(db, orderPayStatus); err != nil {
dao.Rollback(db, tdb)
return err
}
dao.Commit(db, tdb)
return nil
}
// CancelWayOrder 取消运单 todo
@@ -175,3 +217,9 @@ func QueryUserOrderList(userId string, expressType, orderStatus int, pageNum, pa
}
return result, nil
}
// CreateOrder2QBiDa 订单回调成功,且为运费支付时使用该接口
func CreateOrder2QBiDa(orderId string) {
// 加载订单
}

View File

@@ -11,18 +11,21 @@ type OrderController struct {
beego.Controller
}
// Pay 订单支付
// @Title 支付
// @Description 支付
// @Param token header string true "认证token"
// @Param orderID formData string true "订单号"
// @Param payType formData int true "支付平台类型"
// @Param vendorPayType formData string true "平台支付类型"
// @Param orderType formData string true "订单类型member会员express快递recharge充值"
// @Param appId formData string true "appId"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Pay [post]
func (c *OrderController) Pay() {
c.callPay(func(params *tOrderPayParams) (retVal interface{}, errCode string, err error) {
retVal, err = cms.Pay(params.Ctx, params.OrderID, params.PayType, params.VendorPayType)
retVal, err = cms.Pay(params.Ctx, params.OrderID, params.PayType, params.VendorPayType, params.AppId)
return retVal, "", err
})
}