
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 := &DisassociateStoresForStockBundleRequest{
28 ProductCouponId: wxpay_utility.String("200000001"),
29 StockBundleId: wxpay_utility.String("123456789"),
30 StoreList: []StoreInfo{StoreInfo{
31 StoreId: wxpay_utility.String("100000001"),
32 }},
33 BrandId: wxpay_utility.String("120344"),
34 }
35
36 response, err := DisassociateStoresForStockBundle(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 DisassociateStoresForStockBundle(config *wxpay_utility.MchConfig, request *DisassociateStoresForStockBundleRequest) (response *DisassociateStoresResponse, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stock-bundles/{stock_bundle_id}/disassociate-stores"
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, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1)
59 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_bundle_id}", url.PathEscape(*request.StockBundleId), -1)
60 reqBody, err := json.Marshal(request)
61 if err != nil {
62 return nil, err
63 }
64 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
65 if err != nil {
66 return nil, err
67 }
68 httpRequest.Header.Set("Accept", "application/json")
69 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
70 httpRequest.Header.Set("Content-Type", "application/json")
71 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
72 if err != nil {
73 return nil, err
74 }
75 httpRequest.Header.Set("Authorization", authorization)
76
77 client := &http.Client{}
78 httpResponse, err := client.Do(httpRequest)
79 if err != nil {
80 return nil, err
81 }
82 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
83 if err != nil {
84 return nil, err
85 }
86 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
87
88 err = wxpay_utility.ValidateResponse(
89 config.WechatPayPublicKeyId(),
90 config.WechatPayPublicKey(),
91 &httpResponse.Header,
92 respBody,
93 )
94 if err != nil {
95 return nil, err
96 }
97 response := &DisassociateStoresResponse{}
98 if err := json.Unmarshal(respBody, response); err != nil {
99 return nil, err
100 }
101
102 return response, nil
103 } else {
104 return nil, wxpay_utility.NewApiException(
105 httpResponse.StatusCode,
106 httpResponse.Header,
107 respBody,
108 )
109 }
110}
111
112type DisassociateStoresForStockBundleRequest struct {
113 ProductCouponId *string `json:"product_coupon_id,omitempty"`
114 StockBundleId *string `json:"stock_bundle_id,omitempty"`
115 StoreList []StoreInfo `json:"store_list,omitempty"`
116 BrandId *string `json:"brand_id,omitempty"`
117}
118
119func (o *DisassociateStoresForStockBundleRequest) MarshalJSON() ([]byte, error) {
120 type Alias DisassociateStoresForStockBundleRequest
121 a := &struct {
122 ProductCouponId *string `json:"product_coupon_id,omitempty"`
123 StockBundleId *string `json:"stock_bundle_id,omitempty"`
124 *Alias
125 }{
126
127 ProductCouponId: nil,
128 StockBundleId: nil,
129 Alias: (*Alias)(o),
130 }
131 return json.Marshal(a)
132}
133
134type DisassociateStoresResponse struct {
135 TotalCount *int64 `json:"total_count,omitempty"`
136 SuccessStoreList []StoreInfo `json:"success_store_list,omitempty"`
137 FailedStoreList []FailedStoreInfo `json:"failed_store_list,omitempty"`
138}
139
140type StoreInfo struct {
141 StoreId *string `json:"store_id,omitempty"`
142}
143
144type FailedStoreInfo struct {
145 StoreId *string `json:"store_id,omitempty"`
146 Code *string `json:"code,omitempty"`
147 Message *string `json:"message,omitempty"`
148}
149