package main import ( "context" "crypto/x509" "io/ioutil" "log" "net/http" "time" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/utils" ) const ( mchID = "" // 商户ID mchCertificateSerialNumber = "" // 证书序列号 ) func main() { // 通过私钥的文件路径内容加载私钥 privateKey, err := utils.LoadPrivateKeyWithPath("apiclient_key.pem") public, err := utils.LoadCertificateWithPath("apiclient_cert.pem") // 增加客户端配置 ctx := context.Background() opts := []option.ClientOption{ option.WithMerchant(mchID, mchCertificateSerialNumber, privateKey), // 设置商户信息,用于生成签名信息 option.WithWechatPay([]*x509.Certificate{public}), // 设置微信支付平台证书信息,对回包进行校验 option.WithHTTPClient(&http.Client{}), // 可以不设置 option.WithTimeout(2 * time.Second), // 自行进行超时时间配置 } client, err := core.NewClient(ctx, opts...) if err != nil { log.Printf("new wechat pay client err:%s", err.Error()) return } // 后面可以开始写你的逻辑 var H5 = map[string]interface{}{ "appid": "wx862e63bb06c70539", "mchid": mchID, "description": "商品支付", "out_trade_no": time.Now().Format("20060102150405"), "notify_url": "http://127.0.0.1/v1/orde", "amount": map[string]interface{}{ "total":100, // 支付单位分 "currency": "CNY", }, "scene_info": map[string]interface{}{ "device_id":"12346785", "payer_client_ip":"192.168.8.123", "h5_info": map[string]interface{}{ "type": "iOSAndroidWap", }, }, } // h5支付为例 url := "https://api.mch.weixin.qq.com/v3/pay/transactions/h5" response, err := client.Post(ctx, url, H5) if err != nil { log.Printf("weChatPayHttpClient Get:%s err:%s", url, err.Error()) return } if response.Body != nil { defer response.Body.Close() } body, err := ioutil.ReadAll(response.Body) if err != nil { log.Printf("read response body err:%s", err.Error()) return } log.Printf("response body:%s", string(body)) }