香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
mini_xzey/app/Http/Controllers/Notify/NotifyController.php

100 lines
2.6 KiB

<?php
declare(strict_types = 1);
namespace App\Http\Controllers\Notify;
use App\Http\Controllers\Controller;
use App\Http\Logics\Notify\NotifyLogic;
use App\Utils\Traits\Logger;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\Xml;
use Exception;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use ReflectionException;
use Throwable;
class NotifyController extends Controller
{
use Logger;
protected NotifyLogic $notify_logic;
/**
* NotifyController Construct
*/
public function __construct()
{
$this->notify_logic = new NotifyLogic();
$this->setChannel('notify');
}
/**
* @return ResponseInterface
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws ReflectionException
* @throws Throwable
*/
public function notify(): ResponseInterface
{
$app = getWeChatMiniProgramPaymentApp();
$server = $app->getServer();
$message = $server->getRequestMessage();
$this->info('接收回调消息', $message->toArray());
try {
// 验证签名通过,业务处理
// $app->getValidator()->validate($app->getRequest());
// 业务处理
$result = $this->notify_logic->notifyHandle($message);
$this->info('接受回调消息结果', ['result' => $result]);
if ($result) {
return $this->returnSuccess();
}
return $this->returnFailure('订单处理失败');
} catch (Exception $e) {
// 验证失败
$err_msg = '订单处理流程失败:' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine();
$this->error($err_msg, [$message->toArray()]);
// 返回 SUCCESS 或者 FAIL 等其他状态
return $this->returnFailure('订单处理异常');
}
}
/**
* 返回成功
* @return Response
*/
public function returnSuccess(): Response
{
return new Response(200, [],
Xml::build([
'return_code' => 'SUCCESS',
'return_msg' => 'OK'
])
);
}
/**
* 返回失败
* @param string $error_msg
* @return Response
*/
public function returnFailure(string $error_msg): Response
{
return new Response(200, [],
Xml::build([
'return_code' => 'FAIL',
'return_msg' => $error_msg
])
);
}
}