添加微信支付下单
This commit is contained in:
96
trunk/center/paycenter/internal/pay/api.go
Normal file
96
trunk/center/paycenter/internal/pay/api.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"common/remark"
|
||||
"common/resultStatus"
|
||||
"common/webServer"
|
||||
"goutil/intUtil"
|
||||
"paycenter/internal"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
//注册接口
|
||||
webServer.RegisterFunction(new(PayApi))
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "PayApi"
|
||||
desc := "用户接口"
|
||||
author := "tangping"
|
||||
mendor := ""
|
||||
date := "2025年1月8日15:49:03"
|
||||
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
||||
}
|
||||
|
||||
// PayApi 用户接口
|
||||
type PayApi struct {
|
||||
}
|
||||
|
||||
// ---------------------------------------- 接口 --------------------------------------------------
|
||||
func init() {
|
||||
moduleName := "PayApi"
|
||||
methodName := "PlaceAnOrder"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "下单"
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2025年1月8日15:51:34"
|
||||
methodInParam := []string{"int32:充值模版id,string:玩家id,int32:区服id"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
||||
"Data '类型:interface{}'": "响应结果的数据"
|
||||
{
|
||||
"OrderID '类型:int64'": "订单id",
|
||||
"prepayId '类型:int64'": "预支付会话id",
|
||||
}
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
func (a *PayApi) PlaceAnOrder(modelID int32, playerID string, serverID int64) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//校验参数
|
||||
if modelID == 0 || playerID == "" || serverID == 0 {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
//获取对应充值配置
|
||||
|
||||
orderId, err := intUtil.ShuffleIntDigits(time.Now().UnixNano())
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
//下微信订单
|
||||
prepayId, err := internal.Prepay(orderId, "描述!!!!!!!!!!")
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
//处理数据
|
||||
order := &Order{
|
||||
OrderID: orderId,
|
||||
PrepayId: prepayId,
|
||||
OrderMoney: 100,
|
||||
OrderStatus: 1,
|
||||
PlayerID: playerID,
|
||||
ServerID: serverID,
|
||||
UserID: playerID,
|
||||
OrderDate: time.Now(),
|
||||
}
|
||||
|
||||
//添加到数据库
|
||||
AddOrder(order)
|
||||
|
||||
resultMap := make(map[string]any)
|
||||
resultMap["orderID"] = order.OrderID
|
||||
resultMap["prepayId"] = order.PrepayId
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
69
trunk/center/paycenter/internal/pay/logic.go
Normal file
69
trunk/center/paycenter/internal/pay/logic.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// 用户缓存对象
|
||||
orderMap = make(map[int64]*Order)
|
||||
rwmu sync.RWMutex
|
||||
)
|
||||
|
||||
// GetUserByID 根据用户id获取用户信息
|
||||
func GetUserByID(orderID int64) (*Order, error) {
|
||||
|
||||
//判断缓存是否存在
|
||||
var order *Order
|
||||
|
||||
func() *Order {
|
||||
rwmu.RLock()
|
||||
defer rwmu.RUnlock()
|
||||
ok := true
|
||||
if order, ok = orderMap[orderID]; ok {
|
||||
return order
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
if order != nil {
|
||||
return order, nil
|
||||
}
|
||||
|
||||
result := connection.GetPayDB().First(&order, orderID)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
//添加缓存
|
||||
func() {
|
||||
rwmu.Lock()
|
||||
defer rwmu.Unlock()
|
||||
orderMap[order.OrderID] = order
|
||||
}()
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
// AddUserCache 添加用户缓存
|
||||
func AddUserCache(order *Order) {
|
||||
rwmu.Lock()
|
||||
defer rwmu.Unlock()
|
||||
orderMap[order.OrderID] = order
|
||||
}
|
||||
|
||||
// AddOrder 添加订单
|
||||
func AddOrder(order *Order) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetPayDB().Create(&order) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加支付订单失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
|
||||
return order.OrderID, nil
|
||||
}
|
||||
34
trunk/center/paycenter/internal/pay/order.go
Normal file
34
trunk/center/paycenter/internal/pay/order.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&Order{})
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
//订单id
|
||||
OrderID int64 `gorm:"column:order_id;primary_key;comment:订单id" json:"order_id"`
|
||||
//订单日期
|
||||
OrderDate time.Time `gorm:"column:order_date;comment:订单日期" json:"order_date"`
|
||||
//订单号
|
||||
PrepayId string `gorm:"column:prepay_id;comment:预支付交易会话标识" json:"prepay_id"`
|
||||
//订单金额
|
||||
OrderMoney int64 `gorm:"column:order_money;comment:订单金额" json:"order_money"`
|
||||
//订单状态
|
||||
OrderStatus int32 `gorm:"column:order_status;comment:订单状态" json:"order_status"`
|
||||
//用户id
|
||||
UserID string `gorm:"column:user_id;comment:用户id" json:"user_id"`
|
||||
//区服id
|
||||
ServerID int64 `gorm:"column:server_id;comment:区服id" json:"server_id"`
|
||||
//玩家标识
|
||||
PlayerID string `gorm:"column:player_id;comment:玩家标识" json:"player_id"`
|
||||
}
|
||||
|
||||
func (order *Order) TableName() string {
|
||||
return "order"
|
||||
}
|
||||
Reference in New Issue
Block a user