Compare commits
12 Commits
583aee88e2
...
a54212d38c
Author | SHA1 | Date |
---|---|---|
|
a54212d38c | |
|
bb9ff190c3 | |
|
78380d3a70 | |
|
9a413afa51 | |
|
7ed9cb048d | |
|
d76344573d | |
|
b0be00741f | |
|
966aba1dc4 | |
|
a6e8412654 | |
|
92733acffa | |
|
1630304f3d | |
|
50ef2fd13a |
|
@ -58,14 +58,17 @@ const (
|
|||
PayChannelExtJsonError = 1302
|
||||
|
||||
//订单
|
||||
OrdersNotFound = 1401
|
||||
OrdersExist = 1402
|
||||
OrderTypeNotFount = 1403
|
||||
OrderIsDelete = 1405
|
||||
OrderStatusErr = 1406
|
||||
OrderClosed = 1407
|
||||
OrderFailed = 1408
|
||||
OrderPayed = 1409
|
||||
OrdersNotFound = 1401
|
||||
OrdersExist = 1402
|
||||
OrderTypeNotFount = 1403
|
||||
OrderIsDelete = 1405
|
||||
OrderStatusErr = 1406
|
||||
OrderClosed = 1407
|
||||
OrderFailed = 1408
|
||||
OrderPayed = 1409
|
||||
RefundOrderNotFound = 1410
|
||||
OrderStatusRefundNotSupport = 1411
|
||||
|
||||
//请求日志
|
||||
RequestLogErrors = 1500
|
||||
RequestLogNotFound = 1501
|
||||
|
@ -132,17 +135,18 @@ var MsgZH = map[int]string{
|
|||
RequestLogNotFound: "未找到日志信息",
|
||||
RequestResponseValid: "上游返回格式无效",
|
||||
|
||||
OrdersNotFound: "未找到订单",
|
||||
OrdersExist: "订单已存在",
|
||||
OrderTypeNotFount: "未知的支付方式",
|
||||
OrderIsDelete: "订单已删除",
|
||||
OrderStatusErr: "订单状态错误",
|
||||
OrderClosed: "订单已关闭,无法继续支付",
|
||||
OrderFailed: "订单支付失败,请重新发起",
|
||||
OrderPayed: "订单已支付成功,请勿重复支付",
|
||||
NotifySendFail: "回调发送失败",
|
||||
|
||||
PrePayFail: "预支付失败",
|
||||
OrdersNotFound: "未找到订单",
|
||||
OrdersExist: "订单已存在",
|
||||
OrderTypeNotFount: "未知的支付方式",
|
||||
OrderIsDelete: "订单已删除",
|
||||
OrderStatusErr: "订单状态错误",
|
||||
OrderClosed: "订单已关闭,无法继续支付",
|
||||
OrderFailed: "订单支付失败,请重新发起",
|
||||
OrderPayed: "订单已支付成功,请勿重复支付",
|
||||
NotifySendFail: "回调发送失败",
|
||||
RefundOrderNotFound: "退款订单未找到",
|
||||
OrderStatusRefundNotSupport: "订单状态不支持退款",
|
||||
PrePayFail: "预支付失败",
|
||||
}
|
||||
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -3,45 +3,73 @@ 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
|
||||
var appCheckInfo *services.AppCheck
|
||||
originData := "{}"
|
||||
appInfo := GetAppCheckInfo(c)
|
||||
if appInfo != nil {
|
||||
appCheckInfo = appInfo.(*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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
if code != errorcode.Success {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -101,12 +102,17 @@ func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) {
|
|||
}
|
||||
|
||||
type OrderLogsListRequest struct {
|
||||
OrderId int64 `json:"order_id" form:"order_id"`
|
||||
Id int64 `json:"id" form:"id"`
|
||||
OrderId int64 `json:"order_id" form:"order_id"`
|
||||
AppId int64 `json:"app_id" form:"app_id"`
|
||||
OutTradeNo string `json:"out_trade_no" form:"out_trade_no"`
|
||||
entities.PageRequest
|
||||
}
|
||||
|
||||
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 +123,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 +153,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")
|
||||
}
|
||||
|
|
|
@ -27,8 +27,9 @@ type PayReqs struct {
|
|||
|
||||
type RefundReqs struct {
|
||||
PayCommonReqBody
|
||||
OutTradeNo string `json:"out_trade_no" label:"外侧商户订单号"`
|
||||
OrderId string `json:"order_id" label:"平台订单号"`
|
||||
RefundOutTradeNo string `json:"refund_out_trade_no" label:"需要退款的外侧商户订单号"`
|
||||
RefundOrderId string `json:"refundOrder_id" label:"需要退款的平台订单号"`
|
||||
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
|
||||
}
|
||||
|
||||
type PayUrlResp struct {
|
||||
|
@ -40,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"`
|
||||
}
|
||||
|
|
|
@ -129,8 +129,13 @@ func ValidateRequest() gin.HandlerFunc {
|
|||
|
||||
func ValidatePayRequest() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var path = c.FullPath()
|
||||
var handler func() interface{}
|
||||
var (
|
||||
path = c.FullPath()
|
||||
code int
|
||||
log_id int64
|
||||
)
|
||||
|
||||
var handler func() (interface{}, bool)
|
||||
requestData, err := utils.SonicApiDataToStruct(controllers.GetRequest(c), &front.RequestBody{})
|
||||
if err != nil {
|
||||
controllers.ApiRes(c, nil, errorcode.ParamError)
|
||||
|
@ -157,12 +162,7 @@ func ValidatePayRequest() gin.HandlerFunc {
|
|||
controllers.ApiRes(c, nil, errCode)
|
||||
return
|
||||
}
|
||||
//记录请求日志
|
||||
id, code := services.AddRequestLog(dataByte, c.ClientIP(), path)
|
||||
if code != errorcode.Success {
|
||||
controllers.ApiRes(c, nil, errCode)
|
||||
}
|
||||
c.Set("log", id)
|
||||
|
||||
//检查解密后的数据是否与请求一致
|
||||
reCheck := appCheck.ReCheckAfterDecrypt(dataByte, requestDataStruct)
|
||||
if !reCheck {
|
||||
|
@ -170,8 +170,18 @@ func ValidatePayRequest() gin.HandlerFunc {
|
|||
return
|
||||
}
|
||||
//表单验证
|
||||
|
||||
handler = requestmapping.FrontRequestMap[path]
|
||||
v := handler()
|
||||
v, isSaveLog := handler()
|
||||
if isSaveLog {
|
||||
//记录请求日志
|
||||
log_id, code = services.AddRequestLog(dataByte, c.ClientIP(), path)
|
||||
if code != errorcode.Success {
|
||||
controllers.ApiRes(c, nil, errCode)
|
||||
}
|
||||
}
|
||||
|
||||
c.Set("log", log_id)
|
||||
msg, err := controllers.ValidApiData(dataByte, v)
|
||||
if err != nil {
|
||||
utils.Log(c, "参数错误", "path=", path, "err=", err.Error(), "msg=", msg)
|
||||
|
|
|
@ -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) },
|
||||
}
|
||||
|
|
|
@ -5,11 +5,15 @@ import (
|
|||
"PaymentCenter/app/http/entities/front"
|
||||
)
|
||||
|
||||
var FrontRequestMap = map[string]func() interface{}{
|
||||
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.PayReqs) },
|
||||
common.FRONT_V1 + "/pay/refund": func() interface{} { return new(front.RefundReqs) },
|
||||
var FrontRequestMap = map[string]func() (validForm interface{}, isSaveLog bool){
|
||||
common.FRONT_V1 + "/pay/url": func() (interface{}, bool) { return new(front.PayReqs), true },
|
||||
common.FRONT_V1 + "/pay/refund": func() (interface{}, bool) { return new(front.RefundReqs), true },
|
||||
common.FRONT_V1 + "/pay/query": func() (interface{}, bool) { return new(front.QueryReqs), false },
|
||||
}
|
||||
|
||||
var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{
|
||||
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.RequestBody) },
|
||||
|
||||
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.RequestBody) },
|
||||
common.FRONT_V1 + "/pay/refund": func() interface{} { return new(front.RequestBody) },
|
||||
common.FRONT_V1 + "/pay/query": func() interface{} { return new(front.RequestBody) },
|
||||
}
|
||||
|
|
|
@ -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) // 三方日志列表
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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)"`
|
||||
|
|
|
@ -13,7 +13,7 @@ func NewRsa(app *appmodel.App) ApiCrypt {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *Rsa) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
||||
func (r *Rsa) Encrypt(data string) (encryptData []byte, errCode int) {
|
||||
if r.App.PublicKey == "" {
|
||||
return nil, errorcode.AppRsaEncryptKeyNotFound
|
||||
}
|
||||
|
|
|
@ -12,13 +12,13 @@ func NewSm2(app *appmodel.App) ApiCrypt {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *SM2) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
||||
func (r *SM2) Encrypt(data string) (encryptData []byte, errCode int) {
|
||||
|
||||
if r.App.MerchantPublicKey == "" {
|
||||
return nil, errorcode.AppSM2EncryptKeyNotFound
|
||||
}
|
||||
|
||||
encryptDataString, err := sm2.SM2Encrypt(string(data), r.App.MerchantPublicKey)
|
||||
encryptDataString, err := sm2.SM2Encrypt(data, r.App.MerchantPublicKey)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM2EncryptFail
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ func NewSm4(app *appmodel.App) ApiCrypt {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *SM4) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
||||
func (r *SM4) Encrypt(data string) (encryptData []byte, errCode int) {
|
||||
if r.App.MerchantPublicKey == "" || r.App.PrivateKey == "" {
|
||||
return nil, errorcode.AppSM4DecryptKeyNotFound
|
||||
}
|
||||
encryptDataString, err := sm4.Sm4Encrypt(strconv.FormatInt(r.App.Id, 10), r.App.PrivateKey, r.App.MerchantPublicKey, string(data), "", true)
|
||||
encryptDataString, err := sm4.Sm4Encrypt(strconv.FormatInt(r.App.Id, 10), r.App.PrivateKey, r.App.MerchantPublicKey, data, "", true)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM4EncryptFail
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
type (
|
||||
ApiCrypt interface {
|
||||
Encrypt(data []byte) (encryptData []byte, errCode int)
|
||||
Encrypt(data string) (encryptData []byte, errCode int)
|
||||
Decrypt(encryptData string) (decryptData []byte, errCode int)
|
||||
}
|
||||
|
||||
|
|
|
@ -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,62 @@ 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})
|
||||
}
|
||||
if req.Id > 0 {
|
||||
conn = conn.And(builder.Eq{"order_request_log.id": req.Id})
|
||||
}
|
||||
if req.AppId > 0 {
|
||||
conn = conn.And(builder.Eq{"order_request_log.app_id": req.AppId})
|
||||
}
|
||||
if req.OutTradeNo != "" {
|
||||
conn = conn.And(builder.Like{"order_request_log.out_trade_no", req.OutTradeNo})
|
||||
}
|
||||
// 请求日志
|
||||
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
|
||||
if req.Id > 0 {
|
||||
conn = conn.And(builder.Eq{"id": req.Id})
|
||||
}
|
||||
// 三方日志
|
||||
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})
|
||||
}
|
||||
if req.Id > 0 {
|
||||
conn = conn.And(builder.Eq{"id": req.Id})
|
||||
}
|
||||
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) {
|
||||
|
|
|
@ -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"`
|
||||
|
||||
|
|
|
@ -18,15 +18,8 @@ func EnCrypt(app *appmodel.App, data interface{}) ([]byte, int) {
|
|||
if err != nil {
|
||||
return nil, errorcode.AppAesEncryptFail
|
||||
}
|
||||
//aesKey, err := aes.GenerateRandomStringCrypto(16)
|
||||
//if err != nil {
|
||||
// return "", errorcode.AppAesEncryptFail
|
||||
//}
|
||||
//aesData, err := aes.Encrypt(dataByte, []byte(aesKey))
|
||||
//if err != nil {
|
||||
// return "", errorcode.AppAesEncryptFail
|
||||
//}
|
||||
encryptData, errCode := cryptFunc(appCheck.App).Encrypt(dataByte)
|
||||
|
||||
encryptData, errCode := cryptFunc(appCheck.App).Encrypt(string(dataByte))
|
||||
if errCode != apicrypt.CryptNotError {
|
||||
return nil, errCode
|
||||
}
|
||||
|
@ -46,10 +39,6 @@ func DeCrypt(app *appmodel.App, data string, aesKey string) ([]byte, int) {
|
|||
if len(dataByte) == 0 {
|
||||
return nil, errorcode.AppDeEncryptFail
|
||||
}
|
||||
//aesData, err := aes.Decrypt(dataByte, []byte(aesKey))
|
||||
//if err != nil {
|
||||
// return nil, errorcode.AppAesEncryptFail
|
||||
//}
|
||||
|
||||
return dataByte, errorcode.Success
|
||||
}
|
||||
|
|
|
@ -7,25 +7,47 @@ import (
|
|||
"PaymentCenter/app/models/paychannelmodel"
|
||||
"PaymentCenter/app/services"
|
||||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
"context"
|
||||
|
||||
"PaymentCenter/app/models/ordersmodel"
|
||||
"PaymentCenter/app/third/paymentService"
|
||||
)
|
||||
|
||||
type Pay struct {
|
||||
Merchant *merchantmodel.Merchant
|
||||
Channel *paychannelmodel.PayChannel
|
||||
|
||||
paycheck *PayCheck
|
||||
Order *ordersmodel.Orders
|
||||
PayCode int
|
||||
Url string
|
||||
type PayParam struct {
|
||||
Merchant *merchantmodel.Merchant
|
||||
Channel *paychannelmodel.PayChannel
|
||||
App_id int64
|
||||
OutTradeNo string
|
||||
Amount int
|
||||
ExtJson string
|
||||
Desc string
|
||||
ClientIp string
|
||||
}
|
||||
|
||||
func NewPay(paycheck *PayCheck) *Pay {
|
||||
type Pay struct {
|
||||
ctx *context.Context
|
||||
PayParam *PayParam
|
||||
RelationOrder *ordersmodel.Orders
|
||||
Order *ordersmodel.Orders
|
||||
PayCode int
|
||||
Url string
|
||||
}
|
||||
|
||||
func NewPayWithPayCheck(paycheck *PayCheck) *Pay {
|
||||
return &Pay{
|
||||
paycheck: paycheck,
|
||||
PayCode: errorcode.Success,
|
||||
ctx: paycheck.ctx,
|
||||
PayParam: &PayParam{
|
||||
Merchant: paycheck.Merchant,
|
||||
Channel: paycheck.Channel,
|
||||
App_id: paycheck.Reqs.AppId,
|
||||
OutTradeNo: paycheck.Reqs.OutTradeNo,
|
||||
Amount: paycheck.Reqs.Amount,
|
||||
ExtJson: paycheck.Reqs.ExtJson,
|
||||
Desc: paycheck.Reqs.Desc,
|
||||
ClientIp: paycheck.AppCheck.Ip,
|
||||
},
|
||||
RelationOrder: paycheck.OldOrder,
|
||||
PayCode: errorcode.Success,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,18 +56,21 @@ func (w *Pay) CreateOrder(order_type int) {
|
|||
w.PayCode = errorcode.PayChannelNotFound
|
||||
return
|
||||
}
|
||||
w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{
|
||||
MerchantId: w.paycheck.Merchant.Id,
|
||||
PayChannelId: w.paycheck.Channel.Id,
|
||||
AppId: w.paycheck.Reqs.AppId,
|
||||
OutTreadNo: w.paycheck.Reqs.OutTradeNo,
|
||||
order := &ordersmodel.Orders{
|
||||
MerchantId: w.PayParam.Merchant.Id,
|
||||
PayChannelId: w.PayParam.Channel.Id,
|
||||
AppId: w.PayParam.App_id,
|
||||
OutTradeNo: w.PayParam.OutTradeNo,
|
||||
OrderType: order_type,
|
||||
Amount: w.paycheck.Reqs.Amount,
|
||||
ExtJson: w.paycheck.Reqs.ExtJson,
|
||||
Desc: w.paycheck.Reqs.Desc,
|
||||
Amount: w.PayParam.Amount,
|
||||
ExtJson: w.PayParam.ExtJson,
|
||||
Desc: w.PayParam.Desc,
|
||||
Status: common.ORDER_STATUS_WAITPAY,
|
||||
},
|
||||
)
|
||||
}
|
||||
if order_type == common.ORDER_TYPE_REFUND {
|
||||
order.RefundOrderId = w.RelationOrder.Id
|
||||
}
|
||||
w.Order, w.PayCode = services.OrderCreate(order)
|
||||
}
|
||||
|
||||
func (w *Pay) PayUrl() (url string) {
|
||||
|
@ -54,23 +79,23 @@ func (w *Pay) PayUrl() (url string) {
|
|||
ok bool
|
||||
)
|
||||
thirdPay := &paymentService.PayOrderRequest{
|
||||
PayChannelId: w.paycheck.Reqs.PayChannelId,
|
||||
PayChannelId: w.PayParam.Channel.Id,
|
||||
OrderId: w.Order.Id,
|
||||
ChannelType: w.paycheck.Channel.ChannelType,
|
||||
ChannelType: w.PayParam.Channel.ChannelType,
|
||||
Description: w.Order.Desc,
|
||||
Amount: w.Order.Amount,
|
||||
PayerClientIp: w.paycheck.AppCheck.Ip,
|
||||
PayerClientIp: w.PayParam.ClientIp,
|
||||
}
|
||||
if payFunc, ok = PayWayList[w.paycheck.Channel.ChannelType]; !ok {
|
||||
if payFunc, ok = PayWayList[w.PayParam.Channel.ChannelType]; !ok {
|
||||
w.PayCode = errorcode.PayChannelNotBuild
|
||||
return
|
||||
}
|
||||
err := payFunc(thirdPay, w.paycheck.Channel)
|
||||
err := payFunc(thirdPay, w.PayParam.Channel)
|
||||
if err != nil {
|
||||
w.PayCode = errorcode.PayChannelExtJsonError
|
||||
return
|
||||
}
|
||||
res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay)
|
||||
res := paymentService.PaymentService(*w.ctx, *thirdPay)
|
||||
|
||||
if res.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
w.Order.Status = common.ORDER_STATUS_PAYING
|
||||
|
@ -93,21 +118,21 @@ func (w *Pay) Refund() {
|
|||
)
|
||||
thirdPayRefund := &paymentService.OrderRefundRequest{
|
||||
OrderId: w.Order.Id,
|
||||
RefundOrderId: w.paycheck.OldOrder.Id,
|
||||
RefundReason: w.paycheck.Reqs.Desc,
|
||||
RefundAmount: int64(w.paycheck.Reqs.Amount),
|
||||
PayChannel: w.paycheck.Channel.ChannelType,
|
||||
RefundOrderId: w.RelationOrder.Id,
|
||||
RefundReason: w.PayParam.Desc,
|
||||
RefundAmount: int64(w.PayParam.Amount),
|
||||
PayChannel: w.PayParam.Channel.ChannelType,
|
||||
}
|
||||
if refundFunc, ok = RefundWayList[w.paycheck.Channel.ChannelType]; !ok {
|
||||
if refundFunc, ok = RefundWayList[w.PayParam.Channel.ChannelType]; !ok {
|
||||
w.PayCode = errorcode.PayChannelNotBuild
|
||||
return
|
||||
}
|
||||
err := refundFunc(thirdPayRefund, w.paycheck.Channel)
|
||||
err := refundFunc(thirdPayRefund, w.PayParam.Channel)
|
||||
if err != nil {
|
||||
w.PayCode = errorcode.PayChannelExtJsonError
|
||||
return
|
||||
}
|
||||
res := paymentService.OrderRefund(*w.paycheck.ctx, *thirdPayRefund)
|
||||
res := paymentService.OrderRefund(*w.ctx, *thirdPayRefund)
|
||||
if res.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
w.Order.Status = common.ORDER_STATUS_PAYING
|
||||
code := services.OrderUpdate(w.Order, "status")
|
||||
|
|
|
@ -61,7 +61,9 @@ func (w *PayCheck) CheckMerchant() {
|
|||
}
|
||||
|
||||
func (w *PayCheck) CheckOrderPay() {
|
||||
w.GetOrder()
|
||||
w.GetOrder(&types.OrderFindOne{
|
||||
OutTradeNo: w.Reqs.OutTradeNo,
|
||||
})
|
||||
if w.OldOrder != nil {
|
||||
switch w.OldOrder.Status {
|
||||
case common.ORDER_STATUS_CLOSE:
|
||||
|
@ -78,34 +80,40 @@ func (w *PayCheck) CheckOrderPay() {
|
|||
}
|
||||
|
||||
func (w *PayCheck) CheckOrderRefund() {
|
||||
w.GetOrder()
|
||||
w.GetOrder(&types.OrderFindOne{
|
||||
OutTradeNo: w.Reqs.RefundOutTradeNo,
|
||||
OrderId: w.Reqs.RefundOrderId,
|
||||
})
|
||||
if w.OldOrder == nil {
|
||||
w.CheckCode = errorcode.OrdersNotFound
|
||||
w.CheckCode = errorcode.RefundOrderNotFound
|
||||
return
|
||||
}
|
||||
switch w.OldOrder.Status {
|
||||
case common.ORDER_STATUS_CLOSE:
|
||||
w.CheckCode = errorcode.OrderClosed
|
||||
case common.ORDER_STATUS_FAILED:
|
||||
w.CheckCode = errorcode.OrderFailed
|
||||
case common.ORDER_STATUS_WAITPAY:
|
||||
w.CheckCode = errorcode.OrderStatusErr
|
||||
case common.ORDER_STATUS_PAYING:
|
||||
w.CheckCode = errorcode.OrderStatusErr
|
||||
default:
|
||||
if w.OldOrder.Status != common.ORDER_STATUS_PAYED {
|
||||
w.CheckCode = errorcode.OrderStatusRefundNotSupport
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *PayCheck) GetOrder() {
|
||||
func (w *PayCheck) GetOrder(orderCol *types.OrderFindOne) {
|
||||
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{"app_id": w.AppCheck.AppId})
|
||||
if orderCol.OrderId == "" && orderCol.OutTradeNo == "" {
|
||||
w.CheckCode = errorcode.OrdersNotFound
|
||||
return
|
||||
}
|
||||
if orderCol.OrderId != "" {
|
||||
cond = cond.And(builder.Eq{"order_id": orderCol.OrderId})
|
||||
}
|
||||
if orderCol.OutTradeNo != "" {
|
||||
cond = cond.And(builder.Eq{"out_trade_no": orderCol.OutTradeNo})
|
||||
}
|
||||
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
|
||||
if code == errorcode.SystemError {
|
||||
w.CheckCode = code
|
||||
return
|
||||
}
|
||||
if code == errorcode.OrdersExist {
|
||||
if code == errorcode.Success {
|
||||
w.OldOrder = order
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
@ -47,7 +47,7 @@ func ThirdPayInfoCheck(ctx context.Context, payReq *front.PayReqs, appCheck *ser
|
|||
|
||||
func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck *services.AppCheck, ip string) (refund *thirdpay.Pay) {
|
||||
var req types.Reqs
|
||||
copier.Copy(req, refundReq)
|
||||
copier.Copy(&req, refundReq)
|
||||
check := thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
|
||||
// 校验表单
|
||||
check.CheckPayInfo()
|
||||
|
@ -58,22 +58,22 @@ func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck *
|
|||
if check.CheckCode != errorcode.Success {
|
||||
return
|
||||
}
|
||||
refund = thirdpay.NewPay(check)
|
||||
refund = thirdpay.NewPayWithPayCheck(check)
|
||||
refund.CreateOrder(common.ORDER_TYPE_REFUND)
|
||||
refund.Refund()
|
||||
return
|
||||
}
|
||||
|
||||
func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) {
|
||||
pay = thirdpay.NewPay(check)
|
||||
pay = thirdpay.NewPayWithPayCheck(check)
|
||||
// 创建订单
|
||||
if &check.OldOrder != nil {
|
||||
if check.OldOrder != nil {
|
||||
pay.Order = check.OldOrder
|
||||
} else {
|
||||
pay.CreateOrder(common.ORDER_TYPE_PAY)
|
||||
if pay.PayCode != errorcode.Success {
|
||||
return pay
|
||||
}
|
||||
} else {
|
||||
pay.Order = check.OldOrder
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -4,5 +4,12 @@ import "PaymentCenter/app/http/entities/front"
|
|||
|
||||
type Reqs struct {
|
||||
front.PayCommonReqBody
|
||||
OutTradeNo string `json:"out_trade_no"`
|
||||
RefundOutTradeNo string `json:"refund_out_trade_no"`
|
||||
RefundOrderId string `json:"refundOrder_id"`
|
||||
}
|
||||
|
||||
type OrderFindOne struct {
|
||||
OrderId string
|
||||
OutTradeNo string `json:"out_trade_no"`
|
||||
}
|
||||
|
|
|
@ -20,6 +20,13 @@ func Encrypt(plaintext, key []byte) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 加密后的数据会比原文长,因为需要添加一些填充字节
|
||||
// PKCS#7填充
|
||||
plaintext = pkcs7Padding(plaintext, block.BlockSize())
|
||||
|
||||
// 创建一个cipher.BlockMode,这里使用CBC模式
|
||||
// 需要一个iv(初始化向量),它的长度和Block的块大小相同
|
||||
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
|
@ -29,7 +36,8 @@ func Encrypt(plaintext, key []byte) ([]byte, error) {
|
|||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
|
||||
|
||||
return iv, nil
|
||||
// 返回的密文包括iv和加密后的数据
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
// 解密函数
|
||||
|
|
|
@ -32,7 +32,8 @@ func parseRSAPublicKeyFromPEM(pemData []byte) (*rsa.PublicKey, error) {
|
|||
}
|
||||
|
||||
// encrypt 使用RSA公钥加密数据
|
||||
func Encrypt(publicKeyPEM string, plaintext []byte) ([]byte, error) {
|
||||
func Encrypt(publicKeyPEM string, plaintext string) ([]byte, error) {
|
||||
var encryptedData []byte
|
||||
// 将PEM编码的公钥转换为[]byte
|
||||
pemData := []byte(publicKeyPEM)
|
||||
|
||||
|
@ -41,14 +42,24 @@ func Encrypt(publicKeyPEM string, plaintext []byte) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hash := sha256.New()
|
||||
maxBlockSize := pubKey.Size() - 2*hash.Size() - 2
|
||||
// 创建用于加密的随机填充
|
||||
label := []byte("") // OAEP标签,对于某些情况可能是非空的
|
||||
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, pubKey, plaintext, label)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for len(plaintext) > 0 {
|
||||
blockSize := maxBlockSize
|
||||
if len(plaintext) < maxBlockSize {
|
||||
blockSize = len(plaintext)
|
||||
}
|
||||
block := plaintext[:blockSize]
|
||||
encryptedBlock, err := rsa.EncryptOAEP(hash, rand.Reader, pubKey, []byte(block), label)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encryptedData = append(encryptedData, encryptedBlock...)
|
||||
plaintext = plaintext[blockSize:]
|
||||
}
|
||||
return ciphertext, nil
|
||||
return encryptedData, nil
|
||||
}
|
||||
|
||||
// parseRSAPrivateKeyFromPEM 解析PEM编码的RSA私钥
|
||||
|
@ -80,34 +91,38 @@ func parseRSAPrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error) {
|
|||
|
||||
// decrypt 使用RSA私钥解密数据
|
||||
func Decrypt(privateKeyPEM string, encryptedDataBase64 string) ([]byte, error) {
|
||||
var decryptedData []byte
|
||||
// 将PEM编码的私钥转换为[]byte
|
||||
pemData := []byte(privateKeyPEM)
|
||||
|
||||
// 解析PEM数据以获取私钥
|
||||
privKey, err := parseRSAPrivateKeyFromPEM(pemData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keySize := privKey.PublicKey.Size()
|
||||
label := []byte("") // OAEP标签,对于某些情况可能是非空的
|
||||
hash := sha256.New()
|
||||
// 将Base64编码的加密数据解码为字节切片
|
||||
encryptedData, err := base64.StdEncoding.DecodeString(encryptedDataBase64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 根据你的加密方式选择合适的解密函数
|
||||
// 这里假设使用的是OAEP填充和SHA-256哈希函数
|
||||
label := []byte("") // OAEP标签,对于某些情况可能是非空的
|
||||
decrypted, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privKey, encryptedData, label)
|
||||
if err != nil {
|
||||
// 如果失败,可以尝试使用PKCS#1 v1.5填充
|
||||
decrypted, err = rsa.DecryptPKCS1v15(rand.Reader, privKey, encryptedData)
|
||||
for len(encryptedData) > 0 {
|
||||
block := encryptedData[:keySize]
|
||||
// 这里假设使用的是OAEP填充和SHA-256哈希函数
|
||||
decryptedBlock, err := rsa.DecryptOAEP(hash, rand.Reader, privKey, block, label)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
//// 如果失败,可以尝试使用PKCS#1 v1.5填充
|
||||
decryptedBlock, err = rsa.DecryptPKCS1v15(rand.Reader, privKey, encryptedData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
decryptedData = append(decryptedData, decryptedBlock...)
|
||||
encryptedData = encryptedData[keySize:]
|
||||
}
|
||||
|
||||
return decrypted, nil
|
||||
return decryptedData, nil
|
||||
}
|
||||
|
||||
// 生成密钥对
|
||||
|
|
|
@ -18,7 +18,7 @@ func TestRsaEncrypt(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRsaDecrypt(t *testing.T) {
|
||||
data := encrypt()
|
||||
data := "pPnAPy7v2SY9Fu0LFcQH8UBA6VQ2FCfSg3nRZdMXS7mWjBwlacKHuFnh9UhobL7mxnmMyZPP100bpjCg2kvcfOpOp3ci85p+OYWINt4Fh3qgEOTG5FUyziaagGLm882t/I36KsDTVvbMZvC5sg4gZ9JQ5yAR+nuJfr0IxI0se/iD5luV1rms1kZHggd30iXdZtbkbX7xJ4xtnIiJmZU7kL+Xmvv1rDdPLxbol65QfnM1me1IHkXJapqSBnhEEmFQyBx31vp1ccNjkza8ZWbvTPCngc1k4kvlm6lKfwsG4hMuSdXUzveDm+Oo8StAKnyVoerJ202n7Vfx1XhehineQT0TPD7bO0HCEsDXXYEWwvcax8VdzYvHk7qSbH6e154qCr4LgDRSHKwAAExinTrzxx2rtSimieBLaEpDL2v5ch45HnhjRhWTRmM61W1g6sdHaVX1mQxaXvrT4v+h+f4TbIV4r4qeGJ6rXG+yKRoYseLzyGgystoOny9P0UH15W8rWPytV2eioWT7i3Cglg04BWP9mst67LQXeFH4CA6CkwVV2w9nCHrzxX2ouYSQELUEkTlIMry2AlkZubUnupGJLmLLUyZj7pM/6cLjyAgm02/gRc4wwf7JBBq/ipmKXpkhHXWLtQDWJEZTT+ug2v9EXy5dgPNPe8ZI0MILAeipjIc="
|
||||
privateKeyPEM := `-----BEGIN RSA PRIVATE KEY-----
|
||||
` + PRI + `
|
||||
-----END RSA PRIVATE KEY-----`
|
||||
|
@ -27,13 +27,11 @@ func TestRsaDecrypt(t *testing.T) {
|
|||
}
|
||||
|
||||
func encrypt() string {
|
||||
data := "{\"order_no\":4323455642275676219,\"order_type\":1,\"out_tread_no\":\"asdadasdas\",\"amount\":1,\"desc\":\"abc\",\"status\":2,\"create_time\":\"2024-08-07 18:36:43\"}"
|
||||
fmt.Println(len(data))
|
||||
dataJson := []byte(data)
|
||||
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"refundOutTreadNo001\",\"amount\":1,\"desc\":\"退款\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643,\"refund_out_trade_no\":\"asdadasdas\"}"
|
||||
pub := `-----BEGIN PUBLIC KEY-----
|
||||
` + PUB + `
|
||||
-----END PUBLIC KEY-----`
|
||||
en, err := Encrypt(pub, dataJson)
|
||||
en, err := Encrypt(pub, data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -42,9 +40,10 @@ func encrypt() string {
|
|||
}
|
||||
|
||||
func encryptWithAes() string {
|
||||
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}"
|
||||
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"refundOutTreadNo001\",\"amount\":1,\"desc\":\"退款\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643,\"refund_out_trade_no\":\"asdadasdas\"}"
|
||||
aes.Encrypt([]byte(data), []byte(aes.TestKey))
|
||||
dataJson := []byte(data)
|
||||
|
||||
dataJson := base64.StdEncoding.EncodeToString([]byte(data))
|
||||
pub := `-----BEGIN PUBLIC KEY-----
|
||||
` + PUB + `
|
||||
-----END PUBLIC KEY-----`
|
||||
|
@ -58,14 +57,7 @@ func encryptWithAes() string {
|
|||
|
||||
// 测试生成密钥对
|
||||
func TestGenerateRSAKey(t *testing.T) {
|
||||
pub, pri, err := GenerateKey()
|
||||
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}"
|
||||
dataJson := []byte(data)
|
||||
en, err := Encrypt(pub, dataJson)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
content := base64.StdEncoding.EncodeToString(en)
|
||||
res, err := Decrypt(pri, content)
|
||||
fmt.Println("解密", string(res), err)
|
||||
pub, pri, _ := GenerateKey()
|
||||
|
||||
fmt.Println("pub:", pub, "\n", "pri:", pri)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue