
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10)
11
12func main() {
13
14 config, err := wxpay_utility.CreateMchConfig(
15 "19xxxxxxxx",
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 := &CreateReturnOrderRequest{
27 OutReturnNo: wxpay_utility.String("R20250220103930"),
28 SubMchid: wxpay_utility.String("1900000109"),
29 OutOrderId: wxpay_utility.String("merchant_1123123"),
30 TransactionId: wxpay_utility.String("420000000000000010"),
31 RefundId: wxpay_utility.String("5017752501201407033233368018"),
32 SupplementInfo: wxpay_utility.String("5017752501201407033233368018"),
33 Amount: wxpay_utility.Int64(100),
34 }
35
36 response, err := CreateReturnOrder(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 CreateReturnOrder(config *wxpay_utility.MchConfig, request *CreateReturnOrderRequest) (response *FundsOsReturnOrder, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/v3/funds-to-oversea/return/return-orders"
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_utility.BuildAuthorization(config.MchId(), 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_utility.ExtractResponseBody(httpResponse)
81 if err != nil {
82 return nil, err
83 }
84 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
85
86 err = wxpay_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 := &FundsOsReturnOrder{}
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_utility.NewApiException(
103 httpResponse.StatusCode,
104 httpResponse.Header,
105 respBody,
106 )
107 }
108}
109
110type CreateReturnOrderRequest struct {
111 OutReturnNo *string `json:"out_return_no,omitempty"`
112 SubMchid *string `json:"sub_mchid,omitempty"`
113 OutOrderId *string `json:"out_order_id,omitempty"`
114 TransactionId *string `json:"transaction_id,omitempty"`
115 RefundId *string `json:"refund_id,omitempty"`
116 SupplementInfo *string `json:"supplement_info,omitempty"`
117 Amount *int64 `json:"amount,omitempty"`
118}
119
120type FundsOsReturnOrder struct {
121 OutReturnNo *string `json:"out_return_no,omitempty"`
122 SubMchid *string `json:"sub_mchid,omitempty"`
123 ReturnId *string `json:"return_id,omitempty"`
124 OutOrderId *string `json:"out_order_id,omitempty"`
125 TransactionId *string `json:"transaction_id,omitempty"`
126 RefundId *string `json:"refund_id,omitempty"`
127 SupplementInfo *string `json:"supplement_info,omitempty"`
128 Amount *int64 `json:"amount,omitempty"`
129 State *ReturnState `json:"state,omitempty"`
130 CreateTime *string `json:"create_time,omitempty"`
131 SuccessTime *string `json:"success_time,omitempty"`
132 FailReason *FailReason `json:"fail_reason,omitempty"`
133}
134
135type ReturnState string
136
137func (e ReturnState) Ptr() *ReturnState {
138 return &e
139}
140
141const (
142 RETURNSTATE_PROCESSING ReturnState = "PROCESSING"
143 RETURNSTATE_SUCCESS ReturnState = "SUCCESS"
144 RETURNSTATE_FAILED ReturnState = "FAILED"
145)
146
147type FailReason string
148
149func (e FailReason) Ptr() *FailReason {
150 return &e
151}
152
153const (
154 FAILREASON_RETURN_AMOUNT_NOT_ENOUGH FailReason = "RETURN_AMOUNT_NOT_ENOUGH"
155)
156