Merge remote-tracking branch 'origin/dev/dev1.0' into dev/dev1.0

This commit is contained in:
Rzy 2024-08-09 16:12:31 +08:00
commit 7ed9cb048d
19 changed files with 235 additions and 169 deletions

View File

@ -37,3 +37,13 @@ func (m *OrderRequestLogRepo) OrderRequestLogDelete(orderLog *orderrequestlogmod
func (m *OrderRequestLogRepo) OrderRequestLogUpdate(orderLog *orderrequestlogmodel.OrderRequestLog, conn builder.Cond, columns ...string) (int64, error) {
return m.repo.Where(conn).MustCols(columns...).Update(orderLog)
}
// 后台查询订单日志列表
func (m *OrderRequestLogRepo) OrderRequestLogBackendList(conn builder.Cond, pageFilter entities.PageRequest, orderLogList *[]orderrequestlogmodel.OrderRequestLog) (int64, error) {
repo := m.repo.Where(conn)
if pageFilter.Page > 0 {
repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1))
}
repo.Join("left", "orders", "orders.app_id = order_request_log.app_id and orders.out_trade_no = order_request_log.out_trade_no")
return repo.Desc("order_request_log.create_time").FindAndCount(orderLogList)
}

View File

@ -3,45 +3,69 @@ package controllers
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/api"
"PaymentCenter/app/utils"
"PaymentCenter/config"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func ApiRes(c *gin.Context, data interface{}, code int, msg ...string) {
var log_id int64
message := ""
var logId int64
var responseData interface{}
var message string
originData := "{}"
appCheckInfo := GetAppCheckInfo(c).(*services.AppCheck)
// 空数据
if utils.IsNil(data) {
data = struct{}{}
}
// 获取错误信息
if len(msg) > 0 {
message = msg[0]
} else {
message = errorcode.GetMsg(code, "")
}
// 获取日志ID
log, exists := GetApiLogId(c)
if exists {
log_id = log.(int64)
dataByte, _ := json.Marshal(data)
status := common.STATUS_ENABLE
if code == errorcode.Success {
status = common.STATUS_DISABLED
logId = log.(int64)
}
// 加密数据
if code == errorcode.Success {
if apiRsp, ok := data.(front.ApiResponse); ok {
apiRsp.Order, code = api.EnCrypt(appCheckInfo.App, apiRsp.Order)
if code != errorcode.Success {
message = errorcode.GetMsg(code, "")
}
responseData = apiRsp
} else {
responseData = data
}
services.RequestLogUpdate(&orderrequestlogmodel.OrderRequestLog{
Id: log_id,
MerchantResponse: string(dataByte),
Status: status,
})
}
if code == errorcode.Success {
ApiSuccess(c, data, log_id, message)
ApiSuccess(c, responseData, logId, message)
b, _ := json.Marshal(data)
originData = string(b)
} else {
ApiError(c, code, log_id, message)
ApiError(c, code, logId, message)
originData = fmt.Sprintf("{\"code\":%d,\"message\":\"%s\"}", code, message)
}
// 记录日志
if logId > 0 {
services.RequestLogUpdate(&orderrequestlogmodel.OrderRequestLog{
Id: logId,
AppId: appCheckInfo.AppId,
OutTradeNo: GetOutTradeNo(c),
MerchantResponse: originData,
Status: common.STATUS_DISABLED,
})
}
}

View File

@ -5,6 +5,7 @@ import (
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/ordercallbacklogmodel"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/orderthirdpaylogmodel"
@ -34,10 +35,10 @@ func OrderList(c *gin.Context) {
controllers.HandCodeRes(c, data, code)
}
func OrderLogsList(c *gin.Context) {
func OrderRequestLogsList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest)
req.SetDefault()
requestLog, thirdLog, code := services.OrderLogsList(*req)
requestLog, total, code := services.OrderRequestLogsList(*req)
requestLogList := make([]backend.OrderRequestLogResponse, 0, len(requestLog))
linq.From(requestLog).SelectT(func(in orderrequestlogmodel.OrderRequestLog) (out backend.OrderRequestLogResponse) {
@ -45,14 +46,42 @@ func OrderLogsList(c *gin.Context) {
return
}).ToSlice(&requestLogList)
controllers.HandCodeRes(c, entities.PageRsp{
Total: total,
Data: requestLogList,
}, code)
}
func OrderThirdLogsList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest)
req.SetDefault()
thirdLog, total, code := services.OrderThirdLogsList(*req)
thirdLogList := make([]backend.OrderThirdLogResponse, 0, len(thirdLog))
linq.From(thirdLog).SelectT(func(in orderthirdpaylogmodel.OrderThirdPayLog) (out backend.OrderThirdLogResponse) {
out.ResponseFromDb(in)
return
}).ToSlice(&thirdLogList)
controllers.HandCodeRes(c, gin.H{
"request_log_list": requestLogList,
"third_log_list": thirdLogList,
controllers.HandCodeRes(c, entities.PageRsp{
Total: total,
Data: thirdLogList,
}, code)
}
func OrderCallbackLogsList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest)
req.SetDefault()
callbackLog, total, code := services.OrderCallbackLogsList(*req)
callbackLogList := make([]backend.OrderCallbackLogResponse, 0, len(callbackLog))
linq.From(callbackLog).SelectT(func(in ordercallbacklogmodel.OrderCallbackLog) (out backend.OrderCallbackLogResponse) {
out.ResponseFromDb(in)
return
}).ToSlice(&callbackLogList)
controllers.HandCodeRes(c, entities.PageRsp{
Total: total,
Data: callbackLogList,
}, code)
}

