core

package
v0.0.0-...-3cd66b9 Latest Latest
Warning

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

Go to latest
Published: May 10, 2017 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

微信商家平台api

Index

Constants

View Source
const (
	ReturnCodeSuccess = "SUCCESS"
	ReturnCodeFail    = "FAIL"
)
View Source
const (
	ResultCodeSuccess = "SUCCESS"
	ResultCodeFail    = "FAIL"
)

Variables

View Source
var (
	ErrNotFoundReturnCode = errors.New("not found return_code parameter")
	ErrNotFoundResultCode = errors.New("not found result_code parameter")
	ErrNotFoundSign       = errors.New("not found sign parameter")
)

Functions

func EditAddressSign

func EditAddressSign(appId, url, timestamp, nonceStr, accessToken string) string

EditAddressSign 收货地址共享接口签名

func FormatTime

func FormatTime(t time.Time) string

FormatTime 将参数 t 格式化成 北京时间yyyyMMddHHmmss

func JsapiSign

func JsapiSign(appId, timeStamp, nonceStr, packageStr, signType string, apiKey string) string

jssdk 支付签名, signType 只支持 "MD5", "SHA1", 传入其他的值会 panic.

func NativeURL1

func NativeURL1(appId, mchId, productId, timestamp, nonceStr, apiKey string) string

扫码原生支付模式1的地址

func NewTLSHttpClient

func NewTLSHttpClient(certFile, keyFile string) (httpClient *http.Client, err error)

NewTLSHttpClient 创建支持双向证书认证的 http.Client

func ParseTime

func ParseTime(value string) (time.Time, error)

ParseTime 将 北京时间yyyyMMddHHmmss 字符串解析到 time.Time

func Sign

func Sign(params map[string]string, apiKey string, fn func() hash.Hash) string

Sign 微信支付签名.

params: 待签名的参数集合
apiKey: api密钥
fn:     func() hash.Hash, 如果为 nil 则默认用 md5.New

Types

type BizError

type BizError struct {
	XMLName     struct{} `xml:"xml"                    json:"-"`
	ResultCode  string   `xml:"result_code"            json:"result_code"`
	ErrCode     string   `xml:"err_code,omitempty"     json:"err_code,omitempty"`
	ErrCodeDesc string   `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
}

业务错误, result_code 不为 SUCCESS 有返回.

func (*BizError) Error

func (e *BizError) Error() string

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(appId, mchId, apiKey string, httpClient *http.Client) *Client

NewClient 创建一个新的 Client.

如果 httpClient == nil 则默认用 http.DefaultClient.

func (*Client) ApiKey

func (clt *Client) ApiKey() string

func (*Client) AppId

func (clt *Client) AppId() string

func (*Client) MchId

func (clt *Client) MchId() string

func (*Client) PostXML

func (clt *Client) PostXML(url string, req map[string]string) (resp map[string]string, err error)

PostXML 是微信支付通用请求方法.

err == nil 表示协议状态为 SUCCESS(return_code==SUCCESS).

type Context

type Context struct {
	Server *Server

	ResponseWriter http.ResponseWriter
	Request        *http.Request

	RequestBody []byte            // 回调请求的 http-body, 就是消息体的原始内容, 记录log可能需要这个信息
	Msg         map[string]string // 请求消息
	// contains filtered or unexported fields
}

Context 是 Handler 处理消息(事件)的上下文环境. 非并发安全!

func (*Context) Abort

func (ctx *Context) Abort()

Abort 阻止系统调用当前 handler 后续的 handlers, 即当前的 handler 处理完毕就返回, 一般在 middleware 中调用.

func (*Context) Get

func (ctx *Context) Get(key string) (value interface{}, exists bool)

Get 返回 Context 中 key 对应的 value, 如果 key 存在的返回 (value, true), 否则返回 (nil, false).

func (*Context) IsAborted

func (ctx *Context) IsAborted() bool

IsAborted 返回 true 如果 Context.Abort() 被调用了, 否则返回 false.

func (*Context) MustGet

func (ctx *Context) MustGet(key string) interface{}

MustGet 返回 Context 中 key 对应的 value, 如果 key 不存在则会 panic.

func (*Context) Next

func (ctx *Context) Next()

Next 中断当前 handler 程序逻辑执行其后续的 handlers, 一般在 middleware 中调用.

func (*Context) Response

func (ctx *Context) Response(msg map[string]string) (err error)

Response 回复消息给微信服务器

func (*Context) Set

func (ctx *Context) Set(key string, value interface{})

Set 存储 key-value pair 到 Context 中.

func (*Context) SetHandlers

func (ctx *Context) SetHandlers(handlers HandlerChain)

SetHandlers 设置 handlers 给 Context.Next() 调用, 务必在 Context.Next() 调用之前设置, 否则会 panic.

NOTE: 此方法一般用不到, 除非你自己实现一个 Handler 给 Server 使用, 参考 HandlerChain.

type Error

type Error struct {
	XMLName    struct{} `xml:"xml"                  json:"-"`
	ReturnCode string   `xml:"return_code"          json:"return_code"`
	ReturnMsg  string   `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
}

协议错误, return_code 不为 SUCCESS 有返回.

func (*Error) Error

func (e *Error) Error() string

type ErrorHandler

type ErrorHandler interface {
	ServeError(http.ResponseWriter, *http.Request, error)
}
var DefaultErrorHandler ErrorHandler = ErrorHandlerFunc(defaultErrorHandlerFunc)

type ErrorHandlerFunc

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)

func (ErrorHandlerFunc) ServeError

func (fn ErrorHandlerFunc) ServeError(w http.ResponseWriter, r *http.Request, err error)

type Handler

type Handler interface {
	ServeMsg(*Context)
}

type HandlerChain

type HandlerChain []Handler

func (*HandlerChain) AppendHandler

func (chain *HandlerChain) AppendHandler(handlers ...Handler)

func (*HandlerChain) AppendHandlerFunc

func (chain *HandlerChain) AppendHandlerFunc(handlers ...func(*Context))

func (HandlerChain) ServeMsg

func (chain HandlerChain) ServeMsg(ctx *Context)

ServeMsg 实现 Handler 接口

type HandlerFunc

type HandlerFunc func(*Context)

func (HandlerFunc) ServeMsg

func (fn HandlerFunc) ServeMsg(ctx *Context)

ServeMsg 实现 Handler 接口

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(appId, mchId, apiKey string, handler Handler, errorHandler ErrorHandler) *Server

NewServer 创建一个新的 Server.

appId:        可选; 公众号的 appid, 如果设置了值则该 Server 只能处理 appid 为该值的消息(事件)
mchId:        可选; 商户号 mch_id, 如果设置了值则该 Server 只能处理 mch_id 为该值的消息(事件)
apiKey:       必须; 商户的签名 key
handler:      必须; 处理微信服务器推送过来的消息(事件)的 Handler
errorHandler: 可选; 用于处理 Server 在处理消息(事件)过程中产生的错误, 如果没有设置则默认使用 DefaultErrorHandler

func (*Server) ApiKey

func (srv *Server) ApiKey() string

func (*Server) AppId

func (srv *Server) AppId() string

func (*Server) MchId

func (srv *Server) MchId() string

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request, queryParams url.Values)

ServeHTTP 处理微信服务器的回调请求, queryParams 参数可以为 nil.

Jump to

Keyboard shortcuts

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