
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 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
87 if err != nil {
88 return nil, err
89 }
90 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
91
92 err = wxpay_utility.ValidateResponse(
93 config.WechatPayPublicKeyId(),
94 config.WechatPayPublicKey(),
95 &httpResponse.Header,
96 respBody,
97 )
98 if err != nil {
99 return nil, err
100 }
101 response := &MchTransferBillEntity{}
102 if err := json.Unmarshal(respBody, response); err != nil {
103 return nil, err
104 }
105
106 return response, nil
107 } else {
108 return nil, wxpay_utility.NewApiException(
109 httpResponse.StatusCode,
110 httpResponse.Header,
111 respBody,
112 )
113 }
114}
115
116type TransferRequest struct {
117 Receiver *ReceiverEntity `json:"receiver,omitempty"`
118 OutBillNo *string `json:"out_bill_no,omitempty"`
119 Amount *int64 `json:"amount,omitempty"`
120 TransferRemark *string `json:"transfer_remark,omitempty"`
121 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
122 TransferSceneId *string `json:"transfer_scene_id,omitempty"`
123 UserRecvPerception *string `json:"user_recv_perception,omitempty"`
124}
125
126type MchTransferBillEntity struct {
127 SpMchid *string `json:"sp_mchid,omitempty"`
128 ReceiverDetail *ReceiverDetailInfo `json:"receiver_detail,omitempty"`
129 OutBillNo *string `json:"out_bill_no,omitempty"`
130 Amount *int64 `json:"amount,omitempty"`
131 TransferRemark *string `json:"transfer_remark,omitempty"`
132 BillId *string `json:"bill_id,omitempty"`
133 State *TransferStatus `json:"state,omitempty"`
134 AcceptTime *time.Time `json:"accept_time,omitempty"`
135 SuccessTime *time.Time `json:"success_time,omitempty"`
136 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
137}
138
139type ReceiverEntity struct {
140 Type *ReceiverType `json:"type,omitempty"`
141 TransactionInfo *TransactionInfo `json:"transaction_info,omitempty"`
142}
143
144type ReceiverDetailInfo struct {
145 Receiver *ReceiverEntity `json:"receiver,omitempty"`
146}
147
148type TransferStatus string
149
150func (e TransferStatus) Ptr() *TransferStatus {
151 return &e
152}
153
154const (
155 TRANSFERSTATUS_ACCEPTED TransferStatus = "ACCEPTED"
156 TRANSFERSTATUS_SUCCESS TransferStatus = "SUCCESS"
157 TRANSFERSTATUS_CLOSED TransferStatus = "CLOSED"
158 TRANSFERSTATUS_WAIT_USER_CONFIRM TransferStatus = "WAIT_USER_CONFIRM"
159 TRANSFERSTATUS_CANCELING TransferStatus = "CANCELING"
160 TRANSFERSTATUS_CANCELLED TransferStatus = "CANCELLED"
161)
162
163type ReceiverType string
164
165func (e ReceiverType) Ptr() *ReceiverType {
166 return &e
167}
168
169const (
170 RECEIVERTYPE_TRANSACTION_USER ReceiverType = "TRANSACTION_USER"
171)
172
173type TransactionInfo struct {
174 TransactionId *string `json:"transaction_id,omitempty"`
175 Type *TransactionType `json:"type,omitempty"`
176}
177
178type TransactionType string
179
180func (e TransactionType) Ptr() *TransactionType {
181 return &e
182}
183
184const (
185 TRANSACTIONTYPE_WXPAY TransactionType = "WXPAY"
186 TRANSACTIONTYPE_WXVALUE TransactionType = "WXVALUE"
187)
188