当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 97

yiluo1201 / 微信Go SDK
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
env
Loading...
README
MIT

wechat

Latest Tag

这是用Golang封装了微信支付的所有API接口的SDK,并自动生成和解析XML数据,还包括部分服务号、小程序、移动端的工具函数。

  • 支持境内普通商户和境内服务商(境外和银行服务商没有条件测试)。
  • 支持全局配置应用ID、商家ID等信息。
  • 全部参数和返回值均使用struct类型传递,而不是map类型。
  • 包含公众号网页授权码的相关操作。
  • 包含H5、小程序、移动端的支付签名生成方法。

安装

go get -u gitee.com/cuckoopark/wechat

初始化

const (
    isProd       = true                             // 生产环境或沙盒环境
    isMch        = false                            // 是否是企业模式,仅当调用企业付款时为true
    serviceType  = wechat.ServiceTypeNormalDomestic // 普通商户或服务商等类型
    apiKey       = "xxxxxxxx"                       // 微信支付上设置的API Key
    certFilepath = "/xxx/yyy/apiclient_cert.p12"    // 微信证书文件的本地路径,仅部分接口使用,如果不使用这些接口,可以传递空值
)
config := wechat.Config{
    AppId: AppID,
    MchId: MchID,
    SubAppId: SubAppId, // 仅服务商模式有效
    SubMchId: SubMchID, // 仅服务商模式有效
}
client := wechat.NewClient(isProd, isMch, serviceType, apiKey, certFilepath, config)

使用

下面是通用的接口,使用上面初始化时生成的实例client进行相应函数的调用:

  • 提交付款码支付:func (*Client) Micropay(MicropayBody) (MicropayResponse, error)
  • 统一下单:func (*Client) UnifiedOrder(UnifiedOrderBody) (UnifiedOrderResponse, error)
  • 查询订单:func (*Client) QueryOrder(QueryOrderBody) (QueryOrderResponse, error)
  • 关闭订单:func (*Client) CloseOrder(CloseOrderBody) (CloseOrderResponse, error)
  • 撤销订单:func (*Client) Reverse(ReverseBody) (ReverseResponse, error)
  • 申请退款:func (*Client) Refund(RefundBody) (RefundResponse, error)
  • 查询退款:func (*Client) QueryRefund(QueryRefundBody) (QueryRefundResponse, error)
  • 下载对账单:func (*Client) DownloadBill(DownloadBillBody) (string, *DownloadBillResponse, error)
  • 交易保障(JSAPI):func (*Client) ReportJsApi(ReportJsApiBody) (ReportJsApiResponse, error)
  • 交易保障(MICROPAY):func (*Client) ReportMicropay(ReportMicropayBody) (ReportMicropayResponse, error)
  • 下载资金账单:TODO,client.DownloadFundFlow()
  • 拉取订单评价数据:TODO,client.BatchQueryComment()
  • 企业付款到零钱:func (*Client) Change(ChangeBody) (ChangeResponse, error)
  • 授权码查询OpenId:func (*Client) OpenIdByAuthCode(OpenIdByAuthCodeBody) (OpenIdByAuthCodeResponse, error)
  • 获取AccessToken:func GetAccessToken(appId string, appSecret string) (AccessToken, error)
  • 获取用户基本信息(UnionId机制):func GetUserInfo(accessToken string, openId string, lang ...string) (UserInfo, error)
  • 获取小程序支付签名:func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (string)
  • 获取H5支付签名:func GetH5PaySign(appId, nonceStr, packages, signType, timeStamp, apiKey string) (string)
  • 获取APP支付签名:func GetAppPaySign(appId, nonceStr, partnerId, prepayId, signType, timeStamp, apiKey string) (string)

其中带有(*Client)字样的接口,需要使用wechat.NewClient创建的实例对象来调用,而不带的接口,则可以直接使用wechat.XXX调用。

使用样例:

func Test() {
	// 初始化参数
	body := wechat.QueryOrderBody{}
	body.OutTradeNo = "YgENQFTovdeJdFouNyy3nFVOhGD6ZvPH"
	// 请求订单查询
	wxRsp, err := client.QueryOrder(body)
	if err != nil {
		return
	}
	fmt.Printf("返回值: %+v\n", wxRsp)
}

注意事项:

  • 参数或返回值的类型,请查看接口对应的wx_xxxxxx.go文件,里面有XXXBodyXXXResponse与之对应。
  • 参数或返回值中的常量,请参照constant.go文件。
  • 具体使用方法,请参照接口对应的wx_xxxxxx_test.go测试文件。

文档

开发进度

测试方法

修改client_test.go中的生成测试Client的代码,调整沙盒/生产环境、普通商户/服务商等选项,或者修改环境变量,来调整商户参数。

环境变量的脚本在env文件中,修改后加载环境变量:

source env
go test

TODO

  • 测试改为不同情境使用不同的用例。
  • 继续调试境内普通商户和境内服务商的其他模块API文档。
  • 选择性调试境外接口。
MIT License Copyright (c) 2019 Xiaosong Gao Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

微信支付、公众号、小程序相关后端接口的Go语言实现。 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/yiluojerry/wechat.git
git@gitee.com:yiluojerry/wechat.git
yiluojerry
wechat
微信Go SDK
master

搜索帮助

14c37bed 8189591 565d56ea 8189591