diff --git a/internal/data/wechatrepoimpl/cpn.go b/internal/data/wechatrepoimpl/cpn.go index 6ae9b4e..6e8fd34 100644 --- a/internal/data/wechatrepoimpl/cpn.go +++ b/internal/data/wechatrepoimpl/cpn.go @@ -6,6 +6,7 @@ import ( "fmt" "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" @@ -52,7 +53,20 @@ func (c *CpnRepoImpl) Order(ctx context.Context, orderWechat *bo.OrderWechatBo) 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 + } + + if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil { + return + } + + err = fmt.Errorf("微信返回错误=%s", ErrBody.Message) + return } @@ -83,7 +97,16 @@ func (c *CpnRepoImpl) Query(ctx context.Context, orderWechat *bo.OrderWechatBo) resp, result, err := svc.QueryCoupon(ctx, req) if err != nil { - return 0, err + bodyBytes, err := io.ReadAll(result.Response.Body) + if err != nil { + return 0, err + } + + if err = json.Unmarshal(bodyBytes, &ErrBody); err != nil { + return 0, err + } + + return 0, fmt.Errorf("微信返回错误=%s", ErrBody.Message) } if result.Response.StatusCode != CodeSuccess { @@ -109,7 +132,16 @@ func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stock }) if err != nil { - return nil, err + 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 { diff --git a/internal/data/wechatrepoimpl/wechat.go b/internal/data/wechatrepoimpl/wechat.go new file mode 100644 index 0000000..24cdc03 --- /dev/null +++ b/internal/data/wechatrepoimpl/wechat.go @@ -0,0 +1,7 @@ +package wechatrepoimpl + +var ErrBody struct { + StatusCode string `json:"StatusCode"` + Code string `json:"Code"` + Message string `json:"Message"` +}