
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
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 := &CancelPartnerServiceOrderRequest{
28 OutOrderNo: wxpay_utility.String("1234323JKHDFE1243252"),
29 ServiceId: wxpay_utility.String("2002000000000558128851361561536"),
30 SubMchid: wxpay_utility.String("1900000109"),
31 Reason: wxpay_utility.String("用户投诉"),
32 }
33
34 err = CancelPartnerServiceOrder(config, request)
35 if err != nil {
36 fmt.Printf("请求失败: %+v\n", err)
37
38 return
39 }
40
41
42 fmt.Println("请求成功")
43}
44
45func CancelPartnerServiceOrder(config *wxpay_utility.MchConfig, request *CancelPartnerServiceOrderRequest) (err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "POST"
49 path = "/v3/payscore/partner/serviceorder/{out_order_no}/cancel"
50 )
51
52 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
53 if err != nil {
54 return err
55 }
56 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_order_no}", url.PathEscape(*request.OutOrderNo), -1)
57 reqBody, err := json.Marshal(request)
58 if err != nil {
59 return err
60 }
61 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
62 if err != nil {
63 return err
64 }
65 httpRequest.Header.Set("Accept", "application/json")
66 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
67 httpRequest.Header.Set("Content-Type", "application/json")
68 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
69 if err != nil {
70 return err
71 }
72 httpRequest.Header.Set("Authorization", authorization)
73
74 client := &http.Client{}
75 httpResponse, err := client.Do(httpRequest)
76 if err != nil {
77 return err
78 }
79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
80 if err != nil {
81 return err
82 }
83 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
84
85 err = wxpay_utility.ValidateResponse(
86 config.WechatPayPublicKeyId(),
87 config.WechatPayPublicKey(),
88 &httpResponse.Header,
89 respBody,
90 )
91 if err != nil {
92 return err
93 }
94 return nil
95 } else {
96 return wxpay_utility.NewApiException(
97 httpResponse.StatusCode,
98 httpResponse.Header,
99 respBody,
100 )
101 }
102}
103
104type CancelPartnerServiceOrderRequest struct {
105 ServiceId *string `json:"service_id,omitempty"`
106 SubMchid *string `json:"sub_mchid,omitempty"`
107 OutOrderNo *string `json:"out_order_no,omitempty"`
108 Reason *string `json:"reason,omitempty"`
109}
110
111func (o *CancelPartnerServiceOrderRequest) MarshalJSON() ([]byte, error) {
112 type Alias CancelPartnerServiceOrderRequest
113 a := &struct {
114 OutOrderNo *string `json:"out_order_no,omitempty"`
115 *Alias
116 }{
117
118 OutOrderNo: nil,
119 Alias: (*Alias)(o),
120 }
121 return json.Marshal(a)
122}
123