
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10)
11
12func main() {
13
14 config, err := wxpay_brand_utility.CreateBrandConfig(
15 "xxxxxxxx",
16 "1DDE55AD98Exxxxxxxxxx",
17 "/path/to/apiclient_key.pem",
18 "PUB_KEY_ID_xxxxxxxxxxxxx",
19 "/path/to/wxp_pub.pem",
20 )
21 if err != nil {
22 fmt.Println(err)
23 return
24 }
25
26 request := &ConfirmPointExchangeCouponRequest{
27 RecordId: wxpay_brand_utility.String("pbLatjvWOibDc5-TBnbUk1pD12o0"),
28 ExchangeCouponTemplateId: wxpay_brand_utility.String("0Flpo7FqTQ"),
29 CardId: wxpay_brand_utility.String("pbLatjvWOibDc5-TBnbUk1pD12o0"),
30 Openid: wxpay_brand_utility.String("obLatjnx9gnqzS4myYGmLZ7LgLBA"),
31 UserCardCode: wxpay_brand_utility.String("478515832665"),
32 Result: POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_ALLOW.Ptr(),
33 RejectReason: wxpay_brand_utility.String("积分不足"),
34 }
35
36 response, err := ConfirmPointExchangeCoupon(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 ConfirmPointExchangeCoupon(config *wxpay_brand_utility.BrandConfig, request *ConfirmPointExchangeCouponRequest) (response *PointExchangeCoupon, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/brand/card-member/user-points/exchange-coupon/confirm"
52 )
53
54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
55 if err != nil {
56 return nil, err
57 }
58 reqBody, err := json.Marshal(request)
59 if err != nil {
60 return nil, err
61 }
62 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
63 if err != nil {
64 return nil, err
65 }
66 httpRequest.Header.Set("Accept", "application/json")
67 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
68 httpRequest.Header.Set("Content-Type", "application/json")
69 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
70 if err != nil {
71 return nil, err
72 }
73 httpRequest.Header.Set("Authorization", authorization)
74
75 client := &http.Client{}
76 httpResponse, err := client.Do(httpRequest)
77 if err != nil {
78 return nil, err
79 }
80 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
81 if err != nil {
82 return nil, err
83 }
84 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
85
86 err = wxpay_brand_utility.ValidateResponse(
87 config.WechatPayPublicKeyId(),
88 config.WechatPayPublicKey(),
89 &httpResponse.Header,
90 respBody,
91 )
92 if err != nil {
93 return nil, err
94 }
95 response := &PointExchangeCoupon{}
96 if err := json.Unmarshal(respBody, response); err != nil {
97 return nil, err
98 }
99
100 return response, nil
101 } else {
102 return nil, wxpay_brand_utility.NewApiException(
103 httpResponse.StatusCode,
104 httpResponse.Header,
105 respBody,
106 )
107 }
108}
109
110type ConfirmPointExchangeCouponRequest struct {
111 RecordId *string `json:"record_id,omitempty"`
112 ExchangeCouponTemplateId *string `json:"exchange_coupon_template_id,omitempty"`
113 CardId *string `json:"card_id,omitempty"`
114 Openid *string `json:"openid,omitempty"`
115 UserCardCode *string `json:"user_card_code,omitempty"`
116 Result *PointExchangeCouponResult `json:"result,omitempty"`
117 RejectReason *string `json:"reject_reason,omitempty"`
118}
119
120type PointExchangeCoupon struct {
121 RecordId *string `json:"record_id,omitempty"`
122 ExchangeCouponTemplateId *string `json:"exchange_coupon_template_id,omitempty"`
123 BrandId *string `json:"brand_id,omitempty"`
124 CardId *string `json:"card_id,omitempty"`
125 Openid *string `json:"openid,omitempty"`
126 UserCardCode *string `json:"user_card_code,omitempty"`
127 State *PointExchangeCouponState `json:"state,omitempty"`
128 ProductCouponId *string `json:"product_coupon_id,omitempty"`
129 ProductCouponStockType *ProductCouponStockType `json:"product_coupon_stock_type,omitempty"`
130 StockId *string `json:"stock_id,omitempty"`
131 CouponCode *string `json:"coupon_code,omitempty"`
132 RejectReason *string `json:"reject_reason,omitempty"`
133}
134
135type PointExchangeCouponResult string
136
137func (e PointExchangeCouponResult) Ptr() *PointExchangeCouponResult {
138 return &e
139}
140
141const (
142 POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_ALLOW PointExchangeCouponResult = "POINT_EXCHANGE_COUPON_ALLOW"
143 POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_REJECT PointExchangeCouponResult = "POINT_EXCHANGE_COUPON_REJECT"
144)
145
146type PointExchangeCouponState string
147
148func (e PointExchangeCouponState) Ptr() *PointExchangeCouponState {
149 return &e
150}
151
152const (
153 POINTEXCHANGECOUPONSTATE_POINT_EXCHANGE_COUPON_SUCCESS PointExchangeCouponState = "POINT_EXCHANGE_COUPON_SUCCESS"
154 POINTEXCHANGECOUPONSTATE_POINT_EXCHANGE_COUPON_FAIL PointExchangeCouponState = "POINT_EXCHANGE_COUPON_FAIL"
155)
156
157type ProductCouponStockType string
158
159func (e ProductCouponStockType) Ptr() *ProductCouponStockType {
160 return &e
161}
162
163const (
164 PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_BUSI_FAVOR ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_BUSI_FAVOR"
165 PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_CASH_FAVOR ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_CASH_FAVOR"
166 PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_BRAND_FAVOR ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_BRAND_FAVOR"
167)
168