微信立减金 插件加载证书
This commit is contained in:
parent
7a267db263
commit
8003c6b67d
|
@ -2,5 +2,5 @@ package main
|
|||
|
||||
// main 这只是一个演示
|
||||
func main() {
|
||||
alipayRedPack()
|
||||
wechatCpn()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"gitea.cdlsxd.cn/sdk/plugin/instance"
|
||||
"gitea.cdlsxd.cn/sdk/plugin/manage"
|
||||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||||
"log"
|
||||
"plugins/utils/weixin"
|
||||
)
|
||||
|
||||
var wechatCpnConf = &manage.Config{
|
||||
Cmd: "pkg/mac/weixin_cpn.so",
|
||||
Tag: "alipay_redpack",
|
||||
Version: 1,
|
||||
CookieKey: "weixin_cpn",
|
||||
CookieValue: "weixin_cpn",
|
||||
}
|
||||
|
||||
func getWechatCpnConf() []byte {
|
||||
c := &weixin.Server{
|
||||
MchID: "1605446142", // 证书所属商户
|
||||
MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
|
||||
MchAPIv3Key: "ChengDuLanSeXiongDi1234567890123",
|
||||
}
|
||||
marshal, _ := json.Marshal(c)
|
||||
return marshal
|
||||
}
|
||||
|
||||
func wechatCpn() {
|
||||
err := manage.Add(wechatCpnConf)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
queryRequest := &proto.QueryRequest{
|
||||
Config: getWechatCpnConf(),
|
||||
Order: &proto.QueryRequest_Order{
|
||||
OrderNo: "",
|
||||
TradeNo: "69445765514",
|
||||
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
|
||||
Extra: []byte(`{"app_id":"wx9ed74283ad25bca1"}`),
|
||||
},
|
||||
}
|
||||
resQuery, err := instance.Query(context.Background(), alpayRedConf.Tag, queryRequest)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Printf("Query res:%+v", resQuery)
|
||||
}
|
8
go.mod
8
go.mod
|
@ -2,7 +2,12 @@ module plugins
|
|||
|
||||
go 1.22.2
|
||||
|
||||
require gitea.cdlsxd.cn/sdk/plugin v0.0.0-20240830100334-2e99a5df732b
|
||||
require (
|
||||
gitea.cdlsxd.cn/sdk/plugin v0.0.0-20240830100334-2e99a5df732b
|
||||
plugins/utils v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
replace plugins/utils => ./utils
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.7.0 // indirect
|
||||
|
@ -20,6 +25,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.26.0 // indirect
|
||||
golang.org/x/net v0.27.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
|
|
13
go.sum
13
go.sum
|
@ -1,7 +1,10 @@
|
|||
gitea.cdlsxd.cn/sdk/plugin v0.0.0-20240830100334-2e99a5df732b h1:x8Gf1EJ6oLHEIgK/SilgMZ5EDgcEknef9zhGrFvXXMg=
|
||||
gitea.cdlsxd.cn/sdk/plugin v0.0.0-20240830100334-2e99a5df732b/go.mod h1:cd+ZFTmd/ZxrrVc1OZCkrh2wAMPDaAa8ce13FAAkBg0=
|
||||
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/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/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
|
@ -43,9 +46,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.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
|
@ -62,5 +73,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=
|
||||
|
|
|
@ -20,9 +20,10 @@ func transConfig(config []byte) (*weixin.Server, error) {
|
|||
return &c, nil
|
||||
}
|
||||
|
||||
func orderReq(stockCreatorMchId string, order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (cashcoupons.SendCouponRequest, error) {
|
||||
func orderReq(order *proto.OrderRequest_Order, product *proto.OrderRequest_Product) (cashcoupons.SendCouponRequest, error) {
|
||||
type Extra struct {
|
||||
Appid string `json:"app_id"`
|
||||
Appid string `json:"app_id"`
|
||||
StockCreatorMchId string `json:"stock_creator_mchid"`
|
||||
}
|
||||
var extra Extra
|
||||
if order.Extra != nil {
|
||||
|
@ -36,7 +37,7 @@ func orderReq(stockCreatorMchId string, order *proto.OrderRequest_Order, product
|
|||
StockId: core.String(product.ProductNo),
|
||||
OutRequestNo: core.String(order.OrderNo),
|
||||
Appid: core.String(extra.Appid),
|
||||
StockCreatorMchid: core.String(stockCreatorMchId),
|
||||
StockCreatorMchid: core.String(extra.StockCreatorMchId),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ func (p *WeiXinCpnService) Order(ctx context.Context, request *proto.OrderReques
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := orderReq(config.MchID, request.GetOrder(), request.GetProduct())
|
||||
req, err := orderReq(request.GetOrder(), request.GetProduct())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -14,10 +14,9 @@ var server = &WeiXinCpnService{}
|
|||
|
||||
func config() []byte {
|
||||
c := &weixin.Server{
|
||||
MchID: "1605446142",
|
||||
MchID: "1605446142", // 证书所属商户
|
||||
MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
|
||||
MchAPIv3Key: "ChengDuLanSeXiongDi1234567890123",
|
||||
PrivateKeyPath: "/Users/lsxd/code/go/other/mq/wx/wechat_private_key.pem",
|
||||
}
|
||||
marshal, _ := json.Marshal(c)
|
||||
return marshal
|
||||
|
@ -29,7 +28,7 @@ func TestOrder(t *testing.T) {
|
|||
Order: &proto.OrderRequest_Order{
|
||||
OrderNo: "240403164049635931",
|
||||
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
|
||||
Extra: []byte(`{"app_id":"13100720242"`),
|
||||
Extra: []byte(`{"app_id":"13100720242","stock_creator_mchid":"1605446142"`),
|
||||
},
|
||||
Product: &proto.OrderRequest_Product{
|
||||
ProductNo: "3102024032977191",
|
||||
|
|
|
@ -7,32 +7,33 @@ import (
|
|||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
MchID string `json:"mch_id"`
|
||||
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"`
|
||||
MchAPIv3Key string `json:"mch_ap_iv_3_key"`
|
||||
PrivateKey string `json:"private_key"`
|
||||
PrivateKeyPath string `json:"private_key_path"`
|
||||
CertificatePath string `json:"certificate_path"`
|
||||
}
|
||||
|
||||
func newClient(ctx context.Context, c *Server) (*core.Client, error) {
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(c.PrivateKeyPath)
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] get current dir error:%v", c.MchID, err)
|
||||
}
|
||||
filePath := fmt.Sprintf("%s/%s/%s/%s", dir, "cert", "wechat", c.MchID)
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(fmt.Sprintf("%s/%s", filePath, "wechat_private_key.pem"))
|
||||
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),
|
||||
}
|
||||
if c.CertificatePath != "" {
|
||||
payCert, err := utils.LoadCertificateWithPath(c.CertificatePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] load pay certificate error:%v", c.MchID, err)
|
||||
}
|
||||
opts = append(opts, option.WithWechatPayCertificate([]*x509.Certificate{payCert}))
|
||||
payCert, err := utils.LoadCertificateWithPath(fmt.Sprintf("%s/%s", filePath, "wechat_cert.pem"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] load pay certificate error:%v", c.MchID, err)
|
||||
}
|
||||
opts = append(opts, option.WithWechatPayCertificate([]*x509.Certificate{payCert}))
|
||||
client, err := core.NewClient(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MchID[%s] %v", c.MchID, err)
|
||||
|
|
Loading…
Reference in New Issue