wxpay

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2018 License: MIT Imports: 16 Imported by: 0

README

wxpay

Powered by zch Language License

wxpay 提供了以下的方法:

方法名 说明
MicroPay 刷卡支付
UnifiedOrder 统一下单
OrderQuery 查询订单
Reverse 撤销订单
CloseOrder 关闭订单
Refund 申请退款
RefundQuery 查询退款
DownloadBill 下载对账单
Report 交易保障
ShortUrl 转换短链接
AuthCodeToOpenid 授权码查询openid
  • 参数为Params类型,返回类型也是ParamsParams 是一个 map[string]string 类型。
  • 方法内部会将参数会转换成含有appidmch_idnonce_strsign_typesign的XML;
  • 默认使用MD5进行签名;
  • 通过HTTPS请求得到返回数据后会对其做必要的处理(例如验证签名,签名错误则抛出异常)。
  • 对于downloadBill,无论是否成功都返回Map,且都含有return_codereturn_msg。若成功,其中return_codeSUCCESS,另外data对应对账单数据。

安装

$ go get github.com/objcoding/wxpay

示例

// 新建微信支付客户端
client := wxpay.NewClient(wxpay.NewAccount{
	AppID: "appid",
	MchID: "mchid",
	ApiKey: "apiKey",
	isSanbox: false
})

// 统一下单
params := make(wxpay.Params)
params.SetString("body", "test").
		SetString("out_trade_no", "436577857").
		SetInt64("total_fee", 1).
		SetString("spbill_create_ip", "127.0.0.1").
		SetString("notify_url", "http://notify.objcoding.com/notify").
		SetString("trade_type", "APP")
p, _ := client.UnifiedOrder(params)

// 订单查询
params := make(wxpay.Params)
params.SetString("out_trade_no", "3568785")
p, _ := client.OrderQuery(params)

// 退款
params := make(wxpay.Params)
params.SetString("out_trade_no", "3568785").
		SetString("out_refund_no", "19374568").
		SetInt64("total_fee", 1).
		SetInt64("refund_fee", 1)
p, _ := client.Refund(params)

// 退款查询
params := make(wxpay.Params)
params.SetString("out_refund_no", "3568785")
p, _ := client.RefundQuery(params)

// 创建支付账户
account := wxpay.NewAccount("appid", "mchid", "apiKey", false)

// 设置证书
account.SetCertData("证书地址")

// 设置支付账户
client.setAccount(account)

// 设置http请求超时时间
client.SetHttpConnectTimeoutMs(2000)

// 设置http读取信息流超时时间
client.SetHttpReadTimeoutMs(1000)

// 更改签名类型
client.SetSignType(HMACSHA256)

// 签名
signStr := client.Sign(params)

// 校验签名
b := client.ValidSign(params)

// xml解析
params := wxpay.XmlToMap(xmlStr)

// map封装xml请求参数
b := wxpay.MapToXml(params)

// 支付或退款返回成功信息
return wxpay.Notifies{}.OK()

// 支付或退款返回失败信息
return wxpay.Notifies{}.NotOK("支付失败或退款失败了")

License

MIT license

Documentation

Index

Constants

