From df75d09be59e74fae310ac671b2f7e66eeae72fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Wed, 7 Sep 2022 12:21:01 +0800 Subject: [PATCH] - update notify decrypt --- const.go | 2 +- get.go | 4 + pay.partner.transactions.jsapi.notify.gin.go | 80 +++++++++++++++----- refund.domestic.refunds.notify.gin.go | 50 +++++++----- sign.decrypt.go | 5 ++ 5 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 sign.decrypt.go diff --git a/const.go b/const.go index 89b8ac7..9fb9be4 100644 --- a/const.go +++ b/const.go @@ -14,7 +14,7 @@ const ( // SDK 相关信息 const ( - Version = "1.0.14" // SDK 版本 + Version = "1.0.15" // SDK 版本 UserAgentFormat = "WechatPay-Go/%s (%s) GO/%s" // UserAgent中的信息 ) diff --git a/get.go b/get.go index 0a4b2fe..6c1d046 100644 --- a/get.go +++ b/get.go @@ -23,3 +23,7 @@ func (c *Client) GetMchSslKey() string { func (c *Client) GetMchSslSerialNo() string { return c.config.mchSslSerialNo } + +func (c *Client) GetApiV3() string { + return c.config.apiV3 +} diff --git a/pay.partner.transactions.jsapi.notify.gin.go b/pay.partner.transactions.jsapi.notify.gin.go index 8e5e3ed..840d35f 100644 --- a/pay.partner.transactions.jsapi.notify.gin.go +++ b/pay.partner.transactions.jsapi.notify.gin.go @@ -2,10 +2,12 @@ package wechatpayopen import ( "context" + "encoding/json" "github.com/gin-gonic/gin" ) -type ResponsePayPartnerTransactionsJsapiNotifyGin struct { +// PayPartnerTransactionsJsapiNotifyGinRequest JSAPI下单 - 回调通知 - 请求参数 +type PayPartnerTransactionsJsapiNotifyGinRequest struct { Id string `form:"id" json:"status" xml:"id" uri:"id" binding:"required"` // 通知ID CreateTime string `form:"create_time" json:"create_time" xml:"create_time" uri:"create_time" binding:"required"` // 通知创建时间 EventType string `form:"event_type" json:"event_type" xml:"event_type" uri:"event_type" binding:"required"` // 通知类型 @@ -22,25 +24,65 @@ type ResponsePayPartnerTransactionsJsapiNotifyGin struct { // PayPartnerTransactionsJsapiNotifyGin JSAPI下单 - 回调通知 // https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_5.shtml -func (c *Client) PayPartnerTransactionsJsapiNotifyGin(ctx context.Context, ginCtx *gin.Context) (ResponsePayPartnerTransactionsJsapiNotifyGin, error) { - - // 声明接收的变量 - var validateJson struct { - Id string `form:"id" json:"status" xml:"id" uri:"id" binding:"required"` // 通知ID - CreateTime string `form:"create_time" json:"create_time" xml:"create_time" uri:"create_time" binding:"required"` // 通知创建时间 - EventType string `form:"event_type" json:"event_type" xml:"event_type" uri:"event_type" binding:"required"` // 通知类型 - ResourceType string `form:"resource_type" json:"resource_type" xml:"resource_type" uri:"resource_type" binding:"required"` // 通知数据类型 - Resource struct { - Algorithm string `form:"algorithm" json:"algorithm" xml:"algorithm" uri:"algorithm" binding:"required"` // 加密算法类型 - Ciphertext string `form:"ciphertext" json:"ciphertext" xml:"ciphertext" uri:"ciphertext" binding:"required"` // 数据密文 - AssociatedData string `form:"associated_data" json:"associated_data" xml:"associated_data" uri:"associated_data" binding:"omitempty"` // 附加数据 - OriginalType string `form:"original_type" json:"original_type" xml:"original_type" uri:"original_type" binding:"required"` // 原始类型 - Nonce string `form:"nonce" json:"nonce" xml:"nonce" uri:"nonce" binding:"required"` // 随机串 - } `form:"resource" json:"resource" xml:"resource" uri:"resource" binding:"required"` // 通知数据 - Summary string `form:"summary" json:"summary" xml:"summary" uri:"summary" binding:"required"` // 回调摘要 +func (c *Client) PayPartnerTransactionsJsapiNotifyGin(ctx context.Context, ginCtx *gin.Context) (validateJson PayPartnerTransactionsJsapiNotifyGinRequest, response PayPartnerTransactionsJsapiNotifyGinResponse, gcm []byte, err error) { + + // 解析 + err = ginCtx.ShouldBind(&validateJson) + + gcm, err = c.decryptGCM(c.GetApiV3(), validateJson.Resource.Nonce, validateJson.Resource.Ciphertext, validateJson.Resource.AssociatedData) + if err != nil { + return validateJson, response, gcm, err } - err := ginCtx.ShouldBind(&validateJson) + err = json.Unmarshal(gcm, &response) + + return validateJson, response, gcm, err +} - return validateJson, err +type PayPartnerTransactionsJsapiNotifyGinResponse struct { + SpAppid string `json:"sp_appid"` // 服务商应用ID + SpMchid string `json:"sp_mchid"` // 服务商户号 + SubAppid string `json:"sub_appid,omitempty"` // 子商户应用ID + SubMchid string `json:"sub_mchid"` // 子商户号 + OutTradeNo string `json:"out_trade_no"` // 商户订单号 + TransactionId string `json:"transaction_id"` // 微信支付订单号 + TradeType string `json:"trade_type"` // 交易类型 + TradeState string `json:"trade_state"` // 交易状态 + TradeStateDesc string `json:"trade_state_desc"` // 交易状态描述 + BankType string `json:"bank_type"` // 付款银行 + Attach string `json:"attach,omitempty"` // 附加数据 + SuccessTime string `json:"success_time"` // 支付完成时间 + Payer struct { + Openid string `json:"openid"` // 用户服务标识 + SpOpenid string `json:"sp_openid,omitempty"` // 用户服务标识 + SubOpenid string `json:"sub_openid,omitempty"` // 用户子标识 + } `json:"payer"` // -支付者 + Amount struct { + Total int `json:"total"` // 总金额 + PayerTotal int `json:"payer_total"` // 用户支付金额 + Currency string `json:"currency"` // 货币类型 + PayerCurrency string `json:"payer_currency"` // 用户支付币种 + } `json:"amount"` // 订单金额 + SceneInfo struct { + DeviceId string `json:"device_id,omitempty"` //商户端设备号 + } `json:"scene_info,omitempty"` // 场景信息 + PromotionDetail []struct { + CouponId string `json:"coupon_id"` // 券ID + Name string `json:"name,omitempty"` // 优惠名称 + Scope string `json:"scope,omitempty"` // 优惠范围 + Type string `json:"type,omitempty"` // 优惠类型 + Amount int `json:"amount"` // 优惠券面额 + StockId string `json:"stock_id,omitempty"` // 活动ID + WechatpayContribute int `json:"wechatpay_contribute,omitempty"` // 微信出资 + MerchantContribute int `json:"merchant_contribute,omitempty"` // 商户出资 + OtherContribute int `json:"other_contribute,omitempty"` // 其他出资 + Currency string `json:"currency,omitempty"` // 优惠币种 + GoodsDetail []struct { + GoodsId string `json:"goods_id"` // 商品编码 + Quantity int `json:"quantity"` // 商品数量 + UnitPrice int `json:"unit_price"` // 商品单价 + DiscountAmount int `json:"discount_amount"` // 商品优惠金额 + GoodsRemark string `json:"goods_remark,omitempty"` // 商品备注 + } `json:"goods_detail,omitempty"` // 单品列表 + } `json:"promotion_detail,omitempty"` // 优惠功能 } diff --git a/refund.domestic.refunds.notify.gin.go b/refund.domestic.refunds.notify.gin.go index 1fc17d9..ea2766e 100644 --- a/refund.domestic.refunds.notify.gin.go +++ b/refund.domestic.refunds.notify.gin.go @@ -2,10 +2,12 @@ package wechatpayopen import ( "context" + "encoding/json" "github.com/gin-gonic/gin" ) -type ResponseRefundDomesticRefundsNoNotifyGin struct { +// RefundDomesticRefundsNoNotifyGinRequest 申请退款API - 回调通知 - 请求参数 +type RefundDomesticRefundsNoNotifyGinRequest struct { Id string `form:"id" json:"status" xml:"id" uri:"id" binding:"required"` // 通知ID CreateTime string `form:"create_time" json:"create_time" xml:"create_time" uri:"create_time" binding:"required"` // 通知创建时间 EventType string `form:"event_type" json:"event_type" xml:"event_type" uri:"event_type" binding:"required"` // 通知类型 @@ -22,25 +24,35 @@ type ResponseRefundDomesticRefundsNoNotifyGin struct { // RefundDomesticRefundsNoNotifyGin 申请退款API - 回调通知 // https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_11.shtml -func (c *Client) RefundDomesticRefundsNoNotifyGin(ctx context.Context, ginCtx *gin.Context) (ResponseRefundDomesticRefundsNoNotifyGin, error) { - - // 声明接收的变量 - var validateJson struct { - Id string `form:"id" json:"status" xml:"id" uri:"id" binding:"required"` // 通知ID - CreateTime string `form:"create_time" json:"create_time" xml:"create_time" uri:"create_time" binding:"required"` // 通知创建时间 - EventType string `form:"event_type" json:"event_type" xml:"event_type" uri:"event_type" binding:"required"` // 通知类型 - Summary string `form:"summary" json:"summary" xml:"summary" uri:"summary" binding:"required"` // 通知简要说明 - ResourceType string `form:"resource_type" json:"resource_type" xml:"resource_type" uri:"resource_type" binding:"required"` // 通知数据类型 - Resource struct { - Algorithm string `form:"algorithm" json:"algorithm" xml:"algorithm" uri:"algorithm" binding:"required"` // 加密算法类型 - Ciphertext string `form:"ciphertext" json:"ciphertext" xml:"ciphertext" uri:"ciphertext" binding:"required"` // 数据密文 - AssociatedData string `form:"associated_data" json:"associated_data" xml:"associated_data" uri:"associated_data" binding:"omitempty"` // 附加数据 - OriginalType string `form:"original_type" json:"original_type" xml:"original_type" uri:"original_type" binding:"required"` // 原始类型 - Nonce string `form:"nonce" json:"nonce" xml:"nonce" uri:"nonce" binding:"required"` // 随机串 - } `form:"resource" json:"resource" xml:"resource" uri:"resource" binding:"required"` // 通知数据 +func (c *Client) RefundDomesticRefundsNoNotifyGin(ctx context.Context, ginCtx *gin.Context) (validateJson RefundDomesticRefundsNoNotifyGinRequest, response RefundDomesticRefundsNoNotifyGinResponse, gcm []byte, err error) { + + // 解析 + err = ginCtx.ShouldBind(&validateJson) + + gcm, err = c.decryptGCM(c.GetApiV3(), validateJson.Resource.Nonce, validateJson.Resource.Ciphertext, validateJson.Resource.AssociatedData) + if err != nil { + return validateJson, response, gcm, err } - err := ginCtx.ShouldBind(&validateJson) + err = json.Unmarshal(gcm, &response) + + return validateJson, response, gcm, err +} - return validateJson, err +type RefundDomesticRefundsNoNotifyGinResponse struct { + SpMchid string `json:"sp_mchid"` // 服务商户号 + SubMchid string `json:"sub_mchid"` // 子商户号 + OutTradeNo string `json:"out_trade_no"` // 商户订单号 + TransactionId string `json:"transaction_id"` // 微信支付订单号 + OutRefundNo string `json:"out_refund_no"` // 商户退款单号 + RefundId string `json:"refund_id"` // 微信支付退款单号 + RefundStatus string `json:"refund_status"` // 退款状态 + SuccessTime string `json:"success_time"` // 退款成功时间 + UserReceivedAccount string `json:"user_received_account"` // 退款入账账户 + Amount struct { + Total int `json:"total"` // 订单金额 + Refund int `json:"refund"` // 退款金额 + PayerTotal int `json:"payer_total"` // 用户支付金额 + PayerRefund int `json:"payer_refund"` // 用户退款金额 + } `json:"amount"` // 金额信息 } diff --git a/sign.decrypt.go b/sign.decrypt.go new file mode 100644 index 0000000..d5e3dff --- /dev/null +++ b/sign.decrypt.go @@ -0,0 +1,5 @@ +package wechatpayopen + +func (c *Client) SignDecrypt(aesKey, associatedData, nonce, ciphertext string) ([]byte, error) { + return c.decryptGCM(aesKey, nonce, ciphertext, associatedData) +}