Compare commits
No commits in common. "3061987dae4d8aeadb7428102e98dd8092e8e183" and "bf1bc4b1a20cef635d15ab2635b75e406ad3243c" have entirely different histories.
3061987dae
...
bf1bc4b1a2
|
@ -6,13 +6,14 @@ import (
|
||||||
"PaymentCenter/app/http/entities"
|
"PaymentCenter/app/http/entities"
|
||||||
"PaymentCenter/app/http/entities/backend"
|
"PaymentCenter/app/http/entities/backend"
|
||||||
"PaymentCenter/app/models/ordersmodel"
|
"PaymentCenter/app/models/ordersmodel"
|
||||||
"PaymentCenter/app/services/thirdpay/notify"
|
"PaymentCenter/app/models/orderthirdpaylogmodel"
|
||||||
"PaymentCenter/app/third/paymentService"
|
"PaymentCenter/app/third/paymentService"
|
||||||
"PaymentCenter/app/third/paymentService/payCommon"
|
"PaymentCenter/app/third/paymentService/payCommon"
|
||||||
"PaymentCenter/app/utils"
|
"PaymentCenter/app/utils"
|
||||||
"PaymentCenter/config"
|
"PaymentCenter/config"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/qit-team/snow-core/command"
|
"github.com/qit-team/snow-core/command"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -82,7 +83,7 @@ func closeOrder() {
|
||||||
if response.Code == payCommon.PAY_SUCCESS_CODE {
|
if response.Code == payCommon.PAY_SUCCESS_CODE {
|
||||||
orderIds = append(orderIds, orderInfo.Id)
|
orderIds = append(orderIds, orderInfo.Id)
|
||||||
} else {
|
} else {
|
||||||
utils.Log(nil, "关闭订单,上游失败", response)
|
utils.Log(nil, "关闭订单,上游支付失败", response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 修改订单状态为关闭
|
// 修改订单状态为关闭
|
||||||
|
@ -171,115 +172,64 @@ func queryOrder() {
|
||||||
// 成功
|
// 成功
|
||||||
status = common.ORDER_STATUS_PAYED
|
status = common.ORDER_STATUS_PAYED
|
||||||
case "REFUND":
|
case "REFUND":
|
||||||
// 退款 订单支付完成才能退款,所以支付单的状态是支付完成
|
// 退款
|
||||||
status = common.ORDER_STATUS_PAYED
|
status = common.ORDER_STATUS_REFUND
|
||||||
|
|
||||||
case "NOTPAY":
|
case "NOTPAY":
|
||||||
// 未支付
|
// 未支付
|
||||||
return
|
return
|
||||||
case "CLOSED":
|
case "CLOSED":
|
||||||
// 关闭
|
// 关闭
|
||||||
status = common.ORDER_STATUS_CLOSE
|
status = common.ORDER_STATUS_CLOSE
|
||||||
|
|
||||||
}
|
}
|
||||||
// 回调通知下游 todo
|
// 回调通知下游 todo
|
||||||
notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.PayerTotal), "")
|
|
||||||
utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult)
|
// 更新订单状态 todo
|
||||||
}
|
orderUpdate := ordersmodel.Orders{
|
||||||
}(orderInfo)
|
Id: orderInfo.Id,
|
||||||
}
|
Status: status,
|
||||||
wg.Wait()
|
PayerTotal: int(result.Result.PayerTotal),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 主动查询退款订单状态
|
session := ordersmodel.GetInstance().GetDb().NewSession()
|
||||||
func queryRefundOrder() {
|
if err = session.Begin(); err != nil {
|
||||||
var now = time.Now().Format(time.DateTime)
|
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
||||||
utils.Log(nil, "主动查询退款订单状态", now)
|
|
||||||
ctx := context.Background()
|
|
||||||
// 查询退款中的订单
|
|
||||||
repo := data.NewOrderRepo(ordersmodel.GetInstance().GetDb())
|
|
||||||
// 拼接条件
|
|
||||||
cond := builder.NewCond()
|
|
||||||
cond = cond.And(builder.Eq{"status": common.ORDER_STATUS_PAYING}, builder.Gt{"orders.create_time": time.Now().Add(-time.Second * time.Duration(config.GetConf().CronConfig.QueryOrderTime))})
|
|
||||||
cond = cond.And(builder.Eq{"order_type": common.ORDER_TYPE_REFUND})
|
|
||||||
order := make([]ordersmodel.OrdersLeftPayChannelList, 0)
|
|
||||||
err := repo.OrdersLeftPayChannelList(cond, entities.PageRequest{}, &order)
|
|
||||||
if err != nil {
|
|
||||||
utils.Log(nil, "主动查询退款订单状态,查询退款中订单失败", err)
|
|
||||||
return
|
return
|
||||||
} else if len(order) > 0 {
|
}
|
||||||
ch := make(chan struct{}, config.GetConf().CronConfig.ConcurrentNumber)
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
|
|
||||||
for index := range order {
|
|
||||||
ch <- struct{}{}
|
|
||||||
wg.Add(1)
|
|
||||||
orderInfo := order[index]
|
|
||||||
|
|
||||||
// 发起查询上游
|
|
||||||
go func(orderInfo ordersmodel.OrdersLeftPayChannelList) {
|
|
||||||
defer func() {
|
defer func() {
|
||||||
<-ch
|
if err != nil {
|
||||||
wg.Done()
|
session.Rollback()
|
||||||
|
} else {
|
||||||
|
err = session.Commit()
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
orderLogRepo := data.NewOrderThirdPayLogRepo(session)
|
||||||
|
orderRepo := data.NewOrderRepo(session)
|
||||||
|
|
||||||
query := paymentService.OrderRefundQueryRequest{
|
conn := builder.NewCond()
|
||||||
RefundOrderId: orderInfo.Id,
|
conn = conn.And(builder.Eq{"id": orderInfo.Id})
|
||||||
OrderId: orderInfo.RefundOrderId,
|
_, err = orderRepo.OrderUpdate(&orderUpdate, conn)
|
||||||
}
|
if err != nil {
|
||||||
switch utils.PayType(orderInfo.ChannelType) {
|
utils.Log(nil, "主动查询订单支付状态,更新订单状态失败", err)
|
||||||
case common.PAY_CHANNLE_TYPE_WECHAT:
|
|
||||||
wx := backend.WechatPayChannel{}
|
|
||||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx)
|
|
||||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
|
||||||
query.Wx = paymentService.WxPay{
|
|
||||||
AppId: orderInfo.AppId,
|
|
||||||
MchId: wx.MchId,
|
|
||||||
SerialNo: wx.SerialNo,
|
|
||||||
ApiV3Key: wx.ApiV3Key,
|
|
||||||
PrivateKey: wx.PrivateKey,
|
|
||||||
}
|
|
||||||
case common.PAY_CHANNLE_TYPE_ZFB:
|
|
||||||
ali := backend.AliPayChannel{}
|
|
||||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
|
||||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
|
|
||||||
query.Ali = paymentService.AliPay{
|
|
||||||
AppId: orderInfo.AppId,
|
|
||||||
PrivateKey: ali.PrivateKey,
|
|
||||||
AppPublicCert: ali.AppPublicCert,
|
|
||||||
AlipayRootCert: ali.AlipayRootCert,
|
|
||||||
AlipayPublicCert: ali.AlipayPublicCert,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发起查询
|
// 写入日志
|
||||||
result := paymentService.OrderRefundQuery(ctx, query)
|
body, _ := json.Marshal(result)
|
||||||
utils.Log(nil, "主动查询退款订单状态,上游返回数据", result)
|
log := orderthirdpaylogmodel.OrderThirdPayLog{
|
||||||
// 查询成功,校验状态
|
OrderId: orderInfo.Id,
|
||||||
var status int
|
PayCallback: string(body),
|
||||||
if result.Code == payCommon.PAY_SUCCESS_CODE {
|
Status: 0,
|
||||||
// 退款状态 0:未申请,1:退款中,2:退款成功,3:退款失败
|
PayParam: fmt.Sprintf(`{"pay_channel_id":%d}`, orderInfo.PayChannelId),
|
||||||
switch result.Result.RefundStatus {
|
Type: common.THIRD_ORDER_TYPE_ORDER_QUERY,
|
||||||
case 0:
|
MerchantCallback: "{}",
|
||||||
// 未申请
|
|
||||||
utils.Log(nil, "主动查询退款订单状态,未申请", status)
|
|
||||||
return
|
|
||||||
case 1:
|
|
||||||
// 退款中
|
|
||||||
status = common.ORDER_STATUS_PAYING
|
|
||||||
return
|
|
||||||
case 2:
|
|
||||||
// 退款成功
|
|
||||||
status = common.ORDER_STATUS_PAYED
|
|
||||||
case 3:
|
|
||||||
// 退款失败
|
|
||||||
status = common.ORDER_STATUS_FAILED
|
|
||||||
}
|
}
|
||||||
// 回调通知下游 todo
|
_, err = orderLogRepo.OrderThirdPayLogInsertOne(&log)
|
||||||
notifyResult := notify.NewOrderNotifyWithHandle(orderInfo.Id, status, int(result.Result.RefundFee), "")
|
if err != nil {
|
||||||
utils.Log(nil, "主动查询退款订单状态,回调下游", notifyResult)
|
utils.Log(nil, "主动查询订单支付状态,写入支付日志失败", err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}(orderInfo)
|
}(orderInfo)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,5 +17,4 @@ func RegisterSchedule(c *cron.Cron) {
|
||||||
//c.AddFunc("@every 10s", test)
|
//c.AddFunc("@every 10s", test)
|
||||||
c.AddFunc("@every 60s", closeOrder)
|
c.AddFunc("@every 60s", closeOrder)
|
||||||
c.AddFunc("@every 10s", queryOrder)
|
c.AddFunc("@every 10s", queryOrder)
|
||||||
c.AddFunc("@every 60s", queryRefundOrder)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,13 @@ const (
|
||||||
ADMIN_USER_NAME = "User-Name"
|
ADMIN_USER_NAME = "User-Name"
|
||||||
ADMIN_USER_INCLUDEUSERS = "Include-Users"
|
ADMIN_USER_INCLUDEUSERS = "Include-Users"
|
||||||
|
|
||||||
// '订单状态: 1待支付、2支付中、3支付成功、4支付失败、5订单关闭
|
// '订单状态: 1待支付、2支付中、3支付成功、4支付失败、5订单关闭 6支付完成后退款,
|
||||||
ORDER_STATUS_WAITPAY = 1
|
ORDER_STATUS_WAITPAY = 1
|
||||||
ORDER_STATUS_PAYING = 2
|
ORDER_STATUS_PAYING = 2
|
||||||
ORDER_STATUS_PAYED = 3
|
ORDER_STATUS_PAYED = 3
|
||||||
ORDER_STATUS_FAILED = 4
|
ORDER_STATUS_FAILED = 4
|
||||||
ORDER_STATUS_CLOSE = 5
|
ORDER_STATUS_CLOSE = 5
|
||||||
|
ORDER_STATUS_REFUND = 6
|
||||||
|
|
||||||
// 订单类型,1支付,2退款
|
// 订单类型,1支付,2退款
|
||||||
ORDER_TYPE_PAY = 1
|
ORDER_TYPE_PAY = 1
|
||||||
|
@ -62,10 +63,3 @@ var OrderTypeList = map[int]string{
|
||||||
ORDER_TYPE_PAY: "付款",
|
ORDER_TYPE_PAY: "付款",
|
||||||
ORDER_TYPE_REFUND: "退款",
|
ORDER_TYPE_REFUND: "退款",
|
||||||
}
|
}
|
||||||
var OrderStatusMap = map[int]string{
|
|
||||||
ORDER_STATUS_WAITPAY: "等待执行",
|
|
||||||
ORDER_STATUS_PAYING: "执行中",
|
|
||||||
ORDER_STATUS_PAYED: "执行成功",
|
|
||||||
ORDER_STATUS_FAILED: "执行失败",
|
|
||||||
ORDER_STATUS_CLOSE: "订单关闭",
|
|
||||||
}
|
|
||||||
|
|
|
@ -47,9 +47,8 @@ const (
|
||||||
AppSM4DecryptFail = 1231
|
AppSM4DecryptFail = 1231
|
||||||
AppSM4EncryptKeyNotFound = 1232
|
AppSM4EncryptKeyNotFound = 1232
|
||||||
AppSM4EncryptFail = 1233
|
AppSM4EncryptFail = 1233
|
||||||
AppAesEncryptFail = 1234
|
|
||||||
// 加密方式不存在
|
// 加密方式不存在
|
||||||
EncryptTypeNotFound = 1241
|
EncryptTypeNotFound = 1240
|
||||||
|
|
||||||
//渠道
|
//渠道
|
||||||
PayChannelNotFound = 1300
|
PayChannelNotFound = 1300
|
||||||
|
@ -70,9 +69,6 @@ const (
|
||||||
|
|
||||||
//回调
|
//回调
|
||||||
NotifySendFail = 1600
|
NotifySendFail = 1600
|
||||||
|
|
||||||
//预支付
|
|
||||||
PrePayFail = 1701
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var MsgEN = map[int]string{
|
var MsgEN = map[int]string{
|
||||||
|
@ -114,8 +110,6 @@ var MsgZH = map[int]string{
|
||||||
AppSM4EncryptKeyNotFound: "密匙缺失,无法进行sm4加密",
|
AppSM4EncryptKeyNotFound: "密匙缺失,无法进行sm4加密",
|
||||||
AppSM4EncryptFail: "sm4加密失败",
|
AppSM4EncryptFail: "sm4加密失败",
|
||||||
|
|
||||||
AppAesEncryptFail: "aes 加密失败",
|
|
||||||
|
|
||||||
EncryptTypeNotFound: "加密方式不存在",
|
EncryptTypeNotFound: "加密方式不存在",
|
||||||
|
|
||||||
PayChannelNotFound: "支付方式不存在",
|
PayChannelNotFound: "支付方式不存在",
|
||||||
|
@ -133,8 +127,6 @@ var MsgZH = map[int]string{
|
||||||
OrderStatusErr: "订单状态错误",
|
OrderStatusErr: "订单状态错误",
|
||||||
|
|
||||||
NotifySendFail: "回调发送失败",
|
NotifySendFail: "回调发送失败",
|
||||||
|
|
||||||
PrePayFail: "预支付失败",
|
|
||||||
}
|
}
|
||||||
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}
|
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}
|
||||||
|
|
||||||
|
|
|
@ -7,47 +7,35 @@ import (
|
||||||
"PaymentCenter/app/models/ordersmodel"
|
"PaymentCenter/app/models/ordersmodel"
|
||||||
"PaymentCenter/app/services"
|
"PaymentCenter/app/services"
|
||||||
"PaymentCenter/app/services/thirdpay"
|
"PaymentCenter/app/services/thirdpay"
|
||||||
"PaymentCenter/app/services/thirdpay/api"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PayUrl(c *gin.Context) {
|
func Pay(c *gin.Context) {
|
||||||
var res front.PayUrlResp
|
|
||||||
req := controllers.GetRequest(c).(*front.PayReqs)
|
req := controllers.GetRequest(c).(*front.PayReqs)
|
||||||
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
|
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
|
||||||
check := thirdpay.ThirdPayInfoCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP())
|
check := thirdpay.ThirdPayCheck(c.Request.Context(), req, appCheckInfo, c.ClientIP())
|
||||||
if check.CheckCode != errorcode.Success {
|
if check.CheckCode != errorcode.Success {
|
||||||
|
if check.CheckCode == errorcode.OrdersExist {
|
||||||
|
//订单已存在,直接返回订单信息
|
||||||
|
controllers.ApiRes(c, thirdpay.PayCallBack(check.OldOrder, false), errorcode.Success)
|
||||||
|
return
|
||||||
|
}
|
||||||
controllers.ApiRes(c, nil, check.CheckCode)
|
controllers.ApiRes(c, nil, check.CheckCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
check.CheckOrder()
|
payInfo := thirdpay.ThirdPayWeb(check)
|
||||||
if check.CheckCode != errorcode.Success {
|
if payInfo.PayCode != errorcode.Success {
|
||||||
controllers.ApiRes(c, nil, check.CheckCode)
|
controllers.ApiRes(c, nil, payInfo.PayCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pay := thirdpay.ThirdPayWeb(check)
|
controllers.ApiRes(c, thirdpay.PayCallBack(payInfo.Order, true), errorcode.Success)
|
||||||
if pay.PayCode != errorcode.Success {
|
|
||||||
controllers.ApiRes(c, nil, pay.PayCode)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
data := api.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.Url = pay.Url
|
|
||||||
controllers.ApiRes(c, res, pay.PayCode)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询订单
|
// 查询订单
|
||||||
func QueryOrder(c *gin.Context) {
|
func QueryOrder(c *gin.Context) {
|
||||||
req := controllers.GetRequest(c).(*front.QueryReqs)
|
req := controllers.GetRequest(c).(*front.QueryReqs)
|
||||||
|
|
||||||
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
|
appCheckInfo := controllers.GetAppCheckInfo(c).(*services.AppCheck)
|
||||||
// 查询订单表
|
// 查询订单表
|
||||||
order := ordersmodel.Orders{
|
order := ordersmodel.Orders{
|
||||||
|
@ -59,14 +47,10 @@ func QueryOrder(c *gin.Context) {
|
||||||
cond = cond.And(builder.Eq{"out_tread_no": order.OutTreadNo}, builder.Eq{"app_id": order.AppId})
|
cond = cond.And(builder.Eq{"out_tread_no": order.OutTreadNo}, builder.Eq{"app_id": order.AppId})
|
||||||
_, code := services.OrderFindOne(&order, cond)
|
_, code := services.OrderFindOne(&order, cond)
|
||||||
if code != errorcode.OrdersExist {
|
if code != errorcode.OrdersExist {
|
||||||
controllers.ApiRes(c, nil, code)
|
controllers.ApiRes(c, order, code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data := api.NewOrdersResp(&order)
|
data := front.OrdersResponse{}
|
||||||
encryptData, errCode := api.EnCrypt(appCheckInfo.App, data)
|
data.ResponseFromDb(order)
|
||||||
if errCode != errorcode.Success {
|
controllers.ApiRes(c, data, errorcode.Success)
|
||||||
controllers.ApiRes(c, nil, errCode)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
controllers.ApiRes(c, encryptData, errorcode.Success)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package front
|
package front
|
||||||
|
|
||||||
|
import "PaymentCenter/app/models/ordersmodel"
|
||||||
|
|
||||||
type ApiCommonBody struct {
|
type ApiCommonBody struct {
|
||||||
AppId int64 `json:"app_id" validate:"required"`
|
AppId int64 `json:"app_id" validate:"required"`
|
||||||
Timestamp int64 `json:"timestamp" validate:"required"`
|
Timestamp int64 `json:"timestamp" validate:"required"`
|
||||||
|
@ -9,7 +11,6 @@ type RequestBody struct {
|
||||||
AppId int64 `json:"app_id" validate:"required"`
|
AppId int64 `json:"app_id" validate:"required"`
|
||||||
Timestamp int64 `json:"timestamp" validate:"required"`
|
Timestamp int64 `json:"timestamp" validate:"required"`
|
||||||
Data string `json:"data" validate:"required"`
|
Data string `json:"data" validate:"required"`
|
||||||
Key string `json:"key" validate:"max=32"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PayReqs struct {
|
type PayReqs struct {
|
||||||
|
@ -22,12 +23,37 @@ type PayReqs struct {
|
||||||
Desc string `json:"desc" validate:"max=100" label:"商品描述"`
|
Desc string `json:"desc" validate:"max=100" label:"商品描述"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PayUrlResp struct {
|
type OrderApiResp struct {
|
||||||
Order string `json:"order"`
|
Code int
|
||||||
Url string `json:"url"`
|
Msg string
|
||||||
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryReqs struct {
|
type QueryReqs struct {
|
||||||
ApiCommonBody
|
ApiCommonBody
|
||||||
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
|
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OrdersResponse struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
PayChannelId int64 `json:"pay_channel_id"`
|
||||||
|
AppId int64 `json:"app_id"`
|
||||||
|
OutTreadNo string `json:"out_tread_no"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
OrderType int `json:"order_type"`
|
||||||
|
Amount int `json:"amount"`
|
||||||
|
PayerTotal int `json:"payer_total"`
|
||||||
|
CreateTime string `json:"create_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *OrdersResponse) ResponseFromDb(db ordersmodel.Orders) {
|
||||||
|
o.Id = db.Id
|
||||||
|
o.PayChannelId = db.PayChannelId
|
||||||
|
o.AppId = db.AppId
|
||||||
|
o.OutTreadNo = db.OutTreadNo
|
||||||
|
o.Status = db.Status
|
||||||
|
o.OrderType = db.OrderType
|
||||||
|
o.Amount = db.Amount
|
||||||
|
o.PayerTotal = db.PayerTotal
|
||||||
|
o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"PaymentCenter/app/http/entities/front"
|
"PaymentCenter/app/http/entities/front"
|
||||||
"PaymentCenter/app/http/requestmapping"
|
"PaymentCenter/app/http/requestmapping"
|
||||||
"PaymentCenter/app/services"
|
"PaymentCenter/app/services"
|
||||||
"PaymentCenter/app/services/thirdpay/api"
|
"PaymentCenter/app/services/apicrypt"
|
||||||
"PaymentCenter/app/utils"
|
"PaymentCenter/app/utils"
|
||||||
"PaymentCenter/config"
|
"PaymentCenter/config"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -155,8 +155,12 @@ func ValidatePayRequest() gin.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//解密
|
//解密
|
||||||
dataByte, errCode := api.DeCrypt(appCheck.App, requestDataStruct.Data, requestDataStruct.Key)
|
cryptFunc := appCheck.Crypt()
|
||||||
if errCode != errorcode.Success {
|
if cryptFunc == nil {
|
||||||
|
controllers.ApiRes(c, nil, appCheck.GetCode())
|
||||||
|
}
|
||||||
|
dataByte, errCode := cryptFunc(appCheck.App).Decrypt(requestDataStruct.Data)
|
||||||
|
if errCode != apicrypt.CryptNotError {
|
||||||
controllers.ApiRes(c, nil, errCode)
|
controllers.ApiRes(c, nil, errCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var FrontRequestMap = map[string]func() interface{}{
|
var FrontRequestMap = map[string]func() interface{}{
|
||||||
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.PayReqs) },
|
common.FRONT_V1 + "/pay/do": func() interface{} { return new(front.PayReqs) },
|
||||||
}
|
}
|
||||||
|
|
||||||
var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{
|
var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{
|
||||||
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.RequestBody) },
|
common.FRONT_V1 + "/pay/do": func() interface{} { return new(front.RequestBody) },
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ func RegisterRoute(router *gin.Engine) {
|
||||||
|
|
||||||
pay := v1.Group("/pay", middlewares.ValidateRequest(), middlewares.ValidatePayRequest())
|
pay := v1.Group("/pay", middlewares.ValidateRequest(), middlewares.ValidatePayRequest())
|
||||||
{
|
{
|
||||||
pay.POST("/url", front.PayUrl)
|
pay.POST("/do", front.Pay)
|
||||||
pay.POST("/query", front.QueryOrder) //查询订单
|
pay.POST("/query", front.QueryOrder) //查询订单
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ var (
|
||||||
type OrderRequestLog struct {
|
type OrderRequestLog struct {
|
||||||
Id int64
|
Id int64
|
||||||
IpAddress string `xorm:"'ip_address' varchar(16)"`
|
IpAddress string `xorm:"'ip_address' varchar(16)"`
|
||||||
URL string `xorm:"'url' varchar(100)"`
|
URL string `xorm:"'url' varchar(20)"`
|
||||||
MerchantRequest string `xorm:"'merchant_request' JSON"`
|
MerchantRequest string `xorm:"'merchant_request' JSON"`
|
||||||
MerchantResponse string `xorm:"'merchant_response' JSON"`
|
MerchantResponse string `xorm:"'merchant_response' JSON"`
|
||||||
CreateTime time.Time `xorm:"'create_time' datetime created"`
|
CreateTime time.Time `xorm:"'create_time' datetime created"`
|
||||||
|
|
|
@ -19,7 +19,6 @@ type Orders struct {
|
||||||
AppId int64 `xorm:"'app_id' bigint(20)"`
|
AppId int64 `xorm:"'app_id' bigint(20)"`
|
||||||
OutTreadNo string `xorm:"'out_tread_no' varchar(50)"`
|
OutTreadNo string `xorm:"'out_tread_no' varchar(50)"`
|
||||||
OrderType int `xorm:"'order_type' TINYINT"`
|
OrderType int `xorm:"'order_type' TINYINT"`
|
||||||
RefundOrderId int64 `xorm:"'refund_order_id' bigint(20)"`
|
|
||||||
Amount int `xorm:"'amount' int(11)"`
|
Amount int `xorm:"'amount' int(11)"`
|
||||||
PayerTotal int `xorm:"'payer_total' int(11)"`
|
PayerTotal int `xorm:"'payer_total' int(11)"`
|
||||||
ExtJson string `xorm:"'ext_json' varchar(1024)"`
|
ExtJson string `xorm:"'ext_json' varchar(1024)"`
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"PaymentCenter/app/constants/errorcode"
|
"PaymentCenter/app/constants/errorcode"
|
||||||
"PaymentCenter/app/models/appmodel"
|
"PaymentCenter/app/models/appmodel"
|
||||||
"PaymentCenter/app/utils/encrypt/rsa"
|
"PaymentCenter/app/utils/encrypt/rsa"
|
||||||
"encoding/base64"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRsa(app *appmodel.App) ApiCrypt {
|
func NewRsa(app *appmodel.App) ApiCrypt {
|
||||||
|
@ -13,18 +12,18 @@ func NewRsa(app *appmodel.App) ApiCrypt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rsa) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
func (r *Rsa) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||||
if r.App.PublicKey == "" {
|
if r.App.PublicKey == "" {
|
||||||
return nil, errorcode.AppRsaEncryptKeyNotFound
|
return nil, errorcode.AppRsaEncryptKeyNotFound
|
||||||
}
|
}
|
||||||
publicKeyPEM := `-----BEGIN PUBLIC KEY-----
|
publicKeyPEM := `-----BEGIN PUBLIC KEY-----
|
||||||
` + r.App.PublicKey + `
|
` + r.App.PublicKey + `
|
||||||
-----END PUBLIC KEY-----`
|
-----END PUBLIC KEY-----`
|
||||||
encryptByte, err := rsa.Encrypt(publicKeyPEM, data)
|
encryptData, err := rsa.Encrypt(publicKeyPEM, []byte(decryptData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorcode.AppRsaEncryptFail
|
return nil, errorcode.AppRsaEncryptFail
|
||||||
}
|
}
|
||||||
encryptData = []byte(base64.StdEncoding.EncodeToString(encryptByte))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,12 @@ func NewSm2(app *appmodel.App) ApiCrypt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SM2) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
func (r *SM2) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||||
|
|
||||||
if r.App.MerchantPublicKey == "" {
|
if r.App.MerchantPublicKey == "" {
|
||||||
return nil, errorcode.AppSM2EncryptKeyNotFound
|
return nil, errorcode.AppSM2EncryptKeyNotFound
|
||||||
}
|
}
|
||||||
|
//
|
||||||
encryptDataString, err := sm2.SM2Encrypt(string(data), r.App.PrivateKey)
|
encryptDataString, err := sm2.SM2Encrypt(decryptData, r.App.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorcode.AppSM2EncryptFail
|
return nil, errorcode.AppSM2EncryptFail
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,12 @@ func NewSm4(app *appmodel.App) ApiCrypt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SM4) Encrypt(data []byte) (encryptData []byte, errCode int) {
|
func (r *SM4) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||||
if r.App.MerchantPublicKey == "" || r.App.PrivateKey == "" {
|
if r.App.MerchantPublicKey == "" || r.App.PrivateKey == "" {
|
||||||
return nil, errorcode.AppSM4DecryptKeyNotFound
|
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, decryptData, "", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorcode.AppSM4EncryptFail
|
return nil, errorcode.AppSM4EncryptFail
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
ApiCrypt interface {
|
ApiCrypt interface {
|
||||||
Encrypt(data []byte) (encryptData []byte, errCode int)
|
Encrypt(decryptData string) (encryptData []byte, errCode int)
|
||||||
Decrypt(encryptData string) (decryptData []byte, errCode int)
|
Decrypt(encryptData string) (decryptData []byte, errCode int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"PaymentCenter/app/constants/errorcode"
|
"PaymentCenter/app/constants/errorcode"
|
||||||
"PaymentCenter/app/models/appmodel"
|
|
||||||
"PaymentCenter/app/utils"
|
"PaymentCenter/app/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,7 +13,3 @@ func handErr(err error) int {
|
||||||
return errorcode.Success
|
return errorcode.Success
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnCryptData(app *appmodel.App) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"PaymentCenter/app/models/ordersmodel"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OrdersResp struct {
|
|
||||||
OrderNo int64 `json:"order_no"`
|
|
||||||
OrderType int `json:"order_type"`
|
|
||||||
OutTreadNo string `json:"out_tread_no"`
|
|
||||||
Amount int `json:"amount"`
|
|
||||||
Desc string `json:"desc"`
|
|
||||||
|
|
||||||
Status int `json:"status"`
|
|
||||||
CreateTime string `json:"create_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type OrdersWithUrl struct {
|
|
||||||
Order *OrdersResp
|
|
||||||
Url string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewOrdersResp(db *ordersmodel.Orders) *OrdersResp {
|
|
||||||
return &OrdersResp{
|
|
||||||
OrderNo: db.Id,
|
|
||||||
OutTreadNo: db.OutTreadNo,
|
|
||||||
Status: db.Status,
|
|
||||||
OrderType: db.OrderType,
|
|
||||||
Amount: db.Amount,
|
|
||||||
Desc: db.Desc,
|
|
||||||
CreateTime: db.CreateTime.Format("2006-01-02 15:04:05"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *OrdersResp) WithUrl(url string) *OrdersWithUrl {
|
|
||||||
return &OrdersWithUrl{
|
|
||||||
Order: o,
|
|
||||||
Url: url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *OrdersResp) WithAes(url string) *OrdersWithUrl {
|
|
||||||
return &OrdersWithUrl{
|
|
||||||
Order: o,
|
|
||||||
Url: url,
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"PaymentCenter/app/constants/errorcode"
|
|
||||||
"PaymentCenter/app/models/appmodel"
|
|
||||||
"PaymentCenter/app/services"
|
|
||||||
"PaymentCenter/app/services/apicrypt"
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
func EnCrypt(app *appmodel.App, data interface{}) ([]byte, int) {
|
|
||||||
appCheck := services.NewAppCheck(app)
|
|
||||||
cryptFunc := appCheck.Crypt()
|
|
||||||
if cryptFunc == nil {
|
|
||||||
return nil, appCheck.Code
|
|
||||||
}
|
|
||||||
dataByte, err := json.Marshal(data)
|
|
||||||
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)
|
|
||||||
if errCode != apicrypt.CryptNotError {
|
|
||||||
return nil, errCode
|
|
||||||
}
|
|
||||||
return encryptData, errorcode.Success
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeCrypt(app *appmodel.App, data string, aesKey string) ([]byte, int) {
|
|
||||||
appCheck := services.NewAppCheck(app)
|
|
||||||
cryptFunc := appCheck.Crypt()
|
|
||||||
if cryptFunc == nil {
|
|
||||||
return nil, appCheck.Code
|
|
||||||
}
|
|
||||||
dataByte, errCode := cryptFunc(app).Decrypt(data)
|
|
||||||
if errCode != apicrypt.CryptNotError {
|
|
||||||
return nil, errCode
|
|
||||||
}
|
|
||||||
//aesData, err := aes.Decrypt(dataByte, []byte(aesKey))
|
|
||||||
//if err != nil {
|
|
||||||
// return nil, errorcode.AppAesEncryptFail
|
|
||||||
//}
|
|
||||||
|
|
||||||
return dataByte, errorcode.Success
|
|
||||||
}
|
|
|
@ -3,9 +3,7 @@ package do
|
||||||
import (
|
import (
|
||||||
"PaymentCenter/app/constants/common"
|
"PaymentCenter/app/constants/common"
|
||||||
"PaymentCenter/app/constants/errorcode"
|
"PaymentCenter/app/constants/errorcode"
|
||||||
"PaymentCenter/app/models/paychannelmodel"
|
|
||||||
"PaymentCenter/app/services"
|
"PaymentCenter/app/services"
|
||||||
"PaymentCenter/app/third/paymentService/payCommon"
|
|
||||||
|
|
||||||
"PaymentCenter/app/models/ordersmodel"
|
"PaymentCenter/app/models/ordersmodel"
|
||||||
"PaymentCenter/app/third/paymentService"
|
"PaymentCenter/app/third/paymentService"
|
||||||
|
@ -15,7 +13,6 @@ type Pay struct {
|
||||||
paycheck *PayCheck
|
paycheck *PayCheck
|
||||||
Order *ordersmodel.Orders
|
Order *ordersmodel.Orders
|
||||||
PayCode int
|
PayCode int
|
||||||
Url string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPay(paycheck *PayCheck) *Pay {
|
func NewPay(paycheck *PayCheck) *Pay {
|
||||||
|
@ -40,9 +37,9 @@ func (w *Pay) CreateOrder() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Pay) PayUrl() (url string) {
|
func (w *Pay) Pay() {
|
||||||
var (
|
var (
|
||||||
payFunc func(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error
|
payFunc func(commonPayInfo *paymentService.PayOrderRequest, ExtJson string) error
|
||||||
ok bool
|
ok bool
|
||||||
)
|
)
|
||||||
thirdPay := &paymentService.PayOrderRequest{
|
thirdPay := &paymentService.PayOrderRequest{
|
||||||
|
@ -57,23 +54,13 @@ func (w *Pay) PayUrl() (url string) {
|
||||||
w.PayCode = errorcode.PayChannelNotBuild
|
w.PayCode = errorcode.PayChannelNotBuild
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := payFunc(thirdPay, w.paycheck.Channel)
|
err := payFunc(thirdPay, w.paycheck.Channel.ExtJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.PayCode = errorcode.PayChannelExtJsonError
|
w.PayCode = errorcode.PayChannelExtJsonError
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay)
|
res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay)
|
||||||
|
// payCommon.PAY_SUCCESS_CODE
|
||||||
if res.Code == payCommon.PAY_SUCCESS_CODE {
|
w.PayCode = res.Code
|
||||||
w.Order.Status = common.ORDER_STATUS_PAYING
|
|
||||||
code := services.OrderUpdate(w.Order, "status")
|
|
||||||
if code != errorcode.Success {
|
|
||||||
w.PayCode = code
|
|
||||||
}
|
|
||||||
w.Url = res.Result
|
|
||||||
} else {
|
|
||||||
w.PayCode = errorcode.PrePayFail
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,9 +61,9 @@ func (w *PayCheck) CheckOrder() {
|
||||||
cond := builder.NewCond()
|
cond := builder.NewCond()
|
||||||
cond = cond.And(builder.Eq{"out_tread_no": w.WebPayReqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId}, builder.Neq{"status": common.ORDER_STATUS_CLOSE})
|
cond = cond.And(builder.Eq{"out_tread_no": w.WebPayReqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId}, builder.Neq{"status": common.ORDER_STATUS_CLOSE})
|
||||||
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
|
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
|
||||||
if code == errorcode.SystemError {
|
if code == errorcode.OrdersExist {
|
||||||
|
w.OldOrder = order
|
||||||
w.CheckCode = code
|
w.CheckCode = code
|
||||||
}
|
}
|
||||||
w.OldOrder = order
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,30 +2,27 @@ package do
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"PaymentCenter/app/constants/common"
|
"PaymentCenter/app/constants/common"
|
||||||
"PaymentCenter/app/models/paychannelmodel"
|
|
||||||
"PaymentCenter/app/third/paymentService"
|
"PaymentCenter/app/third/paymentService"
|
||||||
"github.com/bytedance/sonic"
|
"github.com/bytedance/sonic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var PayWayList = map[int]func(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error{
|
var PayWayList = map[int]func(commonPayInfo *paymentService.PayOrderRequest, ExtJson string) error{
|
||||||
common.PAY_CHANNEL_WECHAT_H5: WechatH5,
|
common.PAY_CHANNEL_WECHAT_H5: WechatH5,
|
||||||
common.PAY_CHANNEL_ALIPAY_WEB: AlipayWeb,
|
common.PAY_CHANNEL_ALIPAY_WEB: AlipayWeb,
|
||||||
}
|
}
|
||||||
|
|
||||||
func WechatH5(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error {
|
func WechatH5(commonPayInfo *paymentService.PayOrderRequest, ExtJson string) error {
|
||||||
err := sonic.Unmarshal([]byte(channel.ExtJson), &commonPayInfo.Wx)
|
err := sonic.Unmarshal([]byte(ExtJson), &commonPayInfo.Wx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
commonPayInfo.Wx.AppId = channel.AppId
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AlipayWeb(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error {
|
func AlipayWeb(commonPayInfo *paymentService.PayOrderRequest, ExtJson string) error {
|
||||||
err := sonic.Unmarshal([]byte(channel.ExtJson), &commonPayInfo.Ali)
|
err := sonic.Unmarshal([]byte(ExtJson), &commonPayInfo.Ali)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
commonPayInfo.Ali.AppId = channel.AppId
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ type OrderNotifySendContent struct {
|
||||||
OrderId int64 `json:"order_id"`
|
OrderId int64 `json:"order_id"`
|
||||||
OutTreadNo string `json:"out_tread_no"`
|
OutTreadNo string `json:"out_tread_no"`
|
||||||
CompleteTime time.Time `json:"complete_time"`
|
CompleteTime time.Time `json:"complete_time"`
|
||||||
OrderType int `json:"order-type"`
|
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
ErrCode int `json:"err_code"`
|
ErrCode int `json:"err_code"`
|
||||||
|
@ -94,6 +93,7 @@ func (o *OrderNotify) handle() (res *OrderNotifyResp) {
|
||||||
func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) {
|
func (o *OrderNotify) sendNotify(body *OrderNotifySendContent) {
|
||||||
|
|
||||||
bodyByte, _ := sonic.Marshal(&body)
|
bodyByte, _ := sonic.Marshal(&body)
|
||||||
|
|
||||||
headers := make(map[string]string, 1)
|
headers := make(map[string]string, 1)
|
||||||
headers["Content-Type"] = "application/json"
|
headers["Content-Type"] = "application/json"
|
||||||
resByte, err := httpclient.FastHttpPost(o.app.NotifyUrl, headers, bodyByte, 0)
|
resByte, err := httpclient.FastHttpPost(o.app.NotifyUrl, headers, bodyByte, 0)
|
||||||
|
@ -109,9 +109,8 @@ func (o *OrderNotify) setBody() *OrderNotifySendContent {
|
||||||
OutTreadNo: o.order.OutTreadNo,
|
OutTreadNo: o.order.OutTreadNo,
|
||||||
CompleteTime: o.CompleteTime,
|
CompleteTime: o.CompleteTime,
|
||||||
Status: o.order.Status,
|
Status: o.order.Status,
|
||||||
OrderType: o.order.OrderType,
|
|
||||||
Msg: o.Msg,
|
Msg: o.Msg,
|
||||||
ErrCode: o.Status,
|
ErrCode: o.code,
|
||||||
AppId: o.order.AppId,
|
AppId: o.order.AppId,
|
||||||
ChannelId: o.order.PayChannelId,
|
ChannelId: o.order.PayChannelId,
|
||||||
MerchantId: o.order.MerchantId,
|
MerchantId: o.order.MerchantId,
|
||||||
|
@ -119,11 +118,11 @@ func (o *OrderNotify) setBody() *OrderNotifySendContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OrderNotify) updateOrder() {
|
func (o *OrderNotify) updateOrder() {
|
||||||
if _, ok := common.OrderStatusMap[o.Status]; !ok {
|
if o.code != errorcode.Success {
|
||||||
o.code = errorcode.OrderStatusErr
|
o.order.Status = common.ORDER_STATUS_FAILED
|
||||||
return
|
} else {
|
||||||
|
o.order.Status = common.ORDER_STATUS_PAYED
|
||||||
}
|
}
|
||||||
o.order.Status = o.Status
|
|
||||||
o.code = services.OrderUpdate(o.order, "status")
|
o.code = services.OrderUpdate(o.order, "status")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,35 +3,37 @@ package thirdpay
|
||||||
import (
|
import (
|
||||||
"PaymentCenter/app/constants/errorcode"
|
"PaymentCenter/app/constants/errorcode"
|
||||||
"PaymentCenter/app/http/entities/front"
|
"PaymentCenter/app/http/entities/front"
|
||||||
|
"PaymentCenter/app/models/ordersmodel"
|
||||||
"PaymentCenter/app/services"
|
"PaymentCenter/app/services"
|
||||||
thirdpay "PaymentCenter/app/services/thirdpay/do"
|
thirdpay "PaymentCenter/app/services/thirdpay/do"
|
||||||
|
"PaymentCenter/app/services/thirdpay/types"
|
||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ThirdPayWeb(check *thirdpay.PayCheck) *thirdpay.Pay {
|
func ThirdPayWeb(check *thirdpay.PayCheck) *thirdpay.Pay {
|
||||||
pay := thirdpay.NewPay(check)
|
pay := thirdpay.NewPay(check)
|
||||||
// 创建订单
|
// 创建订单
|
||||||
if &check.OldOrder != nil {
|
|
||||||
pay.CreateOrder()
|
pay.CreateOrder()
|
||||||
if pay.PayCode != errorcode.Success {
|
if pay.PayCode != errorcode.Success {
|
||||||
return pay
|
return pay
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
pay.Order = check.OldOrder
|
|
||||||
}
|
|
||||||
// 支付
|
// 支付
|
||||||
pay.PayUrl()
|
pay.Pay()
|
||||||
return pay
|
return pay
|
||||||
}
|
}
|
||||||
|
|
||||||
func ThirdPayInfoCheck(ctx context.Context, req *front.PayReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
|
func ThirdPayCheck(ctx context.Context, req *front.PayReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
|
||||||
check = thirdpay.NewPayCheck(&ctx, req, appCheck, ip)
|
check = thirdpay.NewPayCheck(&ctx, req, appCheck, ip)
|
||||||
// 校验表单
|
// 校验表单
|
||||||
check.CheckForm()
|
check.CheckForm()
|
||||||
if check.CheckCode != errorcode.Success {
|
if check.CheckCode != errorcode.Success {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// 校验订单
|
||||||
|
check.CheckOrder()
|
||||||
|
if check.CheckCode != errorcode.Success {
|
||||||
|
return
|
||||||
|
}
|
||||||
// 校验商户
|
// 校验商户
|
||||||
check.CheckMerchant()
|
check.CheckMerchant()
|
||||||
if check.CheckCode != errorcode.Success {
|
if check.CheckCode != errorcode.Success {
|
||||||
|
@ -45,3 +47,16 @@ func ThirdPayInfoCheck(ctx context.Context, req *front.PayReqs, appCheck *servic
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PayCallBack(order *ordersmodel.Orders, isNew bool) *types.PayResp {
|
||||||
|
res := &types.PayResp{
|
||||||
|
OrderNo: order.Id,
|
||||||
|
OrderType: order.OrderType,
|
||||||
|
Amount: order.Amount,
|
||||||
|
Desc: order.Desc,
|
||||||
|
IsNewCreate: isNew,
|
||||||
|
Status: order.Status,
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
|
@ -1,93 +0,0 @@
|
||||||
package aes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"crypto/aes"
|
|
||||||
"crypto/cipher"
|
|
||||||
"crypto/rand"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"math/big"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
TestKey = "ROK0nAaxcsI0KRCy8VJgLlnlfjWYAOHk"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 加密函数
|
|
||||||
func Encrypt(plaintext, key []byte) ([]byte, error) {
|
|
||||||
block, err := aes.NewCipher(key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
|
||||||
iv := ciphertext[:aes.BlockSize]
|
|
||||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mode := cipher.NewCBCEncrypter(block, iv)
|
|
||||||
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
|
|
||||||
|
|
||||||
return iv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解密函数
|
|
||||||
func Decrypt(ciphertext, key []byte) ([]byte, error) {
|
|
||||||
block, err := aes.NewCipher(key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ciphertext) < aes.BlockSize {
|
|
||||||
return nil, fmt.Errorf("ciphertext too short")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取iv
|
|
||||||
iv := ciphertext[:aes.BlockSize]
|
|
||||||
ciphertext = ciphertext[aes.BlockSize:]
|
|
||||||
|
|
||||||
// 创建一个cipher.BlockMode
|
|
||||||
mode := cipher.NewCBCDecrypter(block, iv)
|
|
||||||
|
|
||||||
// 解密
|
|
||||||
mode.CryptBlocks(ciphertext, ciphertext)
|
|
||||||
|
|
||||||
// 去除PKCS#7填充
|
|
||||||
plaintext, err := pkcs7Unpadding(ciphertext, block.BlockSize())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return plaintext, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PKCS#7填充
|
|
||||||
func pkcs7Padding(ciphertext []byte, blockSize int) []byte {
|
|
||||||
padding := blockSize - len(ciphertext)%blockSize
|
|
||||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
||||||
return append(ciphertext, padtext...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 去除PKCS#7填充
|
|
||||||
func pkcs7Unpadding(ciphertext []byte, blockSize int) ([]byte, error) {
|
|
||||||
length := len(ciphertext)
|
|
||||||
unpadding := int(ciphertext[length-1])
|
|
||||||
if unpadding > blockSize || unpadding == 0 {
|
|
||||||
return nil, fmt.Errorf("invalid padding")
|
|
||||||
}
|
|
||||||
return ciphertext[:(length - unpadding)], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateRandomStringCrypto(length int) (string, error) {
|
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
||||||
var bytes = make([]byte, length)
|
|
||||||
for i := range bytes {
|
|
||||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
bytes[i] = charset[num.Int64()]
|
|
||||||
}
|
|
||||||
return string(bytes), nil
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
package aes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCreateKey(t *testing.T) {
|
|
||||||
key, _ := GenerateRandomStringCrypto(32)
|
|
||||||
fmt.Printf(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAesEncrypt(t *testing.T) {
|
|
||||||
|
|
||||||
fmt.Printf("%s\n", encrypt())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAesDecrypt(t *testing.T) {
|
|
||||||
data := "dk9trGQn2kZyE0rmfYglxwNmr7gc0DGFWvnPDVpLHfzwPiaWKkr+ZffKu0wj9/DQurIgjrQZIW8uV5/e+jxzBE6+5YZuKcT9+5xZSv6ieiXB7WUrKKnjxX/bHEaWgfoa0eDqEBhGth+mgql7qWFZLmuXpoKuAc0FPhdHTiynAttqjNMU4rMeXq52yJLuXrKHGlavhd9DETNDrhPFzyIAw7XjoMfGtBIFkXyDK5X+AKGkRvMJ8KfXR3lk9sSzTyGKRK/S"
|
|
||||||
|
|
||||||
res, err := Decrypt([]byte(data), []byte(TestKey))
|
|
||||||
fmt.Println(string(res), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encrypt() string {
|
|
||||||
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}"
|
|
||||||
|
|
||||||
en, err := Encrypt([]byte(data), []byte(TestKey))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(en)
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
package rsa
|
package rsa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"PaymentCenter/app/utils/encrypt/aes"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -14,6 +13,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRsaEncrypt(t *testing.T) {
|
func TestRsaEncrypt(t *testing.T) {
|
||||||
|
|
||||||
fmt.Printf("%s\n", encrypt())
|
fmt.Printf("%s\n", encrypt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,37 +27,7 @@ func TestRsaDecrypt(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func encrypt() string {
|
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)
|
|
||||||
pub := `-----BEGIN PUBLIC KEY-----
|
|
||||||
` + PUB + `
|
|
||||||
-----END PUBLIC KEY-----`
|
|
||||||
en, err := Encrypt(pub, dataJson)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return base64.StdEncoding.EncodeToString(en)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRsaEncryptWithAes(t *testing.T) {
|
|
||||||
|
|
||||||
fmt.Printf("%s\n", encryptWithAes())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRsaDecryptWithAes(t *testing.T) {
|
|
||||||
data := "cGuoR6Bmmm9fZr/OAnM54eM6Z7M4ysnRr7TV64rIK6mAkGDzJROwSOY443e7UJmLpwIEn8G5jNk6j8K1scxvIMMdSJZ0QOJREjgzbZfNfuXV0LO1lVNu3uhwVQXN/zMZxBHGIcYQnPWSaOHhNy6yMRPLFNRuIb5FuTx7E6VI5UVZpHk9VLv63QX+6+hQMOiqoif/YyXkAqi2xG+unq4MVq9w5aYMOVzHX1eyMiTeRFRB4iKhf6bCJmVvLMmvjHYKQl3/225R4uXaI8nv7y4IHnK8KVYAnuJE6SvEPeJAjpINrY2CRoNqTYkt7DRS2uz5l7aE+KX8GShV2XlDoM8KZA=="
|
|
||||||
privateKeyPEM := `-----BEGIN RSA PRIVATE KEY-----
|
|
||||||
` + PRI + `
|
|
||||||
-----END RSA PRIVATE KEY-----`
|
|
||||||
res, err := Decrypt(privateKeyPEM, data)
|
|
||||||
fmt.Println(string(res), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}"
|
||||||
aes.Encrypt([]byte(data), []byte(aes.TestKey))
|
|
||||||
dataJson := []byte(data)
|
dataJson := []byte(data)
|
||||||
pub := `-----BEGIN PUBLIC KEY-----
|
pub := `-----BEGIN PUBLIC KEY-----
|
||||||
` + PUB + `
|
` + PUB + `
|
||||||
|
|
Loading…
Reference in New Issue