From 0516eb2a3b527e5a22039fa750d98e3fe9b9fb3f Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 26 Sep 2023 10:16:50 +0800 Subject: [PATCH] update --- app/customer/hdl/hdl_demo.go | 23 ++++++++++++ app/enum/enum_sys_cfg.go | 9 +++++ app/router/customer_router.go | 1 + app/svc/svc_wx_pay.go | 38 +++++++++++++++++++ go.mod | 55 +++++++++++++++++++++------- static/wx/merchant/apiclient_key.pem | 28 ++++++++++++++ 6 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 app/svc/svc_wx_pay.go create mode 100644 static/wx/merchant/apiclient_key.pem diff --git a/app/customer/hdl/hdl_demo.go b/app/customer/hdl/hdl_demo.go index b2e95d0..300ce95 100644 --- a/app/customer/hdl/hdl_demo.go +++ b/app/customer/hdl/hdl_demo.go @@ -6,10 +6,12 @@ import ( "applet/app/customer/md" "applet/app/customer/svc" "applet/app/e" + svc3 "applet/app/svc" "context" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/device" "github.com/gin-gonic/gin" + "github.com/wechatpay-apiv3/wechatpay-go/services/certificates" "io/ioutil" ) @@ -31,6 +33,27 @@ func CurlAlipayPlanetEcocampusApiRosterSignUpInfo(c *gin.Context) { return } +func DownloadCertificates(c *gin.Context) { + client, err := svc3.NewWxPayClient(c) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + // 发送请求,以下载微信支付平台证书为例 + apiSvc := certificates.CertificatesApiService{Client: client} + resp, result, err := apiSvc.DownloadCertificates(c) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + + println("status=%d resp=%s", result.Response.StatusCode, resp) + e.OutSuc(c, map[string]interface{}{ + "resp": resp, + }, nil) + return +} + func Demo(c *gin.Context) { ordNo := c.DefaultQuery("ord_no", "") svc2.JudgePackageOrdOrdState(ordNo) diff --git a/app/enum/enum_sys_cfg.go b/app/enum/enum_sys_cfg.go index 0312ffb..0604d2c 100644 --- a/app/enum/enum_sys_cfg.go +++ b/app/enum/enum_sys_cfg.go @@ -33,6 +33,9 @@ const ( JsapiPayNotifyUrl = "jsapi_pay_notify_url" WxAppletAppId = "wx_applet_app_id" WxAppletAppSecret = "wx_applet_app_secret" + WxMchId = "wx_mch_id" + WxMchApiV3Key = "wx_mch_api_v3_key" + WxMchCertificateSerialNumber = "wx_mch_certificate_serial_number" ) func (gt SysCfg) String() string { @@ -93,6 +96,12 @@ func (gt SysCfg) String() string { return "微信-小程序-appid" case WxAppletAppSecret: return "微信-小程序-appSecret" + case WxMchId: + return "微信商户id" + case WxMchApiV3Key: + return "微信商户apiV3密钥" + case WxMchCertificateSerialNumber: + return "微信商户证书序列号" default: return "未知" } diff --git a/app/router/customer_router.go b/app/router/customer_router.go index 52d5e2e..5d4a16e 100644 --- a/app/router/customer_router.go +++ b/app/router/customer_router.go @@ -17,6 +17,7 @@ func CustomerInit(r *gin.RouterGroup) { r.GET("/sanHu", hdl.SanHu) r.POST("/test", hdl.Demo) + r.POST("/downloadCertificates", hdl.DownloadCertificates) //下载微信支付平台证书 r.POST("/curlAlipayPlanetEcocampusApiRosterSignUpInfoTest", hdl.CurlAlipayPlanetEcocampusApiRosterSignUpInfo) r.POST("/alipayJsApiCallBack", hdl.AlipayJsApiCallBack) diff --git a/app/svc/svc_wx_pay.go b/app/svc/svc_wx_pay.go new file mode 100644 index 0000000..75f77f7 --- /dev/null +++ b/app/svc/svc_wx_pay.go @@ -0,0 +1,38 @@ +package svc + +import ( + "applet/app/db" + "applet/app/enum" + "github.com/gin-gonic/gin" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/core/option" + "github.com/wechatpay-apiv3/wechatpay-go/utils" +) + +func NewWxPayClient(ctx *gin.Context) (client *core.Client, err error) { + sysCfgDb := db.SysCfgDb{} + sysCfgDb.Set() + sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxMchId, enum.WxMchApiV3Key, enum.WxMchCertificateSerialNumber) + + var ( + mchID string = sysCfg[enum.WxMchId] // 商户号 + mchCertificateSerialNumber string = sysCfg[enum.WxMchCertificateSerialNumber] // 商户证书序列号 + mchAPIv3Key string = sysCfg[enum.WxMchApiV3Key] // 商户APIv3密钥 + ) + + // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名 + mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./static/wx/merchant/apiclient_key.pem") + if err != nil { + return + } + + // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力 + opts := []core.ClientOption{ + option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key), + } + client, err = core.NewClient(ctx, opts...) + if err != nil { + return + } + return +} diff --git a/go.mod b/go.mod index 21a755e..b31d556 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module applet -go 1.15 +go 1.18 require ( github.com/360EntSecGroup-Skylar/excelize v1.4.1 @@ -14,27 +14,56 @@ require ( github.com/go-playground/validator/v10 v10.4.2 github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.7.1 - github.com/golang/protobuf v1.5.2 // indirect github.com/gomodule/redigo v2.0.0+incompatible - github.com/kr/text v0.2.0 // indirect - github.com/leodido/go-urn v1.2.1 // indirect github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831 github.com/mcuadros/go-defaults v1.2.0 - github.com/medivhzhan/weapp/v2 v2.5.0 // indirect - github.com/onsi/ginkgo v1.15.0 // indirect - github.com/onsi/gomega v1.10.5 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/medivhzhan/weapp/v2 v2.5.0 github.com/qiniu/api.v7/v7 v7.8.2 github.com/robfig/cron/v3 v3.0.1 github.com/sony/sonyflake v1.0.0 github.com/syyongx/php2go v0.9.4 - github.com/ugorji/go v1.2.5 // indirect - go.uber.org/multierr v1.6.0 // indirect + github.com/wechatpay-apiv3/wechatpay-go v0.2.16 go.uber.org/zap v1.16.0 - golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect - golang.org/x/net v0.7.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/yaml.v2 v2.4.0 - honnef.co/go/tools v0.0.1-2020.1.4 // indirect xorm.io/xorm v1.3.2 ) + +require ( + github.com/chromedp/cdproto v0.0.0-20230802225258-3cf4e6d46a89 // indirect + github.com/chromedp/sysutil v1.0.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.2.1 // indirect + github.com/goccy/go-json v0.8.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/snappy v0.0.4 // indirect + github.com/gookit/color v1.3.6 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect + github.com/onsi/ginkgo v1.15.0 // indirect + github.com/onsi/gomega v1.10.5 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/syndtr/goleveldb v1.0.0 // indirect + github.com/ugorji/go/codec v1.2.5 // indirect + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.6.0 // indirect + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect + golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/text v0.7.0 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + google.golang.org/protobuf v1.26.0 // indirect + honnef.co/go/tools v0.0.1-2020.1.4 // indirect + xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect +) diff --git a/static/wx/merchant/apiclient_key.pem b/static/wx/merchant/apiclient_key.pem new file mode 100644 index 0000000..af3ca3f --- /dev/null +++ b/static/wx/merchant/apiclient_key.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDGgwnKlRY+VV+O +JPsHjOMqwD5xUmX5aEZyI/LwL4xG7eRntSRh+iEGZFHAhS14oaSUokPeeJBB3UIP +UTvGzANQysLF7n52K9MCst2rzEdqP2eCIZZxzdr78AbnSqP41CbDaEOG8tBtAYg1 +AWUA8vej8BpyPS66UeoL8cZ6ymHW3eT9lUno6jmQ6U+aZDKDKVoXNMdlVr9E3TWy +fa9FI63nzvXu3XKftrfXkupAE+KDdHS0cyYHWHJrRSSfqR3ibSeXJILs0fZbUWtz +JdbZJhMqZ7oGlxFb06kvAEkHUmfbBKBvqMUjWoBDYfdT2EfAYQukXBXk4qjn1HP6 +OSsMMpMXAgMBAAECggEAFscyeGxjAQQK0uSraVJhPqaQg7BQwy2T58T4O1c5YWoq +qzu90QBCidk5CD4/2XeyyttTrlNnOieJTPilQ0pllv+tGckj+atNqyGbDfuCtk5Q +Mj5oZOaaGg6QvDl0VzkAuatdFD8Ia3mhh3OBg3pdsBH89t7OXdLIaFZ1z+EpGN5p +hS9j/pL63/ND/zbUsTinXlvnki5T55TPJQgSTG9Kaa9bFg4ovcPTjceKlgx5CbXO +0qp+2AAM5JKSFry1uLnaata8lgHAb2trki8xWp8nLV3SvIHY67owVacy44SnXcqw +/lU0RrQDWAjhCDsnbYUcIeV+2VzAcrPZEZXJ82ETAQKBgQDo2UewxxcbfUNUcJnn +OW7hHliRpjUY//N2/G7uiBskVrRxdJxSihVPcnvWqQbu6BXopfs6jpBXAic9N9i9 +ONlMfNIFHsiSeRUw+sMJ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