多笔立减

This commit is contained in:
ziming 2025-08-11 10:15:39 +08:00
parent 5be1f63f04
commit 90150e36e0
3 changed files with 321 additions and 0 deletions

View File

@ -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) {

View File

@ -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
}

66
test/coupon_test.go Normal file
View File

@ -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()
})
}
}