MiniPay

package module
v0.0.0-...-114274f Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

README

MiniPay

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

目录:

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

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

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

全栈编程,专注编程技术.

https://luboke.com

关于我

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

阶段一:基础语法篇

本阶段内容:从零开始学习go语言体系课程,go语言环境集成,常用开发工具集成,go语言基础语法部分的数据类型讲解(包括变量,常量,数值类型,字符,字符串,浮点类型,布尔,指针,数组,切片,map),流程控制,函数,结构体,方法,go语言包与第三方扩展库,面向对象(封装,继承,多态,接口)。

第1章:go语言总述
第2章:go语言数据类型与流程控制
第3章:go语言函数、结构体、方法
第4章:go面向对象

阶段二:进阶篇

本阶段内容:go语言进阶篇,主要讲解:go语言web编程相关的知识,包括http协议,web请求与响应解析,数据库操作,模板引擎,承受着web编程知识的学习会进行两个项目的实战,主要包括微商城后台管理系统与微商城后端接口。

go语言体系课第二阶段之web编程

go语言体系课第二阶段之微商城核心库设计

go语言体系课第二阶段之微商城后台管理系统

go语言体系课第二阶段之web框架

go语言体系课第二阶段之微商城小程序后端接口

go语言体系课第二阶段之基于beegoweb框架的微商城小程序接口

阶段二课程电子书直达

阶段三:高级篇

本阶段内容:go语言高级篇,主要讲解:go语言的并发编程,众所周知,go语言高效的并发模式是其最核心的能力,也是其与现代多核cpu完美结合的最佳组合。那围绕着并发编程,我们将进行并发编程相关功能实战,包括模拟机器人的并发请求与限流处理。一般涉及到并发编程的应用场景,多数与大型项目,海量用户相关,一般大型项目都会涉及到海量的日志,特别是围绕着我们微商城系统相关的海量交易日志的处理与订单交易相关,这也是我们需要详细讨论的内容。那围绕着电商系统又不得不提到的是消息推送,包括小程序支付异步通知完善与权益处理,支付成功之后的消息提醒,站内消息通知等。

go语言体系课第三阶段之go语言高并发处理 go语言体系课第三阶段之大文件与交易日志 go语言体系课第三阶段之消息推送

阶段三电子书直达

阶段四微服务篇(电子书待添加)

本阶段内容: go语言微服务篇,现代编程特别是大型项目的编程,基本都在向微服务靠拢,这也是项目发展到一定阶段的必然趋势,我们在微服务篇主要讲解:微服务相关的知识,包括微服务相关的protobuf数据格式讲解、grpc讲解、grpc+protobuf实现微服务实例、go-micro从零打造微服务、elasticsearch技术栈,包括elasticsearch部署,常用分词组件详解,elasticsearch结合Postman操作curd,go语言操作elasticsearch,elasticsearch微服务化实战微商城搜索功能。

go语言体系课第四阶段微服务篇

赞助我

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

支付宝打赏赞助

支付宝打赏赞助

微信打赏赞助

微信打赏赞助

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTPC  *HTTPClient
	HTTPSC *HTTPSClient
)

Functions

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