Ajax.php
7.7 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
<?php
namespace app\index\controller;
use addons\litestore\model\Litestoreorder;
use addons\third\model\Third;
use addons\wechat\library\Config as ConfigService;
use app\common\controller\Frontend;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Foundation\Application as WXPAY_APP;
use think\Db;
use think\Lang;
/**
* Ajax异步请求接口
* @internal
*/
class Ajax extends Frontend
{
protected $noNeedLogin = ['lang','callback_for_wxgzh','refund_notify'];
protected $noNeedRight = ['*'];
protected $layout = '';
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->id;
$this->model = new Litestoreorder;
}
/**
* 加载语言包
*/
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 callback_for_wxgzh(){
$this->init_wx_pay_for_gzh(false);
$response = $this->wxapp->payment->handleNotify(function($notify, $successful){
$order = $this->model->payDetail($notify->out_trade_no);
if (empty($order)) { // 如果订单不存在
return true;
//return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
if ($successful) {
$order->updatePayStatus($notify->transaction_id);
}
return true; // 返回处理完成
});
$response->send();
}
private function init_wx_pay_for_gzh($Ischeck=false){
//这里首先判断 此用户是否绑定了微信公众号
if($Ischeck){
$third = Third::where(['user_id' => $this->user_id, 'platform' => 'wechat'])->find();
if(!$third){
//从这里自动绑定微信公众号的账户
$this->error('您未绑定微信号',null,1008);
}
}
$config = get_addon_config('litestore');
$third_config = get_addon_config('third');
$third_options = array_intersect_key($third_config, array_flip(['wechat']));
$third_options = $third_options['wechat'];
$options = [
'debug' => true,
'log' => [
'level' => 'debug',
'file' => '/tmp/easywechat.log',
],
'app_id' => $third_options['app_id'],
'secret' => $third_options['app_secret'],
'payment' => [
'merchant_id' => $config['MCHIDGZH'],
'key' => $config['APIKEYGZH'],
'notify_url' => \think\Request::instance()->domain().addon_url('litestore/api.order/callback_for_wxgzh'),
],
];
$this->wxapp = new WXPAY_APP($options);
}
/**
* 退款结果回调
*/
public function refund_notify() {
$app = new Application(ConfigService::load());
$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');
}
}