View File

@ -203,6 +203,15 @@ func GetAppCheckInfo(c *gin.Context) interface{} {
return request
}
// 获取OutTradeNo
func GetOutTradeNo(c *gin.Context) string {
outTradeNo, _ := c.Get("OutTradeNo")
if outTradeNo == nil {
return ""
}
return outTradeNo.(string)
}
func GetApiLogId(c *gin.Context) (interface{}, bool) {
request, exists := c.Get("log")
return request, exists

View File

@ -7,14 +7,15 @@ import (
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay"
"PaymentCenter/app/services/thirdpay/api"
"github.com/gin-gonic/gin"
"xorm.io/builder"
)
func PayUrl(c *gin.Context) {
var res front.PayUrlResp
var res front.ApiResponse
req := controllers.GetRequest(c).(*front.PayReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
check := thirdpay.ThirdPayInfoCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP())
if check.CheckCode != errorcode.Success {
@ -31,14 +32,8 @@ func PayUrl(c *gin.Context) {
controllers.ApiRes(c, nil, pay.PayCode)
return
}
data := thirdpay.NewOrdersResp(pay.Order)
encryptData, errCode := api.EnCrypt(appCheckInfo.App, data)
if errCode != errorcode.Success {
controllers.ApiRes(c, nil, pay.PayCode)
return
}
res.Order = string(encryptData)
res.Order = thirdpay.NewOrdersResp(pay.Order)
res.Url = pay.Url
controllers.ApiRes(c, res, pay.PayCode)
return
@ -46,18 +41,17 @@ func PayUrl(c *gin.Context) {
func Refund(c *gin.Context) {
req := controllers.GetRequest(c).(*front.RefundReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
refund := thirdpay.ThirdPayRefund(c.Request.Context(), req, appCheckInfo, c.ClientIP())
if refund.PayCode != errorcode.Success {
controllers.ApiRes(c, nil, refund.PayCode)
}
data := thirdpay.NewOrdersResp(refund.Order)
encryptData, errCode := api.EnCrypt(appCheckInfo.App, data)
if errCode != errorcode.Success {
controllers.ApiRes(c, nil, refund.PayCode)
return
}
controllers.ApiRes(c, encryptData, refund.PayCode)
var res front.ApiResponse
res.Order = data
controllers.ApiRes(c, res, refund.PayCode)
return
}
@ -65,26 +59,24 @@ func Refund(c *gin.Context) {
// 查询订单
func QueryOrder(c *gin.Context) {
req := controllers.GetRequest(c).(*front.QueryReqs)
c.Set("OutTradeNo", req.OutTradeNo)
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
// 查询订单表
order := ordersmodel.Orders{
OutTreadNo: req.OutTradeNo,
OutTradeNo: req.OutTradeNo,
AppId: req.AppId,
MerchantId: appCheckInfo.App.MerchantId,
}
cond := builder.NewCond()
cond = cond.And(builder.Eq{"out_tread_no": order.OutTreadNo}, builder.Eq{"app_id": order.AppId})
cond = cond.And(builder.Eq{"out_trade_no": order.OutTradeNo}, builder.Eq{"app_id": order.AppId})
_, code := services.OrderFindOne(&order, cond)
if code != errorcode.OrdersExist {
controllers.ApiRes(c, nil, code)
return
}
data := thirdpay.NewOrdersResp(&order)
encryptData, errCode := api.EnCrypt(appCheckInfo.App, data)
if errCode != errorcode.Success {
controllers.ApiRes(c, nil, errCode)
return
}
controllers.ApiRes(c, encryptData, errorcode.Success)
var res front.ApiResponse
res.Order = data
controllers.ApiRes(c, res, errorcode.Success)
}

View File

@ -2,6 +2,7 @@ package backend
import (
"PaymentCenter/app/http/entities"
"PaymentCenter/app/models/ordercallbacklogmodel"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/orderthirdpaylogmodel"
@ -16,7 +17,7 @@ type OrderListRequest struct {
MerchantId int64 `json:"merchant_id" form:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id" form:"pay_channel_id"`
AppId int64 `json:"app_id" form:"app_id"`
OutTreadNo string `json:"out_tread_no" form:"out_tread_no"`
OutTradeNo string `json:"out_trade_no" form:"out_trade_no"`
Status int `json:"status" form:"status"`
OrderType int `json:"order_type" form:"order_type"`
StartTime string `json:"start_time" form:"start_time"`
@ -28,7 +29,7 @@ type OrderList struct {
MerchantId int64 `json:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id"`
AppId int64 `json:"app_id"`
OutTreadNo string `json:"out_tread_no"`
OutTradeNo string `json:"out_trade_no"`
Status int `json:"status"`
OrderType int `json:"order_type"`
StartTime time.Time `json:"start_time"`
@ -56,7 +57,7 @@ func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) {
r.MerchantId = o.MerchantId
r.PayChannelId = o.PayChannelId
r.AppId = o.AppId
r.OutTreadNo = o.OutTreadNo
r.OutTradeNo = o.OutTradeNo
r.Status = o.Status
r.OrderType = o.OrderType
r.PageRequest = o.PageRequest
@ -69,7 +70,7 @@ type OrdersResponse struct {
MerchantId int64 `json:"merchant_id"`
PayChannelId int64 `json:"pay_channel_id"`
AppId int64 `json:"app_id"`
OutTreadNo string `json:"out_tread_no"`
OutTradeNo string `json:"out_trade_no"`
Status int `json:"status"`
OrderType int `json:"order_type"`
Amount int `json:"amount"`
@ -87,7 +88,7 @@ func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) {
o.MerchantId = db.MerchantId
o.PayChannelId = db.PayChannelId
o.AppId = db.AppId
o.OutTreadNo = db.OutTreadNo
o.OutTradeNo = db.OutTradeNo
o.Status = db.Status
o.OrderType = db.OrderType
o.Amount = db.Amount
@ -107,6 +108,8 @@ type OrderLogsListRequest struct {
type OrderRequestLogResponse struct {
Id int64 `json:"id"`
AppId int64 `json:"app_id"`
OutTradeNo string `json:"out_trade_no"`
IpAddress string `json:"ip_address"`
Url string `json:"url"`
MerchantRequest string `json:"merchant_request"`
@ -117,7 +120,8 @@ type OrderRequestLogResponse struct {
func (o *OrderRequestLogResponse) ResponseFromDb(db orderrequestlogmodel.OrderRequestLog) {
o.Id = db.Id
o.AppId = db.AppId
o.OutTradeNo = db.OutTradeNo
o.IpAddress = db.IpAddress
o.Url = db.URL
o.Status = db.Status
@ -146,3 +150,21 @@ func (o *OrderThirdLogResponse) ResponseFromDb(db orderthirdpaylogmodel.OrderThi
o.MerchantCallback = db.MerchantCallback
o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
}
type OrderCallbackLogResponse struct {
Id int64 `json:"id"`
OrderId int64 `json:"order_id"`
MerchantRequest string `json:"merchant_request"`
Status int `json:"status"`
MerchantResponse string `json:"merchant_response"`
CreateTime string `json:"create_time"`
}
func (o *OrderCallbackLogResponse) ResponseFromDb(db ordercallbacklogmodel.OrderCallbackLog) {
o.Id = db.Id
o.OrderId = db.OrderId
o.MerchantRequest = db.MerchantRequest
o.Status = db.Status
o.MerchantResponse = db.MerchantResponse
o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
}

View File

@ -41,3 +41,9 @@ type QueryReqs struct {
ApiCommonBody
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
}
// api 接口返回数据, 统一返回结构, order数据会进行加密
type ApiResponse struct {
Order interface{} `json:"order"`
Url string `json:"url,omitempty"`
}

View File

@ -25,6 +25,8 @@ var BackendRequestMap = map[string]func() interface{}{
common.ADMIN_V1 + "/app/delete": func() interface{} { return new(entities.IdRequest) },
common.ADMIN_V1 + "/app/decrypt": func() interface{} { return new(backend.GenerateDecryptKeyRequest) },
// 订单
common.ADMIN_V1 + "/order/list": func() interface{} { return new(backend.OrderListRequest) },
common.ADMIN_V1 + "/order/log/list": func() interface{} { return new(backend.OrderLogsListRequest) },
common.ADMIN_V1 + "/order/list": func() interface{} { return new(backend.OrderListRequest) },
common.ADMIN_V1 + "/order/log/request": func() interface{} { return new(backend.OrderLogsListRequest) },
common.ADMIN_V1 + "/order/log/callback": func() interface{} { return new(backend.OrderLogsListRequest) },
common.ADMIN_V1 + "/order/log/third": func() interface{} { return new(backend.OrderLogsListRequest) },
}

View File

@ -48,8 +48,10 @@ func RegisterAdminRoute(router *gin.Engine) {
// 订单
order := v1.Group("/order")
order.GET("/list", backend.OrderList) // 订单列表
order.GET("/log/list", backend.OrderLogsList) // 订单日志列表
order.GET("/list", backend.OrderList) // 订单列表
order.GET("/log/request", backend.OrderRequestLogsList) // 请求日志列表
order.GET("/log/callback", backend.OrderCallbackLogsList) // 回调日志列表
order.GET("/log/third", backend.OrderThirdLogsList) // 三方日志列表
}

View File

@ -14,6 +14,8 @@ var (
// 实体
type OrderRequestLog struct {
Id int64
AppId int64 `xorm:"'app_id' bigint(20)"`
OutTradeNo string `xorm:"'out_trade_no' varchar(50)"`
IpAddress string `xorm:"'ip_address' varchar(16)"`
URL string `xorm:"'url' varchar(100)"`
MerchantRequest string `xorm:"'merchant_request' JSON"`

View File

@ -17,7 +17,7 @@ type Orders struct {
MerchantId int64 `xorm:"'merchant_id' bigint(20)"`
PayChannelId int64 `xorm:"'pay_channel_id' bigint(20)"`
AppId int64 `xorm:"'app_id' bigint(20)"`
OutTreadNo string `xorm:"'out_tread_no' varchar(50)"`
OutTradeNo string `xorm:"'out_trade_no' varchar(50)"`
OrderType int `xorm:"'order_type' TINYINT"`
RefundOrderId int64 `xorm:"'refund_order_id' bigint(20)"`
Amount int `xorm:"'amount' int(11)"`

View File

@ -4,6 +4,7 @@ import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/ordercallbacklogmodel"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/orderthirdpaylogmodel"
@ -29,8 +30,8 @@ func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, t
if req.AppId > 0 {
conn = conn.And(builder.Eq{"orders.app_id": req.AppId})
}
if req.OutTreadNo != "" {
conn = conn.And(builder.Like{"orders.out_tread_no", req.OutTreadNo})
if req.OutTradeNo != "" {
conn = conn.And(builder.Like{"orders.out_trade_no", req.OutTradeNo})
}
if req.Status > 0 {
conn = conn.And(builder.Eq{"orders.status": req.Status})
@ -52,27 +53,47 @@ func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, t
return orderList, count, code
}
func OrderLogsList(req backend.OrderLogsListRequest) (requestLog []orderrequestlogmodel.OrderRequestLog, thirdLod []orderthirdpaylogmodel.OrderThirdPayLog, code int) {
// 请求日志列表
func OrderRequestLogsList(req backend.OrderLogsListRequest) (requestLog []orderrequestlogmodel.OrderRequestLog, total int64, code int) {
requestRepo := data.NewOrderRequestLogRepo(paychannelmodel.GetInstance().GetDb())
thirdRepo := data.NewOrderThirdPayLogRepo(paychannelmodel.GetInstance().GetDb())
conn := builder.NewCond()
if req.OrderId > 0 {
conn = conn.And(builder.Eq{"orders.id": req.OrderId})
}
// 请求日志
orderLogList := make([]orderrequestlogmodel.OrderRequestLog, 0)
total, err := requestRepo.OrderRequestLogBackendList(conn, req.PageRequest, &orderLogList)
code = handErr(err)
return orderLogList, total, code
}
// 回调日志列表
func OrderCallbackLogsList(req backend.OrderLogsListRequest) (callback []ordercallbacklogmodel.OrderCallbackLog, total int64, code int) {
callbackRepo := data.NewOrderCallbackLogRepo(paychannelmodel.GetInstance().GetDb())
conn := builder.NewCond()
if req.OrderId > 0 {
conn = conn.And(builder.Eq{"order_id": req.OrderId})
}
// 请求日志
orderLogList := make([]orderrequestlogmodel.OrderRequestLog, 0)
_, err := requestRepo.OrderRequestLogList(conn, req.PageRequest, &orderLogList)
if err != nil {
code = handErr(err)
return
}
// 三方日志
thirdLogList := make([]orderthirdpaylogmodel.OrderThirdPayLog, 0)
_, err = thirdRepo.OrderThirdPayLogList(conn, req.PageRequest, &thirdLogList)
// 回调日志
callbackLogList := make([]ordercallbacklogmodel.OrderCallbackLog, 0)
total, err := callbackRepo.OrderCallbackLogList(conn, req.PageRequest, &callbackLogList)
code = handErr(err)
return orderLogList, thirdLogList, code
return callbackLogList, total, code
}
// 三方日志列表
func OrderThirdLogsList(req backend.OrderLogsListRequest) (thirdLod []orderthirdpaylogmodel.OrderThirdPayLog, total int64, code int) {
thirdRepo := data.NewOrderThirdPayLogRepo(paychannelmodel.GetInstance().GetDb())
// 三方日志
conn := builder.NewCond()
if req.OrderId > 0 {
conn = conn.And(builder.Eq{"order_id": req.OrderId})
}
thirdLogList := make([]orderthirdpaylogmodel.OrderThirdPayLog, 0)
total, err := thirdRepo.OrderThirdPayLogList(conn, req.PageRequest, &thirdLogList)
code = handErr(err)
return thirdLogList, total, code
}
func OrderCreate(orderIn *ordersmodel.Orders) (orderOut *ordersmodel.Orders, code int) {

View File

@ -3,7 +3,7 @@ package api
type OrdersResp struct {
OrderNo int64 `json:"order_no"`
OrderType int `json:"order_type"`
OutTreadNo string `json:"out_tread_no"`
OutTradeNo string `json:"out_trade_no"`
Amount int `json:"amount"`
Desc string `json:"desc"`

View File

@ -60,7 +60,7 @@ func (w *Pay) CreateOrder(order_type int) {
MerchantId: w.PayParam.Merchant.Id,
PayChannelId: w.PayParam.Channel.Id,
AppId: w.PayParam.App_id,
OutTreadNo: w.PayParam.OutTradeNo,
OutTradeNo: w.PayParam.OutTradeNo,
OrderType: order_type,
Amount: w.PayParam.Amount,
ExtJson: w.PayParam.ExtJson,

View File

@ -98,7 +98,7 @@ func (w *PayCheck) CheckOrderRefund() {
func (w *PayCheck) GetOrder() {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"out_tread_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId})
cond = cond.And(builder.Eq{"out_trade_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId})
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
if code == errorcode.SystemError {
w.CheckCode = code

View File

@ -23,7 +23,7 @@ func ThirdPayUrl(check *thirdpay.PayCheck) *thirdpay.Pay {
func NewOrdersResp(db *ordersmodel.Orders) *api.OrdersResp {
return &api.OrdersResp{
OrderNo: db.Id,
OutTreadNo: db.OutTreadNo,
OutTradeNo: db.OutTradeNo,
Status: db.Status,
OrderType: db.OrderType,
Amount: db.Amount,
@ -67,7 +67,7 @@ func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck *
func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) {
pay = thirdpay.NewPayWithPayCheck(check)
// 创建订单
if &check.OldOrder != nil {
if check.OldOrder != nil {
pay.Order = check.OldOrder
} else {
pay.CreateOrder(common.ORDER_TYPE_PAY)

View File

@ -38,7 +38,7 @@ type OrderNotifyResp struct {
type OrderNotifySendContent struct {
OrderId int64 `json:"order_id"`
OutTreadNo string `json:"out_tread_no"`
OutTradeNo string `json:"out_trade_no"`
CompleteTime time.Time `json:"complete_time"`
OrderType int `json:"order-type"`
Status int `json:"status"`
@ -133,7 +133,7 @@ func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) {
func (o *OrderNotify) setBody() *OrderNotifySendContent {
return &OrderNotifySendContent{
OrderId: o.OrderId,
OutTreadNo: o.order.OutTreadNo,
OutTradeNo: o.order.OutTradeNo,
CompleteTime: o.CompleteTime,
Status: o.order.Status,
OrderType: o.order.OrderType,

View File

@ -14,25 +14,16 @@ import (
"github.com/go-pay/gopay/alipay"
"github.com/qit-team/snow-core/log/logger"
"strconv"
"sync"
)
var (
aliClient *alipay.Client
aliClientErr error
aliOnce sync.Once
)
// AliInitClient 使用提供的支付请求参数初始化支付宝客户端
func AliInitClient(aliConfig AliPay) {
func AliInitClient(aliConfig AliPay) (*alipay.Client, error) {
envConfig := config.GetConf()
aliOnce.Do(func() {
// 初始化支付宝客户端
// appid应用ID
// privateKey应用私钥支持PKCS1和PKCS8
// isProd是否是正式环境沙箱环境请选择新版沙箱应用。
aliClient, aliClientErr = alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd)
})
// 初始化支付宝客户端
// appid应用ID
// privateKey应用私钥支持PKCS1和PKCS8
// isProd是否是正式环境沙箱环境请选择新版沙箱应用。
aliClient, aliClientErr := alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd)
// 自定义配置http请求接收返回结果body大小默认 10MB
aliClient.SetBodySize(10) // 没有特殊需求,可忽略此配置
@ -51,10 +42,6 @@ func AliInitClient(aliConfig AliPay) {
// 证书内容
aliClientErr = aliClient.SetCertSnByContent([]byte(aliConfig.AppPublicCert), []byte(aliConfig.AlipayRootCert), []byte(aliConfig.AlipayPublicCert))
}
// GetAliClient 获取已经初始化的支付宝客户端
func GetAliClient() (*alipay.Client, error) {
if aliClient == nil {
return nil, errors.New("client not initialized")
}
@ -67,10 +54,7 @@ func GetAliClient() (*alipay.Client, error) {
// ALiH5PayInfo 支付宝手机网站支付
func ALiH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) {
// 初始化支付宝客户端
AliInitClient(payOrderRequest.Ali)
// 获取支付宝客户端
aliClient, err := GetAliClient()
aliClient, err := AliInitClient(payOrderRequest.Ali)
if err != nil {
fmt.Println("Failed to get client:", err)
return "", err
@ -147,14 +131,12 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error {
// ALiOrderQuery 查询支付宝订单
func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOrderQueryInfo, error) {
// 初始化支付宝客户端
AliInitClient(aliConfig)
// 获取支付宝客户端
aliClient, err := GetAliClient()
aliClient, err := AliInitClient(aliConfig)
if err != nil {
fmt.Println("Failed to get client:", err)
return PayOrderQueryInfo{}, err
}
// 请求参数
bm := make(gopay.BodyMap)
bm.Set("out_trade_no", OrderNo)
@ -210,14 +192,12 @@ func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOr
// AliRefundOrder 支付宝退款申请
func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) {
// 初始化支付宝客户端
AliInitClient(orderRefundRequest.Ali)
// 获取支付宝客户端
aliClient, err := GetAliClient()
aliClient, err := AliInitClient(orderRefundRequest.Ali)
if err != nil {
fmt.Println("Failed to get client:", err)
return OrderRefundInfo{}, err
}
// 请求参数
refundAmount := float64(orderRefundRequest.RefundAmount) / 100.0
bm := make(gopay.BodyMap)
@ -251,10 +231,7 @@ func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest)
// AliRefundOrderQuery 支付宝订单退款查询
func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) {
// 初始化支付宝客户端
AliInitClient(orderRefundQueryRequest.Ali)
// 获取支付宝客户端
aliClient, err := GetAliClient()
aliClient, err := AliInitClient(orderRefundQueryRequest.Ali)
if err != nil {
fmt.Println("Failed to get client:", err)
return OrderRefundInfo{}, err
@ -290,10 +267,7 @@ func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefun
// AliCloseOrder 支付宝订单关闭
func AliCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) {
// 初始化支付宝客户端
AliInitClient(orderCloseRequest.Ali)
// 获取支付宝客户端
aliClient, err := GetAliClient()
aliClient, err := AliInitClient(orderCloseRequest.Ali)
if err != nil {
fmt.Println("Failed to get client:", err)
return OrderCloseInfo{}, err

View File

@ -14,62 +14,48 @@ import (
"github.com/go-pay/gopay/wechat/v3"
"github.com/qit-team/snow-core/log/logger"
"strconv"
"sync"
"time"
)
var (
wxClient *wechat.ClientV3
clientErr error
once sync.Once
)
// InitClient 使用提供的支付请求参数初始化微信客户端
func InitClient(wxConfig WxPay) {
once.Do(func() {
// NewClientV3 初始化微信客户端 v3
// mchid商户ID 或者服务商模式的 sp_mchid
// serialNo商户证书的证书序列号
// apiV3KeyapiV3Key商户平台获取
// privateKey私钥 apiclient_key.pem 读取后的内容
wxClient, clientErr = wechat.NewClientV3(
wxConfig.MchId,
wxConfig.SerialNo,
wxConfig.ApiV3Key,
wxConfig.PrivateKey,
)
})
func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) {
// NewClientV3 初始化微信客户端 v3
// mchid商户ID 或者服务商模式的 sp_mchid
// serialNo商户证书的证书序列号
// apiV3KeyapiV3Key商户平台获取
// privateKey私钥 apiclient_key.pem 读取后的内容
wxClient, clientErr := wechat.NewClientV3(
wxConfig.MchId,
wxConfig.SerialNo,
wxConfig.ApiV3Key,
wxConfig.PrivateKey,
)
// 启用自动同步返回验签并定时更新微信平台API证书开启自动验签时无需单独设置微信平台API证书和序列号
clientErr = wxClient.AutoVerifySign()
// 自定义配置http请求接收返回结果body大小默认 10MB
wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置
// 打开Debug开关输出日志默认是关闭的
wxClient.DebugSwitch = gopay.DebugOn
}
// GetClient 获取已经初始化的微信客户端
func GetClient() (*wechat.ClientV3, error) {
if wxClient == nil {
return nil, errors.New("client not initialized")
}
if clientErr != nil {
return nil, clientErr
}
// 自定义配置http请求接收返回结果body大小默认 10MB
wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置
// 打开Debug开关输出日志默认是关闭的
wxClient.DebugSwitch = gopay.DebugOn
return wxClient, nil
}
// WxH5PayInfo 微信H5支付
func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) {
// 初始化微信客户端
InitClient(payOrderRequest.Wx)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(payOrderRequest.Wx)
if err != nil {
return "", err
}
expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
// 初始化 BodyMap
envConfig := config.GetConf()
@ -105,10 +91,7 @@ func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, er
// WxPayCallBack 微信支付回调
func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error {
// 初始化微信客户端
InitClient(wxConfig)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(wxConfig)
if err != nil {
return err
}
@ -181,10 +164,7 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error {
// WxOrderQuery 查询微信订单
func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrderQueryInfo, error) {
// 初始化微信客户端
InitClient(wxConfig)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(wxConfig)
if err != nil {
return PayOrderQueryInfo{}, err
}
@ -228,13 +208,11 @@ func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrder
// WxOrderRefund 微信退款申请
func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) {
// 初始化微信客户端
InitClient(orderRefundRequest.Wx)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(orderRefundRequest.Wx)
if err != nil {
return OrderRefundInfo{}, err
}
// 初始化 BodyMap
bm := make(gopay.BodyMap)
bm.Set("out_trade_no", strconv.FormatInt(orderRefundRequest.OrderId, 10)).
@ -288,10 +266,7 @@ func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) (
// WxOrderRefundQuery 微信订单退款查询
func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) {
// 初始化微信客户端
InitClient(orderRefundQueryRequest.Wx)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(orderRefundQueryRequest.Wx)
if err != nil {
return OrderRefundInfo{}, err
}
@ -329,13 +304,11 @@ func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefund
// WxCloseOrder 微信订单关闭
func WxCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) {
// 初始化微信客户端
InitClient(orderCloseRequest.Wx)
// 获取微信客户端
wxClient, err := GetClient()
wxClient, err := InitClient(orderCloseRequest.Wx)
if err != nil {
return OrderCloseInfo{}, err
}
wxRsp, err := wxClient.V3TransactionCloseOrder(ctx, strconv.FormatInt(orderCloseRequest.OrderId, 10))
if err != nil {
return OrderCloseInfo{}, err