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

This commit is contained in:
陈俊宏 2024-08-08 17:25:58 +08:00
commit f6bdfdf84c
27 changed files with 230 additions and 291 deletions

View File

@ -82,11 +82,10 @@ func closeOrder() {
// 发起关闭订单请求
response := paymentService.OrderClose(ctx, req)
// 成功
if response.Code == payCommon.PAY_SUCCESS_CODE {
orderIds = append(orderIds, orderInfo.Id)
} else {
utils.Log(nil, "关闭订单,上游失败", response)
if response.Code != payCommon.PAY_SUCCESS_CODE {
utils.Log(nil, "关闭订单,上游失败", response, orderInfo.Id)
}
orderIds = append(orderIds, orderInfo.Id)
}
// 修改订单状态为关闭
cond = builder.NewCond()

View File

@ -48,6 +48,7 @@ const (
AppSM4EncryptKeyNotFound = 1232
AppSM4EncryptFail = 1233
AppAesEncryptFail = 1234
AppDeEncryptFail = 1250
// 加密方式不存在
EncryptTypeNotFound = 1241
@ -73,8 +74,9 @@ const (
//回调
NotifySendFail = 1600
//预支付
PrePayFail = 1701
//订单结果
PrePayFail = 1701
PreRefundFail = 1702
)
var MsgEN = map[int]string{
@ -120,6 +122,8 @@ var MsgZH = map[int]string{
EncryptTypeNotFound: "加密方式不存在",
AppDeEncryptFail: "未知原因导致解密失败请检查加密数据是和app加密配置",
PayChannelNotFound: "支付方式不存在",
PayChannelNotBuild: "支付方式尚未开通",
PayChannelExtJsonError: "支付方式扩展参数错误",

View File

@ -21,7 +21,7 @@ func PayUrl(c *gin.Context) {
controllers.ApiRes(c, nil, check.CheckCode)
return
}
check.CheckOrder()
check.CheckOrderPay()
if check.CheckCode != errorcode.Success {
controllers.ApiRes(c, nil, check.CheckCode)
return
@ -44,6 +44,24 @@ func PayUrl(c *gin.Context) {
return
}
func Refund(c *gin.Context) {
req := controllers.GetRequest(c).(*front.RefundReqs)
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)
return
}
// 查询订单
func QueryOrder(c *gin.Context) {
req := controllers.GetRequest(c).(*front.QueryReqs)

View File

@ -12,15 +12,25 @@ type RequestBody struct {
Key string `json:"key" validate:"max=32"`
}
type PayReqs struct {
type PayCommonReqBody struct {
ApiCommonBody
PayChannelId int64 `json:"pay_channel_id" validate:"required" label:"支付渠道"`
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
Amount int `json:"amount" validate:"required" label:"支付金额,单位分"`
ExtJson string `json:"ext_json" label:"扩展参数"`
Desc string `json:"desc" validate:"max=100" label:"商品描述"`
}
type PayReqs struct {
PayCommonReqBody
OutTradeNo string `json:"out_trade_no" validate:"required" label:"外侧商户订单号"`
}
type RefundReqs struct {
PayCommonReqBody
OutTradeNo string `json:"out_trade_no" label:"外侧商户订单号"`
OrderId string `json:"order_id" label:"平台订单号"`
}
type PayUrlResp struct {
Order string `json:"order"`
Url string `json:"url"`

View File

@ -147,7 +147,6 @@ func ValidatePayRequest() gin.HandlerFunc {
//获取app信息
appCheck := services.GetAppCheck(requestDataStruct.AppId, c.ClientIP())
//存入请求记录
if appCheck.Code != errorcode.Success {
controllers.ApiRes(c, nil, appCheck.Code)
return

View File

@ -6,7 +6,8 @@ import (
)
var FrontRequestMap = map[string]func() interface{}{
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.PayReqs) },
common.FRONT_V1 + "/pay/url": func() interface{} { return new(front.PayReqs) },
common.FRONT_V1 + "/pay/refund": func() interface{} { return new(front.RefundReqs) },
}
var FrontRequestMapBeforeDecrypt = map[string]func() interface{}{

View File

@ -60,6 +60,7 @@ func RegisterRoute(router *gin.Engine) {
{
pay.POST("/url", front.PayUrl)
pay.POST("/query", front.QueryOrder) //查询订单
pay.POST("/refund", front.Refund)
}
}

View File

@ -42,10 +42,7 @@ func (a *AppCheck) Check() *AppCheck {
a.Code = errorcode.AppDisabled
return a
}
if !a.App.DeleteTime.IsZero() {
a.Code = errorcode.AppNotFound
return a
}
return a
}

View File

@ -18,7 +18,7 @@ func (r *SM2) Encrypt(data []byte) (encryptData []byte, errCode int) {
return nil, errorcode.AppSM2EncryptKeyNotFound
}
encryptDataString, err := sm2.SM2Encrypt(string(data), r.App.PrivateKey)
encryptDataString, err := sm2.SM2Encrypt(string(data), r.App.MerchantPublicKey)
if err != nil {
return nil, errorcode.AppSM2EncryptFail
}

View File

@ -87,8 +87,5 @@ func GetAndCheckMerchant(merchant *merchantmodel.Merchant, conn builder.Cond, co
return nil, code
}
if !merchantInfo.DeleteTime.IsZero() {
return nil, errorcode.MerchantNotFound
}
return
}

View File

@ -100,7 +100,7 @@ func PayChannelFindOne(channel *paychannelmodel.PayChannel, conn builder.Cond, c
channelInfo, err := repo.PayChannelFindOne(channel, conn, col...)
if err != nil {
if err == sql.ErrNoRows {
return nil, errorcode.MerchantNotFound
return nil, errorcode.PayChannelNotFound
}
return nil, errorcode.SystemError
}
@ -112,8 +112,6 @@ func GetAndCheckPayChannel(channel *paychannelmodel.PayChannel, conn builder.Con
if code != errorcode.Success {
return nil, code
}
if !channelInfo.DeleteTime.IsZero() {
return nil, errorcode.PayChannelNotFound
}
return
}

View File

@ -43,6 +43,9 @@ func DeCrypt(app *appmodel.App, data string, aesKey string) ([]byte, int) {
if errCode != apicrypt.CryptNotError {
return nil, errCode
}
if len(dataByte) == 0 {
return nil, errorcode.AppDeEncryptFail
}
//aesData, err := aes.Decrypt(dataByte, []byte(aesKey))
//if err != nil {
// return nil, errorcode.AppAesEncryptFail

View File

@ -3,6 +3,7 @@ package do
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/models/merchantmodel"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/third/paymentService/payCommon"
@ -12,6 +13,9 @@ import (
)
type Pay struct {
Merchant *merchantmodel.Merchant
Channel *paychannelmodel.PayChannel
paycheck *PayCheck
Order *ordersmodel.Orders
PayCode int
@ -33,12 +37,12 @@ func (w *Pay) CreateOrder(order_type int) {
w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{
MerchantId: w.paycheck.Merchant.Id,
PayChannelId: w.paycheck.Channel.Id,
AppId: w.paycheck.Channel.Id,
OutTreadNo: w.paycheck.WebPayReqs.OutTradeNo,
AppId: w.paycheck.Reqs.AppId,
OutTreadNo: w.paycheck.Reqs.OutTradeNo,
OrderType: order_type,
Amount: w.paycheck.WebPayReqs.Amount,
ExtJson: w.paycheck.WebPayReqs.ExtJson,
Desc: w.paycheck.WebPayReqs.Desc,
Amount: w.paycheck.Reqs.Amount,
ExtJson: w.paycheck.Reqs.ExtJson,
Desc: w.paycheck.Reqs.Desc,
Status: common.ORDER_STATUS_WAITPAY,
},
)
@ -50,7 +54,7 @@ func (w *Pay) PayUrl() (url string) {
ok bool
)
thirdPay := &paymentService.PayOrderRequest{
PayChannelId: w.paycheck.WebPayReqs.PayChannelId,
PayChannelId: w.paycheck.Reqs.PayChannelId,
OrderId: w.Order.Id,
ChannelType: w.paycheck.Channel.ChannelType,
Description: w.Order.Desc,
@ -81,3 +85,37 @@ func (w *Pay) PayUrl() (url string) {
return
}
func (w *Pay) Refund() {
var (
refundFunc func(commonRefundInfo *paymentService.OrderRefundRequest, channel *paychannelmodel.PayChannel) error
ok bool
)
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,
}
if refundFunc, ok = RefundWayList[w.paycheck.Channel.ChannelType]; !ok {
w.PayCode = errorcode.PayChannelNotBuild
return
}
err := refundFunc(thirdPayRefund, w.paycheck.Channel)
if err != nil {
w.PayCode = errorcode.PayChannelExtJsonError
return
}
res := paymentService.OrderRefund(*w.paycheck.ctx, *thirdPayRefund)
if res.Code == payCommon.PAY_SUCCESS_CODE {
w.Order.Status = common.ORDER_STATUS_PAYING
code := services.OrderUpdate(w.Order, "status")
if code != errorcode.Success {
w.PayCode = code
}
} else {
w.PayCode = errorcode.PreRefundFail
}
return
}

View File

@ -4,9 +4,9 @@ import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/types"
"xorm.io/builder"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/merchantmodel"
"PaymentCenter/app/models/orderrequestlogmodel"
"PaymentCenter/app/models/ordersmodel"
@ -16,7 +16,7 @@ import (
type PayCheck struct {
ctx *context.Context
WebPayReqs *front.PayReqs
Reqs *types.Reqs
AppCheck *services.AppCheck
RequestLog *orderrequestlogmodel.OrderRequestLog
Channel *paychannelmodel.PayChannel
@ -25,29 +25,32 @@ type PayCheck struct {
CheckCode int
}
func NewPayCheck(ctx *context.Context, reqs *front.PayReqs, appCheck *services.AppCheck, ip string) *PayCheck {
func NewPayCheck(ctx *context.Context, reqs *types.Reqs, appCheck *services.AppCheck, ip string) *PayCheck {
if appCheck == nil {
appCheck = services.GetAppCheck(reqs.AppId, ip)
}
return &PayCheck{
ctx: ctx,
WebPayReqs: reqs,
AppCheck: appCheck,
CheckCode: appCheck.Code,
ctx: ctx,
Reqs: reqs,
AppCheck: appCheck,
CheckCode: appCheck.Code,
}
}
func (w *PayCheck) CheckForm() {
//if _, ok := common.OrderTypeList[w.WebPayReqs.OrderType]; !ok { //判断是否是支持的支付渠道
// w.CheckCode = errorcode.PayChannelNotFound
// return
//}
func (w *PayCheck) CheckPayInfo() {
w.CheckMerchant()
if w.CheckCode != errorcode.Success {
return
}
w.CheckPayChannel()
if w.CheckCode != errorcode.Success {
return
}
}
func (w *PayCheck) CheckPayChannel() {
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": w.WebPayReqs.PayChannelId})
conn = conn.And(builder.Eq{"id": w.Reqs.PayChannelId})
w.Channel, w.CheckCode = services.GetAndCheckPayChannel(&paychannelmodel.PayChannel{}, conn)
}
@ -57,9 +60,45 @@ func (w *PayCheck) CheckMerchant() {
w.Merchant, w.CheckCode = services.GetAndCheckMerchant(&merchantmodel.Merchant{}, conn)
}
func (w *PayCheck) CheckOrder() {
func (w *PayCheck) CheckOrderPay() {
w.GetOrder()
if w.OldOrder != nil {
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_PAYED:
w.CheckCode = errorcode.OrderPayed
default:
}
}
return
}
func (w *PayCheck) CheckOrderRefund() {
w.GetOrder()
if w.OldOrder == nil {
w.CheckCode = errorcode.OrdersNotFound
}
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:
}
return
}
func (w *PayCheck) GetOrder() {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"out_tread_no": w.WebPayReqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId})
cond = cond.And(builder.Eq{"out_tread_no": w.Reqs.OutTradeNo}, builder.Eq{"app_id": w.AppCheck.AppId})
order, code := services.OrderFindOne(&ordersmodel.Orders{}, cond)
if code == errorcode.SystemError {
w.CheckCode = code
@ -67,15 +106,6 @@ func (w *PayCheck) CheckOrder() {
}
if code == errorcode.OrdersExist {
w.OldOrder = order
switch order.Status {
case common.ORDER_STATUS_CLOSE:
w.CheckCode = errorcode.OrderClosed
case common.ORDER_STATUS_FAILED:
w.CheckCode = errorcode.OrderFailed
case common.ORDER_STATUS_PAYED:
w.CheckCode = errorcode.OrderPayed
}
}
return
}

View File

@ -0,0 +1,31 @@
package do
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/third/paymentService"
"github.com/bytedance/sonic"
)
var RefundWayList = map[int]func(commonRefundInfo *paymentService.OrderRefundRequest, channel *paychannelmodel.PayChannel) error{
common.PAY_CHANNEL_WECHAT_H5: WechatH5Refund,
common.PAY_CHANNEL_ALIPAY_WEB: AlipayWebRefund,
}
func WechatH5Refund(commonRefundInfo *paymentService.OrderRefundRequest, channel *paychannelmodel.PayChannel) error {
err := sonic.Unmarshal([]byte(channel.ExtJson), &commonRefundInfo.Wx)
if err != nil {
return err
}
commonRefundInfo.Wx.AppId = channel.AppId
return nil
}
func AlipayWebRefund(commonRefundInfo *paymentService.OrderRefundRequest, channel *paychannelmodel.PayChannel) error {
err := sonic.Unmarshal([]byte(channel.ExtJson), &commonRefundInfo.Ali)
if err != nil {
return err
}
commonRefundInfo.Ali.AppId = channel.AppId
return nil
}

View File

@ -8,7 +8,9 @@ import (
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/api"
thirdpay "PaymentCenter/app/services/thirdpay/do"
"PaymentCenter/app/services/thirdpay/types"
"context"
"github.com/jinzhu/copier"
)
func ThirdPayUrl(check *thirdpay.PayCheck) *thirdpay.Pay {
@ -30,41 +32,35 @@ func NewOrdersResp(db *ordersmodel.Orders) *api.OrdersResp {
}
}
func ThirdPayRefund(check *thirdpay.PayCheck) *thirdpay.Pay {
pay := thirdpay.NewPay(check)
// 创建订单
if &check.OldOrder != nil {
pay.CreateOrder(common.ORDER_TYPE_PAY)
if pay.PayCode != errorcode.Success {
return pay
}
} else {
pay.Order = check.OldOrder
func ThirdPayInfoCheck(ctx context.Context, payReq *front.PayReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
var req types.Reqs
copier.Copy(&req, payReq)
check = thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
// 校验表单
check.CheckPayInfo()
if check.CheckCode != errorcode.Success {
return
}
// 支付
pay.PayUrl()
return pay
return
}
func ThirdPayInfoCheck(ctx context.Context, req *front.PayReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
check = thirdpay.NewPayCheck(&ctx, req, appCheck, ip)
func ThirdPayRefund(ctx context.Context, refundReq *front.RefundReqs, appCheck *services.AppCheck, ip string) (refund *thirdpay.Pay) {
var req types.Reqs
copier.Copy(req, refundReq)
check := thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
// 校验表单
check.CheckForm()
check.CheckPayInfo()
if check.CheckCode != errorcode.Success {
return
}
// 校验商户
check.CheckMerchant()
check.CheckOrderRefund()
if check.CheckCode != errorcode.Success {
return
}
// 校验支付通道
check.CheckPayChannel()
if check.CheckCode != errorcode.Success {
return
}
refund = thirdpay.NewPay(check)
refund.CreateOrder(common.ORDER_TYPE_REFUND)
refund.Refund()
return
}

View File

@ -133,10 +133,7 @@ func (o *OrderNotify) checkApp() {
if o.Code != errorcode.Success {
return
}
if o.app.DeleteTime.IsZero() {
o.Code = errorcode.AppDisabled
return
}
if o.app.NotifyUrl == "" {
o.Code = errorcode.AppNotifyUrlNotFound
return
@ -148,15 +145,11 @@ func (o *OrderNotify) checkApp() {
func (o *OrderNotify) checkOrder() {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"id": o.OrderId})
o.order, o.Code = services.OrderFindOne(&ordersmodel.Orders{}, cond)
if o.Code != errorcode.Success {
return
}
if o.order.DeleteTime.IsZero() {
o.Code = errorcode.OrderIsDelete
if o.Code != errorcode.OrdersExist {
return
}
if o.order.Status != common.ORDER_STATUS_PAYING {
o.Code = errorcode.OrderStatusErr
return

View File

@ -1,10 +0,0 @@
package types
type PayResp struct {
OrderNo int64 `json:"order_no"`
OrderType int `json:"out_trade_no"`
Amount int `json:"order_type"`
Desc string `json:"amount"`
IsNewCreate bool `json:"is_new_create"`
Status int `json:"status"`
}

View File

@ -0,0 +1,8 @@
package types
import "PaymentCenter/app/http/entities/front"
type Reqs struct {
front.PayCommonReqBody
OutTradeNo string `json:"out_trade_no"`
}

View File

@ -1,164 +0,0 @@
package openapiService
import (
"PaymentCenter/app/models/orderdetailsmodel"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/usercouponmodel"
"PaymentCenter/app/utils"
"PaymentCenter/config"
"context"
"crypto/aes"
"encoding/base64"
"gitee.com/chengdu_blue_brothers/openapi-go-sdk/api"
"gitee.com/chengdu_blue_brothers/openapi-go-sdk/notify"
"net/http"
"time"
)
func CreatClient() (client *api.Client, err error) {
merchantId := config.GetConf().OpenApi.MerchantId
secretKey := config.GetConf().OpenApi.SecretKey
isProd := config.GetConf().OpenApi.IsProd
timeout := 10 * time.Second // 请求超时时间
client, err = api.NewClient(merchantId, secretKey, isProd, timeout)
if err != nil {
return nil, err
}
return client, nil
}
type RechargeOrderReq struct {
OutTradeNo string
ProductId int
RechargeAccount string
AccountType int
Number int
}
func RechargeOrder(rechargeOrderReq RechargeOrderReq) (result *api.RechargeOrderResp, err error) {
req := &api.RechargeOrderReq{
OutTradeNo: rechargeOrderReq.OutTradeNo,
ProductId: rechargeOrderReq.ProductId,
RechargeAccount: rechargeOrderReq.RechargeAccount,
AccountType: 0,
Number: 1,
NotifyUrl: config.GetConf().OpenApi.NotifyUrl,
}
client, err := CreatClient()
if err != nil {
return nil, err
}
result, err = client.RechargeOrder(context.Background(), req)
if err != nil {
return nil, err
}
if result.Code != api.CodeCreateOrderSuccess {
return
}
return
}
func ReCardOrder(CardOrderReq api.CardOrderReq) (result *api.CardOrderResp, err error) {
req := &api.CardOrderReq{
OutTradeNo: CardOrderReq.OutTradeNo,
ProductId: CardOrderReq.ProductId,
AccountType: 0,
Number: 1,
NotifyUrl: config.GetConf().OpenApi.NotifyUrl,
}
client, err := CreatClient()
if err != nil {
return nil, err
}
result, err = client.CardOrder(context.Background(), req)
if err != nil {
return nil, err
}
if result.Code != api.CodeCreateOrderSuccess {
return
}
return
}
func DecryptCard(encCode string) (decode string, err error) {
defer func() error {
if r := recover(); r != nil {
utils.Log(nil, "解密错误", err)
}
return err
}()
client, err := CreatClient()
if err != nil {
return
}
decryptedCode, err := decryptAES(encCode, client.GetSecretKey())
if err != nil {
return
}
return decryptedCode, nil
}
// AES 解密
func decryptAES(encryptedData string, secretKey string) (string, error) {
// 第一步对加密的卡密做base64 decode
encryptedBytes, err := base64.StdEncoding.DecodeString(encryptedData)
if err != nil {
return "", err
}
// 第二步使用aes-256-ecb解密
cipher, _ := aes.NewCipher([]byte(secretKey))
decrypted := make([]byte, len(encryptedBytes))
size := 16
for bs, be := 0, size; bs < len(encryptedBytes); bs, be = bs+size, be+size {
cipher.Decrypt(decrypted[bs:be], encryptedBytes[bs:be])
}
paddingSize := int(decrypted[len(decrypted)-1])
return string(decrypted[0 : len(decrypted)-paddingSize]), nil
}
func ParseAndVerify(request *http.Request) (req *notify.OrderReq, err error) {
// 第一步初使化client实例
merchantId := config.GetConf().OpenApi.MerchantId
secretKey := config.GetConf().OpenApi.SecretKey
client := notify.NewNotify(merchantId, secretKey)
// 第二步:验签并解析结果
req, err = client.ParseAndVerify(request)
if err != nil {
return nil, err
}
return
}
// NotifyOperation /**
func NotifyOperation(order ordersmodel.Orders, req *notify.OrderReq) (err error) {
var session = ordersmodel.GetInstance().GetDb().NewSession()
session.Begin()
updateOrder := ordersmodel.Orders{Status: 3}
_, err = session.Where("Id = ?", order.Id).Update(&updateOrder)
if err != nil {
session.Rollback()
return
}
//卡密
if req.CardCode != "" {
//订单详情
updateOrderDetail := orderdetailsmodel.OrderDetails{Url: req.CardCode.Value()}
_, err = session.Where("OrderId = ?", order.Id).Update(&updateOrderDetail)
if err != nil {
session.Rollback()
return
}
userCouponDetail := usercouponmodel.UserCoupon{OrderInfo: req.CardCode.Value()}
_, err = session.Where("OrderId = ?", order.Id).Update(&userCouponDetail)
if err != nil {
session.Rollback()
return
}
}
session.Commit()
return
}

View File

@ -40,14 +40,6 @@ type AliPay struct {
AlipayPublicCert string `json:"alipay_public_cert"` // 支付宝公钥
}
type AliPayA struct {
AppId string `json:"app_id"` // 应用ID
PrivateKey string `json:"private_key"` // 应用私钥
AppPublicCert string `json:"app_public_cert"` // 应用公钥
AlipayRootCert string `json:"alipay_root_cert"` // 支付宝根证书
AlipayPublicCert string `json:"alipay_public_cert"` // 支付宝公钥
}
type PayOrderResponse struct {
Code int `json:"code"`
ErrorMsg string `json:"error_msg"`

View File

@ -8,6 +8,7 @@ import (
"encoding/base64"
"encoding/pem"
"fmt"
"strings"
)
// parseRSAPublicKeyFromPEM 解析PEM编码的RSA公钥
@ -136,5 +137,9 @@ func GenerateKey() (string, string, error) {
Bytes: derPkix,
}
pubPem := pem.EncodeToMemory(pubBlock)
return string(pubPem), string(privPem), nil
pri := strings.Replace(string(privPem), "-----BEGIN RSA PRIVATE KEY-----\n", "", -1)
pri = strings.Replace(pri, "\n-----END RSA PRIVATE KEY-----\n", "", -1)
pub := strings.Replace(string(pubPem), "-----BEGIN PUBLIC KEY-----\n", "", -1)
pub = strings.Replace(pub, "\n-----END PUBLIC KEY-----\n", "", -1)
return pub, pri, nil
}

View File

@ -41,20 +41,6 @@ func encrypt() string {
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}"
aes.Encrypt([]byte(data), []byte(aes.TestKey))

View File

@ -6,9 +6,9 @@ import (
)
const (
SELF_PRI = "EA7CB6F907A96264D6763F8C46AB96B476538D2ABC880A459E10BE5A1C30013D"
SELF_PRI = "BD13D44C74422F80ADDF54AD2DE2888C4092AF6F61E41FABADA6C47F17574A41"
SELF_PUB = "04363DF574D4FE34EE58FB8A3F7CB08E6CA5EBB3B7335CBAE10A2900551F6450AB3AD25DBC0A76EFA9E6D44D2C51E3027483F7BFD09996457888BAFD1AF673817F"
SELF_PUB = "04F26C6CDA5E58153999FDF3023259F16EDE0A556C39FCF4BC97C04A10A8A6CF1D52297B53DB4DD72FEEF8221394C8478E6424F4E84E4A6E2784C1A4A1C99F2DC2"
)
func TestGenerateSM2KeyPair(t *testing.T) {
@ -47,7 +47,7 @@ func TestSM2Decrypt(t *testing.T) {
}
func encrypt() string {
data := "{\"name\":\"张三\",\"sex\":1,\"is_human\":true}"
data := "{\"order_no\":5476377146882523228,\"order_type\":1,\"out_tread_no\":\"asdadasdas\",\"amount\":1,\"desc\":\"abc\",\"status\":2,\"create_time\":\"2024-08-08 14:47:35\"}"
en, err := SM2Encrypt(data, SELF_PUB)
if err != nil {
panic(err)

View File

@ -2,6 +2,7 @@ package sm4
import (
"fmt"
"strconv"
"testing"
)
@ -25,7 +26,10 @@ func TestSM4Encrypt(t *testing.T) {
}
func TestSM4Decrypt(t *testing.T) {
uid, en := encrypt()
//uid := "1729382256910270475"
//en := "{\"accessToken\":\"77F59F157466F309E703F43CFDCFFECF\",\"encryptKey\":\"04237241E4A465C18645985B3C1DAE987B5DDF1C077AA27D677C843C52E833A24711C63F2512D1A4C37E85A5C9CEAC94156C97460CB5E8966CAD2A0C2596A64ED69CF3306D031C4AADAA73D165FB7EEC34E5AAF532A301169847560329F7F1E40E9FD09EB191976BB49ABFE611E05158EF\",\"request\":\"mkuquQ1RB2AtZYzBtWtLPJ3MMULWj7I5RmK3dKENYVW8OgB//I/+MAD/XnjxYYJlRWM8uL/rWL9o\\r\\n0L7W9fSBvOXWwz8reiwcAF/JZ5dnacZtVe0NPujDfeVIJp+9ua7hxQcegcEsIRS9CQqtF/rJN7M9\\r\\nxxCLSzEiJY8bIdRLBsfmB0uVIE2144s2tH7Q8R2fSOVbiSTH1PW3Ye0kkyCcBw==\",\"signature\":\"ed99a2ce827a4d443e2639ccfb78865e913854e2d61d1746dafb640e19cbb4f8#3793b1cac082d9e1cdfc2b7b8387a8c29e44b2f3fb35b352d02f1b2c9321e0a7\"}"
decrypt, err := Sm4Decrypt(uid, SELF_PRI, PARTY_PUB, en, true)
if err != nil {
panic(err)
@ -34,8 +38,8 @@ func TestSM4Decrypt(t *testing.T) {
}
func encrypt() (string, string) {
uid := "1234567890"
data := "{\"name\":\"张三\",\"sex\":1,\"is_human\":true}"
uid := strconv.FormatInt(int64(5476377146882523149), 10)
data := "{\"pay_channel_id\":1729382256910270476,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523149,\"timestamp\":1723096731}"
en, err := Sm4Encrypt(uid, PARTY_PRI, SELF_PUB, data, "", true)
if err != nil {
panic(err)

1
go.mod
View File

@ -82,6 +82,7 @@ require (
github.com/hetiansu5/accesslog v1.0.0 // indirect
github.com/hetiansu5/cores v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect

2
go.sum
View File

@ -443,6 +443,8 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=