wxpay

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

README

wxpay

go语言实现微信支付对接

usage


// 初始化一个App支付
payConfig := &wxpay.WxPayConfig{
	AppId:         "微信分配的公众账号ID(企业号corpid即为此appId)",
	AppSecret:     "APP密钥",
	MchId:         "微信支付分配的商户号",
	NotifyUrl:     "接收微信支付异步通知回调地址",
	TradeType:     "APP",  // 支持 JSAPI,NATIVE,APP
}

// 统一下单
params := map[string]string{
  "out_trade_no": "1000000000000",  // 商户订单号
	"body":         "test",  // 商品描述
	"total_fee":    "100",   // 100分 = 1元
}
resp, err := wxpay.UnifiedOrder(payConfig, params)

// 查询订单
params := map[string]string{
	"out_trade_no": "100000000000",
}
resp, err := wxpay.OrderQuery(payConfig, params)

// 关闭订单
params := map[string]string{
	"out_trade_no": "1000000000000",
}
resp, err := wxpay.CloseOrder(payConfig, params)

// 申请退款 -- 需要证书
tlsConfig, err := NewWxPayTlsConfig(
  "app/apiclient_cert.pem",
  "app/apiclient_key.pem",
  "app/rootca.pem",     // 无则不传
)

params := map[string]string{
	"out_trade_no":  "1000000000000",
	"out_refund_no": fmt.Sprintf("%d", time.Now().Unix()),
	"total_fee":     "100",
	"refund_fee":    "100",
	"op_user_id":    "test",
}
resp, err := wxpay.Refund(payConfig, params)

// 查询退款
params := map[string]string{
	"out_trade_no": "1000000000000",
}
resp, err := wxpay.RefundQuery(payConfig, params)

Documentation

Index

Constants

View Source
const (
	WXPAY_UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder" // 统一下单
	WXPAY_ORDERQUERY_URL   = "https://api.mch.weixin.qq.com/pay/orderquery"   // 查询订单
	WXPAY_CLOSEORDER_URL   = "https://api.mch.weixin.qq.com/pay/closeorder"   // 关闭订单
	WXPAY_REFUNDQUERY_URL  = "https://api.mch.weixin.qq.com/pay/refundquery"  // 退款查询
	// 需要证书
	WXPAY_REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund" // 申请退款
)

Variables

This section is empty.

Functions

func NewWxPayTlsConfig

func NewWxPayTlsConfig(paths ...string) (tlsConfig *tls.Config, err error)

安全证书 导入顺序 cert、key、rootca

Types

type BaseResponse

type BaseResponse struct {
	XmlName    xml.Name `xml:"xml"`
	ReturnCode string   `xml:"return_code"`
	ReturnMsg  string   `xml:"return_msg"`
	ResultCode string   `xml:"result_code"`  // 业务结果
	ErrCode    string   `xml:"err_code"`     // 错误码
	ErrCodeDes string   `xml:"err_code_des"` // 错误描述
	AppId      string   `xml:"appid"`        // 公众账号ID
	SubAppId   string   `xml:"sub_appid"`    // 子商户AppID
	MchId      string   `xml:"mch_id"`       // 商户号
	SubMchId   string   `xml:"sub_mch_id"`   // 子商户ID
	DeviceInfo string   `xml:"device_info"`  // 设备号
	NonceStr   string   `xml:"nonce_str"`    // 随机字符串
	Sign       string   `xml:"sign"`         // 签名
}

基础返回数据

type CloseOrderResponse

type CloseOrderResponse struct {
	BaseResponse `xml:",innerXml"`
}

关闭订单

type NotifyResponse

type NotifyResponse struct {
	BaseResponse `xml:",innerXml"`

	OpenId         string `xml:"openid"`           // 用户标识
	SubOpenId      string `xml:"sub_openid"`       // 子商户用户表示
	IsSubscribe    string `xml:"is_subscribe"`     // 是否关注公众账号
	SubIsSubscribe string `xml:"sub_is_subscribe"` // 是否关注子商户的公众号
	TradeType      string `xml:"trade_type"`       // 交易类型
	BankType       string `xml:"bank_type"`        // 付款银行
	TotalFee       string `xml:"total_fee"`        // 总金额
	FeeType        string `xml:"fee_type"`         // 货币种类
	CashFee        string `xml:"cash_fee"`         // 现金支付金额
	CashFeeType    string `xml:"cash_fee_type"`    // 现金支付货币类型
	CouponFee      string `xml:"coupon_fee"`       // 代金券或立减优惠金额
	CouponCount    string `xml:"coupon_count"`     // 代金券或立减优惠使用数量
	CouponFee0     string `xml:"coupon_fee_0"`     // 单个代金券支付金额,$n为下标,从0开始编号
	CouponId0      string `xml:"coupon_id_0"`      // 代金券ID,$n为下标,从0开始编号
	CouponType0    string `xml:"coupon_type_0"`    // 代金券类型
	TransactionId  string `xml:"transaction_id"`   // 微信支付订单号
	OutTradeNo     string `xml:"out_trade_no"`     // 商户订单号
	Attach         string `xml:"attach"`           // 商家数据包,原样返回
	TimeEnd        string `xml:"time_end"`         // 支付完成时间
}

