wxpay

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: MIT Imports: 21 Imported by: 0

README

wxpay

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/jdomzhang/wxpay

go modules

// go.mod
require github.com/jdomzhang/wxpay v1.1.1

示例

// 小程序统一下单
mp := wxpay.NewMP("appid", "mchid", "apiKey")
ouput, _ := mp.Prepay(&wxpay.InputMP{
	OpenID: "xxx",
	Body: "test",
	OutTradeNo: "1234",
	TotalFee: 1,
	IP: "127.0.0.1",
	NotifyURL: "https://example.com/notify",
})


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

// 新建微信支付客户端
client := wxpay.NewClient(account1)

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

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

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

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

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

// 统一下单
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.jdomzhang.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)

// 签名
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                      = "HMAC-SHA256"
	MD5                             = "MD5"
	Sign                            = "sign"
	MicroPayUrl                     = "https://api.mch.weixin.qq.com/pay/micropay"
	UnifiedOrderUrl                 = "https://api.mch.weixin.qq.com/pay/unifiedorder"
	OrderQueryUrl                   = "https://api.mch.weixin.qq.com/pay/orderquery"
	ReverseUrl                      = "https://api.mch.weixin.qq.com/secapi/pay/reverse"
	CloseOrderUrl                   = "https://api.mch.weixin.qq.com/pay/closeorder"
	RefundUrl                       = "https://api.mch.weixin.qq.com/secapi/pay/refund"
	RefundQueryUrl                  = "https://api.mch.weixin.qq.com/pay/refundquery"
	DownloadBillUrl                 = "https://api.mch.weixin.qq.com/pay/downloadbill"
	DownloadFundFlowUrl             = "https://api.mch.weixin.qq.com/pay/downloadfundflow"
	ReportUrl                       = "https://api.mch.weixin.qq.com/payitil/report"
	ShortUrl                        = "https://api.mch.weixin.qq.com/tools/shorturl"
	AuthCodeToOpenidUrl             = "https://api.mch.weixin.qq.com/tools/authcodetoopenid"
	ProfitSharingAddReceiver        = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"
	ProfitSharing                   = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing"
	ProfitSharingMulti              = "https://api.mch.weixin.qq.com/secapi/pay/multiprofitsharing"
	ProfitSharingFinish             = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish"
	SandboxMicroPayUrl              = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
	SandboxUnifiedOrderUrl          = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
	SandboxOrderQueryUrl            = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
	SandboxReverseUrl               = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/reverse"
	SandboxCloseOrderUrl            = "https://api.mch.weixin.qq.com/sandboxnew/pay/closeorder"
	SandboxRefundUrl                = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/refund"
	SandboxRefundQueryUrl           = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"
	SandboxDownloadBillUrl          = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"
	SandboxDownloadFundFlowUrl      = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadfundflow"
	SandboxReportUrl                = "https://api.mch.weixin.qq.com/sandboxnew/payitil/report"
	SandboxShortUrl                 = "https://api.mch.weixin.qq.com/sandboxnew/tools/shorturl"
	SandboxAuthCodeToOpenidUrl      = "https://api.mch.weixin.qq.com/sandboxnew/tools/authcodetoopenid"
	SandboxProfitSharingAddReceiver = "https://api.mch.weixin.qq.com/sandboxnew/pay/profitsharingaddreceiver"
	SandboxProfitSharing            = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/profitsharing"
	SandboxProfitSharingMulti       = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/multiprofitsharing"
	SandboxProfitSharingFinish      = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/profitsharingfinish"
)
View Source
const (
	RECEIVER_TYPE_MERCHANT_ID         = "MERCHANT_ID"
	RECEIVER_TYPE_PERSONAL_OPENID     = "PERSONAL_OPENID"
	RECEIVER_TYPE_PERSONAL_SUB_OPENID = "PERSONAL_SUB_OPENID"
)
View Source
const (
	RELATION_TYPE_SERVICE_PROVIDER = "SERVICE_PROVIDER" // 服务商
	RELATION_TYPE_STORE            = "STORE"            // 门店
	RELATION_TYPE_STAFF            = "STAFF"            // 员工
	RELATION_TYPE_STORE_OWNER      = "STORE_OWNER"      // 店主
	RELATION_TYPE_PARTNER          = "PARTNER"          // 合作伙伴
	RELATION_TYPE_HEADQUARTER      = "HEADQUARTER"      // 总部
	RELATION_TYPE_BRAND            = "BRAND"            // 品牌方
	RELATION_TYPE_DISTRIBUTOR      = "DISTRIBUTOR"      // 分销商
	RELATION_TYPE_USER             = "USER"             // 用户
	RELATION_TYPE_SUPPLIER         = "SUPPLIER"         // 供应商
	RELATION_TYPE_CUSTOM           = "CUSTOM"           // 自定义
)
View Source
const (
	SUCCESS = "SUCCESS"
)
View Source
const (
	TRADE_TYPE_JSAPI = "JSAPI"
)

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 NewSubAccount added in v1.1.2

func NewSubAccount(appID, mchID, apiKey, subAppID, subMchID string) *Account

创建微信支付子账号-服务商模式

func (*Account) SetCertData

func (a *Account) SetCertData(certPath string) error

设置证书

type AmountReceiver added in v1.1.4

type AmountReceiver struct {
	Type        string `json:"type"`        // 分账接收方类型
	Account     string `json:"account"`     // 分账接收方帐号
	Amount      int    `json:"amount"`      // 分账金额
	Description string `json:"description"` // 描述
}

AmountReceiver 分账金额接收方 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_6&index=2

func NewDefaultAmountReceiver added in v1.1.4

