<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Pay\Application;

class Pay extends Api
{
    protected $noNeedLogin = ['*'];

    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',

            /**
             * 接口请求相关配置,超时时间等,具体可用参数请参考:
             * 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);
        $response = $app->getClient()->postJson('v3/pay/transactions/jsapi', [
            'appid' => $app->getConfig()['app_id'],     //注意在配置文件中加上app_id
            'mchid' => $app->getConfig()['mch_id'],         //商户号
            'out_trade_no' => md5(123456),//订单号
            'notify_url' => 'https://ceshi.excellentkk.cn/api/pay_notify',
            "description" => "Image形象店-深圳腾大-QQ公仔",
            "amount" => [
                "total" => 1,
                "currency" => "CNY"
            ],
            "payer" => [
                "openid" => "oLO1l5FlKiF7A5eEIXKKLDPRjjOI" // <---- 请修改为服务号下单用户的 openid
            ]
        ]);

        return $this->success('ok',$response->toArray());
    }
}