
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "time"
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 := &TransferRequest{
28 Receiver: &ReceiverEntity{
29 Type: RECEIVERTYPE_MERCHANT.Ptr(),
30 MchInfo: &MerchantInfo{
31 Mchid: wxpay_utility.String("1900001108"),
32 },
33 },
34 OutBillNo: wxpay_utility.String("plfk2020042013"),
35 Amount: wxpay_utility.Int64(10000),
36 TransferRemark: wxpay_utility.String("直播违规扣罚"),
37 SponsorMchid: wxpay_utility.String("1900001109"),
38 TransferSceneId: wxpay_utility.String("1001"),
39 UserRecvPerception: wxpay_utility.String("退货运费补偿"),
40 }
41
42 response, err := CompensationBillTransfer(config, request)
43 if err != nil {
44 fmt.Printf("请求失败: %+v\n", err)
45
46 return
47 }
48
49
50 fmt.Printf("请求成功: %+v\n", response)
51}
52
53func CompensationBillTransfer(config *wxpay_utility.MchConfig, request *TransferRequest) (response *MchTransferBillEntity, err error) {
54 const (
55 host = "https://api.mch.weixin.qq.com"
56 method = "POST"
57 path = "/v3/platsolution/ecommerce/mch-transfer/compensate-bills"
58 )
59
60 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
61 if err != nil {
62 return nil, err
63 }
64 reqBody, err := json.Marshal(request)
65 if err != nil {
66 return nil, err
67 }
68 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Accept", "application/json")
73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
74 httpRequest.Header.Set("Content-Type", "application/json")
75 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest.Header.Set("Authorization", authorization)
80
81 client := &http.Client{}
82 httpResponse, err := client.Do(httpRequest)
83 if err != nil {
84 return nil, err
85 }
86
87 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
88 if err != nil {
89 return nil, err
90 }
91
92 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
93
94 err = wxpay_utility.ValidateResponse(
95 config.WechatPayPublicKeyId(),
96 config.WechatPayPublicKey(),
97 &httpResponse.Header,
98 respBody,
99 )
100 if err != nil {
101 return nil, err
102 }
103
104 if err := json.Unmarshal(respBody, response); err != nil {
105 return nil, err
106 }
107
108 return response, nil
109 } else {
110 return nil, wxpay_utility.NewApiException(
111 httpResponse.StatusCode,
112 httpResponse.Header,
113 respBody,
114 )
115 }
116}
117
118type TransferRequest struct {
119 Receiver *ReceiverEntity `json:"receiver,omitempty"`
120 OutBillNo *string `json:"out_bill_no,omitempty"`
121 Amount *int64 `json:"amount,omitempty"`
122 TransferRemark *string `json:"transfer_remark,omitempty"`
123 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
124 TransferSceneId *string `json:"transfer_scene_id,omitempty"`
125 UserRecvPerception *string `json:"user_recv_perception,omitempty"`
126}
127
128type MchTransferBillEntity struct {
129 SpMchid *string `json:"sp_mchid,omitempty"`
130 ReceiverDetail *ReceiverDetailInfo `json:"receiver_detail,omitempty"`
131 OutBillNo *string `json:"out_bill_no,omitempty"`
132 Amount *int64 `json:"amount,omitempty"`
133 TransferRemark *string `json:"transfer_remark,omitempty"`
134 BillId *string `json:"bill_id,omitempty"`
135 State *TransferStatus `json:"state,omitempty"`
136 AcceptTime *time.Time `json:"accept_time,omitempty"`
137 SuccessTime *time.Time `json:"success_time,omitempty"`
138 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
139}
140
141type ReceiverEntity struct {
142 Type *ReceiverType `json:"type,omitempty"`
143 TransactionInfo *TransactionInfo `json:"transaction_info,omitempty"`
144}
145
146type ReceiverDetailInfo struct {
147 Receiver *ReceiverEntity `json:"receiver,omitempty"`
148}
149
150type TransferStatus string
151
152func (e TransferStatus) Ptr() *TransferStatus {
153 return &e
154}
155
156const (
157 TRANSFERSTATUS_ACCEPTED TransferStatus = "ACCEPTED"
158 TRANSFERSTATUS_SUCCESS TransferStatus = "SUCCESS"
159 TRANSFERSTATUS_CLOSED TransferStatus = "CLOSED"
160 TRANSFERSTATUS_WAIT_USER_CONFIRM TransferStatus = "WAIT_USER_CONFIRM"
161 TRANSFERSTATUS_CANCELING TransferStatus = "CANCELING"
162 TRANSFERSTATUS_CANCELLED TransferStatus = "CANCELLED"
163)
164
165type ReceiverType string
166
167func (e ReceiverType) Ptr() *ReceiverType {
168 return &e
169}
170
171const (
172 RECEIVERTYPE_TRANSACTION_USER ReceiverType = "TRANSACTION_USER"
173)
174
175type TransactionInfo struct {
176 TransactionId *string `json:"transaction_id,omitempty"`
177 Type *TransactionType `json:"type,omitempty"`
178}
179
180type TransactionType string
181
182func (e TransactionType) Ptr() *TransactionType {
183 return &e
184}
185
186const (
187 TRANSACTIONTYPE_WXPAY TransactionType = "WXPAY"
188 TRANSACTIONTYPE_WXVALUE TransactionType = "WXVALUE"
189)
190