voucher/test/coupon.go

252 lines
6.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}