支付SDK:Easywechat 6.17
PayController.php控制器代码:
<?php
namespace app\api\controller\pay;
use support\Request;
use app\api\logic\pay\PayLogic;
use app\common\server\JsonServer;
use app\common\basics\ApiBase;
class PayController extends ApiBase
{
/**
@author Alex
*/
public function notifyApplet(Request $request) {
try {
// 获取微信回调数据
$content = $request->rawBody();
$data = json_decode($content, true);
if (!isset($data['resource'])) {
return JsonServer::error('回调数据异常');
}
$result = PayLogic::handleNotify($data);
} catch (\RuntimeException $e) {
return JsonServer::error($e->getMessage());
} catch (\Exception $e) {
return JsonServer::error('系统错误: '.$e->getMessage());
}
}
}
PayLogic.php代码:
<?php
namespace app\api\logic\pay;
use support\Request;
use app\common\basics\Logic;
use app\common\server\WechatPayService;
class PayLogic extends Logic
{
/**
class WechatPayService
{
/**
@author Alex
*/
public function __construct($appid, $verify = false)
{
try {
if (!$this->checkCertsExist()) {
throw new \RuntimeException('证书文件不存在,请手动下载并放置到指定目录。');
}
$config = [
'mch_id' => $this->wechat['mch_id'],
'app_id' => $this->wechat['app_id'],
'certificate' => base_path('public'.$this->certUri),
'private_key' => base_path('public'.$this->keyUri), // 商户API证书私钥路径
'secret_key' => $this->wechat['secret_key'],
'platform_certs' => [
file_get_contents(base_path('public'.$this->platformUri)),
],
'http' => [
'throw' => true,
'timeout' => 5.0,
],
'notify_url' => UrlServer::getFileUrl().'/api/pay/pay/notifyApplet',
];
$this->app = new Application($config);
} catch (\Exception $e) {
throw new \RuntimeException('微信支付服务初始化失败');
}
}
// 处理支付回调
public function handleNotify()
{
$server = $this->app->getServer();
$server->handlePaid(function (Message $message, \Closure $next) {
var_dump($message->out_trade_no);
return $next($message);
});
}
}
报错:Invalid request body.
handleNotify方法不会执行打印var_dump,输出不了订单号
什么问题,能否请大神帮忙看看
使用easywechat6.17,webman2.1
https://www.workerman.net/a/1556 这里面有解决方案
参照这个写法依旧问题还是存在