forked from gofunbox/wechat_app_pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechatAPISDK.php
158 lines (145 loc) · 4.56 KB
/
wechatAPISDK.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
<?php
/**
* 微信支付
* @author funbox www.funboxpower.com
* @copyright 2014
*/
require_once ('base/HttpClient.class.php');
class Wechat{
var $wechat_config;
var $wechat_time;
var $wechat_noncestr;
function __construct($wechat_config){
$this->wechat_config = $wechat_config;
}
/**
* Wechat::buildPackage()
* 生成package
* @param array $parameter
* @return string
*/
public function buildPackage($parameter) {
$filter = array('bank_type', 'body', 'partner', 'out_trade_no', 'total_fee', 'fee_type', 'notify_url', 'spbill_create_ip', 'input_charset');
$base = array(
'notify_url' => $this->wechat_config['notify_url'],
'bank_type' => 'WX',
'fee_type' => '1',
'input_charset' => 'UTF-8',
'partner' => $this->wechat_config['partner_id'],
);
$parameter = array_merge($parameter, $base);
$array = array();
foreach ($parameter as $k => $v) {
if (in_array($k, $filter)) {
$array[$k] = $v;
}
}
ksort($array);
reset($array);
$signPars = '';
foreach ($array as $k => $v) {
$signPars .= $k."=".$v."&";
}
$sign = strtoupper(md5($signPars.'key='.$this->wechat_config['partner_key']));
$signPars = '';
foreach ($array as $k => $v) {
$signPars .= strtolower($k) . "=" . urlencode($v) . "&";
}
return $signPars . 'sign=' . $sign;
}
/**
* Wechat::getXmlArray()
* 从xml中获取数组
* @return array
*/
public function getXmlArray() {
$xmlData = file_get_contents("php://input");
if ($xmlData) {
$postObj = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA);
if (! is_object($postObj)) {
return false;
}
$array = json_decode(json_encode($postObj), true); // xml对象转数组
return array_change_key_case($array, CASE_LOWER); // 所有键小写
} else {
return false;
}
}
/**
* Wechat::verifyNotify()
* 验证服务器通知
* @param array $data
* @return array
*/
public function verifyNotify($data) {
$xml = $this->getXmlArray();
if (! $xml || ! $data) {
return false;
}
$AppSignature = $xml['appsignature'];
unset($xml['signmethod'], $xml['appsignature']);
$sign = $this->buildSign($xml);
if ($AppSignature != $sign) {
return false;
} elseif ($data['trade_state'] != 0) {
return false;
}
return $xml;
}
/**
* Wechat::buildSign()
* 生成sign值
* @param array $array
* @return string
*/
public function buildSign($array) {
$signPars = "";
$array['appkey'] = $this->wechat_config['pay_sign_key'];
ksort($array);
reset($array);
foreach($array as $k => $v) {
$signPars.=$k."=".$v."&";
}
$signPars = rtrim($signPars, '&'); // 去除最后一个&符号
$sign = sha1($signPars);
return $sign;
}
/**
* wechat::getAccessToken()
* 获取access_token
* @return string
*/
public function getAccessToken() {
$request = HttpClient::quickGet('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->wechat_config['app_id'] . '&secret=' . $this->wechat_config['app_secret'] , $this->wechat_config['cacert_url']);
$requestArray = json_decode($request, true);
if (isset($requestArray['errcode'])) {
return false;
}
$accessToken = $requestArray['access_token'];
return $accessToken;
}
/**
* Wechat::createorder()
* 生成预支付订单
* @param array $access_token
* @param array $parameter
* @return array
*/
public function createOrder($access_token , $parameter) {
$url = 'https://api.weixin.qq.com/pay/genprepay?access_token='.$access_token;
$params = array(
'appid' => $this->wechat_config['app_id'],
'traceid'=>'',
'noncestr' => uniqid(),
'package' => $this->buildPackage($parameter),
'timestamp' => time(),
);
//用于之后的手机唤起 sign
$this->wechat_noncestr = $params['noncestr'];
$this->wechat_time = $params['timestamp'];
$params['app_signature'] = $this->buildSign($params);
$params['sign_method'] = 'sha1';
$result = HttpClient::quickPost($url, json_encode($params));
return json_decode($result, true);
}
}