203 lines
4.7 KiB
Go
203 lines
4.7 KiB
Go
package wechatrepoimpl
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"github.com/go-kratos/kratos/v2/log"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||
"io"
|
||
"net/http"
|
||
"voucher/internal/biz/bo"
|
||
"voucher/internal/biz/vo"
|
||
"voucher/internal/biz/wechatrepo"
|
||
"voucher/internal/conf"
|
||
"voucher/internal/data"
|
||
"voucher/internal/pkg/request"
|
||
)
|
||
|
||
type CpnRepoImpl struct {
|
||
bc *conf.Bootstrap
|
||
Server *data.Server
|
||
}
|
||
|
||
func NewCpnRepoImpl(bc *conf.Bootstrap) (wechatrepo.WechatCpnRepo, error) {
|
||
server := &data.Server{
|
||
MchID: bc.Wechat.MchID,
|
||
MchCertificateSerialNumber: bc.Wechat.MchCertificateSerialNumber,
|
||
}
|
||
|
||
if err := server.Validate(); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &CpnRepoImpl{bc: bc, Server: server}, nil
|
||
}
|
||
|
||
func (c *CpnRepoImpl) Order(ctx context.Context, order *bo.OrderBo) (couponId string, err error) {
|
||
|
||
req := cashcoupons.SendCouponRequest{
|
||
OutRequestNo: core.String(order.OrderNo),
|
||
// 微信为发券方商户分配的公众账号ID,接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。
|
||
Appid: core.String(order.AppID),
|
||
Openid: core.String(order.Account),
|
||
StockId: core.String(order.BatchNo),
|
||
StockCreatorMchid: core.String(order.MerchantNo),
|
||
}
|
||
|
||
client, err := data.GetClient(ctx, c.Server)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
svc := cashcoupons.CouponApiService{Client: client}
|
||
|
||
resp, result, err := svc.SendCoupon(ctx, req)
|
||
|
||
if err != nil {
|
||
|
||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
log.Errorf("请求微信返回错误=%s", string(bodyBytes))
|
||
|
||
if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil {
|
||
return
|
||
}
|
||
|
||
err = fmt.Errorf(ErrBody.Message)
|
||
|
||
return
|
||
}
|
||
|
||
if result.Response.StatusCode != CodeSuccess {
|
||
err = fmt.Errorf("请求错误")
|
||
return
|
||
}
|
||
|
||
couponId = *resp.CouponId
|
||
|
||
return
|
||
}
|
||
|
||
func (c *CpnRepoImpl) Query(ctx context.Context, orderWechat *bo.OrderBo) (vo.OrderStatus, error) {
|
||
|
||
req := cashcoupons.QueryCouponRequest{
|
||
CouponId: core.String(orderWechat.VoucherNo),
|
||
Appid: core.String(orderWechat.AppID),
|
||
Openid: core.String(orderWechat.Account),
|
||
}
|
||
|
||
client, err := data.GetClient(ctx, c.Server)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
svc := cashcoupons.CouponApiService{Client: client}
|
||
|
||
resp, result, err := svc.QueryCoupon(ctx, req)
|
||
if err != nil {
|
||
|
||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
log.Errorf("请求微信返回错误=%s", string(bodyBytes))
|
||
|
||
if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
err = fmt.Errorf(ErrBody.Message)
|
||
|
||
return 0, fmt.Errorf("微信返回错误=%s", ErrBody.Message)
|
||
}
|
||
|
||
if result.Response.StatusCode != CodeSuccess {
|
||
return 0, fmt.Errorf("请求错误")
|
||
}
|
||
|
||
return CpnStatus(*resp.Status).GetStatus()
|
||
}
|
||
|
||
func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error) {
|
||
|
||
client, err := data.GetClient(ctx, c.Server)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
svc := cashcoupons.StockApiService{Client: client}
|
||
|
||
response, result, err := svc.QueryStock(ctx, cashcoupons.QueryStockRequest{
|
||
StockId: core.String(stockId),
|
||
StockCreatorMchid: core.String(stockCreatorMchId),
|
||
})
|
||
|
||
if err != nil {
|
||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return nil, fmt.Errorf("微信返回错误=%s", ErrBody.Message)
|
||
}
|
||
|
||
if result.Response.StatusCode != CodeSuccess {
|
||
err = fmt.Errorf("查询活动微信返回错误StatusCode[%d]Status[%s]", result.Response.StatusCode, result.Response.Status)
|
||
return nil, err
|
||
}
|
||
|
||
return response, nil
|
||
}
|
||
|
||
func (c *CpnRepoImpl) Notify(ctx context.Context) error {
|
||
return nil
|
||
}
|
||
|
||
func (c *CpnRepoImpl) RegisterNotifyTag(ctx context.Context, stockID string) error {
|
||
|
||
// 注册“刚哥那边”回调中心tag,一个批次只能注册一次,消费时根据不同的tag消费(区分测试/正式注册tag处理)
|
||
h := http.Header{
|
||
"Content-Type": []string{"application/json"},
|
||
}
|
||
|
||
b := struct {
|
||
TagName string `json:"tag_name"`
|
||
StockID string `json:"stock_id"`
|
||
}{
|
||
TagName: c.bc.WechatNotifyMQ.Tag,
|
||
StockID: stockID,
|
||
}
|
||
|
||
body, err := json.Marshal(b)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
isSuccess := func(code int) bool {
|
||
return code == http.StatusOK
|
||
}
|
||
|
||
_, _, err = request.Post(
|
||
ctx,
|
||
c.bc.WechatNotifyMQ.RegisterTagUrl,
|
||
body,
|
||
request.WithHeaders(h),
|
||
request.WithStatusCodeFunc(isSuccess),
|
||
)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|