GitHub地址:wxpay
wxpay 提供了对应的方法:
| 方法名 | 说明 |
|---|---|
| MicroPay | 刷卡支付 |
| UnifiedOrder | 统一下单 |
| OrderQuery | 查询订单 |
| Reverse | 撤销订单 |
| CloseOrder | 关闭订单 |
| Refund | 申请退款 |
| RefundQuery | 查询退款 |
| DownloadBill | 下载对账单 |
| Report | 交易保障 |
| ShortUrl | 转换短链接 |
| AuthCodeToOpenid | 授权码查询openid |
ParamsParamsParamsappidmch_idnonce_strsign_typesignreturn_codereturn_msgreturn_codeSUCCESSdata
安装
go get github.com/objcoding/wxpay
示例
// 新建微信支付客户端
client := wxpay.NewClient(wxpay.NewAccount{
AppID: "appid",
MchID: "mchid",
ApiKey: "apiKey",
}, false) // sandbox环境请传true
// 统一下单
params := make(wxpay.Params)
params.SetString("body", "test").
SetString("out_trade_no", "436577857").
SetInt64("total_fee", 1).
SetString("spbill_create_ip", "127.0.0.1").
SetString("notify_url", "http://notify.objcoding.com/notify").
SetString("trade_type", "APP")
p, _ := client.UnifiedOrder(params)
// 订单查询
params := make(wxpay.Params)
params.SetString("out_trade_no", "3568785")
p, _ := client.OrderQuery(params)
// 退款
params := make(wxpay.Params)
params.SetString("out_trade_no", "3568785").
SetString("out_refund_no", "19374568").
SetInt64("total_fee", 1).
SetInt64("refund_fee", 1)
p, _ := client.Refund(params)
// 退款查询
params := make(wxpay.Params)
params.SetString("out_refund_no", "3568785")
p, _ := client.RefundQuery(params)
// 创建支付账户
account := wxpay.NewAccount("appid", "mchid", "apiKey")
// 设置证书
account.SetCertData("证书地址")
// 新建微信支付客户端
client := wxpay.NewClient(account, false) // sandbox环境请传true
// 设置http请求超时时间
client.SetHttpConnectTimeoutMs(2000)
// 设置http读取信息流超时时间
client.SetHttpReadTimeoutMs(1000)
// 更改签名类型
client.SetSignType(HMACSHA256)
// 设置支付账户
client.setAccount(account)
// 签名 signStr := client.Sign(params) // 校验签名 b := client.ValidSign(params)
// 支付或退款返回成功信息
return wxpay.Notifies{}.OK()
// 支付或退款返回失败信息
return wxpay.Notifies{}.NotOK("支付失败或退款失败了")
License
MIT license