core

package
v0.0.0-...-4f3ec7a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

微信支付api v3 证书相关API接口

微信支付api v3 authorization生成器

微信支付api v3 错误类

微信支付api v3 订单相关API接口

微信支付api v3 签名器

Index

Constants

View Source
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 相关常量

View Source
const (
	WechatPayTimestamp = "Wechatpay-Timestamp" // 微信支付回包时间戳
	WechatPayNonce     = "Wechatpay-Nonce"     // 微信支付回包随机字符串
	WechatPaySignature = "Wechatpay-Signature" // 微信支付回包签名信息
	WechatPaySerial    = "Wechatpay-Serial"    // 微信支付回包平台序列号
	RequestID          = "Request-Id"          // 微信支付回包请求ID
)

http response header 相关常量

View Source
const (
	FormatMessage = "%s\n%s\n%d\n%s\n%s\n"                                           //格式化信息
	Symbols       = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 生成随机字符串时依赖所有内容
	NonceLength   = 32                                                               // 随机字符串的长度
	FiveMinute    = 5 * 60                                                           // 回包校验最长时间
	CERTIFICATE   = "CERTIFICATE"                                                    // 证书标识
)

Variables

View Source
var WithoutValidator = &WechatPayNullValidator{}

Functions

func CheckResponse

func CheckResponse(res *http.Response) ([]byte, error)

CheckResponse 校验回包是否有错误

当http回包的状态码的范围不是200-299之间的时候,会返回相应的错误信息,主要包括http状态码、回包错误码、回包错误信息提示

func CreateFormField

func CreateFormField(w *multipart.Writer, fieldName, contentType string, fieldValue []byte) error

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

func CreateFormFile(w *multipart.Writer, filename, contentType string, file []byte) error

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 DoRequest

func DoRequest(ctx context.Context, hc *http.Client, credential Credential, validator Validator,
	method, requestURL, contentType, reqBody, signBody string) ([]byte, error)

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 GenerateNonceStr

func GenerateNonceStr(length int) string

GenerateNonceStr 获取随机字符串

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)

func TradeBill

func TradeBill(ctx context.Context, hc *http.Client, credential Credential, validator Validator, date time.Time, billType, tarType string) ([]byte, error)

TradeBill 申请交易账单 文档链接: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_6.shtml

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 自定义的错误类型

func (*Error) Error

func (e *Error) Error() string

Error 返回自定义错误类型的字符串内容

type SHA256WithRSASigner

type SHA256WithRSASigner struct {
	MchCertificateSerialNo string          // 商户证书序列号
	PrivateKey             *rsa.PrivateKey // 商户私钥
}

Sha256WithRSASigner Sha256WithRSA 签名器

func (*SHA256WithRSASigner) GetName

func (s *SHA256WithRSASigner) GetName() string

GetName 获取签名器的名称

func (*SHA256WithRSASigner) GetType

func (s *SHA256WithRSASigner) GetType() string

获取签名器的类型

func (*SHA256WithRSASigner) GetVersion

func (s *SHA256WithRSASigner) GetVersion() string

获取签名器的版本

func (*SHA256WithRSASigner) Sign

func (s *SHA256WithRSASigner) Sign(ctx context.Context, message string) (*SignatureResult, error)

对信息使用Sha256WithRsa的方式进行签名

type SignatureResult

type SignatureResult struct {
	MchCertificateSerialNo string // 商户序列号
	Signature              string // 签名
}

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

type WechatPayCredentials struct {
	Signer Signer // 签名器
	MchID  string // 商户号
}

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 默认验证器

func (*WechatPayDefaultVerifier) Verify

func (verifier *WechatPayDefaultVerifier) Verify(ctx context.Context, serialNumber, message, signature string) error

Verify 不对信息进行验证,仅下载证书的时候使用

type WechatPayNullValidator

type WechatPayNullValidator struct {
}

WechatPayNullValidator 回包校验器

func (*WechatPayNullValidator) Validate

func (validator *WechatPayNullValidator) Validate(ctx context.Context, body []byte, header http.Header) error

Validate 使用验证器对回包进行校验

type WechatPayValidator

type WechatPayValidator struct {
	Verifier Verifier // 验证器
}

WechatPayValidator 回包校验器

func (*WechatPayValidator) Validate

func (validator *WechatPayValidator) Validate(ctx context.Context, body []byte, header http.Header) error

Validate 使用验证器对回包进行校验

type WechatPayVerifier

type WechatPayVerifier struct {
	Certificates map[string]*x509.Certificate // key 微信支付平台证书序列号 value 微信支付平台证书 (需要通过下载证书接口获得)
}

WechatPayVerifier 微信支付验证器

func (*WechatPayVerifier) Verify

func (verifier *WechatPayVerifier) Verify(ctx context.Context, serialNumber, message, signature string) error

Verify 对回包中的签名信息进行验证,验证信息是否被篡改、劫持等

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL