plugin weixin utils
This commit is contained in:
parent
1439143116
commit
54b8829f00
1
go.mod
1
go.mod
|
@ -21,6 +21,7 @@ require (
|
|||
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
|
||||
github.com/oklog/run v1.0.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/wechatpay-apiv3/wechatpay-go v0.2.18 // indirect
|
||||
golang.org/x/crypto v0.21.0 // indirect
|
||||
golang.org/x/net v0.22.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
|
|
12
go.sum
12
go.sum
|
@ -1,7 +1,9 @@
|
|||
codeup.aliyun.com/6552e56cc3b2728a4557fc18/plugin v0.0.0-20240717031352-74b2db795c21 h1:vX3EfkjN75Xmh6PHXsY3acYUeZhUPoGEa3oNiq9DpwY=
|
||||
codeup.aliyun.com/6552e56cc3b2728a4557fc18/plugin v0.0.0-20240717031352-74b2db795c21/go.mod h1:N8iliISBNbAZqGzr9bI2WQ/NcEfDgD9ZHmT+eVwp8Sk=
|
||||
github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
|
||||
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
|
||||
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
|
||||
|
@ -45,9 +47,17 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/wechatpay-apiv3/wechatpay-go v0.2.18 h1:vj5tvSmnEIz3ZsnFNNUzg+3Z46xgNMJbrO4aD4wP15w=
|
||||
github.com/wechatpay-apiv3/wechatpay-go v0.2.18/go.mod h1:A254AUBVB6R+EqQFo3yTgeh7HtyqRRtN2w9hQSOrd4Q=
|
||||
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
|
@ -64,5 +74,7 @@ google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
|
|||
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -18,8 +18,8 @@ union_pay_cpn:
|
|||
alipay_cpn:
|
||||
make build name=alipay_cpn
|
||||
|
||||
.PHONY: wx_cpn
|
||||
wx_cpn:
|
||||
.PHONY: weixin_cpn
|
||||
weixin_cpn:
|
||||
make build name=weixin_cpn
|
||||
|
||||
.PHONY: all
|
||||
|
|
|
@ -2,6 +2,9 @@ module plugins/weixin_cpn
|
|||
|
||||
go 1.22.2
|
||||
|
||||
replace plugins/utils => ../../utils
|
||||
require plugins/utils v1.0.0
|
||||
|
||||
require (
|
||||
codeup.aliyun.com/6552e56cc3b2728a4557fc18/plugin v0.0.0-20240717031352-74b2db795c21
|
||||
github.com/hashicorp/go-plugin v1.6.1
|
||||
|
|
|
@ -7,18 +7,12 @@ import (
|
|||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||||
"go/types"
|
||||
"plugins/utils/weixin"
|
||||
"plugins/weixin_cpn/internal/vo"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MchID string `json:"mch_id"`
|
||||
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"`
|
||||
MchAPIv3Key string `json:"mch_ap_iv_3_key"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
}
|
||||
|
||||
func transConfig(config []byte) (*Config, error) {
|
||||
var c Config
|
||||
func transConfig(config []byte) (*weixin.Config, error) {
|
||||
var c weixin.Config
|
||||
err := json.Unmarshal(config, &c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -2,45 +2,15 @@ package internal
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"sync"
|
||||
"plugins/utils/weixin"
|
||||
)
|
||||
|
||||
var once sync.Once
|
||||
var errorMutex sync.RWMutex
|
||||
|
||||
var instanceMap = make(map[string]*cashcoupons.CouponApiService)
|
||||
var errorMap = make(map[string]error)
|
||||
|
||||
func getSrv(ctx context.Context, c *Config) (*cashcoupons.CouponApiService, error) {
|
||||
errorMutex.Lock()
|
||||
defer errorMutex.Unlock()
|
||||
if errorMap[c.MchID] != nil {
|
||||
instanceMap[c.MchID] = nil
|
||||
errorMap[c.MchID] = nil
|
||||
}
|
||||
once.Do(func() {
|
||||
instanceMap[c.MchID], errorMap[c.MchID] = newInstance(ctx, c)
|
||||
})
|
||||
return instanceMap[c.MchID], errorMap[c.MchID]
|
||||
}
|
||||
|
||||
func newInstance(ctx context.Context, config *Config) (*cashcoupons.CouponApiService, error) {
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(config.PrivateKeyPath)
|
||||
func getApiSrv(ctx context.Context, c *weixin.Config) (*cashcoupons.CouponApiService, error) {
|
||||
client, err := weixin.New(ctx, c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load merchant private key error:%v", err)
|
||||
}
|
||||
opts := []core.ClientOption{
|
||||
option.WithWechatPayAutoAuthCipher(config.MchID, config.MchCertificateSerialNumber, mchPrivateKey, config.MchAPIv3Key),
|
||||
}
|
||||
client, err := core.NewClient(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new wechat pay client err:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
svc := cashcoupons.CouponApiService{Client: client}
|
||||
return &svc, err
|
||||
return &svc, nil
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func (p *WeiXinCpnService) Order(ctx context.Context, request *proto.OrderReques
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
svc, err := getSrv(ctx, config)
|
||||
svc, err := getApiSrv(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func (p *WeiXinCpnService) Order(ctx context.Context, request *proto.OrderReques
|
|||
|
||||
func (p *WeiXinCpnService) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
|
||||
config, err := transConfig(request.Config)
|
||||
svc, err := getSrv(ctx, config)
|
||||
svc, err := getApiSrv(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -6,13 +6,14 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"plugins/utils/weixin"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var server = &WeiXinCpnService{}
|
||||
|
||||
func config() []byte {
|
||||
c := &Config{
|
||||
c := &weixin.Config{
|
||||
MchID: "1605446142",
|
||||
MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
|
||||
MchAPIv3Key: "ChengDuLanSeXiongDi1234567890123",
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
module plugins/utils
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require github.com/wechatpay-apiv3/wechatpay-go v0.2.18
|
||||
|
||||
require github.com/stretchr/testify v1.8.4 // indirect
|
|
@ -0,0 +1,62 @@
|
|||
package weixin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MchID string `json:"mch_id"`
|
||||
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"`
|
||||
MchAPIv3Key string `json:"mch_ap_iv_3_key"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
once sync.Once
|
||||
mutex sync.RWMutex
|
||||
clients map[string]*core.Client
|
||||
}
|
||||
|
||||
var instance manager
|
||||
|
||||
func init() {
|
||||
instance = manager{
|
||||
clients: make(map[string]*core.Client),
|
||||
}
|
||||
}
|
||||
|
||||
func New(ctx context.Context, c *Config) (*core.Client, error) {
|
||||
instance.mutex.Lock()
|
||||
defer instance.mutex.Unlock()
|
||||
if instance.clients[c.MchID] != nil {
|
||||
return instance.clients[c.MchID], nil
|
||||
}
|
||||
var err error
|
||||
instance.once.Do(func() {
|
||||
instance.clients[c.MchID], err = client(ctx, c)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return instance.clients[c.MchID], nil
|
||||
}
|
||||
|
||||
func client(ctx context.Context, c *Config) (*core.Client, error) {
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(c.PrivateKeyPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] load merchant private key error:%v", c.MchID, err)
|
||||
}
|
||||
opts := []core.ClientOption{
|
||||
option.WithWechatPayAutoAuthCipher(c.MchID, c.MchCertificateSerialNumber, mchPrivateKey, c.MchAPIv3Key),
|
||||
}
|
||||
cl, err := core.NewClient(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] new wechat pay client err:%v", c.MchID, err)
|
||||
}
|
||||
return cl, nil
|
||||
}
|
Loading…
Reference in New Issue