commit b0ec11f07230d17f621afe69006e301d9664bc3d Author: kanade Date: Wed Apr 27 16:08:52 2022 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..237037e --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# 支付 + +### 微信支付 + +```go +package main + +import ( + "context" + "git.oa00.com/go/wxpay" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/services/payments/app" + "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi" + "log" + "time" +) + +func main() { + // 初始化配置 + if err := pay.InitWxpay(pay.Config{ + MchID: "mchID", // 支付商户号 + MchAPIv3Key: "mchAPIv3Key", // apiV3密钥 + PrivateKey: "privateKey", // 私钥证书密钥 + PrivateCert: "privateCert", // 私钥证书 + }); err != nil { + log.Panicln("微信支付错误:", err) + } + + // app支付 + apiService := app.AppApiService{Client: pay.Wxpay.Client} + result, _, err := apiService.PrepayWithRequestPayment(context.Background(), app.PrepayRequest{ + Appid: core.String("appid"), + Mchid: core.String("mchID"), + Description: core.String("subject"), + OutTradeNo: core.String("payNo"), + TimeExpire: core.Time(time.Now().Add(time.Minute * time.Duration(15))), + NotifyUrl: core.String("notifyURL"), + Amount: &app.Amount{ + Total: core.Int64(int64(0.01 * 100)), + Currency: core.String("CNY"), + }, + }) + log.Println(result, err) + + // 小程序/微信公众号支付 + apiService := jsapi.JsapiApiService{Client: pay.Wxpay.Client} + result, _, err = apiService.PrepayWithRequestPayment(context.Background(), jsapi.PrepayRequest{ + Appid: core.String("appid"), + Mchid: core.String("mchID"), + Description: core.String("subject"), + OutTradeNo: core.String("payNo"), + TimeExpire: core.Time(time.Now().Add(time.Minute * time.Duration(15))), + NotifyUrl: core.String("notifyURL"), + Amount: &jsapi.Amount{ + Total: core.Int64(int64(0.01 * 100)), + Currency: core.String("CNY"), + }, + Payer: &jsapi.Payer{ + Openid: core.String("openId"), + }, + }) + log.Println(result, err) + + // 详细使用文档见 https://github.com/wechatpay-apiv3/wechatpay-go +} +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..daebc54 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.oa00.com/go/wxpay + +go 1.17 + +require ( + git.oa00.com/go/rsa v1.0.0 + github.com/wechatpay-apiv3/wechatpay-go v0.2.11 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..49834aa --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +git.oa00.com/go/rsa v1.0.0 h1:cscSyfYeNxqUjEt/rUiPjPlqeZW/pdLDqVTrS64llJM= +git.oa00.com/go/rsa v1.0.0/go.mod h1:y4272MGmgvdEGcXIcPRBwteDo/zeqTEtRpqJBpK1Luk= +github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= +github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/wechatpay-apiv3/wechatpay-go v0.2.11 h1:KEVmn6VxFeoCdnTno4yLOWMmdG/wS7KWURzeuVXH6RU= +github.com/wechatpay-apiv3/wechatpay-go v0.2.11/go.mod h1:W8ucVAOCKOii933cWROLaDLmRQ2cg/vHHVF4vGAVq9Q= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/wxpay.go b/wxpay.go new file mode 100644 index 0000000..0c7b200 --- /dev/null +++ b/wxpay.go @@ -0,0 +1,54 @@ +package wxpay + +import ( + "context" + "errors" + "fmt" + "git.oa00.com/go/rsa" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers" + "github.com/wechatpay-apiv3/wechatpay-go/core/downloader" + "github.com/wechatpay-apiv3/wechatpay-go/core/notify" + "github.com/wechatpay-apiv3/wechatpay-go/core/option" +) + +var Wxpay = wxpay{} + +type wxpay struct { + Client *core.Client + Handler *notify.Handler +} +type Config struct { + MchID string // 商户号 + MchAPIv3Key string // 商户APIv3密钥 + PrivateKey string // 私钥 + PrivateCert string // 私钥证书 +} + +// InitWxpay @Title 初始化微信支付 +func InitWxpay(config Config) error { + // 加载私钥 + privateKey, err := rsa.ParsePrivateKey(config.PrivateKey, rsa.PKCS8) + if err != nil { + return errors.New("私钥错误") + } + // 加载私钥证书 + certificate, err := rsa.ParseCertificate(config.PrivateCert) + if err != nil { + return errors.New("证书错误") + } + // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力 + opts := []core.ClientOption{ + option.WithWechatPayAutoAuthCipher(config.MchID, fmt.Sprintf("%x", certificate.SerialNumber), privateKey, config.MchAPIv3Key), + } + Wxpay.Client, err = core.NewClient(context.Background(), opts...) + if err != nil { + return errors.New("微信支付错误") + } + + // 获取商户号对应的微信支付平台证书访问器 + certVisitor := downloader.MgrInstance().GetCertificateVisitor(config.MchID) + // 使用证书访问器初始化 `notify.Handler` + Wxpay.Handler = notify.NewNotifyHandler(config.MchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certVisitor)) + return nil +}