diff --git a/app/constants/errorcode/error_code.go b/app/constants/errorcode/error_code.go index 3a10b36..caf393e 100644 --- a/app/constants/errorcode/error_code.go +++ b/app/constants/errorcode/error_code.go @@ -73,8 +73,9 @@ const ( //回调 NotifySendFail = 1600 - //预支付 - PrePayFail = 1701 + //订单结果 + PrePayFail = 1701 + PreRefundFail = 1702 ) var MsgEN = map[int]string{ diff --git a/app/http/controllers/front/api.go b/app/http/controllers/front/api.go index f60f770..aa9d660 100644 --- a/app/http/controllers/front/api.go +++ b/app/http/controllers/front/api.go @@ -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) diff --git a/app/http/entities/front/pay.go b/app/http/entities/front/pay.go index d73e193..5e0ce7a 100644 --- a/app/http/entities/front/pay.go +++ b/app/http/entities/front/pay.go @@ -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"` diff --git a/app/http/middlewares/base.go b/app/http/middlewares/base.go index b8b0c86..0f80b8e 100644 --- a/app/http/middlewares/base.go +++ b/app/http/middlewares/base.go @@ -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 diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index 7548886..5acf719 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -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{}{ diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 345e11a..a695f00 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -60,6 +60,7 @@ func RegisterRoute(router *gin.Engine) { { pay.POST("/url", front.PayUrl) pay.POST("/query", front.QueryOrder) //查询订单 + pay.POST("/refund", front.Refund) } } diff --git a/app/services/apicrypt/sm2.go b/app/services/apicrypt/sm2.go index 6c89625..6d01139 100644 --- a/app/services/apicrypt/sm2.go +++ b/app/services/apicrypt/sm2.go @@ -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 } diff --git a/app/services/pay_channel.go b/app/services/pay_channel.go index fd88eb6..29beab0 100644 --- a/app/services/pay_channel.go +++ b/app/services/pay_channel.go @@ -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 } diff --git a/app/services/thirdpay/do/pay.go b/app/services/thirdpay/do/pay.go index 25f5f7e..1a3e85f 100644 --- a/app/services/thirdpay/do/pay.go +++ b/app/services/thirdpay/do/pay.go @@ -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 @@ -34,11 +38,11 @@ func (w *Pay) CreateOrder(order_type int) { MerchantId: w.paycheck.Merchant.Id, PayChannelId: w.paycheck.Channel.Id, AppId: w.paycheck.Channel.Id, - OutTreadNo: w.paycheck.WebPayReqs.OutTradeNo, + 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 +} diff --git a/app/services/thirdpay/do/pay_check.go b/app/services/thirdpay/do/pay_check.go index daa9a57..52a0170 100644 --- a/app/services/thirdpay/do/pay_check.go +++ b/app/services/thirdpay/do/pay_check.go @@ -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 } diff --git a/app/services/thirdpay/do/refund_way.go b/app/services/thirdpay/do/refund_way.go new file mode 100644 index 0000000..b43e4b9 --- /dev/null +++ b/app/services/thirdpay/do/refund_way.go @@ -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 +} diff --git a/app/services/thirdpay/pay.go b/app/services/thirdpay/pay.go index 6f6d41f..4b1d521 100644 --- a/app/services/thirdpay/pay.go +++ b/app/services/thirdpay/pay.go @@ -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 } diff --git a/app/services/thirdpay/types/callback.go b/app/services/thirdpay/types/callback.go deleted file mode 100644 index aab4b4a..0000000 --- a/app/services/thirdpay/types/callback.go +++ /dev/null @@ -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"` -} diff --git a/app/services/thirdpay/types/types.go b/app/services/thirdpay/types/types.go new file mode 100644 index 0000000..dcf4e1a --- /dev/null +++ b/app/services/thirdpay/types/types.go @@ -0,0 +1,8 @@ +package types + +import "PaymentCenter/app/http/entities/front" + +type Reqs struct { + front.PayCommonReqBody + OutTradeNo string `json:"out_trade_no"` +} diff --git a/app/third/openapiService/openapi_service.go b/app/third/openapiService/openapi_service.go deleted file mode 100644 index ba5c569..0000000 --- a/app/third/openapiService/openapi_service.go +++ /dev/null @@ -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 -} diff --git a/app/third/paymentService/payment_service.go b/app/third/paymentService/payment_service.go index 2ba409a..266f549 100644 --- a/app/third/paymentService/payment_service.go +++ b/app/third/paymentService/payment_service.go @@ -39,14 +39,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"` diff --git a/app/utils/encrypt/rsa/rsa_test.go b/app/utils/encrypt/rsa/rsa_test.go index ec149a3..46e1c60 100644 --- a/app/utils/encrypt/rsa/rsa_test.go +++ b/app/utils/encrypt/rsa/rsa_test.go @@ -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)) diff --git a/app/utils/encrypt/sm2/sm2_test.go b/app/utils/encrypt/sm2/sm2_test.go index 5397fb1..6c7fc06 100644 --- a/app/utils/encrypt/sm2/sm2_test.go +++ b/app/utils/encrypt/sm2/sm2_test.go @@ -6,7 +6,7 @@ import ( ) const ( - SELF_PRI = "EA7CB6F907A96264D6763F8C46AB96B476538D2ABC880A459E10BE5A1C30013D" + SELF_PRI = "BD13D44C74422F80ADDF54AD2DE2888C4092AF6F61E41FABADA6C47F17574A41" SELF_PUB = "04363DF574D4FE34EE58FB8A3F7CB08E6CA5EBB3B7335CBAE10A2900551F6450AB3AD25DBC0A76EFA9E6D44D2C51E3027483F7BFD09996457888BAFD1AF673817F" ) @@ -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) diff --git a/go.mod b/go.mod index fdcd3e0..8f8235e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 1bf415f..ac6cc1d 100644 --- a/go.sum +++ b/go.sum @@ -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=