回调

type OrderQueryResponse

type OrderQueryResponse struct {
	BaseResponse `xml:",innerXml"`

	OpenId         string `xml:"openid"`           // 用户标识
	SubOpenId      string `xml:"sub_openid"`       // 子商户用户表示
	IsSubscribe    string `xml:"is_subscribe"`     // 是否关注公众账号
	SubIsSubscribe string `xml:"sub_is_subscribe"` // 是否关注子商户的公众号
	TradeType      string `xml:"trade_type"`       // 交易类型
	TradeState     string `xml:"trade_state"`      // 交易状态
	BankType       string `xml:"bank_type"`        // 付款银行
	TotalFee       string `xml:"total_fee"`        // 总金额
	FeeType        string `xml:"fee_type"`         // 货币种类
	CashFee        string `xml:"cash_fee"`         // 现金支付金额
	CashFeeType    string `xml:"cash_fee_type"`    // 现金支付货币类型
	CouponFee      string `xml:"coupon_fee"`       // 代金券或立减优惠金额
	CouponCount    string `xml:"coupon_count"`     // 代金券或立减优惠使用数量
	TransactionId  string `xml:"transaction_id"`   // 微信支付订单号
	OutTradeNo     string `xml:"out_trade_no"`     // 商户订单号
	Attach         string `xml:"attach"`           // 附加数据
	TimeEnd        string `xml:"time_end"`         // 支付完成时间
	TradeStateDesc string `xml:"trade_state_desc"` // 交易状态描述
}

查询订单

type RefundQueryResponse

type RefundQueryResponse struct {
	BaseResponse `xml:",innerXml"`

	TransactionId string `xml:"transaction_id"` // 微信订单号
	OutTradeNo    string `xml:"out_trade_no"`   // 商户订单号
	TotalFee      string `xml:"total_fee"`      // 订单总金额
	FeeType       string `xml:"fee_type"`       // 订单金额货币种类
	CashFee       string `xml:"cash_fee"`       // 现金支付金额
	RefundCount   string `xml:"refund_count"`   // 退款笔数
}

查询退款

type RefundResponse

type RefundResponse struct {
	BaseResponse `xml:",innerXml"`

	TransactionId     string `xml:"transaction_id"`      // 微信订单号
	OutTradeNo        string `xml:"out_trade_no"`        // 商户订单号
	OutRefundNo       string `xml:"out_refund_no"`       // 商户退款单号
	RefundId          string `xml:"refund_id"`           // 微信退款单号
	RefundChannel     string `xml:"refund_channel"`      // 退款渠道
	RefundFee         string `xml:"refund_fee"`          // 退款金额
	TotalFee          string `xml:"total_fee"`           // 订单总金额
	FeeType           string `xml:"fee_type"`            // 订单金额货币种类
	CashFee           string `xml:"cash_fee"`            // 现金支付金额
	CashRefundFee     string `xml:"cash_refund_fee"`     // 现金退款金额
	CouponRefundFee   string `xml:"coupon_refund_fee"`   // 代金券或立减优惠退款金额
	CouponRefundCount string `xml:"coupon_refund_count"` // 代金券或立减优惠使用数量
	CouponRefundId    string `xml:"coupon_refund_id"`    // 代金券或立减优惠ID
}

退款

type UnifiedOrderResponse

type UnifiedOrderResponse struct {
	BaseResponse `xml:",innerXml"`

	TradeType string `xml:"trade_type"` // 交易类型
	PrepayId  string `xml:"prepay_id"`  // 预支付交易会话标识
	CodeUrl   string `xml:"code_url"`   // 二维码链接
	MWebUrl   string `xml:"mweb_url"`   // h5支付跳转地址
}

统一下单

type WxPayConfig

type WxPayConfig struct {
	AppId          string
	MchId          string
	AppSecret      string
	SpbillCreateIp string
	NotifyUrl      string
	TlsConfig      *tls.Config
	Timeout        time.Duration
}

func NewWxPayConfig

func NewWxPayConfig(appId, appSecret, mchId, notifyUrl, spbillCreateIp string, tlsConfig *tls.Config) (config WxPayConfig)

支付配置

func (*WxPayConfig) CloseOrder

func (config *WxPayConfig) CloseOrder(params map[string]string) (resp CloseOrderResponse, err error)

func (*WxPayConfig) Notify

func (config *WxPayConfig) Notify(body []byte) (resp NotifyResponse, err error)

func (*WxPayConfig) OrderQuery

func (config *WxPayConfig) OrderQuery(params map[string]string) (resp OrderQueryResponse, err error)

func (*WxPayConfig) Refund

func (config *WxPayConfig) Refund(params map[string]string) (resp RefundResponse, err error)

func (*WxPayConfig) RefundQuery

func (config *WxPayConfig) RefundQuery(params map[string]string) (resp RefundQueryResponse, err error)

func (*WxPayConfig) UnifiedOrder

func (config *WxPayConfig) UnifiedOrder(trade_type string, params map[string]string) (resp UnifiedOrderResponse, err error)

Jump to

Keyboard shortcuts

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