-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
148 lines (127 loc) · 3.77 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package qqenvelop
import (
"fmt"
"github.com/birjemin/qqenvelop/utils"
"log"
"net/url"
"strconv"
"strings"
)
// QPayHbURL send envelop url
const qPayHbURL = "https://api.qpay.qq.com/cgi-bin/hongbao/qpay_hb_mch_send.cgi"
// SendQPayHb model
type SendQPayHb struct {
MchID string
MchName string
AppSecret string
HTTPRequest *utils.HTTPClient
QqAppID string // default empty
NotifyURL string // default empty
Charset int // default utf8
}
// ParamsSendQPayHb params of envelop
type ParamsSendQPayHb struct {
TotalAmount int
Wishing string
ActName string
IconID int
TotalNum int // default 1
BannerID int // default empty
NotSendMsg int // default 0
MinValue int // default TotalAmount
MaxValue int // default TotalAmount
}
// RespSendQPayHb response
type RespSendQPayHb struct {
RetCode string `json:"retcode"`
RetMsg string `json:"retmsg"`
ReturnCode string `json:"return_code"`
ReturnMsg string `json:"return_msg"`
ListID string `json:"listid"`
}
// SendQPayHb send red envelop
// Doc: https://mp.qpay.tenpay.com/buss/wiki/221/1220
func (s *SendQPayHb) SendQPayHb(OpenID string, attributes ParamsSendQPayHb) (*RespSendQPayHb, error) {
return s.doSendQPayHb(qPayHbURL, OpenID, attributes)
}
// doSendQPayHb handle this action
func (s *SendQPayHb) doSendQPayHb(URL, OpenID string, attributes ParamsSendQPayHb) (*RespSendQPayHb, error) {
total := strconv.Itoa(attributes.TotalAmount)
params := map[string]string{
"charset": strconv.Itoa(s.getCharset()),
"nonce_str": utils.Hex(10),
"mch_billno": s.generateBillNo(),
"mch_id": s.MchID,
"mch_name": s.MchName,
"re_openid": OpenID,
"total_amount": total,
"total_num": strconv.Itoa(s.getTotalNum(attributes.TotalNum)),
"wishing": attributes.Wishing,
"act_name": attributes.ActName,
"icon_id": strconv.Itoa(attributes.IconID),
"not_send_msg": strconv.Itoa(attributes.NotSendMsg),
}
// 接收红包者openid或者uin,当re_openid是openid时,qqappid必填
if s.QqAppID != "" {
params["qqappid"] = s.QqAppID
}
if s.NotifyURL != "" {
params["notify_url"] = s.NotifyURL
}
if attributes.BannerID != 0 {
params["banner_id"] = strconv.Itoa(attributes.BannerID)
}
if attributes.MinValue == 0 {
params["min_value"] = total
} else {
params["min_value"] = strconv.Itoa(attributes.MinValue)
}
if attributes.MaxValue == 0 {
params["max_value"] = total
} else {
params["max_value"] = strconv.Itoa(attributes.MaxValue)
}
params["sign"] = generateSign(generateQueryStr(s.AppSecret, params))
var resp = new(RespSendQPayHb)
if err := s.HTTPRequest.HTTPPost(URL, params); err != nil {
log.Println("[send]do, post failed", err)
return resp, err
}
if err := s.HTTPRequest.GetResponseJSON(resp); err != nil {
log.Println("[send]do, response json failed", err)
return resp, err
}
return resp, nil
}
// generateBillNo get bill_no
func (s *SendQPayHb) generateBillNo() string {
return s.MchID + utils.GetDateNum() + strconv.Itoa(utils.RandNum(1000000000, 10000000000))
}
// getCharset get charset
// option: 1 - utf8, 2 - gbk
// default value: 1
func (s *SendQPayHb) getCharset() int {
if s.Charset != 2 {
s.Charset = 1
}
return s.Charset
}
// getTotalNum get
// desc: total_num default 1
func (s *SendQPayHb) getTotalNum(num int) int {
if num <= 0 {
num = 1
}
return num
}
// generateSign generate sign
func generateSign(queryStr string) string {
return strings.ToUpper(utils.GetMD5String(queryStr))
}
// generateQueryStr generate query str
// doc: https://mp.qpay.tenpay.com/buss/wiki/221/1244
func generateQueryStr(appSecret string, params map[string]string) string {
query := utils.QuerySortByKeyStr2(params)
query, _ = url.QueryUnescape(query)
return fmt.Sprintf("%s&key=%s", query, appSecret)
}