voucher/test/coupon.go

279 lines
7.1 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"
"time"
"voucher/internal/biz/do"
"voucher/internal/conf"
"voucher/internal/data"
)
var bc = &conf.Bootstrap{
Wechat: &conf.Wechat{
MchID: "1710953361",
MchCertificateSerialNumber: "6006B8208815DB5EAC5BF2E783CB9D34082C3772",
WechatPayPublicKeyID: "PUB_KEY_ID_0117109533612025031800326400002563",
Name: "蓝色兄弟-new",
},
}
func SendCoupon() {
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("FZQM1948534036766040066"), // {CouponId:129623470711}
// 微信为发券方商户分配的公众账号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("21104160"),
StockCreatorMchid: core.String("1715349578"),
}
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() {
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),
//}
//appId := "wx5d3e839568f24b2b"
//openId := "ocuH-0Nymo4sJLRNabIBbg9H2XCo"
//couponId := "116076813524"
appId := "wxd27e255810842ba8"
openId := "o3dEt5cA8jt3Kz5wNzAO6-3YQHsE"
couponId := "129623470711"
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() {
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("21104160"),
StockCreatorMchid: core.String("1715349578"),
}
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)
fmt.Printf("\nWxResp:%+v", WxResp(resp))
return
}
func WxResp(wxResp *cashcoupons.Stock) (reps *do.WxResp) {
availableStock := *wxResp.StockUseRule.MaxCoupons - *wxResp.DistributedCoupons
couponAmount := *wxResp.StockUseRule.FixedNormalCoupon.CouponAmount / 100
remainingBudget := availableStock * couponAmount
stockUsageRate := float64(*wxResp.DistributedCoupons) / float64(*wxResp.StockUseRule.MaxCoupons) * 100
req := &do.WxResp{
Amount: couponAmount,
AllBudget: *wxResp.StockUseRule.MaxAmount / 100,
AllStock: *wxResp.StockUseRule.MaxCoupons,
UsedStock: *wxResp.DistributedCoupons,
UsedBudget: *wxResp.DistributedCoupons * couponAmount,
AvailableStock: availableStock,
AvailableBudget: remainingBudget,
StockUsageRate: stockUsageRate,
}
inputFormat := time.RFC3339
if wxResp.AvailableBeginTime != nil {
availableBeginTime, _ := time.Parse(inputFormat, *wxResp.AvailableBeginTime)
req.StartTime = &availableBeginTime
}
if wxResp.AvailableEndTime != nil {
availableEndTime, _ := time.Parse(inputFormat, *wxResp.AvailableEndTime)
req.EndTime = &availableEndTime
}
return req
}
func QueryCallback() {
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
}