
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 := &CreateDeductOrderRequest{
27 ContractId: wxpay_utility.String("20251105000000123456789"),
28 OutTradeNo: wxpay_utility.String("1217752501201407033233368018"),
29 OutRecordId: wxpay_utility.String("1234567abcde"),
30 DeductAmount: wxpay_utility.String("10000"),
31 NotifyUrl: wxpay_utility.String("https://www.test.com"),
32 Description: wxpay_utility.String("信贷还款"),
33 Attach: wxpay_utility.String("备注信息"),
34 }
35
36 response, err := PartnerCreateDeductOrder(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 PartnerCreateDeductOrder(config *wxpay_utility.MchConfig, request *CreateDeductOrderRequest) (response *DeductOrder, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/v3/credit-repayment/partner/deduct-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 := &DeductOrder{}
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 CreateDeductOrderRequest struct {
111 ContractId *string `json:"contract_id,omitempty"`
112 OutTradeNo *string `json:"out_trade_no,omitempty"`
113 OutRecordId *string `json:"out_record_id,omitempty"`
114 DeductAmount *string `json:"deduct_amount,omitempty"`
115 NotifyUrl *string `json:"notify_url,omitempty"`
116 Description *string `json:"description,omitempty"`
117 Attach *string `json:"attach,omitempty"`
118}
119
120type DeductOrder struct {
121 OutTradeNo *string `json:"out_trade_no,omitempty"`
122 OutRecordId *string `json:"out_record_id,omitempty"`
123 Appid *string `json:"appid,omitempty"`
124 SubAppid *string `json:"sub_appid,omitempty"`
125 Openid *string `json:"openid,omitempty"`
126 ContractId *string `json:"contract_id,omitempty"`
127 OrderState *DeductOrderState `json:"order_state,omitempty"`
128 DeductAmount *string `json:"deduct_amount,omitempty"`
129 PaySuccessAmount *string `json:"pay_success_amount,omitempty"`
130 Description *string `json:"description,omitempty"`
131 Attach *string `json:"attach,omitempty"`
132 TransactionId *string `json:"transaction_id,omitempty"`
133}
134
135type DeductOrderState string
136
137func (e DeductOrderState) Ptr() *DeductOrderState {
138 return &e
139}
140
141const (
142 DEDUCTORDERSTATE_DEDUCT_ORDER_STATE_PENDING DeductOrderState = "DEDUCT_ORDER_STATE_PENDING"
143 DEDUCTORDERSTATE_DEDUCT_ORDER_STATE_NOT_PAY DeductOrderState = "DEDUCT_ORDER_STATE_NOT_PAY"
144 DEDUCTORDERSTATE_DEDUCT_ORDER_STATE_PAY_SUCCESS DeductOrderState = "DEDUCT_ORDER_STATE_PAY_SUCCESS"
145 DEDUCTORDERSTATE_DEDUCT_ORDER_STATE_PAY_FAIL DeductOrderState = "DEDUCT_ORDER_STATE_PAY_FAIL"
146)
147