Ajax.php
5.4 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
<?php
namespace app\index\controller;
use app\common\controller\Frontend;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Lang;
/**
* Ajax异步请求接口
* @internal
*/
class Ajax extends Frontend
{
protected $noNeedLogin = ['lang','refund_notify'];
protected $noNeedRight = ['*'];
protected $layout = '';
/**
* 加载语言包
*/
public function lang()
{
header('Content-Type: application/javascript');
header("Cache-Control: public");
header("Pragma: cache");
$offset = 30 * 60 * 60 * 24; // 缓存一个月
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
$controllername = input("controllername");
$this->loadlang($controllername);
//强制输出JSON Object
$result = jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
return $result;
}
/**
* 退款结果回调
*/
public function refund_notify() {
$app = new Application();
$response = $app->payment->handleRefundNotify(function ($message, $reqInfo) {
// Log::write(date('Y-m-d H:i') . '订单退款操作,返回数据:' . json_encode($message, JSON_UNESCAPED_UNICODE), 'order_refund');
// 其中 $message['req_info'] 获取到的是加密解密信息
// $reqInfo 成功时为1
// 你的业务逻辑...
if($reqInfo) {
$refund_where = [
'order_sn' => $message['req_info']['out_trade_no'],
'transaction_id' => $message['req_info']['transaction_id'],
'out_refund_no' => $message['req_info']['out_refund_no']
];
$refund = Db::name('Refund')->where($refund_where)->find();
// Log::write(date('Y-m-d H:i') . '订单退款操作1,返回数据:' . json_encode($refund, JSON_UNESCAPED_UNICODE), 'order_refund');
if($refund['status'] == 1) {
return true;
} else {
Db::startTrans();
$update = [
'out_refund_no' => $message['req_info']['out_refund_no'],
'refund_id' => $message['req_info']['refund_id'],
'refund_account' => $message['req_info']['refund_account'],
'refund_request_source' => $message['req_info']['refund_request_source'],
'updatetime' => time(),
'more' => json_encode($message)
];
$ask_order_result = true;
// 处理退款状态
if($message['req_info']['refund_status'] == 'SUCCESS') {
$update['status'] = 1;
$update['success_time'] = strtotime($message['req_info']['success_time']);
// Log::write(date('Y-m-d H:i') . '订单退款操作2,返回数据:' . json_encode($update, JSON_UNESCAPED_UNICODE), 'order_refund');
if($refund['type'] == 1) {
// 修改订单状态为已退款
$ask_order_update = [
'id' => $refund['ask_order_id'],
'status' => 4
];
$ask_order_result = Db::name('ask_order')->update($ask_order_update);
}
if($refund['type'] == 2) {
// 修改订单状态为已退款
$ask_order_update = [
'id' => $refund['ask_order_id'],
'status' => 2
];
$ask_order_result = Db::name('ask_orderson')->update($ask_order_update);
}
} elseif($message['req_info']['refund_status'] == 'CHANGE') {
$update['status'] = 2;
} elseif($message['req_info']['refund_status'] == 'REFUNDCLOSE') {
$update['status'] = 3;
}
// Log::write(date('Y-m-d H:i') . '订单退款操作3,返回数据:' . json_encode($refund_where, JSON_UNESCAPED_UNICODE), 'order_refund');
$result = Db::name('Refund')->where($refund_where)->update($update);
if(!$result || !$ask_order_result) {
// Log::write(date('Y-m-d H:i') . '订单退款操作5,返回数据:' . json_encode([$result,$ask_order_result], JSON_UNESCAPED_UNICODE), 'order_refund');
Db::rollback();
return false;
}
// Log::write(date('Y-m-d H:i') . '订单退款操作4,返回数据:' . json_encode([$result], JSON_UNESCAPED_UNICODE), 'order_refund');
Db::commit();
}
}
return true; // 返回 true 告诉微信“我已处理完成”
// 或返回错误原因 $fail('参数格式校验错误');
});
$response->send();
}
/**
* 上传文件
*/
public function upload()
{
return action('api/common/upload');
}
}