
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
11)
12
13func main() {
14
15 config, err := wxpay_brand_utility.CreateBrandConfig(
16 "xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &PreSendUserProductCouponBundleRequest{
28 Openid: wxpay_brand_utility.String("oh-394z-6CGkNoJrsDLTTUKiAnp4"),
29 ProductCouponId: wxpay_brand_utility.String("200000001"),
30 StockBundleId: wxpay_brand_utility.String("100232301"),
31 Appid: wxpay_brand_utility.String("wx233544546545989"),
32 SendRequestNo: wxpay_brand_utility.String("34657_20250101_123456"),
33 Attach: wxpay_brand_utility.String("example_attach"),
34 }
35
36 response, err := PreSendUserProductCouponBundle(config, request)
37 if err != nil {
38 fmt.Printf("请求失败: %+v\n", err)
39
40 return
41 }
42
43
44 fmt.Printf("请求成功: %+v\n", response)
45}
46
47func PreSendUserProductCouponBundle(config *wxpay_brand_utility.BrandConfig, request *PreSendUserProductCouponBundleRequest) (response *PreSendUserProductCouponBundleResponse, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/brand/marketing/product-coupon/users/{openid}/pre-send-coupon-bundle"
52 )
53
54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
55 if err != nil {
56 return nil, err
57 }
58 reqUrl.Path = strings.Replace(reqUrl.Path, "{openid}", url.PathEscape(*request.Openid), -1)
59 reqBody, err := json.Marshal(request)
60 if err != nil {
61 return nil, err
62 }
63 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
64 if err != nil {
65 return nil, err
66 }
67 httpRequest.Header.Set("Accept", "application/json")
68 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
69 httpRequest.Header.Set("Content-Type", "application/json")
70 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
71 if err != nil {
72 return nil, err
73 }
74 httpRequest.Header.Set("Authorization", authorization)
75
76 client := &http.Client{}
77 httpResponse, err := client.Do(httpRequest)
78 if err != nil {
79 return nil, err
80 }
81 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
82 if err != nil {
83 return nil, err
84 }
85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
86
87 err = wxpay_brand_utility.ValidateResponse(
88 config.WechatPayPublicKeyId(),
89 config.WechatPayPublicKey(),
90 &httpResponse.Header,
91 respBody,
92 )
93 if err != nil {
94 return nil, err
95 }
96 response := &PreSendUserProductCouponBundleResponse{}
97 if err := json.Unmarshal(respBody, response); err != nil {
98 return nil, err
99 }
100
101 return response, nil
102 } else {
103 return nil, wxpay_brand_utility.NewApiException(
104 httpResponse.StatusCode,
105 httpResponse.Header,
106 respBody,
107 )
108 }
109}
110
111type PreSendUserProductCouponBundleRequest struct {
112 ProductCouponId *string `json:"product_coupon_id,omitempty"`
113 StockBundleId *string `json:"stock_bundle_id,omitempty"`
114 Appid *string `json:"appid,omitempty"`
115 Openid *string `json:"openid,omitempty"`
116 SendRequestNo *string `json:"send_request_no,omitempty"`
117 Attach *string `json:"attach,omitempty"`
118}
119
120func (o *PreSendUserProductCouponBundleRequest) MarshalJSON() ([]byte, error) {
121 type Alias PreSendUserProductCouponBundleRequest
122 a := &struct {
123 Openid *string `json:"openid,omitempty"`
124 *Alias
125 }{
126
127 Openid: nil,
128 Alias: (*Alias)(o),
129 }
130 return json.Marshal(a)
131}
132
133type PreSendUserProductCouponBundleResponse struct {
134 Token *string `json:"token,omitempty"`
135}
136