func NewDefaultAmountReceiver(mchID string, amount int, description string) *AmountReceiver

NewDefaultAmountReceiver /

type Client

type Client struct {
	EnableDebug bool // 是否输出调试信息
	// contains filtered or unexported fields
}

Client /

func NewClient

func NewClient(account *Account) *Client

NewClient / 创建微信支付客户端

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) DownloadFundFlow added in v1.1.1

func (c *Client) DownloadFundFlow(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) ProfitSharing added in v1.1.4

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

ProfitSharing 单次分账

func (*Client) ProfitSharingAddReceiver added in v1.1.4

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

ProfitSharingAddReceiver 添加分账接收方

func (*Client) ProfitSharingFinish added in v1.1.4

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

ProfitSharingFinish 完结分账

func (*Client) ProfitSharingMulti added in v1.1.4

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

ProfitSharingMulti 多次分账

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 FinishSharing added in v1.1.4

type FinishSharing struct {
	TransactionID string `json:"transactionID"` // 微信支付订单号
	OutOrderNo    string `json:"outOrderNo"`    // 商户分账单号
	Description   string `json:"description"`   // 分账完结描述
}

FinishSharing / 完结分账 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_5&index=6

type JsapiIn added in v1.1.1

type JsapiIn struct {
	OpenID        string
	SubOpenID     string
	Body          string
	OutTradeNo    string
	TotalFee      int64
	IP            string
	NotifyURL     string
	ProfitSharing bool
}

JsapiIn 输入参数,支付结果通知文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8

type JsapiOut added in v1.1.1

type JsapiOut struct {
	AppID     string `json:"appId"`
	NonceStr  string `json:"nonceStr"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	TimeStamp string `json:"timeStamp"`
	PaySign   string `json:"paySign"`
}

type MP added in v1.1.1

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

MP is mini-program

func NewMP added in v1.1.1

func NewMP(appID, mchID, mchKey string) *MP

NewMP 小程序支付

func NewSubMP added in v1.1.3

func NewSubMP(appID, mchID, mchKey, subAppID, subMchID string) *MP

NewSubMP 小程序支付-使用服务商提供的支付商户

func (*MP) EnableDebug added in v1.1.7

func (c *MP) EnableDebug() *MP

func (*MP) Prepay added in v1.1.1

func (c *MP) Prepay(input *JsapiIn) (*JsapiOut, Params, error)

Prepay 微信统一下单,接口文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

func (*MP) ProfitSharing added in v1.1.4

func (c *MP) ProfitSharing(input *SharingInfo) (Params, error)

ProfitSharing 请求单次分账 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_1&index=1

func (*MP) ProfitSharingAddReceiver added in v1.1.4

func (c *MP) ProfitSharingAddReceiver(receiver *ShareReceiver) (Params, error)

ProfitSharingAddReceiver 添加分账接收方 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_3&index=4

func (*MP) ProfitSharingFinish added in v1.1.4

func (c *MP) ProfitSharingFinish(input *FinishSharing) (Params, error)

ProfitSharingFinish 完结分账 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_5&index=6

func (*MP) ProfitSharingMulti added in v1.1.4

func (c *MP) ProfitSharingMulti(input *SharingInfo) (Params, error)

ProfitSharingMulti 请求多次分账 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_6&index=2

func (*MP) QueryOrder added in v1.1.1

func (c *MP) QueryOrder(outTradeNo string) (string, Params, error)

QueryOrder 查询订单,接口文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2

func (*MP) Refund added in v1.1.1

func (c *MP) Refund(input *RefundIn) (Params, error)

Refund 微信退款,必须设置证书,接口文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4

func (*MP) SetCertBase64 added in v1.1.4

func (c *MP) SetCertBase64(certDataBase64 string) error

SetCertBase64 设置操作证书 base64编码

func (*MP) SetCertPath added in v1.1.1

func (c *MP) SetCertPath(certPath string) error

SetCertPath 设置操作证书,请查看API证书说明 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

func (*MP) Sign added in v1.1.5

func (c *MP) Sign(input map[string]string) string

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) Remove added in v1.1.4

func (p Params) Remove(key 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

type RefundIn added in v1.1.1

type RefundIn struct {
	AppID       string `json:"appID"`
	MchID       string `json:"mchID"`
	SubAppID    string `json:"subAppID"`
	SubMchID    string `json:"subMchID"`
	OutTradeNo  string `json:"outTradeNo"`
	OutRefundNo string `json:"outRefundNo"`
	TotalFee    int64  `json:"totalFee"`
	RefundFee   int64  `json:"refundFee"`
}

RefundIn / 普通商户:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 服务商模式:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_sl_api.php?chapter=9_4

type ShareReceiver added in v1.1.4

type ShareReceiver struct {
	Type         string `json:"type"`          // 分账接收方类型
	Account      string `json:"account"`       // 分账接收方帐号
	Name         string `json:"name"`          // 分账接收方全称,类型是MERCHANT_ID时,是商户全称(必传)
	Description  string `json:"description"`   // 描述
	RelationType string `json:"relation_type"` // 与分账方的关系类型
}

ShareReceiver 分账接收方 https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_3&index=4

func NewDefaultShareReceiver added in v1.1.4

func NewDefaultShareReceiver(mchID string, name string) *ShareReceiver

NewDefaultShareReceiver /

type SharingInfo added in v1.1.4

type SharingInfo struct {
	TransactionID string           `json:"transactionID"` // 微信支付订单号
	OutOrderNo    string           `json:"outOrderNo"`    // 商户分账单号
	Receivers     []AmountReceiver `json:"receivers"`     // 分账接收方列表
}

SharingInfo / https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_6&index=2

Jump to

Keyboard shortcuts

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