wepay

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

基础通讯业务实现 sam

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"
	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"
	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"
	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"
)

常量

Variables

View Source
var (
	//RandChar 随机字串
	RandChar = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
	//RandCharLen 随机字串长度
	RandCharLen int32 = 36
	//TradeNumStr 商户订单号前缀
	TradeNumStr = "WX"
)

Functions

func MapToXML

func MapToXML(params Params) string

MapToXML MapToXML 使用mxj第三方库,转换map2xml

Types

type AppPayResp

type AppPayResp struct {
	AppID     string `json:"appid"`     //appid
	PartnerID string `json:"partnerid"` //商户ID
	PrepayID  string `json:"prepayid"`  //prepayid
	Package   string `json:"package"`   //package
	NonceStr  string `json:"noncestr"`  //随机字串
	Timestamp string `json:"timestamp"` //时间
	Sign      string `json:"sign"`      //签名
}

AppPayResp App支付时,返回的结构体 包括prepayID和Sign等其他信息

type AppletPayResp added in v0.4.2

type AppletPayResp struct {
	Timestamp string `json:"timeStamp"` //时间
	NonceStr  string `json:"nonceStr"`  //随机字串
	Package   string `json:"package"`   //package
	SignType  string `json:"signType"`
	Sign      string `json:"paySign"` //签名
}

AppletPayResp 微信小程序支付时,返回的结构体 包括prepayID和Sign等其他信息

type Client

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

请求与返回的封装

func (*Client) AuthCodeToOpenid

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

AuthCodeToOpenid 授权码查询OPENID接口

func (*Client) CloseOrder

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

CloseOrder 关闭订单

func (*Client) DownloadBill

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

DownloadBill 对账单下载

func (*Client) MicroPay

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

MicroPay 刷卡支付

func (*Client) Notify

func (c *Client) Notify(req *http.Request) (Params, error)

Notify 异步通知处理

func (*Client) OrderQuery

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

OrderQuery 订单查询

func (*Client) Refund

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

Refund 退款

func (*Client) RefundNotify added in v0.6.8

func (c *Client) RefundNotify(req *http.Request) (Params, error)

RefundNotify 退款异步通知处理

func (*Client) RefundQuery

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

RefundQuery 退款查询

func (*Client) Report

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

Report 交易保障

func (*Client) Reverse

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

Reverse 撤销订单

func (*Client) SetHTTPConnectTimeoutMs

func (c *Client) SetHTTPConnectTimeoutMs(ms int)

SetHTTPConnectTimeoutMs SetHTTPConnectTimeoutMs

func (*Client) SetHTTPReadTimeoutMs

func (c *Client) SetHTTPReadTimeoutMs(ms int)

SetHTTPReadTimeoutMs SetHTTPReadTimeoutMs

func (*Client) SetSignType

func (c *Client) SetSignType(signType string)

SetSignType SetSignType

func (*Client) ShortURL

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

ShortURL 转换短链接

func (*Client) Sign

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

Sign 签名

func (*Client) UnifiedOrder

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

UnifiedOrder 统一下单

func (*Client) ValidSign

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

ValidSign 验证签名

type Notifies

type Notifies struct{}

Notifies Notifies

func (*Notifies) NotOK

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

NotOK 通知不成功

func (*Notifies) OK

func (n *Notifies) OK() string

OK 通知成功

type NotifyRet

type NotifyRet struct {
	XMLName    xml.Name `xml:"xml"`
	ReturnCode string   `xml:"return_code"`
	ReturnMsg  string   `xml:"return_msg"`
}

NotifyRet 异步通知的返回值 返回

type Params

type Params map[string]interface{}

Params Params

func XMLToMap

func XMLToMap(xmlStr, key string) Params

XMLToMap XMLToMap 使用mxj第三方库,转换xml2map

func (Params) ContainsKey

func (p Params) ContainsKey(key string) bool

ContainsKey 判断key是否存在

func (Params) GetInt64

func (p Params) GetInt64(k string) int64

GetInt64 GetInt64

func (Params) GetString

func (p Params) GetString(k string) string

GetString GetString

func (Params) SetInt64

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

SetInt64 SetInt64

func (Params) SetString

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

SetString SetString

type RefundNotifyReqInfo added in v0.6.8

type RefundNotifyReqInfo struct {
	TransactionId       string `json:"transaction_id"`
	OutTradeNo          string `json:"out_trade_no"`
	RefundId            string `json:"refund_id"`
	OutRefundNo         string `json:"out_refund_no"`
	TotalFee            int64  `json:"total_fee"`
	SettlementTotalFee  int64  `json:"settlement_total_fee"`  //应结订单金额
	RefundFee           int64  `json:"refund_fee"`            //申请退款金额
	SettlementRefundFee int64  `json:"settlement_refund_fee"` //退款金额
	RefundStatus        string `json:"refund_status"`         //退款状态 SUCCESS-退款成功 CHANGE-退款异常 REFUNDCLOSE—退款关闭
	RefundRecvAccout    string `json:"refund_recv_accout"`    //退款入账账户
	RefundAccount       string `json:"refund_account"`        //退款资金来源 REFUND_SOURCE_RECHARGE_FUNDS 可用余额退款/基本账户  REFUND_SOURCE_UNSETTLED_FUNDS 未结算资金退款
	RefundRequestSource string `json:"refund_request_source"` //退款发起来源 API接口  VENDOR_PLATFORM商户平台
}

