Documentation ¶
Overview ¶
微信支付api v3 证书相关API接口
微信支付api v3 authorization生成器
微信支付api v3 错误类
微信支付api v3 订单相关API接口
微信支付api v3 签名器
Index ¶
- Constants
- Variables
- func CheckResponse(res *http.Response) ([]byte, error)
- func CreateFormField(w *multipart.Writer, fieldName, contentType string, fieldValue []byte) error
- func CreateFormFile(w *multipart.Writer, filename, contentType string, file []byte) error
- func Delete(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func DoRequest(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func DownloadBill(ctx context.Context, hc *http.Client, credential Credential, bill model.Bill) ([]byte, error)
- func FundflowBill(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func GenerateNonceStr(length int) string
- func Get(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func GetCertificates(ctx context.Context, hc *http.Client, credential Credential) ([]model.CertificateInfo, error)
- func OrderQuery(ctx context.Context, hc *http.Client, reqURL string, credential Credential, ...) (model.TradeQuery, error)
- func Patch(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func Post(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func Put(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- func Refunds(ctx context.Context, hc *http.Client, refundsReq model.RefundsReq, ...) (model.RefundsOrder, error)
- func TradeBill(ctx context.Context, hc *http.Client, credential Credential, ...) ([]byte, error)
- type Credential
- type Error
- type SHA256WithRSASigner
- type SignatureResult
- type Signer
- type Validator
- type Verifier
- type WechatPayCredentials
- type WechatPayDefaultVerifier
- type WechatPayNullValidator
- type WechatPayValidator
- type WechatPayVerifier
Constants ¶
const ( Version = "1.0" // client版本 Authorization = "Authorization" // header中的Authorization字段 Accept = "Accept" // header中的Accept字段 ContentType = "Content-Type" // header中的ContentType字段 ApplicationJSON = "application/json" // ContentType为application/json ImageJPG = "image/jpg" // ContentType为image/jpg VideoMP4 = "video/mp4" // ContentType为video/mp4 UserAgent = "User-Agent" // header中的UserAgent字段 UserAgentContent = "WechatPay-Go-HttpClient/" + Version // UserAgent中的信息 HeaderAuthorization = "WECHATPAY2-SHA256-RSA2048 mchid=\"%s\",nonce_str=\"%s\",timestamp=\"%d\",serial_no=\"%s\"," + "signature=\"%s\"" //Authorization信息 )
http request header 相关常量
const ( WechatPayTimestamp = "Wechatpay-Timestamp" // 微信支付回包时间戳 WechatPayNonce = "Wechatpay-Nonce" // 微信支付回包随机字符串 WechatPaySignature = "Wechatpay-Signature" // 微信支付回包签名信息 WechatPaySerial = "Wechatpay-Serial" // 微信支付回包平台序列号 RequestID = "Request-Id" // 微信支付回包请求ID )
http response header 相关常量
const ( FormatMessage = "%s\n%s\n%d\n%s\n%s\n" //格式化信息 Symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 生成随机字符串时依赖所有内容 NonceLength = 32 // 随机字符串的长度 FiveMinute = 5 * 60 // 回包校验最长时间 CERTIFICATE = "CERTIFICATE" // 证书标识 )
Variables ¶
var WithoutValidator = &WechatPayNullValidator{}
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)
func Delete ¶
func Delete(ctx context.Context, hc *http.Client, credential Credential, validator Validator, requestURL string, requestBody interface{}) ([]byte, error)
Delete 向微信支付发送一个http delete请求
func DownloadBill ¶
func DownloadBill(ctx context.Context, hc *http.Client, credential Credential, bill model.Bill) ([]byte, error)
DownloadBill 下载账单 文档链接: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml
func FundflowBill ¶
func FundflowBill(ctx context.Context, hc *http.Client, credential Credential, validator Validator, date time.Time, accountType, tarType string) ([]byte, error)
FundflowBill 申请资金账单 文档链接: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_7.shtml
func Get ¶
func Get(ctx context.Context, hc *http.Client, credential Credential, validator Validator, requestURL string) ([]byte, error)
Get 向微信支付发送一个http get请求
func GetCertificates ¶
func GetCertificates(ctx context.Context, hc *http.Client, credential Credential) ([]model.CertificateInfo, error)
GetCertificatesContext 获取平台证书列表 文档链接: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/wechatpay/wechatpay5_1.shtml
func OrderQuery ¶
func OrderQuery(ctx context.Context, hc *http.Client, reqURL string, credential Credential, validator Validator) (model.TradeQuery, error)
OrderQuery 查询订单API 文档链接: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
func Patch ¶
func Patch(ctx context.Context, hc *http.Client, credential Credential, validator Validator, requestURL string, requestBody interface{}) ([]byte, error)
Patch 向微信支付发送一个http patch请求
func Post ¶
func Post(ctx context.Context, hc *http.Client, credential Credential, validator Validator, requestURL string, requestBody interface{}) ([]byte, error)
Post 向微信支付发送一个http post请求
func Put ¶
func Put(ctx context.Context, hc *http.Client, credential Credential, validator Validator, requestURL string, requestBody interface{}) ([]byte, error)
Put 向微信支付发送一个http put请求
func Refunds ¶
func Refunds(ctx context.Context, hc *http.Client, refundsReq model.RefundsReq, credential Credential, validator Validator) (model.RefundsOrder, error)
Types ¶
type Credential ¶
type Credential interface { GenerateAuthorizationHeader(ctx context.Context, method, canonicalURL, signBody string) (authorization string, err error) }
Credential Authorization信息生成器
type Error ¶
type Error struct { StatusCode int `json:"status_code"` // http 回包的状态码信息 Code string `json:"code"` // 将回包中body解析后的错误码信息,仅回包不符合预期时存在 Message string `json:"message"` // 将回包中body解析后的message信息,仅回包不符合预期时存在 Details []interface{} `json:"details,omitempty"` // 将回包中body解析出的detail信息,仅回包不符合预期时存在 Body string `json:"body,omitempty"` // http回包中的body信息 Header http.Header `json:"header,omitempty"` // http回包中的header信息 }
Error 自定义的错误类型
type SHA256WithRSASigner ¶
type SHA256WithRSASigner struct { MchCertificateSerialNo string // 商户证书序列号 PrivateKey *rsa.PrivateKey // 商户私钥 }
Sha256WithRSASigner Sha256WithRSA 签名器
func (*SHA256WithRSASigner) GetName ¶
func (s *SHA256WithRSASigner) GetName() string
GetName 获取签名器的名称
func (*SHA256WithRSASigner) Sign ¶
func (s *SHA256WithRSASigner) Sign(ctx context.Context, message string) (*SignatureResult, error)
对信息使用Sha256WithRsa的方式进行签名
type SignatureResult ¶
SignatureResult 签名结果
type Signer ¶
type Signer interface { GetName() string // 获取签名器的名称 GetType() string // 获取签名器的类型 GetVersion() string // 获取签名器的版本 Sign(ctx context.Context, message string) (*SignatureResult, error) // 对信息进行签名 }
Signer 签名生成器
type Validator ¶
type Validator interface {
Validate(ctx context.Context, body []byte, header http.Header) error // 对http回包进行校验
}
Validator 回包校验器
type Verifier ¶
type Verifier interface {
Verify(ctx context.Context, serial, message, signature string) error // 对签名信息进行验证
}
Verifier 验证器
type WechatPayCredentials ¶
WechatPayCredentials authorization生成器
func (*WechatPayCredentials) GenerateAuthorizationHeader ¶
func (c *WechatPayCredentials) GenerateAuthorizationHeader(ctx context.Context, method, canonicalURL, signBody string) (authorization string, err error)
GenerateAuthorizationHeader 生成http request header 中的authorization信息
type WechatPayDefaultVerifier ¶
type WechatPayDefaultVerifier struct { }
WechatPayDefaultVerifier 默认验证器
type WechatPayValidator ¶
type WechatPayValidator struct {
Verifier Verifier // 验证器
}
WechatPayValidator 回包校验器
type WechatPayVerifier ¶
type WechatPayVerifier struct {
Certificates map[string]*x509.Certificate // key 微信支付平台证书序列号 value 微信支付平台证书 (需要通过下载证书接口获得)
}
WechatPayVerifier 微信支付验证器