forked from marsbaiyun/beecloud-php
/
BCWxmpRedpack.php
304 lines (280 loc) · 11.4 KB
/
BCWxmpRedpack.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
$BeeCloudWxmpSdkPath = dirname(__FILE__);
set_include_path(get_include_path().PATH_SEPARATOR.$BeeCloudWxmpSdkPath);
$BeeCloudWxmpLastErr = null;
function BCWxmpSetExecErr($msg) {
global $BeeCloudWxmpLastErr;
$BeeCloudWxmpLastErr = $msg;
}
function BCWxmpGetExecErr() {
global $BeeCloudWxmpLastErr;
return $BeeCloudWxmpLastErr;
}
class BCWxmpRedPackSetting {
static public $serverURL = "https://api.beecloud.cn/1";
static public $wxmpRedpackDebug = false;
static public function setWxmpRedpackDebug($debug) {
self::$wxmpRedpackDebug = !!$debug;
}
static public function getServerRandomUrl() {
$ipList = array(0=>"https://120.24.222.220/1",
1=>"https://115.28.40.236/1",
2=>"https://123.57.71.81/1",
3=>"https://121.41.120.98/1");
$seed = rand(0,3);
return $ipList[$seed];
}
}
class BCWxmpRedPackHttp {
static final public function request($url, $method, array $data, $timeout) {
try {
$timeout = (isset($timeout) && is_int($timeout)) ? $timeout : 20;
$ch = curl_init();
/*支持SSL 不验证CA根验证*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
/*重定向跟随*/
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, "BCPhp");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (!empty($timeout)) {
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
}
//设置 CURLINFO_HEADER_OUT 选项之后 curl_getinfo 函数返回的数组将包含 cURL
//请求的 header 信息。而要看到回应的 header 信息可以在 curl_setopt 中设置
//CURLOPT_HEADER 选项为 true
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
//fail the request if the HTTP code returned is equal to or larger than 400
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$header = array("Content-Type:application/json;charset=utf-8;", "Connection: keep-alive;");
$methodIgnoredCase = strtolower($method);
switch ($methodIgnoredCase) {
case "raw_post":
$header = array("Content-Type:application/json;charset=utf-8;", "Connection: keep-alive;");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //POST数据
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //POST数据
curl_setopt($ch, CURLOPT_URL, $url);
break;
case "post":
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //POST数据
curl_setopt($ch, CURLOPT_URL, $url);
break;
case "put":
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //POST数据
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_URL, $url);
break;
case "get":
curl_setopt($ch, CURLOPT_URL, $url."?para=".urlencode(json_encode($data)));
break;
case "raw_get":
curl_setopt($ch, CURLOPT_URL, $url);
break;
default:
throw new Exception('不支持的HTTP方式');
}
$result = curl_exec($ch);
if (curl_errno($ch) > 0) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
} catch (Exception $e) {
return $e->getMessage();
}
}
static public function formatResponse($raw) {
$result = json_decode($raw);
if (isset($result) && $result->resultCode == 0) {
return $result;
}
if (!isset($result) || $result == null){
BCWxmpSetExecErr($raw);
} else {
BCWxmpSetExecErr($result->errMsg);
}
return false;
}
}
class BCWxmpApiUtil {
static public $debugMsg = null;
static public function checkSignature(array $data, $token){
$signature = $data["signature"];
$timestamp = $data["timestamp"];
$nonce = $data["nonce"];
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
static public function responseDebugText($text) {
$fromUsername = self::$debugMsg->fromUserName;
if (!isset($fromUsername))
return false;
$toUsername = self::$debugMsg->toUserName;
$uxtime = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0<FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $uxtime, $text);
return $resultStr;
}
static public function sendWxmpRedpack($serverUrl, $fromUsername, $redpack, $beecloud, $timeout) {
$timeout = isset($timeout) ? $timeout : 30;
$now = time();
$redpack["appId"] = $beecloud->appId;
$redpack["appSign"] = $beecloud->appSign;
$redpack["re_openid"] = "$fromUsername";
$redpack["mch_billno"] = ($beecloud->mchId) . date("Ymd", $now) . $now;//mch_id + yyyymmdd + timestamp
return BCWxmpRedPackHttp::request($serverUrl . "/pay/wxmp/redPackExtra", "get", $redpack, $timeout);
}
static public function _getCallMsg($msgStr) {
$msgObj = new stdClass();
$postObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$msgObj->keyword = trim($postObj->Content);
$msgObj->type = $postObj->MsgType;
$msgObj->event = $postObj->Event;
$msgObj->xmlObj = $postObj;
$msgObj->fromUserName = $postObj->FromUserName;
$msgObj->toUserName = $postObj->ToUserName;
//xmlObj->BCDebug for debug use
return $msgObj;
}
}
class BCWxmpApi {
public $appSign;
public $appId;
public $msg;
public $mchId;
public function __construct($appId, $appSecret, $mchId) {
if (empty($appId) || empty($appSecret) || empty($mchId)) {
throw new Exception('除 lockPath ,请检查参数不能为空');
}
if (!is_string($appId) || !is_string($appSecret) || !is_string($mchId)) {
throw new Exception('输入必须是String类型');
}
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->appSign = md5($appId.$appSecret);
$this->msg = new stdClass();
$this->mchId = $mchId;
}
/**
* 返回为 object : { type: "微信类型",
* keyword:"用户输入的字符串“,
* event: "微信事件类型"
* fromUserName: "发送信息的用户openId"
* toUserName: "目标公众号id"
* xmlObj: 解析的完整结果
* }
* 如果 返回false 表示数据不能解析
**/
public function getCallMsg($postStr) {
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->msg = BCWxmpApiUtil::_getCallMsg($postStr);
if (BCWxmpRedPackSetting::$wxmpRedpackDebug) {
BCWxmpApiUtil::$debugMsg = $this->msg;
}
return $this->msg;
}
return false;
}
/**
* @param $news {title, description, picUrl, linkUrl}
* @return string
*/
public function responseNews($news) {
$fromUsername = $this->msg->fromUserName;
$toUsername = $this->msg->toUserName;
$uxtime = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $uxtime, $news->title, $news->description, $news->picUrl, $news->linkUrl);
return $resultStr;
}
public function responseText($text) {
$fromUsername = $this->msg->fromUserName;
$toUsername = $this->msg->toUserName;
$uxtime = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0<FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $uxtime, $text);
return $resultStr;
}
/**
* 微信内给 “发送消息的用户” 发送红包;该接口云端处理红包的相关检查检查
* @param array $redpack
* @param $time curl request 超时时间
* @return mixed|string
*
* $redpack 说明:
*
*
* $redpack = array(
* "nick_name" => "BeeCloud",
* "send_name" => "BeeCloud",
* "total_amount" => 100, //(分)红包固定金额
* "wishing" => "接入BeeCloud微信红包SDK,就可以实现发放微信红包功能,策划各种脑洞大开的粉丝活动啦!",
* "act_name" => "BeeCloud红包雨",
* "remark" => "BeeCloud",
* "count_per_user" => 100, //在当前时间t到 t - period时间内每个用户能得到红包个数上限(选填,默认为1),此处特别注意count_per_user, period根据你的需求的设置
* "period" => 300000, //(ms)用户领取红包的判重时间长度,默认为‘所有时间内’
* "probability" => 0.3 //(float)单次获得红包概率 范围0-1, 默认为1
* );
*/
public function sendRedpack(array $redpack, $time) {
$fromUserName = $this->msg->fromUserName;
return $this->sendRedpackTo($fromUserName, $redpack, $time);
}
/**
* 向指定openID用户发送红包
* @param to $userOpenID
* @param array $redpack
* @param $time curl request 超时时间
* @return mixed|string
*/
public function sendRedpackTo($userOpenID, array $redpack, $time) {
$time = isset($time) ? $time : 30;
return BCWxmpApiUtil::sendWxmpRedpack(BCWxmpRedPackSetting::getServerRandomUrl(), $userOpenID, $redpack, $this, $time);
}
}