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.
94 lines
3.2 KiB
94 lines
3.2 KiB
<?php
|
|
declare(strict_types = 1);
|
|
|
|
namespace App\Http\Controllers\Notify;
|
|
|
|
use App\Dictionary\WeChat\Payment\V2Api;
|
|
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\Pay\Message;
|
|
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();
|
|
|
|
$server->handlePaid(function (Message $message, \Closure $next) use ($app) {
|
|
// $message->out_trade_no 获取商户订单号
|
|
// $message->payer['openid'] 获取支付者 openid
|
|
// 🚨🚨🚨 注意:推送信息不一定靠谱哈,请务必验证
|
|
// 建议是拿订单号调用微信支付查询接口,
|
|
$this->info('接收回调消息', $message->toArray());
|
|
try{
|
|
// 验证通过,业务处理
|
|
$app->getValidator()->validate($app->getRequest());
|
|
|
|
// 查询订单
|
|
$response = $app->getClient()->postXml(V2Api::QUERY_ORDER->value, [
|
|
'body' => [
|
|
'appid' => config('wechat.payment.app_id'),
|
|
'mch_id' => config('wechat.payment.mch_id'),
|
|
'out_trade_no' => $message->out_trade_no
|
|
]
|
|
]);
|
|
if ($response->isFailed()) {
|
|
// 查询失败
|
|
$this->warning('订单查询失败', [$message->out_trade_no]);
|
|
return $next($message);
|
|
}
|
|
|
|
// 不成功
|
|
if (
|
|
!isset($response['return_code'], $response['result_code']) ||
|
|
$response["return_code"] !== "SUCCESS" ||
|
|
$response["result_code"] !== "SUCCESS"
|
|
) {
|
|
$this->warning('订单查询支付失败', [$message->out_trade_no, $response]);
|
|
return $next($message);
|
|
}
|
|
|
|
// 业务处理
|
|
$result = $this->notify_logic->notifyHandle($message);
|
|
|
|
$this->info('接受回调消息结果', ['result' => $result]);
|
|
} catch(\Exception $e){
|
|
// 验证失败
|
|
$err_msg = '订单验证签名失败:'. $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine();
|
|
$this->error($err_msg, [$message->toArray()]);
|
|
}
|
|
|
|
return $next($message);
|
|
});
|
|
|
|
return $server->serve();
|
|
}
|
|
}
|
|
|