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