MiniPay

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

README

MiniPay

go语言版微信小程序支付系统 开源地址

目录:

go语言版微信小程序支付系统 简介

go语言版微信小程序支付系统

支付微信小程序端任何业务的支付需要

全栈编程

http://www.luboke.com

关于我

作者具有多年产品设计与一线开发经验,adobe认证产品专家ACPE,一名具备PM思维的RD. 擅长go,python,php,mysql,socket编程,js,小程序等前端技术。 对电商抢购、支付场景具有丰富的实战经验。对大数据分布式存储,系统架构层面具有一定的经验. 欢迎fork me: https://github.com/codersay 我的B站: https://space.bilibili.com/443684499 我录制的go语言微商城系统原型已发布在慕课网: https://www.imooc.com/learn/1151 我的微信号: codersay

赞助我

如果我的努力值得你的肯定,请赞助我,让我在开源的路上,做更好,走更远。 赞助我的方式包括:支付宝打赏微信打赏给go语言版微信小程序支付系统一个star向我反馈意见和建议

支付宝打赏赞助

支付宝打赏赞助

微信打赏赞助

微信打赏赞助

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTPC  *HTTPClient
	HTTPSC *HTTPSClient
)

Functions

func Float2String

func Float2String(moneyFee float64) string

微信金额浮点转字符串

func InitMiniPay

func InitMiniPay(pay *MiniPayParams)

初始化支付公用参数

func LocalIP

func LocalIP() string

LocalIP 获取机器的IP

func MiniPayNotifyCallback

func MiniPayNotifyCallback(w http.ResponseWriter, body []byte) (*MiniPayAsyncResult, *MiniPayCommonResult, error)

MiniCallback 微信支付 response是ResponseWriter接口的实现。 WriteHeader方法: 设置响应的状态码,返回错误状态码特别有用。WriteHeader方法在执行完毕之后就不允许对首部进行写入了。

向客户端返回JSON数据: 首先使用Header方法将内容类型设置成application/json,然后将JSON数据写入ResponseWriter中 go 处理http response

func MinipaySign

func MinipaySign(key string, m map[string]interface{}) (string, error)

func Order2Pay

func Order2Pay(payArg *PayArg) (map[string]interface{}, error)

用户下单支付接口

func RandomString

func RandomString() string

RandomStr 获取一个随机字符串

func XmlToMap

func XmlToMap(xmlData []byte) map[string]interface{}

Types

type HTTPClient

type HTTPClient struct {
	http.Client
}

http客户端

func (*HTTPClient) PostData

func (c *HTTPClient) PostData(url, format string, data string) ([]byte, error)

http post数据

type HTTPSClient

type HTTPSClient struct {
	http.Client
}

HTTPS客户端

func NewHTTPSClient

func NewHTTPSClient(certPEMBlock, keyPEMBlock []byte) *HTTPSClient

NewHTTPSClient 获取默认https客户端

func (*HTTPSClient) GetData

func (c *HTTPSClient) GetData(url string) ([]byte, error)

https get数据

func (*HTTPSClient) PostData

func (httpsclient *HTTPSClient) PostData(url string, contentType string, data string) ([]byte, error)

https 提交post数据

type MiniPayAsyncResult

type MiniPayAsyncResult struct {
	//统一下单与查询结果通用部分
	MiniPayCommonResult

	//统一下单与查询结果通用部分
	MiniPayReturnSuccessData

	// 查询结果或者下单返回公用部分
	MiniPayResultData
}

异步支付 返回结果

type MiniPayCommonResult

type MiniPayCommonResult struct {
	ReturnCode string `xml:"return_code" json:"return_code,omitempty"`
	ReturnMsg  string `xml:"return_msg" json:"return_msg,omitempty"`
}

MiniPayCommonResult 基本信息,状态码与状态描述 统一下单与查询结果通用部分 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_2 返回结果

type MiniPayParams

type MiniPayParams struct {
	AppID string // appID
	MchID string // 商户号
	Key   string // 密钥
}

微信小程序支付公共参数

func Minipay

func Minipay() *MiniPayParams

返回支付公用参数

func (*MiniPayParams) UnifiedPay

func (this *MiniPayParams) UnifiedPay(payArg *PayArg) (map[string]interface{}, error)

Pay 支付

type MiniPayResultData

