
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 SubMchid: wxpay_utility.String("1900001109"),
29 Receiver: &ReceiverEntity{
30 Type: RECEIVERTYPE_MERCHANT.Ptr(),
31 MchInfo: &MerchantInfo{
32 Mchid: wxpay_utility.String("1900001108"),
33 },
34 },
35 OutBillNo: wxpay_utility.String("plfk2020042013"),
36 Amount: wxpay_utility.Int64(10000),
37 TransferRemark: wxpay_utility.String("直播违规扣罚"),
38 }
39
40 response, err := InsuranceClaimBillTransfer(config, request)
41 if err != nil {
42 fmt.Printf("请求失败: %+v\n", err)
43
44 return
45 }
46
47
48 fmt.Printf("请求成功: %+v\n", response)
49}
50
51func InsuranceClaimBillTransfer(config *wxpay_utility.MchConfig, request *TransferRequest) (response *MchTransferBillEntity, err error) {
52 const (
53 host = "https://api.mch.weixin.qq.com"
54 method = "POST"
55 path = "/v3/platsolution/ecommerce/mch-transfer/insurance-claim-bills"
56 )
57
58 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
59 if err != nil {
60 return nil, err
61 }
62 reqBody, err := json.Marshal(request)
63 if err != nil {
64 return nil, err
65 }
66 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
67 if err != nil {
68 return nil, err
69 }
70 httpRequest.Header.Set("Accept", "application/json")
71 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
72 httpRequest.Header.Set("Content-Type", "application/json")
73 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
74 if err != nil {
75 return nil, err
76 }
77 httpRequest.Header.Set("Authorization", authorization)
78
79 client := &http.Client{}
80 httpResponse, err := client.Do(httpRequest)
81 if err != nil {
82 return nil, err
83 }
84
85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
86 if err != nil {
87 return nil, err
88 }
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
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 SubMchid *string `json:"sub_mchid,omitempty"`
118 Receiver *ReceiverEntity `json:"receiver,omitempty"`
119 OutBillNo *string `json:"out_bill_no,omitempty"`
120 Amount *int64 `json:"amount,omitempty"`
121 TransferRemark *string `json:"transfer_remark,omitempty"`
122}
123
124type MchTransferBillEntity struct {
125 SpMchid *string `json:"sp_mchid,omitempty"`
126 SubMchid *string `json:"sub_mchid,omitempty"`
127 ReceiverDetail *ReceiverDetailInfo `json:"receiver_detail,omitempty"`
128 OutBillNo *string `json:"out_bill_no,omitempty"`
129 Amount *int64 `json:"amount,omitempty"`
130 TransferRemark *string `json:"transfer_remark,omitempty"`
131 BillId *string `json:"bill_id,omitempty"`
132 State *TransferStatus `json:"state,omitempty"`
133 AcceptTime *time.Time `json:"accept_time,omitempty"`
134 SuccessTime *time.Time `json:"success_time,omitempty"`
135 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
136}
137
138type ReceiverEntity struct {
139 Type *ReceiverType `json:"type,omitempty"`
140 TransactionInfo *TransactionInfo `json:"transaction_info,omitempty"`
141}
142
143type ReceiverDetailInfo struct {
144 Receiver *ReceiverEntity `json:"receiver,omitempty"`
145}
146
147type TransferStatus string
148
149func (e TransferStatus) Ptr() *TransferStatus {
150 return &e
151}
152
153const (
154 TRANSFERSTATUS_ACCEPTED TransferStatus = "ACCEPTED"
155 TRANSFERSTATUS_SUCCESS TransferStatus = "SUCCESS"
156 TRANSFERSTATUS_CLOSED TransferStatus = "CLOSED"
157 TRANSFERSTATUS_WAIT_USER_CONFIRM TransferStatus = "WAIT_USER_CONFIRM"
158 TRANSFERSTATUS_CANCELING TransferStatus = "CANCELING"
159 TRANSFERSTATUS_CANCELLED TransferStatus = "CANCELLED"
160)
161
162type ReceiverType string
163
164func (e ReceiverType) Ptr() *ReceiverType {
165 return &e
166}
167
168const (
169 RECEIVERTYPE_MERCHANT ReceiverType = "MERCHANT"
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