
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
11)
12
13func main() {
14
15 config, err := wxpay_utility.CreateMchConfig(
16 "19xxxxxxxx",
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 := &UploadCouponCodesRequest{
28 OutRequestNo: wxpay_utility.String("upload_34657_20250101_123456"),
29 ProductCouponId: wxpay_utility.String("1000000013"),
30 StockId: wxpay_utility.String("1000000013001"),
31 CodeList: []string{
32 "code_0000001",
33 "code_0000002",
34 "code_0000003",
35 },
36 BrandId: wxpay_utility.String("120344"),
37 }
38
39 response, err := UploadCouponCodes(config, request)
40 if err != nil {
41 fmt.Printf("请求失败: %+v\n", err)
42
43 return
44 }
45
46
47 fmt.Printf("请求成功: %+v\n", response)
48}
49
50func UploadCouponCodes(config *wxpay_utility.MchConfig, request *UploadCouponCodesRequest) (response *UploadCouponCodesResponse, err error) {
51 const (
52 host = "https://api.mch.weixin.qq.com"
53 method = "POST"
54 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/upload-coupon-codes"
55 )
56
57 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
58 if err != nil {
59 return nil, err
60 }
61 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1)
62 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1)
63 reqBody, err := json.Marshal(request)
64 if err != nil {
65 return nil, err
66 }
67 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
68 if err != nil {
69 return nil, err
70 }
71 httpRequest.Header.Set("Accept", "application/json")
72 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
73 httpRequest.Header.Set("Content-Type", "application/json")
74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
75 if err != nil {
76 return nil, err
77 }
78 httpRequest.Header.Set("Authorization", authorization)
79
80 client := &http.Client{}
81 httpResponse, err := client.Do(httpRequest)
82 if err != nil {
83 return nil, err
84 }
85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
86 if err != nil {
87 return nil, err
88 }
89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
90
91 err = wxpay_utility.ValidateResponse(
92 config.WechatPayPublicKeyId(),
93 config.WechatPayPublicKey(),
94 &httpResponse.Header,
95 respBody,
96 )
97 if err != nil {
98 return nil, err
99 }
100 response := &UploadCouponCodesResponse{}
101 if err := json.Unmarshal(respBody, response); err != nil {
102 return nil, err
103 }
104
105 return response, nil
106 } else {
107 return nil, wxpay_utility.NewApiException(
108 httpResponse.StatusCode,
109 httpResponse.Header,
110 respBody,
111 )
112 }
113}
114
115type UploadCouponCodesRequest struct {
116 OutRequestNo *string `json:"out_request_no,omitempty"`
117 ProductCouponId *string `json:"product_coupon_id,omitempty"`
118 StockId *string `json:"stock_id,omitempty"`
119 CodeList []string `json:"code_list,omitempty"`
120 BrandId *string `json:"brand_id,omitempty"`
121}
122
123func (o *UploadCouponCodesRequest) MarshalJSON() ([]byte, error) {
124 type Alias UploadCouponCodesRequest
125 a := &struct {
126 ProductCouponId *string `json:"product_coupon_id,omitempty"`
127 StockId *string `json:"stock_id,omitempty"`
128 *Alias
129 }{
130
131 ProductCouponId: nil,
132 StockId: nil,
133 Alias: (*Alias)(o),
134 }
135 return json.Marshal(a)
136}
137
138type UploadCouponCodesResponse struct {
139 TotalCount *int64 `json:"total_count,omitempty"`
140 SuccessCodeList []string `json:"success_code_list,omitempty"`
141 FailedCodeList []FailedCouponCodeInfo `json:"failed_code_list,omitempty"`
142 AlreadyExistCodeList []string `json:"already_exist_code_list,omitempty"`
143 DuplicateCodeList []string `json:"duplicate_code_list,omitempty"`
144}
145
146type FailedCouponCodeInfo struct {
147 CouponCode *string `json:"coupon_code,omitempty"`
148 Code *string `json:"code,omitempty"`
149 Message *string `json:"message,omitempty"`
150}
151