<?php
namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Pay\Application;
use EasyWeChat\Pay\Message;
use think\facade\Log;

class PayNotify extends Api{
    public function index(){
            $config = [
                'mch_id' => 1635725673,
                'app_id' => 'wx6e14cb98394e36bc',

                // 商户证书
                'private_key' => config_path() .'certs/apiclient_key.pem',
                'certificate' => config_path() .'certs/apiclient_cert.pem',

                // v3 API 秘钥
//            'secret_key' => '43A03299A3C3FED3D8CE7B820Fxxxxx',

                // v2 API 秘钥
                'v2_secret_key' => '95d195Dcf6ec66156dfeeb4E7435faef',

                // 平台证书:微信支付 APIv3 平台证书,需要使用工具下载
                // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
//            'platform_certs' => [
//                // '/path/to/wechatpay/cert.pem',
//            ],

                /**
                 * 接口请求相关配置,超时时间等,具体可用参数请参考:
                 * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
                 */
                'http' => [
                    'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
                    'timeout' => 5.0,
                    // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
                ],
            ];

            $app= new Application($config);
            $server = $app->getServer();
            $server->handlePaid(function (Message $message, \Closure $next) {
                Log::error($message);
                // $message->out_trade_no 获取商户订单号
                // $message->payer['openid'] 获取支付者 openid
                // 🚨🚨🚨 注意:推送信息不一定靠谱哈,请务必验证
                // 建议是拿订单号调用微信支付查询接口,以查询到的订单状态为准
                return $next($message);
            });

// 默认返回 ['code' => 'SUCCESS', 'message' => '成功']
            return $server->serve();

    }

}