From 90150e36e057f1b951b412a10326f356617eafcb Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 11 Aug 2025 10:15:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=AC=94=E7=AB=8B=E5=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/data/wechat.go | 5 + test/coupon.go | 250 ++++++++++++++++++++++++++++++++++++++++ test/coupon_test.go | 66 +++++++++++ 3 files changed, 321 insertions(+) create mode 100644 test/coupon_test.go diff --git a/internal/data/wechat.go b/internal/data/wechat.go index 096d56b..1c613ac 100644 --- a/internal/data/wechat.go +++ b/internal/data/wechat.go @@ -17,6 +17,7 @@ type Server struct { MchID string `validate:"required"` MchCertificateSerialNumber string `validate:"required"` WechatPayPublicKeyID string `validate:"required"` + Dir string } func (c *Server) Validate() error { @@ -39,6 +40,10 @@ func NewClient(ctx context.Context, c Server) (*core.Client, error) { return nil, err } + if c.Dir != "" { + dir = c.Dir + } + filePath := fmt.Sprintf("%s/%s/%s/%s", dir, "cert", "wechat", c.MchID) if !helper.FileExists(filePath) { diff --git a/test/coupon.go b/test/coupon.go index 56e5404..5676fa1 100644 --- a/test/coupon.go +++ b/test/coupon.go @@ -1 +1,251 @@ package test + +import ( + "context" + "encoding/json" + "fmt" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons" + "io" + "os" + "path/filepath" + "voucher/internal/conf" + "voucher/internal/data" +) + +func SendCoupon() { + + bc := &conf.Bootstrap{ + Wechat: &conf.Wechat{ + MchID: "1710953361", + MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772", + WechatPayPublicKeyID: "PUB_KEY_ID_0117109533612025031800326400002563", + Name: "蓝色兄弟-new", + }, + } + + ctx := context.Background() + + dir, err := os.Getwd() + if err != nil { + fmt.Printf("os.Getwd() error = %v", err) + return + } + parentDir := filepath.Dir(dir) + + server := data.Server{ + MchID: bc.Wechat.MchID, + MchCertificateSerialNumber: bc.Wechat.MchCertificateSerialNumber, + WechatPayPublicKeyID: bc.Wechat.WechatPayPublicKeyID, + Dir: parentDir, + } + client, err := data.GetClient(ctx, server) + if err != nil { + fmt.Println(err) + return + } + + req := cashcoupons.SendCouponRequest{ + OutRequestNo: core.String("LQ1948534036766040066"), + // 微信为发券方商户分配的公众账号ID,接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。 + Appid: core.String("wxd27e255810842ba8"), + Openid: core.String("o3dEt5cA8jt3Kz5wNzAO6-3YQHsE"), + StockId: core.String("20391098"), + StockCreatorMchid: core.String("1652465541"), + } + fmt.Printf("\nreq:%+v", req) + + svc := cashcoupons.CouponApiService{Client: client} + + resp, result, err := svc.SendCoupon(ctx, req) + + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("\nresp:%+v\n result:%+v", resp, result) + return +} + +func QueryCoupon() { + + bc := &conf.Bootstrap{ + Wechat: &conf.Wechat{ + MchID: "1710953361", + MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772", + WechatPayPublicKeyID: "PUB_KEY_ID_0117109533612025031800326400002563", + Name: "蓝色兄弟-new", + }, + } + + ctx := context.Background() + + dir, err := os.Getwd() + if err != nil { + fmt.Printf("os.Getwd() error = %v", err) + return + } + parentDir := filepath.Dir(dir) + + server := data.Server{ + MchID: bc.Wechat.MchID, + MchCertificateSerialNumber: bc.Wechat.MchCertificateSerialNumber, + WechatPayPublicKeyID: bc.Wechat.WechatPayPublicKeyID, + Dir: parentDir, + } + client, err := data.GetClient(ctx, server) + if err != nil { + fmt.Println(err) + return + } + + //req := cashcoupons.QueryCouponRequest{ + // //CouponId: core.String("101270193400"), + // CouponId: core.String("101270193400"), + // Appid: core.String("wx619991cc795028f5"), + // Openid: core.String("oSNb4fmScVLmXILaolXVdBpJmYbQ"), + //} + + //req := cashcoupons.QueryCouponRequest{ + // CouponId: core.String("101274529925"), + // Appid: core.String("wx619991cc795028f5"), + // Openid: core.String("oSNb4fpnLNdkFPk-O43o6f2hxxRo"), + //} + + appId := "wx619991cc795028f5" + openId := "oSNb4ftgnWC22Z0cWTjsQebdr2Yk" + couponId := "113831004454" + req := cashcoupons.QueryCouponRequest{ + CouponId: core.String(couponId), + Appid: core.String(appId), + Openid: core.String(openId), + } + + svc := cashcoupons.CouponApiService{Client: client} + + resp, result, err := svc.QueryCoupon(ctx, req) + + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("\nresp:%+v\n", resp) + + bodyBytes, err := io.ReadAll(result.Response.Body) + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("\nresult:%s\n", bodyBytes) + return +} + +func QueryProduct() { + + bc := &conf.Bootstrap{ + Wechat: &conf.Wechat{ + MchID: "1710953361", + MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772", + WechatPayPublicKeyID: "PUB_KEY_ID_0117109533612025031800326400002563", + Name: "蓝色兄弟-new", + }, + } + + ctx := context.Background() + + dir, err := os.Getwd() + if err != nil { + fmt.Printf("os.Getwd() error = %v", err) + return + } + parentDir := filepath.Dir(dir) + + server := data.Server{ + MchID: bc.Wechat.MchID, + MchCertificateSerialNumber: bc.Wechat.MchCertificateSerialNumber, + WechatPayPublicKeyID: bc.Wechat.WechatPayPublicKeyID, + Dir: parentDir, + } + client, err := data.GetClient(ctx, server) + if err != nil { + fmt.Println(err) + return + } + + req := cashcoupons.QueryStockRequest{ + StockId: core.String("20811630"), + StockCreatorMchid: core.String("1652465541"), + } + + svc := cashcoupons.StockApiService{Client: client} + resp, _, err := svc.QueryStock(ctx, req) + if err != nil { + fmt.Println(err) + return + } + + j, _ := json.Marshal(resp) + fmt.Printf("\nresp:%s\n", string(j)) + + availableStock := *resp.StockUseRule.MaxCoupons - *resp.DistributedCoupons + couponAmount := *resp.StockUseRule.FixedNormalCoupon.CouponAmount / 100 + + fmt.Printf("\n批次号:%s", *req.StockId) + fmt.Printf("\n面额:%d", couponAmount) + fmt.Printf("\n总预算:%d", *resp.StockUseRule.MaxAmount/100) + fmt.Printf("\n总库存:%d", *resp.StockUseRule.MaxCoupons) + fmt.Printf("\n已发券数:%d", *resp.DistributedCoupons) + fmt.Printf("\n已发券金额:%d", *resp.DistributedCoupons*couponAmount) + fmt.Printf("\n剩余库存:%d", availableStock) + fmt.Printf("\n剩余预算:%d", availableStock*couponAmount) + return +} + +func QueryCallback() { + + bc := &conf.Bootstrap{ + Wechat: &conf.Wechat{ + MchID: "1710953361", + MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772", + WechatPayPublicKeyID: "PUB_KEY_ID_0117109533612025031800326400002563", + Name: "蓝色兄弟-new", + }, + } + + ctx := context.Background() + + dir, err := os.Getwd() + if err != nil { + fmt.Printf("os.Getwd() error = %v", err) + return + } + parentDir := filepath.Dir(dir) + + server := data.Server{ + MchID: bc.Wechat.MchID, + MchCertificateSerialNumber: bc.Wechat.MchCertificateSerialNumber, + WechatPayPublicKeyID: bc.Wechat.WechatPayPublicKeyID, + Dir: parentDir, + } + client, err := data.GetClient(ctx, server) + if err != nil { + fmt.Println(err) + return + } + + svc := cashcoupons.CallBackUrlApiService{Client: client} + + response, _, err := svc.QueryCallback(ctx, cashcoupons.QueryCallbackRequest{ + Mchid: core.String("1652465541"), + }) + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("\nresp:%+v\n", response) + return +} diff --git a/test/coupon_test.go b/test/coupon_test.go new file mode 100644 index 0000000..b103ac2 --- /dev/null +++ b/test/coupon_test.go @@ -0,0 +1,66 @@ +package test + +import ( + "testing" +) + +func Test_SendCoupon(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "发券", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SendCoupon() + }) + } +} + +func Test_QueryCoupon(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "查询券订单信息", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SendCoupon() + }) + } +} + +func Test_QueryProduct(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "查询券商品信息", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + QueryProduct() + }) + } +} + +func Test_QueryCallback(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "查询券核销通知地址", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + QueryCallback() + }) + } +}