RefundNotifyReqInfo 退款异步通知加密字段

type TradeType

type TradeType string

TradeType 支付场景类型

const (
	// TradeTypeJSAPI 公众号支付
	TradeTypeJSAPI TradeType = "JSAPI"

	// TradeTypeNative 扫码支付
	TradeTypeNative TradeType = "NATIVE"

	// TradeTypeApp App支付
	TradeTypeApp TradeType = "APP"

	//TradeTypeH5 非微信浏览器的H5唤起支付
	TradeTypeH5 TradeType = "MWEB"

	//TradeTypeApplet 小程序
	TradeTypeApplet TradeType = ""
)

type WxAPI

type WxAPI struct {
	Client *Client
}

WxAPI 调用实体

func NewWxAPI

func NewWxAPI(wxConfig *WxConfig) *WxAPI

NewWxAPI init account基本的商户信息 notifyURL 异步通知地址 endMinute 订单有效期

func (*WxAPI) AppTrade

func (m *WxAPI) AppTrade(body, outTradeNo string, totalFee int, clientIP string) (ret *AppPayResp, err error)

AppTrade APP下单

func (*WxAPI) AppletTrade added in v0.4.2

func (m *WxAPI) AppletTrade(body, outTradeNo string, totalFee int, openID, clientIP string) (ret *AppletPayResp, err error)

AppletTrade 小程序支付 需要传入公众号对应用户的openID

func (*WxAPI) H5Trade

func (m *WxAPI) H5Trade(body, outTradeNo string, totalFee int, clientIP string) (ret string, err error)

H5Trade H5下单

func (*WxAPI) JSAPITrade

func (m *WxAPI) JSAPITrade(body, outTradeNo string, totalFee int, openID, clientIP string) (ret string, err error)

JSAPITrade 公众号支付 需要传入公众号对应用户的openID

func (*WxAPI) NativeTrade

func (m *WxAPI) NativeTrade(body, outTradeNo string, totalFee int) (ret string, err error)

NativeTrade 扫码支付下单 使用时把返回的code_url生成二维码,供前台用户扫码支付

func (*WxAPI) Notify

func (m *WxAPI) Notify(req *http.Request) (ret *NotifyRet, outTradeNo, tradeNo, openID string, err error)

Notify 异步通知 返回异步通知状态信息 调用方拿到返回值后,需要根据 outTradeNo tradeNo openID等值,做进一点检验,如果检验失败,设置ret.ReturnCode="FAIL"; 成功时,需要回写返回值到本地 最后以xml方式输出 ret

func (*WxAPI) OrderQuery

func (m *WxAPI) OrderQuery(transactionID, outTradeNo string) (state bool, tradeNo string, err error)

OrderQuery 订单查询

返回是否成功,和错误信息

func (*WxAPI) Refund added in v0.6.8

func (m *WxAPI) Refund(transactionID, outTradeNo, outRefundNo, refundDesc string, totalFee, refundFee int) (refundId string, err error)

Refund 申请退款

func (*WxAPI) RefundNotify added in v0.6.8

func (m *WxAPI) RefundNotify(req *http.Request) (ret *NotifyRet, outTradeNo, refundStatus, recvName string, err error)

RefundNotify 退款异步通知 返回异步通知状态信息 以xml方式输出 ret

func (*WxAPI) RefundQuery added in v0.6.8

func (m *WxAPI) RefundQuery(transactionID, outTradeNo, outRefundNo, refundId string) (status string, recvName string, err error)

RefundQuery 退款查询

func (*WxAPI) UnifiedOrder

func (m *WxAPI) UnifiedOrder(body string, outTradeNo string, totalFee int, openID string, clientIP string, tradeType TradeType) (Params, error)

UnifiedOrder 统一下单接口

type WxConfig

type WxConfig struct {
	AppId    string //传入的appID
	MchId    string //分配的mchID
	APIKey   string //分配的apiKey
	ServerIP string //服务器IP

	NotifyURL       string //异步通知地址
	OrderTime       int    //订单有效分钟数
	RefundNotifyUrl string //订单退款异步通知地址
	// contains filtered or unexported fields
}

WxConfig WxConfig

func NewWxConfig

func NewWxConfig(appId, mchId, apiKey, serverIP string, isSandbox bool, notifyUrl string, orderTime int) *WxConfig

NewWxConfig 一个新的配置信息 也可以自己组装

func (*WxConfig) SetCertData

func (m *WxConfig) SetCertData(certPath string) (err error)

SetCertData 设置证书

func (*WxConfig) SetRefundNotifyUrl added in v0.6.8

func (m *WxConfig) SetRefundNotifyUrl(url string)

Jump to

Keyboard shortcuts

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