diff --git a/README.md b/README.md index 04abbdd..f598681 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,16 @@ # wxpay 微信支付(开发中) + +# Usage +```bash +go get -u github.com/nilorg/wxpay +``` +# Import +```go +import "github.com/nilorg/wxpay" +``` + # 文档 1. [协议规范](https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_1) @@ -16,4 +26,6 @@ 10. 交易保障 11. 退款结果通知 12. 拉取订单评价数据 -13. 现金红包 ✅ \ No newline at end of file +13. 现金红包-发红包 ✅ +14. 现金红包-发裂变红包 ✅ +15. 现金红包-查红包记录 \ No newline at end of file diff --git a/client.go b/client.go index 7d7e21c..f40f755 100644 --- a/client.go +++ b/client.go @@ -115,8 +115,8 @@ func (c *Client) UnifiedOrder(req *UnifiedOrderRequest) (resp *UnifiedOrderRespo return } -// SendredPack 发现金红包 -func (c *Client) SendredPack(req *SendredPackRequest) (resp *SendredPackResponse, err error) { +// SendRedPack 发现金红包 +func (c *Client) SendRedPack(req *SendRedPackRequest) (resp *SendRedPackResponse, err error) { req.MchID = c.conf.MchID err = req.SignMD5(c.conf.APIKey) if err != nil { @@ -129,7 +129,38 @@ func (c *Client) SendredPack(req *SendredPackRequest) (resp *SendredPackResponse return } - resp = new(SendredPackResponse) + resp = new(SendRedPackResponse) + err = xml.Unmarshal(body, resp) + if err != nil { + resp = nil + return + } + if resp.ReturnCode == "FAIL" { + err = fmt.Errorf("通信错误:%s", resp.ReturnMsg) + return + } + if resp.ResultCode == "FAIL" { + err = fmt.Errorf("业务错误:%s", resp.ErrCodeDes) + return + } + return +} + +// SendGroupRedPack 发裂变红包 +func (c *Client) SendGroupRedPack(req *SendGroupRedPackRequest) (resp *SendGroupRedPackResponse, err error) { + req.MchID = c.conf.MchID + err = req.SignMD5(c.conf.APIKey) + if err != nil { + return + } + + var body []byte + body, err = c.execute("/mmpaymkttransfers/sendgroupredpack", req) + if err != nil { + return + } + + resp = new(SendGroupRedPackResponse) err = xml.Unmarshal(body, resp) if err != nil { resp = nil diff --git a/go.mod b/go.mod index e49a878..ddb34d5 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,4 @@ module github.com/nilorg/wxpay go 1.13 -require ( - github.com/bitly/go-simplejson v0.5.0 - github.com/coreos/etcd v3.3.15+incompatible // indirect - github.com/nilorg/sdk v0.0.0-20190818083213-d52976243caf -) +require github.com/nilorg/sdk v0.0.0-20190818083213-d52976243caf diff --git a/go.sum b/go.sum index f0392d0..514ac69 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,2 @@ -github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y= -github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= -github.com/coreos/etcd v3.3.15+incompatible h1:+9RjdC18gMxNQVvSiXvObLu29mOFmkgdsB4cRTlV+EE= -github.com/coreos/etcd v3.3.15+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/nilorg/sdk v0.0.0-20190818083213-d52976243caf h1:RueXnD1RRiIx8BHlCjy48n1lEA9ArWxL6vO3etNUxwY= github.com/nilorg/sdk v0.0.0-20190818083213-d52976243caf/go.mod h1:X1swpPdqguAZaBDoEPyEWHSsJii0YQ1o+3piMv6W3JU= diff --git a/send_group_red_pack.go b/send_group_red_pack.go new file mode 100644 index 0000000..f07159b --- /dev/null +++ b/send_group_red_pack.go @@ -0,0 +1,58 @@ +package wxpay + +import ( + "encoding/xml" + + "github.com/nilorg/sdk/random" +) + +// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4 + +// SendGroupRedPackRequest 发裂变红包 +type SendGroupRedPackRequest struct { + XMLName xml.Name `xml:"xml"` + NonceStr string `xml:"nonce_str"` + Sign string `xml:"sign"` + MchBillno string `xml:"mch_billno"` + MchID string `xml:"mch_id"` + SendName string `xml:"send_name"` + ReOpenID string `xml:"re_openid"` + TotalAmount uint `xml:"total_amount"` + TotalNum uint `xml:"total_num"` + AmtType string `xml:"amt_type"` + Wishing string `xml:"wishing"` + ActName string `xml:"act_name"` + Remark string `xml:"remark"` + SceneID string `xml:"scene_id"` + RiskInfo string `xml:"risk_info"` +} + +// NewSendGroupRedPackRequest 创建裂变红包 +func NewSendGroupRedPackRequest() *SendGroupRedPackRequest { + return &SendGroupRedPackRequest{ + NonceStr: random.AZaz09(32), + } +} + +// SignMD5 md5 +func (req *SendGroupRedPackRequest) SignMD5(apiKey string) error { + params, err := SignStructToParameter(*req) + if err != nil { + return err + } + params["key"] = apiKey + value := SignMD5(params) + req.Sign = value + return nil +} + +// SendGroupRedPackResponse ... +type SendGroupRedPackResponse struct { + ResponseStatus + MchBillno string `xml:"mch_billno"` + MchID string `xml:"mch_id"` + WxAppID string `xml:"wxappid"` + ReOpenID string `xml:"re_openid"` + TotalAmount uint `xml:"total_amount"` + SendListID uint `xml:"send_listid"` +} diff --git a/sendred_pack.go b/send_red_pack.go similarity index 79% rename from sendred_pack.go rename to send_red_pack.go index 9c03ebc..fa5cc8a 100644 --- a/sendred_pack.go +++ b/send_red_pack.go @@ -8,8 +8,8 @@ import ( // https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3 -// SendredPackRequest 发红包 -type SendredPackRequest struct { +// SendRedPackRequest 发红包 +type SendRedPackRequest struct { XMLName xml.Name `xml:"xml"` NonceStr string `xml:"nonce_str"` Sign string `xml:"sign"` @@ -27,15 +27,15 @@ type SendredPackRequest struct { RiskInfo string `xml:"risk_info"` } -// NewSendredPackRequest 创建现金红包 -func NewSendredPackRequest() *SendredPackRequest { - return &SendredPackRequest{ +// NewSendRedPackRequest 创建现金红包 +func NewSendRedPackRequest() *SendRedPackRequest { + return &SendRedPackRequest{ NonceStr: random.AZaz09(32), } } // SignMD5 md5 -func (req *SendredPackRequest) SignMD5(apiKey string) error { +func (req *SendRedPackRequest) SignMD5(apiKey string) error { params, err := SignStructToParameter(*req) if err != nil { return err @@ -46,8 +46,8 @@ func (req *SendredPackRequest) SignMD5(apiKey string) error { return nil } -// SendredPackResponse ... -type SendredPackResponse struct { +// SendRedPackResponse ... +type SendRedPackResponse struct { ResponseStatus MchBillno string `xml:"mch_billno"` MchID string `xml:"mch_id"`