
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "time"
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 := &PartnerJsapiContractPrepayRequest{
28 SpAppid: wxpay_utility.String("wx8888888888888888"),
29 SpMchid: wxpay_utility.String("1230000109"),
30 SubAppid: wxpay_utility.String("wxd678efh567hg6999"),
31 SubMchid: wxpay_utility.String("1900000109"),
32 Description: wxpay_utility.String("Image形象店-深圳腾大-QQ公仔"),
33 OutTradeNo: wxpay_utility.String("1217752501201407033233368018"),
34 TimeExpire: wxpay_utility.Time(time.Now()),
35 Attach: wxpay_utility.String("自定义数据"),
36 NotifyUrl: wxpay_utility.String(" https://www.weixin.qq.com/wxpay/pay.php"),
37 GoodsTag: wxpay_utility.String("WXG"),
38 SettleInfo: &PartnerSettleInfo{
39 ProfitSharing: wxpay_utility.Bool(true),
40 },
41 SupportFapiao: wxpay_utility.Bool(true),
42 Amount: &CommReqAmountInfo{
43 Total: wxpay_utility.Int64(100),
44 Currency: wxpay_utility.String("CNY"),
45 },
46 Payer: &PartnerJsapiReqPayerInfo{
47 SpOpenid: wxpay_utility.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o "),
48 SubOpenid: wxpay_utility.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o "),
49 },
50 Detail: &OrderDetail{
51 CostPrice: wxpay_utility.Int64(608800),
52 InvoiceId: wxpay_utility.String("微信123"),
53 GoodsDetail: []GoodsDetail{GoodsDetail{
54 MerchantGoodsId: wxpay_utility.String("1246464644"),
55 WechatpayGoodsId: wxpay_utility.String("1001"),
56 GoodsName: wxpay_utility.String("iPhoneX 256G"),
57 Quantity: wxpay_utility.Int64(1),
58 UnitPrice: wxpay_utility.Int64(528800),
59 }},
60 },
61 SceneInfo: &CommReqSceneInfo{
62 PayerClientIp: wxpay_utility.String("14.23.150.211"),
63 DeviceId: wxpay_utility.String("013467007045764"),
64 StoreInfo: &StoreInfo{
65 Id: wxpay_utility.String("0001"),
66 Name: wxpay_utility.String("腾讯大厦分店"),
67 AreaCode: wxpay_utility.String("440305"),
68 Address: wxpay_utility.String("广东省深圳市南山区科技中一道10000号"),
69 },
70 },
71 ContractInfo:&PartnerJsapiContractInfo{
72 ContractAppID: wxpay_utility.String("wxd678efh567hg8888"),
73 ContractCode: wxpay_utility.String("100001256"),
74 ContractDisplayAccount: wxpay_utility.String("123456"),
75 RequestSerial: wxpay_utility.String("1659"),
76 ContractNotifyUrl: wxpay_utility.String("https://yoursite.com"),
77 ContractMchID: wxpay_utility.String("1200009811"),
78 PlanID: wxpay_utility.String("3484306348")
79 },
80 }
81
82 response, err := PartnerJsapiContractPrepay(config, request)
83 if err != nil {
84 fmt.Printf("请求失败: %+v\n", err)
85
86 return
87 }
88
89
90 fmt.Printf("请求成功: %+v\n", response)
91}
92
93func PartnerJsapiContractPrepay(config *wxpay_utility.MchConfig, request *PartnerJsapiContractPrepayRequest) (response *PartnerJsapiContractPrepayResponse, err error) {
94 const (
95 host = "https://api.mch.weixin.qq.com"
96 method = "POST"
97 path = "/v3/pay/partner/transactions/jsapi-with-contract"
98 )
99
100 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
101 if err != nil {
102 return nil, err
103 }
104 reqBody, err := json.Marshal(request)
105 if err != nil {
106 return nil, err
107 }
108 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
109 if err != nil {
110 return nil, err
111 }
112 httpRequest.Header.Set("Accept", "application/json")
113 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
114 httpRequest.Header.Set("Content-Type", "application/json")
115 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
116 if err != nil {
117 return nil, err
118 }
119 httpRequest.Header.Set("Authorization", authorization)
120
121 client := &http.Client{}
122 httpResponse, err := client.Do(httpRequest)
123 if err != nil {
124 return nil, err
125 }
126 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
127 if err != nil {
128 return nil, err
129 }
130 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
131
132 err = wxpay_utility.ValidateResponse(
133 config.WechatPayPublicKeyId(),
134 config.WechatPayPublicKey(),
135 &httpResponse.Header,
136 respBody,
137 )
138 if err != nil {
139 return nil, err
140 }
141 response := &PartnerJsapiContractPrepayResponse{}
142 if err := json.Unmarshal(respBody, response); err != nil {
143 return nil, err
144 }
145
146 return response, nil
147 } else {
148 return nil, wxpay_utility.NewApiException(
149 httpResponse.StatusCode,
150 httpResponse.Header,
151 respBody,
152 )
153 }
154}
155
156type PartnerJsapiContractPrepayRequest struct {
157 SpAppid *string `json:"sp_appid,omitempty"`
158 SpMchid *string `json:"sp_mchid,omitempty"`
159 SubAppid *string `json:"sub_appid,omitempty"`
160 SubMchid *string `json:"sub_mchid,omitempty"`
161 Description *string `json:"description,omitempty"`
162 OutTradeNo *string `json:"out_trade_no,omitempty"`
163 TimeExpire *time.Time `json:"time_expire,omitempty"`
164 Attach *string `json:"attach,omitempty"`
165 NotifyUrl *string `json:"notify_url,omitempty"`
166 GoodsTag *string `json:"goods_tag,omitempty"`
167 SettleInfo *PartnerSettleInfo `json:"settle_info,omitempty"`
168 SupportFapiao *bool `json:"support_fapiao,omitempty"`
169 Amount *CommReqAmountInfo `json:"amount,omitempty"`
170 Payer *PartnerJsapiReqPayerInfo `json:"payer,omitempty"`
171 Detail *OrderDetail `json:"detail,omitempty"`
172 SceneInfo *CommReqSceneInfo `json:"scene_info,omitempty"`
173 ContractInfo *PartnerJsapiContractInfo `json:"contract_info,omitempty"`
174}
175
176type PartnerJsapiContractPrepayResponse struct {
177 PrepayId *string `json:"prepay_id,omitempty"`
178}
179
180type PartnerSettleInfo struct {
181 ProfitSharing *bool `json:"profit_sharing,omitempty"`
182}
183
184type CommReqAmountInfo struct {
185 Total *int64 `json:"total,omitempty"`
186 Currency *string `json:"currency,omitempty"`
187}
188
189type PartnerJsapiReqPayerInfo struct {
190 SpOpenid *string `json:"sp_openid,omitempty"`
191 SubOpenid *string `json:"sub_openid,omitempty"`
192}
193
194type OrderDetail struct {
195 CostPrice *int64 `json:"cost_price,omitempty"`
196 InvoiceId *string `json:"invoice_id,omitempty"`
197 GoodsDetail []GoodsDetail `json:"goods_detail,omitempty"`
198}
199
200type CommReqSceneInfo struct {
201 PayerClientIp *string `json:"payer_client_ip,omitempty"`
202 DeviceId *string `json:"device_id,omitempty"`
203 StoreInfo *StoreInfo `json:"store_info,omitempty"`
204}
205
206type PartnerJsapiContractInfo struct{
207 ContractAppID *string`json:"contract_appid,omitempty"`
208 ContractCode *string`json:"contract_code,omitempty"`
209 ContractDisplayAccount *string`json:"contract_display_account,omitempty"`
210 RequestSerial *string`json:"contract_appid,omitempty"`
211 ContractNotifyUrl *string`json:"contract_notify_url"`
212 ContractMchID *string`json:"contract_mchid,omitempty"`
213 PlanID *string`json:"plan_id,omitempty"`
214}
215
216type GoodsDetail struct {
217 MerchantGoodsId *string `json:"merchant_goods_id,omitempty"`
218 WechatpayGoodsId *string `json:"wechatpay_goods_id,omitempty"`
219 GoodsName *string `json:"goods_name,omitempty"`
220 Quantity *int64 `json:"quantity,omitempty"`
221 UnitPrice *int64 `json:"unit_price,omitempty"`
222}
223
224type StoreInfo struct {
225 Id *string `json:"id,omitempty"`
226 Name *string `json:"name,omitempty"`
227 AreaCode *string `json:"area_code,omitempty"`
228 Address *string `json:"address,omitempty"`
229}
230