Files
jx-print/services/order.go
2021-07-23 14:11:59 +08:00

94 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"encoding/json"
"fmt"
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/dao"
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
"git.rosy.net.cn/jx-print/putils"
"git.rosy.net.cn/jx-print/services/api"
"github.com/jmoiron/sqlx"
"strings"
"time"
)
var (
findOrderPayPriceMap = map[string]func(db *sqlx.DB, typeID string) (int64, error){
model.OrderTypeFlow: func(db *sqlx.DB, typeID string) (payPrice int64, err error) {
var flowCfg []*model.FlowConfig
if config, err2 := dao.GetConfig(db, model.ConfigTypeSys, model.OrderTypeFlow); err2 == nil && config != nil {
if err = json.Unmarshal([]byte(config.Value), &flowCfg); err == nil && len(flowCfg) > 0 {
for _, v := range flowCfg {
if v.ID == utils.Str2Int(typeID) {
payPrice = v.Price
break
}
}
}
} else {
err = err2
}
return payPrice, err
},
}
payFunc = map[string]func(db *sqlx.DB, order *model.PayOrder, vendorPayType string) (err error){
model.PayTypeTL: func(db *sqlx.DB, order *model.PayOrder, vendorPayType string) (err error) {
param := &tonglianpayapi.CreateUnitorderOrderParam{
Trxamt: int(order.PayPrice),
NotifyUrl: "http://print.jxc4.com/callback/tlPayCallback",
Reqsn: order.OrderID,
PayType: vendorPayType,
}
if vendorPayType != tonglianpayapi.PayTypeWxCode {
return fmt.Errorf("暂不支持的支付类型vendorPayType :%s", vendorPayType)
}
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
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
err = dao.Update(db, order, "prepay_id", "transaction_id")
}
return err
},
}
)
func CreateOrder(tokenInfo *model.TokenInfo, orderType, origin, thingID, typeID string) (orderID string, err error) {
var (
db = globals.GetDB()
now = time.Now()
payPrice int64
)
if payPrice, err = findOrderPayPriceMap[orderType](db, typeID); err != nil {
return "", err
}
orderID = utils.Int64ToStr(putils.GenOrderNo())
payOrder := &model.PayOrder{
CreatedAt: &now,
OrderID: orderID,
PayPrice: payPrice,
UserID: tokenInfo.User.UserID,
OrderType: orderType,
Origin: origin,
ThingID: thingID,
TypeID: typeID,
}
err = dao.Insert(db, payOrder)
return orderID, err
}
func Pay(tokenInfo *model.TokenInfo, orderID, payType, vendorPayType string) (order *model.PayOrder, err error) {
var (
db = globals.GetDB()
)
order, _ = dao.GetOrder(db, orderID)
err = payFunc[payType](db, order, vendorPayType)
return order, err
}