支付宝 接入一系列修改
This commit is contained in:
@@ -2,12 +2,13 @@ package pay
|
||||
|
||||
import (
|
||||
"common/remark"
|
||||
"common/resultStatus"
|
||||
"common/resultstatus"
|
||||
"common/webServer"
|
||||
"goutil/logUtilPlus"
|
||||
"goutil/webUtil"
|
||||
"net/http"
|
||||
"paycenter/internal"
|
||||
"paycenter/internal/alipay"
|
||||
"paycenter/internal/wxpay"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -74,7 +75,7 @@ func (a *PayApi) PlaceAnOrder(orderId int64, modelID int32, currency int64, stor
|
||||
clientIp := webUtil.GetRequestIP(r)
|
||||
|
||||
//下微信订单
|
||||
prepayId, err := internal.Prepay(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
|
||||
prepayId, err := wxpay.Prepay(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
@@ -139,7 +140,7 @@ func (a *PayApi) CallBack(orderIDStr string) (responseObj *webServer.ResponseObj
|
||||
}
|
||||
|
||||
//查询订单状态
|
||||
statusStr, err := internal.QueryOrderByOutTradeNo(orderID)
|
||||
statusStr, err := wxpay.QueryOrderByOutTradeNo(orderID)
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.DataError)
|
||||
return
|
||||
@@ -193,7 +194,7 @@ func (a *PayApi) AliPayPlaceAnOrder(orderId int64, modelID int32, currency int64
|
||||
clientIp := webUtil.GetRequestIP(r)
|
||||
|
||||
//下微信订单
|
||||
prepayId, err := internal.AliPayPlace(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
|
||||
prepayId, err := alipay.AliPayPlace(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
|
||||
if err != nil {
|
||||
responseObj.SetResultStatus(resultStatus.APIDataError)
|
||||
return
|
||||
@@ -209,4 +210,5 @@ func (a *PayApi) AliPayPlaceAnOrder(orderId int64, modelID int32, currency int64
|
||||
resultMap["orderID"] = order.OrderID
|
||||
resultMap["prepayId"] = order.PrepayId
|
||||
responseObj.SetData(resultMap)
|
||||
return responseObj
|
||||
}
|
||||
|
||||
@@ -2,94 +2,68 @@ package pay
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"common/timer"
|
||||
"goutil/logUtilPlus"
|
||||
"paycenter/internal"
|
||||
"paycenter/internal/wxpay"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
go CheckOrderStatus()
|
||||
timer.Register(timer.TriggerTypeHalfHour, CheckOrderStatus)
|
||||
}
|
||||
|
||||
// CheckOrderStatus 查询订单状态
|
||||
func CheckOrderStatus() {
|
||||
// CheckOrderStatus 检查订单状态
|
||||
// @return error
|
||||
func CheckOrderStatus(nowTime time.Time) error {
|
||||
|
||||
//捕获异常
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logUtilPlus.ErrorLog("CheckOrderStatus panic:", err)
|
||||
restartConsumer()
|
||||
//检索最近一个月的订单
|
||||
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 {
|
||||
//检索最近一个月的订单
|
||||
for i := 0; i < 2; i++ {
|
||||
// 处理查询结果
|
||||
for _, order := range orders {
|
||||
|
||||
//取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())
|
||||
//查询订单状态
|
||||
statusStr, err := wxpay.QueryOrderByOutTradeNo(order.OrderID)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("查询订单状态失败", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
// 处理查询结果
|
||||
for _, order := range orders {
|
||||
|
||||
//查询订单状态
|
||||
statusStr, err := internal.QueryOrderByOutTradeNo(order.OrderID)
|
||||
if statusStr == "SUCCESS" {
|
||||
//修改订单状态
|
||||
err = ChangeOrderStatus(order.OrderID, 1)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("查询订单状态失败", err.Error())
|
||||
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)
|
||||
} else if statusStr == "CLOSED" { //已关闭
|
||||
order.OrderStatus = 2
|
||||
//修改订单状态
|
||||
connection.AsyncSave(connection.GetPayDB(), &order)
|
||||
} else if order.OrderTime.Add(time.Hour * 1).Before(time.Now()) { //超一个小时未支付 直接关闭订单
|
||||
//直接关闭订单
|
||||
err = wxpay.CloseOrder(order.OrderID)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("关闭订单失败", err.Error())
|
||||
continue
|
||||
}
|
||||
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++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user