mch

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

微信商家平台

Index

Constants

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

Variables

View Source
var DefaultErrorHandler = ErrorHandlerFunc(func(w http.ResponseWriter, r *http.Request, err error) {})
View Source
var LogInfoln = log.Println

Functions

func EditAddressSign

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

收货地址共享接口签名

func NativeURL1

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

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

func NativeURL2

func NativeURL2(codeURL string) string

扫码原生支付模式2的地址, 建议直接用 code_url

func NewTLSHttpClient

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

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

func ServeHTTP

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

func SetLogInfoln

func SetLogInfoln(fn func(v ...interface{}))

沒有加锁, 请确保在初始化阶段调用!

func Sign

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

微信支付签名.

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

Types

type DefaultServer

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

func NewDefaultServer

func NewDefaultServer(appId, mchId, apiKey string, handler MessageHandler) *DefaultServer

func (*DefaultServer) APIKey

func (srv *DefaultServer) APIKey() string

func (*DefaultServer) AppId

func (srv *DefaultServer) AppId() string

func (*DefaultServer) MchId

func (srv *DefaultServer) MchId() string

func (*DefaultServer) MessageHandler

func (srv *DefaultServer) MessageHandler() MessageHandler

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"`
}

func (*Error) Error

func (e *Error) Error() string

type ErrorHandler

type ErrorHandler interface {
	ServeError(http.ResponseWriter, *http.Request, error)
}

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 Interceptor

type Interceptor interface {
	// 拦截 http 请求, 根据需要做一些判断, 返回是否允许后续逻辑继续处理请求, 如返回 false 则表示请求到此为止.
	// 请注意, 后续逻辑需要读取 r.Body 里的内容, 请谨慎读取!
	Intercept(w http.ResponseWriter, r *http.Request, queryValues url.Values) (shouldContinue bool)
}

http 请求拦截器

type InterceptorFunc

type InterceptorFunc func(w http.ResponseWriter, r *http.Request, queryValues url.Values) (shouldContinue bool)

func (InterceptorFunc) Intercept

func (fn InterceptorFunc) Intercept(w http.ResponseWriter, r *http.Request, queryValues url.Values) (shouldContinue bool)

type MessageHandler

type MessageHandler interface {
	ServeMessage(http.ResponseWriter, *Request)
}

微信服务器推送过来的消息(事件)处理接口

type MessageHandlerFunc

type MessageHandlerFunc func(http.ResponseWriter, *Request)

func (MessageHandlerFunc) ServeMessage

func (fn MessageHandlerFunc) ServeMessage(w http.ResponseWriter, r *Request)

type MultiServerFrontend

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

多个 Server 的前端, http.Handler 的实现.

MultiServerFrontend 可以处理多个APP的消息(事件), 但是要求在回调 URL 上加上一个
查询参数(参数名与 urlServerQueryName 一致), 通过这个参数的值来索引对应的 Server.

例如回调 URL 为(urlServerQueryName == "mch_server"):
  http://www.xxx.com/weixin?mch_server=1234567890
那么就可以在后端调用
 MultiServerFrontend.SetServer("1234567890", Server)
来增加一个 Server 来处理 mch_server=1234567890 的消息(事件).

MultiServerFrontend 并发安全, 可以在运行中动态增加和删除 Server.

func NewMultiServerFrontend

func NewMultiServerFrontend(urlServerQueryName string, errHandler ErrorHandler, interceptor Interceptor) *MultiServerFrontend

NewMultiServerFrontend 创建一个新的 MultiServerFrontend.

urlServerQueryName: 回调 URL 上参数名, 这个参数的值就是索引 Server 的 key
errHandler:         错误处理 handler, 可以为 nil
interceptor:        拦截器, 可以为 nil

func (*MultiServerFrontend) DeleteAllServer

func (frontend *MultiServerFrontend) DeleteAllServer()

func (*MultiServerFrontend) DeleteServer

func (frontend *MultiServerFrontend) DeleteServer(serverKey string)

func (*MultiServerFrontend) ServeHTTP

func (frontend *MultiServerFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*MultiServerFrontend) SetServer

func (frontend *MultiServerFrontend) SetServer(serverKey string, server Server) (err error)

type Proxy

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

func NewProxy

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

创建一个新的 Proxy.

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

func (*Proxy) AppId

func (pxy *Proxy) AppId() string

func (*Proxy) MchId

func (pxy *Proxy) MchId() string

func (*Proxy) PostXML

func (pxy *Proxy) PostXML(url string, req map[string]string) (resp map[string]string, err error)

微信支付通用请求方法.

注意: err == nil 表示协议状态都为 SUCCESS(return_code == SUCCESS).

type Request

type Request struct {
	HttpRequest *http.Request // 可以为 nil, 因为某些 http 框架没有提供此参数

	RawMsgXML []byte            // 消息的 XML 文本
	Msg       map[string]string // 解析后的消息
}

消息(事件)请求信息

type Server

type Server interface {
	AppId() string  // AppId, 用于约束消息的 appid, 如果为空表示不约束
	MchId() string  // MchId, 用于约束消息的 mch_id, 如果为空表示不约束
	APIKey() string // API密钥

	MessageHandler() MessageHandler // 获取 MessageHandler
}

type ServerFrontend

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

func NewServerFrontend

func NewServerFrontend(server Server, handler ErrorHandler, interceptor Interceptor) *ServerFrontend

handler, interceptor 均可以为 nil

func (*ServerFrontend) ServeHTTP

func (frontend *ServerFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)

实现 http.Handler.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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