Skip to content

Commit

Permalink
添加发裂变红包
Browse files Browse the repository at this point in the history
  • Loading branch information
DeyiXu committed Sep 21, 2019
1 parent 82d27e5 commit 883b5e7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 21 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -16,4 +26,6 @@
10. 交易保障
11. 退款结果通知
12. 拉取订单评价数据
13. 现金红包 ✅
13. 现金红包-发红包 ✅
14. 现金红包-发裂变红包 ✅
15. 现金红包-查红包记录
37 changes: 34 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
58 changes: 58 additions & 0 deletions send_group_red_pack.go
Original file line number Diff line number Diff line change
@@ -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"`
}
16 changes: 8 additions & 8 deletions sendred_pack.go → send_red_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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"`
Expand Down

0 comments on commit 883b5e7

Please sign in to comment.