View Source
const (
	FAIL                         = "FAIL"
	SUCCESS                      = "SUCCESS"
	HMACSHA256                   = "HMACSHA256"
	MD5                          = "MD5"
	FIELD_SIGN                   = "sign"
	FIELD_SIGN_TYPE              = "sign_type"
	MICROPAY_URL                 = "https://api.mch.weixin.qq.com/pay/micropay"
	UNIFIEDORDER_URL             = "https://api.mch.weixin.qq.com/pay/unifiedorder"
	ORDERQUERY_URL               = "https://api.mch.weixin.qq.com/pay/orderquery"
	REVERSE_URL                  = "https://api.mch.weixin.qq.com/secapi/pay/reverse"
	CLOSEORDER_URL               = "https://api.mch.weixin.qq.com/pay/closeorder"
	REFUND_URL                   = "https://api.mch.weixin.qq.com/secapi/pay/refund"
	REFUNDQUERY_URL              = "https://api.mch.weixin.qq.com/pay/refundquery"
	DOWNLOADBILL_URL             = "https://api.mch.weixin.qq.com/pay/downloadbill"
	REPORT_URL                   = "https://api.mch.weixin.qq.com/payitil/report"
	SHORTURL_URL                 = "https://api.mch.weixin.qq.com/tools/shorturl"
	AUTHCODETOOPENID_URL         = "https://api.mch.weixin.qq.com/tools/authcodetoopenid"
	SANDBOX_MICROPAY_URL         = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
	SANDBOX_UNIFIEDORDER_URL     = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
	SANDBOX_ORDERQUERY_URL       = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
	SANDBOX_REVERSE_URL          = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/reverse"
	SANDBOX_CLOSEORDER_URL       = "https://api.mch.weixin.qq.com/sandboxnew/pay/closeorder"
	SANDBOX_REFUND_URL           = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/refund"
	SANDBOX_REFUNDQUERY_URL      = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"
	SANDBOX_DOWNLOADBILL_URL     = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"
	SANDBOX_REPORT_URL           = "https://api.mch.weixin.qq.com/sandboxnew/payitil/report"
	SANDBOX_SHORTURL_URL         = "https://api.mch.weixin.qq.com/sandboxnew/tools/shorturl"
	SANDBOX_AUTHCODETOOPENID_URL = "https://api.mch.weixin.qq.com/sandboxnew/tools/authcodetoopenid"
)

Variables

This section is empty.

Functions

func MapToXml

func MapToXml(params Params) string

Types

type Account

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

func NewAccount

func NewAccount(appID string, mchID string, apiKey string, isSanbox bool) *Account

创建微信支付账号

func (*Account) SetCertData

func (a *Account) SetCertData(certPath string)

设置证书

type Client

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

func NewClient

func NewClient(account *Account) *Client

创建微信支付客户端

func (*Client) AuthCodeToOpenid

func (c *Client) AuthCodeToOpenid(params Params) (Params, error)

授权码查询OPENID接口

func (*Client) CloseOrder

func (c *Client) CloseOrder(params Params) (Params, error)

关闭订单

func (*Client) DownloadBill

func (c *Client) DownloadBill(params Params) (Params, error)

对账单下载

func (*Client) MicroPay

func (c *Client) MicroPay(params Params) (Params, error)

刷卡支付

func (*Client) OrderQuery

func (c *Client) OrderQuery(params Params) (Params, error)

订单查询

func (*Client) Refund

func (c *Client) Refund(params Params) (Params, error)

退款

func (*Client) RefundQuery

func (c *Client) RefundQuery(params Params) (Params, error)

退款查询

func (*Client) Report

func (c *Client) Report(params Params) (Params, error)

交易保障

func (*Client) Reverse

func (c *Client) Reverse(params Params) (Params, error)

撤销订单

func (*Client) SetAccount

func (c *Client) SetAccount(account *Account)

func (*Client) SetHttpConnectTimeoutMs

func (c *Client) SetHttpConnectTimeoutMs(ms int)

func (*Client) SetHttpReadTimeoutMs

func (c *Client) SetHttpReadTimeoutMs(ms int)

func (*Client) SetSignType

func (c *Client) SetSignType(signType string)

func (*Client) ShortUrl

func (c *Client) ShortUrl(params Params) (Params, error)

转换短链接

func (*Client) Sign

func (c *Client) Sign(params Params) string

签名

func (*Client) UnifiedOrder

func (c *Client) UnifiedOrder(params Params) (Params, error)

统一下单

func (*Client) ValidSign

func (c *Client) ValidSign(params Params) bool

验证签名

type Notifies

type Notifies struct{}

func (*Notifies) NotOK

func (n *Notifies) NotOK(errMsg string) string

通知不成功

func (*Notifies) OK

func (n *Notifies) OK() string

通知成功

type Params

type Params map[string]string

func XmlToMap

func XmlToMap(xmlStr string) Params

func (Params) ContainsKey

func (p Params) ContainsKey(key string) bool

判断key是否存在

func (Params) GetInt64

func (p Params) GetInt64(k string) int64

func (Params) GetString

func (p Params) GetString(k string) string

func (Params) SetInt64

func (p Params) SetInt64(k string, i int64) Params

func (Params) SetString

func (p Params) SetString(k, s string) Params

map本来已经是引用类型了,所以不需要 *Params

Jump to

Keyboard shortcuts

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