From 6aae0515359a56a976f24652097a02b115931a74 Mon Sep 17 00:00:00 2001 From: kanade Date: Fri, 1 Apr 2022 17:55:04 +0800 Subject: [PATCH] init --- .gitignore | 1 + README.md | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ alipay.go | 25 +++++++++++++ go.mod | 13 +++++++ go.sum | 25 +++++++++++++ wxpay.go | 49 +++++++++++++++++++++++++ 6 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 alipay.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 wxpay.go 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..a55232c --- /dev/null +++ b/README.md @@ -0,0 +1,103 @@ +# 支付 + +### 微信支付 + +```go +package main + +import ( + "context" + "git.oa00.com/go/pay" + "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.WxpayConfig{ + MchID: "mchID", // 支付商户号 + MchCertificateSerialNumber: "mchCertificateSerialNumber", // 证书序列号 + 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 +} +``` + +### 支付宝支付 +```go +package main + +import ( + "git.oa00.com/go/pay" + "github.com/smartwalle/alipay/v3" + "log" + "time" +) + +func main() { + // 初始化配置 + if err := pay.InitAlipay(pay.AlipayConfig{ + AppId: "appId",// appId + PrivateKey: "privateKey",// 商户私钥 + AliPublicKey: "aliPublicKey",// 支付宝公钥 + IsProduction: isProduction,// 是否线上环境 + }); err != nil { + log.Panicln("支付宝支付错误:", err) + } + + // app支付 + param := alipay.TradeAppPay{} + param.OutTradeNo = "payNo" + param.TotalAmount = "0.01" + param.Subject = "subject" + param.TimeExpire = time.Now().Add(time.Minute * time.Duration(15)).Format("2006-01-02 15:04:05") + param.NotifyURL = "notifyURL" + result, err := pay.Alipay.TradeAppPay(param) + log.Println(result, err) + + // 详细使用文档见 https://github.com/smartwalle/alipay +} +``` \ No newline at end of file diff --git a/alipay.go b/alipay.go new file mode 100644 index 0000000..9799c6c --- /dev/null +++ b/alipay.go @@ -0,0 +1,25 @@ +package pay + +import "github.com/smartwalle/alipay/v3" + +type AlipayConfig struct { + AppId string + PrivateKey string + AliPublicKey string + IsProduction bool +} + +var Alipay *alipay.Client + +// InitAlipay @Title 初始化支付宝配置 +func InitAlipay(config AlipayConfig) (err error) { + Alipay, err = alipay.New(config.AppId, config.PrivateKey, config.IsProduction) + if err != nil { + return err + } + // 加载阿里公钥 + if err = Alipay.LoadAliPayPublicKey(config.AliPublicKey); err != nil { + return err + } + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83d58c0 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module git.oa00.com/go/pay + +go 1.17 + +require ( + github.com/smartwalle/alipay/v3 v3.1.7 + github.com/wechatpay-apiv3/wechatpay-go v0.2.11 +) + +require ( + github.com/smartwalle/crypto4go v1.0.2 // indirect + golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1cd9a4a --- /dev/null +++ b/go.sum @@ -0,0 +1,25 @@ +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/smartwalle/alipay/v3 v3.1.7 h1:J4U5slABafKVD/b9gPCZe/3HAPB8Pa2NOYOPcugEJBo= +github.com/smartwalle/alipay/v3 v3.1.7/go.mod h1:cZUMCCnsux9YAxA0/f3PWUR+7wckWtE1BqxbVRtGij0= +github.com/smartwalle/crypto4go v1.0.2 h1:9DUEOOsPhmp00438L4oBdcL8EZG1zumecft5bWj5phI= +github.com/smartwalle/crypto4go v1.0.2/go.mod h1:LQ7vCZIb7BE5+MuMtJBuO8ORkkQ01m4DXDBWPzLbkMY= +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= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 h1:rlLehGeYg6jfoyz/eDqDU1iRXLKfR42nnNh57ytKEWo= +golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +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..6b65ad1 --- /dev/null +++ b/wxpay.go @@ -0,0 +1,49 @@ +package pay + +import ( + "base/app/lib/rsa" + "context" + "errors" + "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 WxpayConfig struct { + MchID string // 商户号 + MchCertificateSerialNumber string // 商户证书序列号 + MchAPIv3Key string // 商户APIv3密钥 + PrivateKey string // 私钥 + PrivateCert string // 私钥证书 +} + +// InitWxpay @Title 初始化微信支付 +func InitWxpay(config WxpayConfig) error { + // 加载私钥证书 + privateKey, err := rsa.ParsePrivateKey(config.PrivateKey, rsa.PKCS8) + if err != nil { + return errors.New("私钥错误") + } + // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力 + opts := []core.ClientOption{ + option.WithWechatPayAutoAuthCipher(config.MchID, config.MchCertificateSerialNumber, 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 +}