forked from lampnick/wxpay-refund-notify-decrypt-helper
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWxpayRefundNotifyHelper.php
123 lines (115 loc) · 2.7 KB
/
WxpayRefundNotifyHelper.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
<?php
namespace libs\wxpay;
/**
* @Author nick
* @Blog http://www.lampnick.com
* @Email nick@lampnick.com
*/
class WxpayRefundNotifyHelper
{
const MCH_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
const CIPHER = MCRYPT_RIJNDAEL_128;
const MCRYPT_MODE = MCRYPT_MODE_ECB;
/**
* You should implements this method to handle you own business logic.
* @param array $decryptedData
* @param string $msg this message will return to wechat if something error.
* @return bool
*/
protected function handelInternal(array $decryptedData, string &$msg)
{
//You should implements this method to handle you own business logic.
return true;
}
/**
* handle wechat pay refund notify
*/
public function handle()
{
try {
$xml = file_get_contents("php://input");
$data = $this->xml2array($xml);
$encryptData = $data['req_info'];
$decryptedData = $this->xml2array($this->decryptData($encryptData, self::MCH_KEY));
$msg = 'OK';
$result = $this->handelInternal($decryptedData, $msg);
$returnArray['return_msg'] = $msg;
if (true === $result) {
$returnArray['return_code'] = 'SUCCESS';
} else {
$returnArray['return_code'] = 'FAIL';
}
$this->replyNotify($returnArray);
} catch (\Exception $e) {
throw new \Exception($e);
}
}
/**
* reply to wechat
* @param $xml
*/
public function replyNotify($xml)
{
if (is_array($xml)) {
$xml = $this->toXml($xml);
}
echo $xml;
}
/**
* @param string $xml
* @return array
* @throws \Exception
*/
public function xml2array(string $xml)
{
if (empty($xml)) {
throw new \Exception('Error xml data!');
}
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $index);
xml_parser_free($p);
$result = [];
foreach ($values as $val) {
$result[strtolower($val['tag'])] = $val['value'];
}
return $result;
}
/**
* output xml
* @param array $array
* @return string
* @throws \Exception
*/
public function toXml(array $array)
{
if (empty($array)) {
throw new \Exception("array is empty!");
}
$xml = "<xml>";
foreach ($array as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* decrypt data
* @param string $encryptData
* @param string $md5LowerKey
* @return array
*/
public function decryptData(string $encryptData, string $Key = '')
{
//1. base64_decode
// openssl_decrypt only accept base64 input param
//2. md5 original key
$md5LowerKey = strtolower(md5($Key));
//3. decrypt AES ECB
$decrypted = openssl_decrypt($encryptData, "AES-256-ECB", $md5LowerKey);
return $decrypted;
}
}