Notify.php
8.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace app\api\controller;
use app\api\model\Appointment;
use app\api\model\Doctor;
use app\api\model\DoctorLevel;
use app\api\model\DoctorMoneyLog;
use app\api\model\GiftOrder;
use app\common\controller\Api;
use app\common\controller\Wechat;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Log;
use think\Validate;
/**
* @ApiInternal
* 支付回调
*/
class Notify extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $layout = '';
public function _initialize()
{
parent::_initialize();
}
/**
* 异步回调(预约)
* @ApiInternal
*/
public function notifyAppointment()
{
$payment = Wechat::payment();
$response = $payment->handlePaidNotify(function($message, $fail){
if ($message['return_code'] !== 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
return $fail('通信失败,请稍后再通知我');
}
// 用户支付失败 - 支付失败也需要返回true,表示服务器已处理,不然微信会反复通知,没有意义
if ($message['result_code'] === 'FAIL') {
return true;
}
// 支付成功后的业务逻辑
if ($message['result_code'] === 'SUCCESS') {
Db::startTrans();
try {
// 处理订单
$this->handleAppointment($message);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
return $fail('服务器处理失败,请稍后再通知我');
} catch (Exception $e) {
Db::rollback();
return $fail('服务器处理失败,请稍后再通知我');
}
}
return true;
});
$response->send(); // return $response;
}
/**
* 异步回调(预约)-零元
* @ApiInternal
*/
public function notifyAppointmentZero($message)
{
Db::startTrans();
try {
$this->handleAppointment($message);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
return false;
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
return false;
}
return true;
}
/**
* 异步回调(预约)-处理订单
*/
private function handleAppointment($message)
{
$appointment = Appointment::get(['order_sn'=>$message['out_trade_no']]);
if (!$appointment || $appointment['status'] == '1') { // 如果订单不存在 或者 订单已经支付过了
return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
$appointment->transaction_id = $message['transaction_id']; // 回填微信的订单号
$appointment->pay_time = time(); // 更新支付时间为当前时间
$appointment->status = '1';
$appointment->save();
// 医生信息
$doctor = $appointment['doctor'];
if($doctor){
// 即将到账
$level = DoctorLevel::where('id',$doctor['level_id'])->find();
$doctor_income = $level ? round($appointment['pay_fee'] * ($level['income_ratio'] / 100),2) : 0;
if($doctor_income > 0){
DoctorMoneyLog::create([
'doctor_id' => $appointment['doctor_id'],
'money' => $doctor_income,
'before' => $doctor['money'],
'after' => $doctor['money'],
'memo' => '即将到账',
'appointment_id' => $appointment['id'],
'status' => '2'
]);
}
// 给被预约医生发送短信提醒
if(Validate::regex($doctor['mobile'], "^1\d{10}$")){
$ret = send_sms2([
'content' => '【在线会诊】预约人名称:' . $appointment['user']['nickname'] . ',下单时间:' . date('Y-m-d H:i:s') . ',预约时间:' . date('Y-m-d H:i',$appointment['date_time']) . ',价格:' .$appointment['pay_fee']. '元',//短信内容
'mobile' => $doctor['mobile'],//手机号码
'tKey' => time(),
]);
$ret = json_decode($ret,true);
if($ret['code'] != 200){
Log::write('短信发送失败:' . $ret['msg']);
}
}
// 给预约用户发送短信提醒
if(Validate::regex($appointment['user']['mobile'], "^1\d{10}$")){
$ret = send_sms2([
'content' => '【在线会诊】被预约医生名称:' . $doctor['nickname'] . ',下单时间:' . date('Y-m-d H:i:s') . ',预约时间:' . date('Y-m-d H:i',$appointment['date_time']) . ',价格:' .$appointment['pay_fee']. '元',//短信内容
'mobile' => $appointment['user']['mobile'],//手机号码
'tKey' => time(),
]);
$ret = json_decode($ret,true);
if($ret['code'] != 200){
Log::write('短信发送失败:' . $ret['msg']);
}
}
}
return true;
}
/**
* 异步回调(礼物)
* @ApiInternal
*/
public function notifyGift()
{
$payment = Wechat::payment();
$response = $payment->handlePaidNotify(function($message, $fail){
if ($message['return_code'] !== 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
return $fail('通信失败,请稍后再通知我');
}
// 用户支付失败 - 支付失败也需要返回true,表示服务器已处理,不然微信会反复通知,没有意义
if ($message['result_code'] === 'FAIL') {
return true;
}
// 支付成功后的业务逻辑
if ($message['result_code'] === 'SUCCESS') {
Db::startTrans();
try {
// 处理订单
$this->handleGiftOrder($message);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
return $fail('服务器处理失败,请稍后再通知我');
} catch (Exception $e) {
Db::rollback();
return $fail('服务器处理失败,请稍后再通知我');
}
}
return true;
});
$response->send(); // return $response;
}
/**
* 异步回调(礼物)-零元
* @ApiInternal
*/
public function notifyGiftZero($message)
{
Db::startTrans();
try {
$this->handleGiftOrder($message);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
return false;
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
return false;
}
return true;
}
/**
* 异步回调(礼物)-处理订单
*/
private function handleGiftOrder($message)
{
$order = GiftOrder::get(['order_sn'=>$message['out_trade_no']]);
if (!$order || $order['pay_status'] == '1') { // 如果订单不存在 或者 订单已经支付过了
return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
$order->transaction_id = $message['transaction_id']; // 回填微信的订单号
$order->pay_time = time(); // 更新支付时间为当前时间
$order->pay_status = '1';
$order->save();
// 打赏金额
$appointment = $order['appointment'];
$appointment->setInc('gift_fee',$order['pay_fee']);
return true;
}
}