Documentation ¶
Overview ¶
Package core 微信支付api v3 go http-client 基础库,你可以使用它来创建一个client,并向微信支付发送http请求
只需要你在初始化客户端的时候,传递credential以及validator
credential用来生成http header中的authorization信息
validator则用来校验回包是否被篡改
如果http请求返回的err为nil,一般response.Body 都不为空,你可以尝试对其进行序列化
请注意及时关闭response.Body
Index ¶
- func CheckResponse(res *http.Response) error
- func CreateFormField(w *multipart.Writer, fieldName, contentType string, fieldValue []byte) error
- func CreateFormFile(w *multipart.Writer, filename, contentType string, file []byte) error
- type Client
- func (client *Client) Delete(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
- func (client *Client) Get(ctx context.Context, requestURL string) (*http.Response, error)
- func (client *Client) Patch(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
- func (client *Client) Post(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
- func (client *Client) Put(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
- func (client *Client) RequestInfo() *http.Request
- func (client *Client) Upload(ctx context.Context, requestURL, meta, reqBody, formContentType string) (*http.Response, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckResponse ¶
CheckResponse 校验回包是否有错误
当http回包的状态码的范围不是200-299之间的时候,会返回相应的错误信息,主要包括http状态码、回包错误码、回包错误信息提示
func CreateFormField ¶
CreateFormField 设置form-data 中的普通属性
示例内容
Content-Disposition: form-data; name="meta"; Content-Type: application/json { "filename": "file_test.mp4", "sha256": " hjkahkjsjkfsjk78687dhjahdajhk " }
如果要设置上述内容
CreateFormField(w, "meta", "application/json", meta)
func CreateFormFile ¶
CreateFormFile 设置form-data中的文件
示例内容:
Content-Disposition: form-data; name="file"; filename="file_test.mp4"; Content-Type: video/mp4 pic1 //pic1即为媒体视频的二进制内容
如果要设置上述内容,则CreateFormFile(w, "file_test.mp4", "video/mp4", pic1)
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 微信支付apiv3 基础client
func NewClient ¶
NewClient 初始化一个微信支付apiv3 http client
初始化的时候你可以传递多个配置信息, 比如
ctx := context.Background() opts := []option.ClientOption{ option.WithMerchant(mchID, mchCertificateSerialNumber, privateKey), // 设置商户信息,用于生成签名信息 option.WithWechatPay(wechatPayCertificateList), // 设置微信支付平台证书信息,对回包进行校验 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 }
func (*Client) Delete ¶
func (client *Client) Delete(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
Delete 向微信支付发送一个http delete请求
func (*Client) Patch ¶
func (client *Client) Patch(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
Patch 向微信支付发送一个http patch请求
func (*Client) Post ¶
func (client *Client) Post(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
Post 向微信支付发送一个http post请求
func (*Client) Put ¶
func (client *Client) Put(ctx context.Context, requestURL string, requestBody interface{}) (*http.Response, error)
Put 向微信支付发送一个http put请求
func (*Client) RequestInfo ¶
RequestInfo 返回client请求里的http request信息
func (*Client) Upload ¶
func (client *Client) Upload(ctx context.Context, requestURL, meta, reqBody, formContentType string) (*http.Response, error)
Upload 向微信支付发送上传图片或视频文件请求
上传接口文档https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/tool/chapter3_1.shtml
你需要传入上传接口的url、文件名、文件流以及meta部分,
当前meta部分需要你自己定义一个结构体,后续我们会将上传接口以api的形式提供,直接封装好
pictureByes, err := ioutil.ReadFile(filePath) if err != nil { return err } // 计算文件序列化后的sha256 h := sha256.New() if _, err = h.Write(pictureByes); err != nil { return err } type Meta struct { FileName string `json:"filename" binding:"required"` // 商户上传的媒体图片的名称,商户自定义,必须以JPG、BMP、PNG为后缀。 Sha256 string `json:"sha256" binding:"required"` // 图片文件的文件摘要,即对图片文件的二进制内容进行sha256计算得到的值。 } meta := &Meta{} pictureSha256 := h.Sum(nil) meta.FileName = fileName meta.Sha256 = fmt.Sprintf("%x", string(pictureSha256)) metaByte, _ := json.Marshal(meta) requestBody := &bytes.Buffer{} writer := multipart.NewWriter(requestBody) if err = CreateFormField(writer, "meta", "application/json", metaByte); err != nil { return err } if err = CreateFormFile(writer, fileName, "image/jpg", pictureByes); err != nil { return err } if err = writer.Close(); err != nil { return err } response, err := client.Upload(ctx, uploadURL, string(metaByte), requestBody.String(), writer.FormDataContentType())
Directories ¶
Path | Synopsis |
---|---|
Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口
|
Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口 Package auth 微信支付api v3 go http-client 签名校验相关接口 |
credentials
Package credentials 微信支付api v3 go http-client authorization生成器
|
Package credentials 微信支付api v3 go http-client authorization生成器 |
signers
Package signers 微信支付api v3 go http-client 签名生成器 Package signers 微信支付api v3 go http-client 签名生成器
|
Package signers 微信支付api v3 go http-client 签名生成器 Package signers 微信支付api v3 go http-client 签名生成器 |
validators
Package validators 微信支付api v3 go http-client 回包校验器
|
Package validators 微信支付api v3 go http-client 回包校验器 |
verifiers
Package verifiers 微信支付api v3 go http-client 验证器 Package verifiers 微信支付api v3 go http-client 验证器
|
Package verifiers 微信支付api v3 go http-client 验证器 Package verifiers 微信支付api v3 go http-client 验证器 |
Package consts 微信支付api v3 go http-client 常量
|
Package consts 微信支付api v3 go http-client 常量 |
Package errors 微信支付api v3 go http-client 错误类
|
Package errors 微信支付api v3 go http-client 错误类 |
Package option 微信支付api v3 go http-client Option选项
|
Package option 微信支付api v3 go http-client Option选项 |
Package setting 微信支付api v3 go http-client 配置
|
Package setting 微信支付api v3 go http-client 配置 |