Webman v2.1+Easywechat 6.17微信支付回调报错

Alex-9930

框架:Webman v2.1

支付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
{
/**

  • Notes: 微信支付回调
  • @NotNeedLogin
  • @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
    {
    /**

  • Notes: 微信支付回调
  • @param $data
  • @author Alex
  • @return mixed
    */
    public static function handleNotify($data)
    {
    try {
    $data['appid'] = ConfigServer::get('WeChat', 'appId', '2');
    $wechatPay = new WechatPayService($data['appid']);
    $wechatPay->handleNotify($data);
    } catch (\Exception $e) {
    throw new \RuntimeException($e->getMessage());
    }
    }
    }
    WechatPayService.php代码:
    <?php
    namespace app\common\server;
    use EasyWeChat\Pay\Application;
    use app\common\server\UrlServer;

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,输出不了订单号
什么问题,能否请大神帮忙看看

截图报错信息里报错文件相关代码

截图
截图
截图

操作系统及workerman/webman等框架组件具体版本

使用easywechat6.17,webman2.1

62 1 0
1个回答

TM

https://www.workerman.net/a/1556 这里面有解决方案

🔝