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