type MiniPayResultData struct {
	OpenID             string `xml:"openid,omitempty" json:"openid,omitempty"`
	IsSubscribe        string `xml:"is_subscribe,omitempty" json:"is_subscribe,omitempty"`
	TradeType          string `xml:"trade_type,omitempty" json:"trade_type,omitempty"`
	TradeState         string `xml:"trade_state,omitempty" json:"trade_state,omitempty"`
	BankType           string `xml:"bank_type,omitempty" json:"bank_type,omitempty"`
	TotalFee           string `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
	SettlementTotalFee string `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"`
	FeeType            string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
	CashFee            string `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"`
	CashFeeType        string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"`

	//代金券金额	coupon_fee	否	Int	100	“代金券”金额<=订单金额,订单金额-“代金券”金额=现金支付金额,详见支付金额
	//代金券使用数量	coupon_count	否	Int	1	代金券使用数量
	//代金券类型	coupon_type_$n	否	String	CASH
	//CASH--充值代金券
	//NO_CASH---非充值优惠券
	//
	//开通免充值券功能,并且订单使用了优惠券后有返回(取值:CASH、NO_CASH)。$n为下标,从0开始编号,举例:coupon_type_$0
	//
	//代金券ID	coupon_id_$n	否	String(20)	10000 	代金券ID, $n为下标,从0开始编号
	//单个代金券支付金额	coupon_fee_$n	否	Int	100	单个代金券支付金额, $n为下标,从0开始编号
	TransactionID  string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
	OutTradeNO     string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
	Attach         string `xml:"attach,omitempty" json:"attach,omitempty"`
	TimeEnd        string `xml:"time_end,omitempty" json:"time_end,omitempty"`
	TradeStateDesc string `xml:"trade_state_desc" json:"trade_state_desc,omitempty"`
}

查询结果或者下单返回公用部分 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_2 以下字段在return_code 、result_code、trade_state都为SUCCESS时有返回 ,如trade_state不为 SUCCESS,则只返回out_trade_no(必传)和attach(选传)。

type MiniPayReturnSuccessData

type MiniPayReturnSuccessData struct {
	AppID string `xml:"appid,omitempty" json:"appid,omitempty"`
	MchID string `xml:"mch_id,omitempty" json:"mch_id,omitempty"`
	//DeviceInfo 统一下单默认就有,查询结果在return_code 、result_code、trade_state都为SUCCESS时有返回,这里统一放在这里
	DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
	NonceStr   string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
	Sign       string `xml:"sign,omitempty" json:"sign,omitempty"`
	ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`
	ErrCode    string `xml:"err_code,omitempty" json:"err_code,omitempty"`
	ErrCodeDes string `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
}

MiniPayReturnSuccessData 返回通用数据 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1 统一下单 返回结果 的一部分 以下字段在return_code为SUCCESS的时候有返回 统一下单与查询结果通用部分

type MiniPaySyncResult

type MiniPaySyncResult struct {
	//统一下单与查询结果通用部分
	MiniPayCommonResult

	//统一下单与查询结果通用部分
	MiniPayReturnSuccessData

	MinipayStateData
}

统一下单请求的响应,即同步的响应

func PostMiniPay

func PostMiniPay(url string, data map[string]interface{}) (MiniPaySyncResult, error)

对微信下订单或者查订单

type MinipayStateData

type MinipayStateData struct {
	TradeType string `xml:"trade_type" json:"trade_type,omitempty"`
	PrepayID  string `xml:"prepay_id" json:"prepay_id,omitempty"`
	CodeURL   string `xml:"code_url" json:"code_url,omitempty"`
}

支付结果,以下字段在return_code 和result_code都为SUCCESS的时候有返回

type PayArg

type PayArg struct {
	//Golang中,如果指定一个field序列化成JSON的变量名字为-,则序列化的时候自动忽略这个field。
	//而omitempty的作用是当一个field的值是empty的时候,序列化JSON时候忽略这个field(Newtonsoft.Json的类似用法参考这里和例子)。
	//使用omitempty熟悉,如果该字段为nil或0值(数字0,字符串"",空数组[]等),则打包的JSON结果不会有这个字段。
	//type Message struct {
	//	Name string `json:"msg_name"`       // 对应JSON的msg_name
	//	Body string `json:"body,omitempty"` // 如果为空置则忽略字段
	//	Time int64  `json:"-"`              // 直接忽略字段
	//}
	APPID       string  // appID
	TradeNum    string  `json:"tradeNum,omitempty"`
	MoneyFee    float64 `json:"MoneyFee,omitempty"`
	CallbackURL string  `json:"callbackURL,omitempty"`
	Body        string  `json:"body,omitempty"`
	OpenID      string  `json:"openid,omitempty"`
}

Charge 发起小程序预下单的支付参数,基他参数在发起拼接时临时改,比如minipay.go文件 第57行 payHandle["nonce_str"] = RandomStr() https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1 支付需要的参数,这里是变化的参数,即服务端传递的

Jump to

Keyboard shortcuts

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