234 lines
9.3 KiB
PHP
234 lines
9.3 KiB
PHP
<?php
|
||
|
||
namespace App\Controller;
|
||
|
||
use App\Amqp\Producer\AssignDoctorProducer;
|
||
use App\Constants\DoctorTitleCode;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Exception\BusinessException;
|
||
use App\Model\Hospital;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserPatient;
|
||
use App\Services\BaseService;
|
||
use App\Services\ImService;
|
||
use App\Utils\Log;
|
||
use Extend\TencentIm\Account;
|
||
use Extend\TencentIm\Message;
|
||
use Extend\TencentIm\Profile;
|
||
use Extend\Wechat\WechatPay;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\HttpMessage\Stream\SwooleFileStream;
|
||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||
use Hyperf\Utils\ApplicationContext;
|
||
use Psr\Http\Message\ResponseInterface;
|
||
|
||
class CallBackController extends AbstractController
|
||
{
|
||
/**
|
||
* 患者端微信支付回调
|
||
* @return ResponseInterface
|
||
* @throws \Throwable
|
||
*/
|
||
public function wxPaySuccessCallBack(): ResponseInterface
|
||
{
|
||
try {
|
||
// 处理支付结果事件
|
||
$WechatPay = new WechatPay(1);
|
||
$app = $WechatPay->createApp();
|
||
$server = $app->getServer();
|
||
|
||
$message = $server->getRequestMessage();
|
||
if (empty($message)){
|
||
return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE))));
|
||
}
|
||
|
||
// 验证推送消息签名
|
||
$app->getValidator()->validate($app->getRequest());
|
||
|
||
Log::getInstance()->info("微信支付回调数据:" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));
|
||
|
||
if (empty($message['out_trade_no'])){
|
||
Log::getInstance()->info("微信支付回调数据错误");
|
||
return $server->serve();
|
||
}
|
||
|
||
// 查询订单
|
||
$params = array();
|
||
$params['inquiry_no'] = $message['out_trade_no'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
Log::getInstance()->info("非法订单");
|
||
return $server->serve();
|
||
}
|
||
|
||
// 验证订单状态
|
||
if ($order_inquiry['inquiry_status'] != 1){
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance()->info("订单状态错误:当前为" . $order_inquiry['inquiry_status']);
|
||
return $server->serve();
|
||
}
|
||
|
||
// 支付状态无需验证,如第一次支付失败,会修改支付状态,再次支付时,会出现验证不通过的情况
|
||
|
||
// 修改支付状态
|
||
$data = array();
|
||
if ($message['trade_state'] == "SUCCESS"){
|
||
// 支付成功
|
||
$data['inquiry_pay_status'] = 2;
|
||
$data['pay_time'] = date('Y-m-d H:i:s',strtotime($message['success_time']));// 支付时间
|
||
if ($order_inquiry['inquiry_type'] == 1 || $order_inquiry['inquiry_type'] == 3){
|
||
// 专家-公益
|
||
$data['inquiry_status'] = 3;// 3:待接诊
|
||
}elseif ($order_inquiry['inquiry_type'] == 2 || $order_inquiry['inquiry_type'] == 4){
|
||
// 快速-购药
|
||
$data['inquiry_status'] = 2;// 2:待分配
|
||
}
|
||
}elseif($message['trade_state'] == "CLOSED"){
|
||
// 已关闭
|
||
$data['inquiry_pay_status'] = 6;
|
||
}elseif($message['trade_state'] == "REVOKED"){
|
||
// 已撤销(付款码支付)
|
||
$data['inquiry_pay_status'] = 7;
|
||
}elseif($message['trade_state'] == "USERPAYING"){
|
||
// 用户支付中(付款码支付)
|
||
$data['inquiry_pay_status'] = 3;
|
||
}elseif($message['trade_state'] == "PAYERROR"){
|
||
// 支付失败(其他原因,如银行返回失败)
|
||
$data['inquiry_pay_status'] = 4;
|
||
}
|
||
|
||
$data['escrow_trade_no'] = $message['transaction_id'];
|
||
$data['updated_at'] = date('Y-m-d H:i:s',time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
OrderInquiry::edit($params,$data);
|
||
}catch (\Exception $e) {
|
||
// 验证失败
|
||
Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage());
|
||
return $this->wxPayErrorReturn($e->getMessage());
|
||
}
|
||
|
||
try {
|
||
if ($message['trade_state'] == "SUCCESS"){
|
||
if ($order_inquiry['inquiry_type'] == 2 || $order_inquiry['inquiry_type'] == 4){
|
||
// 快速-购药
|
||
// 加入分配医生队列
|
||
$data = array();
|
||
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
|
||
$message = new AssignDoctorProducer($data);
|
||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||
$result = $producer->produce($message);
|
||
if (!$result) {
|
||
Log::getInstance()->info("加入分配医生队列失败");
|
||
return $server->serve();
|
||
}
|
||
}elseif ($order_inquiry['inquiry_type'] == 1 || $order_inquiry['inquiry_type'] == 3){
|
||
// 专家-公益
|
||
$ImService = new ImService();
|
||
|
||
// 检测并创建患者账号
|
||
$ImService->createAccount($order_inquiry['user_id']);
|
||
|
||
// 获取订单医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
Log::getInstance()->info("医生数据错误");
|
||
return $server->serve();
|
||
}
|
||
|
||
// 检测并创建医生账号
|
||
$ImService->createAccount($user_doctor['user_id']);
|
||
|
||
// 医生给患者发送消息
|
||
$message = new Message();
|
||
|
||
$arg = array();
|
||
$arg['From_Account'] = $user_doctor['user_id']; // 发送方user_id 如系统发送,无需填写
|
||
$arg['To_Account'] = $order_inquiry['user_id']; // 接收方user_id
|
||
|
||
$arg['MsgBody'] = [
|
||
[
|
||
"MsgType" => "TIMTextElem",
|
||
"MsgContent" => [
|
||
"Text" => "等待医生接诊",
|
||
],
|
||
]
|
||
];
|
||
|
||
// 自定义消息
|
||
$cloud_custom_data = array();
|
||
$cloud_custom_data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$cloud_custom_data['inquiry_type'] = $order_inquiry['inquiry_type'];
|
||
$cloud_custom_data['message_type'] = 1;//1:系统发送 其余:用户发送
|
||
|
||
$arg['CloudCustomData'] = json_encode($cloud_custom_data,JSON_UNESCAPED_UNICODE);
|
||
|
||
$message->sendMessage($arg);
|
||
}
|
||
}
|
||
}catch (\Exception $e) {
|
||
// 验证失败
|
||
Log::getInstance()->error("微信支付回调数据处理成功,发送系统问诊消息失败:" . $e->getMessage());
|
||
return $server->serve();
|
||
}
|
||
|
||
return $server->serve();
|
||
}
|
||
|
||
public function wxPayRefundCallBack(): ResponseInterface
|
||
{
|
||
try {
|
||
// 处理支付结果事件
|
||
$WechatPay = new WechatPay(1);
|
||
$app = $WechatPay->createApp();
|
||
$server = $app->getServer();
|
||
|
||
$message = $server->getRequestMessage();
|
||
if (empty($message)){
|
||
Db::rollBack();
|
||
return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "回调数据为空"], JSON_UNESCAPED_UNICODE))));
|
||
}
|
||
|
||
// 验证推送消息签名
|
||
$app->getValidator()->validate($app->getRequest());
|
||
|
||
Log::getInstance()->info("微信退款回调数据:" . json_encode($message->toArray(),JSON_UNESCAPED_UNICODE));
|
||
return $server->serve();
|
||
}catch (\Exception $e) {
|
||
// 验证失败
|
||
Db::rollBack();
|
||
Log::getInstance()->error("微信支付回调数据验证失败:" . $e->getMessage());
|
||
return $this->wxPayErrorReturn($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 微信支付返回错误响应
|
||
* @param string $message
|
||
* @return ResponseInterface
|
||
*/
|
||
protected function wxPayErrorReturn(string $message): ResponseInterface
|
||
{
|
||
return $this->response
|
||
->withStatus(500)
|
||
->withBody(
|
||
new SwooleStream(
|
||
strval(
|
||
json_encode(['code' => 'ERROR', 'message' => $message], JSON_UNESCAPED_UNICODE)
|
||
)
|
||
)
|
||
);
|
||
}
|
||
// im回调
|
||
public function imCallBack(){
|
||
$request_params = $this->request->all();
|
||
dump($request_params);
|
||
Log::getInstance()->info(json_encode($request_params,JSON_UNESCAPED_UNICODE));
|
||
}
|
||
} |