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