一波更新
This commit is contained in:
@@ -5,7 +5,11 @@ import (
|
||||
"common/resultStatus"
|
||||
"common/webServer"
|
||||
"goutil/intUtil"
|
||||
"goutil/logUtilPlus"
|
||||
"goutil/webUtil"
|
||||
"net/http"
|
||||
"paycenter/internal"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -24,7 +28,7 @@ func init() {
|
||||
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
||||
}
|
||||
|
||||
// PayApi 用户接口
|
||||
// PayApi 支付接口
|
||||
type PayApi struct {
|
||||
}
|
||||
|
||||
@@ -37,7 +41,7 @@ func init() {
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2025年1月8日15:51:34"
|
||||
methodInParam := []string{"int32:充值模版id,string:玩家id,int32:区服id"}
|
||||
methodInParam := []string{"int64:订单id(由服务器生成),int32:充值模版id,int64:充值金额,string:商品id, string:客户端IP地址,string:玩家id,int32:区服id"}
|
||||
methodOutParam := `
|
||||
{
|
||||
"Code '类型:int'": "响应结果的状态值",
|
||||
@@ -50,7 +54,14 @@ func init() {
|
||||
}`
|
||||
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
|
||||
}
|
||||
func (a *PayApi) PlaceAnOrder(modelID int32, playerID string, serverID int64) (responseObj *webServer.ResponseObject) {
|
||||
|
||||
// 需要获取请求信息
|
||||
func init() {
|
||||
moduleName := "PayApi"
|
||||
methodName := "PlaceAnOrder"
|
||||
webServer.AddNeedGetRequestInfo(moduleName, methodName)
|
||||
}
|
||||
func (a *PayApi) PlaceAnOrder(orderId int64, modelID int32, currency int64, storeId string, playerID string, serverID int64, r *http.Request) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//校验参数
|
||||
@@ -67,23 +78,17 @@ func (a *PayApi) PlaceAnOrder(modelID int32, playerID string, serverID int64) (r
|
||||
return
|
||||
}
|
||||
|
||||
//客户端请求ip
|
||||
clientIp := webUtil.GetRequestIP(r)
|
||||
|
||||
//下微信订单
|
||||
prepayId, err := internal.Prepay(orderId, "描述!!!!!!!!!!")
|
||||
prepayId, err := internal.Prepay(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
|
||||
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(),
|
||||
}
|
||||
order := NewOrder(orderId, prepayId, playerID, serverID, 1)
|
||||
|
||||
//添加到数据库
|
||||
AddOrder(order)
|
||||
@@ -94,3 +99,71 @@ func (a *PayApi) PlaceAnOrder(modelID int32, playerID string, serverID int64) (r
|
||||
responseObj.SetData(resultMap)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
moduleName := "PayApi"
|
||||
methodName := "CallBack"
|
||||
skipVerifyTokenPage := true
|
||||
methodDesc := "支付成功回调"
|
||||
methodAuthor := "tangping"
|
||||
methodMendor := ""
|
||||
methodDate := "2025年1月21日16:40:57"
|
||||
methodInParam := []string{"string:订单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) CallBack(orderIDStr string) (responseObj *webServer.ResponseObject) {
|
||||
responseObj = webServer.GetInitResponseObj()
|
||||
|
||||
//参数错误
|
||||
if orderIDStr == "" {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
}
|
||||
|
||||
//orderID 转换成string
|
||||
orderID, err := strconv.ParseInt(orderIDStr, 10, 64)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("订单id转换失败", err.Error())
|
||||
responseObj.SetResultStatus(resultStatus.APIParamError)
|
||||
return
|
||||
}
|
||||
|
||||
//是否已经处理
|
||||
order, err := GetUserByID(orderID)
|
||||
|
||||
//支付成功
|
||||
if err == nil && order != nil && order.OrderStatus == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
//查询订单状态
|
||||
statusStr, err := internal.QueryOrderByOutTradeNo(orderID)
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
//状态错误
|
||||
if statusStr != "SUCCESS" {
|
||||
logUtilPlus.WarnLog("订单状态错误", statusStr)
|
||||
return
|
||||
}
|
||||
|
||||
err = ChangeOrderStatus(orderID, 1)
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
}
|
||||
|
||||
return responseObj
|
||||
}
|
||||
|
||||
95
trunk/center/paycenter/internal/pay/check_order.go
Normal file
95
trunk/center/paycenter/internal/pay/check_order.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
"paycenter/internal"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
go CheckOrderStatus()
|
||||
}
|
||||
|
||||
// CheckOrderStatus 查询订单状态
|
||||
func CheckOrderStatus() {
|
||||
|
||||
//捕获异常
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logUtilPlus.ErrorLog("CheckOrderStatus panic:", err)
|
||||
restartConsumer()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
//检索最近一个月的订单
|
||||
for i := 0; i < 2; i++ {
|
||||
|
||||
//取i的负数
|
||||
dbDate := connection.GetToMonthAdd(int32(-i))
|
||||
var orders []Order // 使用切片存储查询结果
|
||||
|
||||
//这里使用原始sql
|
||||
sql := "select * from order_" + strconv.Itoa(int(dbDate)) + " where order_status = 0"
|
||||
dbResult := connection.GetPayDB().Exec(sql).Find(&orders)
|
||||
if dbResult.Error != nil {
|
||||
logUtilPlus.ErrorLog("查询订单状态失败", dbResult.Error.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
// 处理查询结果
|
||||
for _, order := range orders {
|
||||
|
||||
//查询订单状态
|
||||
statusStr, err := internal.QueryOrderByOutTradeNo(order.OrderID)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("查询订单状态失败", err.Error())
|
||||
continue
|
||||
}
|
||||
if statusStr == "SUCCESS" {
|
||||
//修改订单状态
|
||||
err = ChangeOrderStatus(order.OrderID, 1)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("修改订单状态失败", err.Error())
|
||||
continue
|
||||
}
|
||||
} else if statusStr == "CLOSED" { //已关闭
|
||||
order.OrderStatus = 2
|
||||
//修改订单状态
|
||||
connection.AsyncSave(connection.GetPayDB(), &order)
|
||||
} else if order.OrderTime.Add(time.Hour * 1).Before(time.Now()) { //超一个小时未支付 直接关闭订单
|
||||
//直接关闭订单
|
||||
internal.CloseOrder(order.OrderID)
|
||||
order.OrderStatus = 2
|
||||
connection.AsyncSave(connection.GetPayDB(), &order)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//休息30分钟
|
||||
time.Sleep(time.Minute * 30)
|
||||
}
|
||||
}
|
||||
|
||||
// restartConsumer 重启消费者
|
||||
func restartConsumer() {
|
||||
// 设置重试次数
|
||||
maxRetries := 5
|
||||
retryCount := 0
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(5 * time.Second): // 等待5秒后重试
|
||||
if retryCount >= maxRetries {
|
||||
logUtilPlus.ErrorLog("查询订单状态,达到最大重试次数")
|
||||
return
|
||||
}
|
||||
logUtilPlus.InfoLog("查询订单状态,重试次数: %d", retryCount+1)
|
||||
go CheckOrderStatus()
|
||||
return
|
||||
}
|
||||
retryCount++
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@ package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
"fmt"
|
||||
"paycenter/internal/mesqueue"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -59,14 +60,41 @@ func AddOrder(order *Order) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetPayDB().Create(&order) // 通过数据的指针来创建
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加支付订单失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
// 异步 写入到数据库
|
||||
connection.AsyncCreate(connection.GetPayDB(), &order)
|
||||
|
||||
//添加缓存
|
||||
AddOrderCache(order)
|
||||
|
||||
return order.OrderID, nil
|
||||
}
|
||||
|
||||
// ChangeOrderStatus 改变订单状态
|
||||
func ChangeOrderStatus(orderID int64, status int32) error {
|
||||
|
||||
//根据订单id获取订单
|
||||
order, err := GetUserByID(orderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//判断订单是否存在
|
||||
if order == nil {
|
||||
return fmt.Errorf("订单不存在,订单id %d", orderID)
|
||||
}
|
||||
|
||||
if status == 1 {
|
||||
mesqueue.AddQueue(mesqueue.GameMsg{
|
||||
GameId: "",
|
||||
OrderID: orderID,
|
||||
})
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
order.OrderStatus = status
|
||||
order.IsNotifyGameServer = 1
|
||||
|
||||
//异步更新数据库
|
||||
connection.AsyncSave(connection.GetPayDB(), &order)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -13,13 +14,11 @@ func init() {
|
||||
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"`
|
||||
//订单状态
|
||||
//订单状态 0:未支付 1:已支付 2:已关闭
|
||||
OrderStatus int32 `gorm:"column:order_status;comment:订单状态" json:"order_status"`
|
||||
//用户id
|
||||
UserID string `gorm:"column:user_id;comment:用户id" json:"user_id"`
|
||||
@@ -27,8 +26,42 @@ type Order struct {
|
||||
ServerID int64 `gorm:"column:server_id;comment:区服id" json:"server_id"`
|
||||
//玩家标识
|
||||
PlayerID string `gorm:"column:player_id;comment:玩家标识" json:"player_id"`
|
||||
//是否通知游戏服 0:未通知 1:已通知
|
||||
IsNotifyGameServer int32 `gorm:"column:is_notify_game_server;comment:是否通知游戏服" json:"is_notify_game_server"`
|
||||
//订单日期
|
||||
OrderDate time.Time `gorm:"column:order_date;comment:订单日期" json:"order_date"`
|
||||
//订单时间
|
||||
OrderTime time.Time `gorm:"column:order_time;type:date;comment:订单时间" json:"order_time"`
|
||||
//订单类型 1:微信支付 2:支付宝支付
|
||||
OrderType int32 `gorm:"column:order_type;comment:订单类型" json:"order_type"`
|
||||
|
||||
//表的名字 这里是表的后缀
|
||||
TableNameData int32 `gorm:"column:table_name;comment:表的名字 这里是表的后缀" json:"table_name"`
|
||||
}
|
||||
|
||||
// TableName 表名
|
||||
func (order *Order) TableName() string {
|
||||
return "order"
|
||||
return "order_" + strconv.Itoa(int(order.TableNameData))
|
||||
}
|
||||
|
||||
// NewOrder 创建订单
|
||||
// @param orderId 订单id
|
||||
// @param prepayId 预支付id
|
||||
// @param playerID 玩家id
|
||||
// @param serverID 服务器id
|
||||
// @param orderType 订单类型
|
||||
func NewOrder(orderId int64, prepayId string, playerID string, serverID int64, orderType int32) *Order {
|
||||
return &Order{
|
||||
OrderID: orderId,
|
||||
PrepayId: prepayId,
|
||||
OrderMoney: 100,
|
||||
OrderStatus: 1,
|
||||
PlayerID: playerID,
|
||||
ServerID: serverID,
|
||||
UserID: playerID,
|
||||
OrderDate: time.Now(),
|
||||
OrderTime: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
OrderType: orderType,
|
||||
TableNameData: connection.GetMonth(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user