
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 := &PresignContractByH5Request{
27 Appid: wxpay_utility.String("wxd678efh567hg6787"),
28 PlanId: wxpay_utility.String("WxCreditRepaymentPlan12345678"),
29 OutContractCode: wxpay_utility.String("WxCreditRepayment20200910100000"),
30 DisplayName: wxpay_utility.String("*明"),
31 ContractNotifyUrl: wxpay_utility.String("https://yourapp.com/notify"),
32 RepaymentDay: wxpay_utility.Int64(1),
33 RepaymentAmountLimit: wxpay_utility.String("10000"),
34 SubMchid: wxpay_utility.String("1000000109"),
35 SubAppid: wxpay_utility.String("wxcbda96de0b165489"),
36 RealIdentity: &RealIdentity{
37 IdentityType: REALIDENTITYTYPE_REAL_IDENTITY_TYPE_ID_CARD.Ptr(),
38 IdentityId: wxpay_utility.String("1axtI9EZUr0343c89xQznxau+cRWPKP4YhVAoj/mEoNSgJh0nvuqQJ1cdXZHGUzyBjOtuLeVq+OLOaWGUScSM8+QLBk+kOK8QyR1TUo44YrmIl5KkjWLa3dpNJt+nIkpN1e7yr2mxO9LGqJdeQXDCHvOiUTK6lnAVxWY0ZrF4OQFmHqOCvqTaF3VxD5a8AT3gxyayF04IJSfPV0NheZvWFBHIUeRPf1/WQmJ/wXu2MfAuESB0dLBglT85i0Rws2GopaTUNiBuJBHdg01bR+YZvSYv8EJPyxuyYVh5bg/wSotNTYnVp13OUzMBKu25yOW5+RnunesXwq9ogPnSj7EDA=="),
39 RealName: wxpay_utility.String("OEimkKuua8igpd+0YDgqF2Z61leeGD7x87j3PhzsBhAK+/lltXENEOf6ThgV7niChzIpDujIpefE2JFZCJFurf6IV5rxNWBbjFeHYyUGAbf8zDx2Wbi7y7BGjApltIhsxAk+LhQ648Kcw4FMidL/79Gh3nHUfSGX+nEl3gtNi24Ilt8Vu0MMXhA1suDzlpbNDDrYnAhb1eeMziSu8jW7lDouwJXjGh9zgFPUuTxfOpteJIZSy8HWR+QPugdXBNouEezDPN8kojKbJHRtjaBJY3n3agkXShI5yvnc+DCYM+4VgNWZpTx9D0w3KAylJPKAAluYMzj4VXzZurjpruDg2A=="),
40 },
41 TradeInfo: &TradeInfo{
42 OutTradeNo: wxpay_utility.String("1217752501201407033233368018"),
43 DeductAmount: wxpay_utility.String("10000"),
44 Description: wxpay_utility.String("信贷还款"),
45 Attach: wxpay_utility.String("备注信息"),
46 NotifyUrl: wxpay_utility.String("https://www.test.com"),
47 },
48 JumpInfo: &JumpInfo{
49 JumpAppid: wxpay_utility.String("example_jump_appid"),
50 },
51 }
52
53 response, err := PartnerPresignContractByH5(config, request)
54 if err != nil {
55 fmt.Printf("请求失败: %+v\n", err)
56
57 return
58 }
59
60
61 fmt.Printf("请求成功: %+v\n", response)
62}
63
64func PartnerPresignContractByH5(config *wxpay_utility.MchConfig, request *PresignContractByH5Request) (response *PresignContractByH5Response, err error) {
65 const (
66 host = "https://api.mch.weixin.qq.com"
67 method = "POST"
68 path = "/v3/credit-repayment/partner/contracts/h5-presign"
69 )
70
71 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
72 if err != nil {
73 return nil, err
74 }
75 reqBody, err := json.Marshal(request)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
80 if err != nil {
81 return nil, err
82 }
83 httpRequest.Header.Set("Accept", "application/json")
84 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
85 httpRequest.Header.Set("Content-Type", "application/json")
86 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
87 if err != nil {
88 return nil, err
89 }
90 httpRequest.Header.Set("Authorization", authorization)
91
92 client := &http.Client{}
93 httpResponse, err := client.Do(httpRequest)
94 if err != nil {
95 return nil, err
96 }
97 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
98 if err != nil {
99 return nil, err
100 }
101 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
102
103 err = wxpay_utility.ValidateResponse(
104 config.WechatPayPublicKeyId(),
105 config.WechatPayPublicKey(),
106 &httpResponse.Header,
107 respBody,
108 )
109 if err != nil {
110 return nil, err
111 }
112 response := &PresignContractByH5Response{}
113 if err := json.Unmarshal(respBody, response); err != nil {
114 return nil, err
115 }
116
117 return response, nil
118 } else {
119 return nil, wxpay_utility.NewApiException(
120 httpResponse.StatusCode,
121 httpResponse.Header,
122 respBody,
123 )
124 }
125}
126
127type PresignContractByH5Request struct {
128 Appid *string `json:"appid,omitempty"`
129 PlanId *string `json:"plan_id,omitempty"`
130 OutContractCode *string `json:"out_contract_code,omitempty"`
131 DisplayName *string `json:"display_name,omitempty"`
132 ContractNotifyUrl *string `json:"contract_notify_url,omitempty"`
133 RepaymentDay *int64 `json:"repayment_day,omitempty"`
134 RepaymentAmountLimit *string `json:"repayment_amount_limit,omitempty"`
135 SubMchid *string `json:"sub_mchid,omitempty"`
136 SubAppid *string `json:"sub_appid,omitempty"`
137 RealIdentity *RealIdentity `json:"real_identity,omitempty"`
138 TradeInfo *TradeInfo `json:"trade_info,omitempty"`
139 JumpInfo *JumpInfo `json:"jump_info,omitempty"`
140}
141
142type PresignContractByH5Response struct {
143 RedirectUrl *string `json:"redirect_url,omitempty"`
144}
145
146type RealIdentity struct {
147 IdentityType *RealIdentityType `json:"identity_type,omitempty"`
148 IdentityId *string `json:"identity_id,omitempty"`
149 RealName *string `json:"real_name,omitempty"`
150}
151
152type TradeInfo struct {
153 OutTradeNo *string `json:"out_trade_no,omitempty"`
154 DeductAmount *string `json:"deduct_amount,omitempty"`
155 Description *string `json:"description,omitempty"`
156 Attach *string `json:"attach,omitempty"`
157 NotifyUrl *string `json:"notify_url,omitempty"`
158}
159
160type JumpInfo struct {
161 JumpAppid *string `json:"jump_appid,omitempty"`
162}
163
164type RealIdentityType string
165
166func (e RealIdentityType) Ptr() *RealIdentityType {
167 return &e
168}
169
170const (
171 REALIDENTITYTYPE_REAL_IDENTITY_TYPE_INVALID RealIdentityType = "REAL_IDENTITY_TYPE_INVALID"
172 REALIDENTITYTYPE_REAL_IDENTITY_TYPE_ID_CARD RealIdentityType = "REAL_IDENTITY_TYPE_ID_CARD"
173)
174