NotifyController.php
7.3 KB
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
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/6/13
* Time: 17:53
*/
namespace api\portal\controller;
use api\portal\controller\NotifyCommonController;
use think\Config;
use think\Db;
class NotifyController extends NotifyCommonController
{
// 微信支付回调
public function notify() {
require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Api.php";
require_once VENDOR_PATH."WxpayAPI/lib/WxPay.Notify.php";
require_once VENDOR_PATH.'WxpayAPI/example/log.php';
$post = $this->request->param();
if($post==null){
$post = file_get_contents("php://input");
if($post == null){
$post = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
if(!empty($post)) {
$xml = $post; //微信的回调数据
$base = new \WxPayResults(); //实例化数据对象结果类
$data = $base->FromXml($xml);
if($base->CheckSign() == true){
if ($data["return_code"] == "SUCCESS") {
if (!empty($data['out_trade_no'])) {
$where['order_sn'] = $data['out_trade_no'];
}
// $where['money'] = $data['total_fee']/100;
$pay_type = 1;
$title = 'pc微信扫码支付';
if(!empty($data["trade_type"]) && $data["trade_type"] == 'MWEB') {
$pay_type = 2;
$title = '网页H5支付';
}
$result = $this->changeOrderStatus($where,$data['transaction_id'],$pay_type,$title);
Db::name('Test')->insert(['data'=>$result]);
if($result) {
$this->return_success();
}
}
}
}
}
/**
* 微信支付退款结果回调
*/
public function refund_notify() {
$post = $this->request->param();
if($post==null){
$post = file_get_contents("php://input");
if($post == null){
$post = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
if(!empty($post)) {
$xml = $post; //微信的回调数据
$data = $this->xmlToArray($xml);
if($data['return_code'] == 'SUCCESS' && !empty($data['req_info'])) {
$key = md5(Config::get('wx_key'));
$array = $this->xmlToArray($this->refund_decrypt($data['req_info'],$key));
$refund_where = [
'order_sn' => $array['out_refund_no'],
'refund_id' => $array['refund_id'],
];
$order_where = [
'transaction_id' => $array['transaction_id'],
];
$handle_sql = true;
Db::startTrans();
if($array['refund_status'] == 'SUCCESS') {
$refundInfo = Db::name('Refund')->where($refund_where)->find();
$refund_update = [
'status' => 2,
'refund_time' => strtotime($array['success_time']),
'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
$order_update = [
'status' => 4,
'refund_time' => strtotime($array['success_time'])
];
$order_result = Db::name('Order')->where($order_where)->update($order_update);
$order_type = '';
if($refundInfo['user_type'] == 1) {
$order_type = 4;
}if($refundInfo['user_type'] == 2) {
$order_type = 5;
}
$insert = [
'order_id' => $refundInfo['order_id'],
'user_id' => $refundInfo['user_id'],
'user_type' => $refundInfo['user_type'],
'title' => '退款',
'money' => $refundInfo['money'],
'order_type' => $order_type,
'type' => 1,
'reason' => $refundInfo['reason'],
'create_time' => time(),
'update_time' => time()
];
$money_detail_insert = Db::name('MoneyDetail')->insertGetId($insert);
if(!$refund_result || !$order_result || !$money_detail_insert) {
$handle_sql = false;
}
} elseif($array['refund_status'] == 'CHANGE') {
$refund_update = [
'status' => 4,
// 'refund_time' => strtotime($array['success_time']),
// 'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
if(!$refund_result) {
$handle_sql = false;
}
} elseif($array['refund_status'] == 'REFUNDCLOSE') {
$refund_update = [
'status' => 3,
// 'refund_time' => strtotime($array['success_time']),
// 'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
if(!$refund_result) {
$handle_sql = false;
}
}
if(!$handle_sql) {
Db::rollback();
} else {
Db::commit();
$this->return_success();
}
}
}
}
/**
* 微信退款回调数据
* (1)对加密串A做base64解码,得到加密串B
* (2)对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 )
* (3)用key*对加密串B做AES-256-ECB解密(PKCS7Padding)
*/
private function refund_decrypt($str, $key) {
$str = base64_decode($str);
$decrypted = openssl_decrypt($str, 'AES-256-ECB', $key, OPENSSL_RAW_DATA);
return $decrypted;
}
//xml转换成数组
private function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
/*
* 给微信发送确认订单金额和签名正确,SUCCESS信息
*/
private function return_success(){
$return['return_code'] = 'SUCCESS';
$return['return_msg'] = 'OK';
$xml_post = '<xml>
<return_code>'.$return['return_code'].'</return_code>
<return_msg>'.$return['return_msg'].'</return_msg>
</xml>';
echo $xml_post;exit;
}
}