<?php

namespace SRVX\Service\AliBPass;

use SRVX\Service\BaseService;
use EcoAopClient;


class AliEcoService extends BaseService
{

    static private $config = [];
    static private $aopClient;

//    public $gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    public $apiVersion = '1.0';
    public $signType = 'RSA2';
    public $postCharset = 'UTF-8';
    public $format = 'json';

    public $serverUrl = 'https://cloudpaygw.alipay-eco.com';
    public $appId = '20230803121700004722802100016020';
    public $privateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCSGJ9ob4kmGy6y4YeynjKJc/LSg02EXjLErPZSfVrAOy69FTRfBDbazlxpfl6YCAxDdT5ZcspB5kS2XYOY4dmqqPmF4VywmcjmZM8IxZFl1p7YC8BOlP6JrzDPwVl3hn4z3Em1byj8ANLY/AxAaW9W43FSIX+cSfdJThTlxGcbHZFM3kqu++hgFc/E+ar1nB0garqh03ESjatO4b6tuHM57ou7M+fXsoIf3gaL1IDZpeAoRh5sLCBLIbLz55tF0DcOaFsK7NUImnabJDRtrM1IN0lDZkQYAzL+JFsAq+ncRJm60Vd1jq/pXML/cFVHQQgdnjUd2u2NJTSMiCKCftmdAgMBAAECggEARzt+LduEEXJbeJiCI1MC7QX9tSqUFyAevMpgZ6P7JZM1nLfECzfwzwqjAES/Nn1mDVYTCmXvu+XcRtadnWMxUAaHcwGIwgqZa8xnLa220OKuHETOhQIcgWFL+WAakS6gRgT4Y2BJqWV1aJqsFpPR15mZ3SO8JqXhznolsknqw8sO+DBJu+QXn1HdntSGWrLU4GblPlYAs64y/MzPMUWfA4ShDRn9MZK6ypnh2GB5ezgnFxx1jRGOy96IqikI8f+M/CVdgzErLIl+qTd/IbfL8B6Gccb4ZwimL/x1H1bqYCA6xINMZLo5eDHCvBTU4AW7sItw1Q7MQ8l3wBl7hW62gQKBgQDZT1SEaeBtOJxt3vrK7o/kCWNh/4h9oENFhDIuTvLQk9tirKw0BSiZedjbEHqnqM+bgeuvxazCp9uqoYXgC/dtIoo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9TgHH3LKYrsEDbQzPJyMPFhD0zDb36b62q3tQYc1w8JknS6XeXMkX1Tu4zZbAxyIiv3J1FK8y7dtoRi9gLgo0uLsXWDn5AwAlzqfOERgLMWD2r6FE8tOXVbMha6IImQIDAQAB'; //平台公钥
    public $aesKey = 'HOrPS0V4BthAPfgSdbHWwy0TwPnC8TQAQZIOjc8vOzI='; //加密密钥

    public function __construct()
    {
        if ((APP_ENV == 'dev')) {
            $this->serverUrl = 'https://dev-cloudpaygw.alipay-eco.com';
        }
        $this->getConfig();
        $this->aopClient();
    }


    private function aopClient()
    {
        if (!self::$aopClient instanceof EcoAopClient) {
            self::$aopClient = new EcoAopClient(self::$config);
            self::$aopClient->appId = self::$config['ali_app_id'];
            //支付宝私钥值
            self::$aopClient->rsaPrivateKey = self::$config['ali_private_key'];
            //支付宝公钥值
            self::$aopClient->alipayrsaPublicKey = self::$config['ali_public_key'];
        }
//        self::$aopClient->gatewayUrl = $this->gatewayUrl;
        self::$aopClient->gatewayUrl = $this->serverUrl;
//        self::$aopClient->apiVersion = $this->apiVersion;
        self::$aopClient->signType = $this->signType;
        self::$aopClient->postCharset = $this->postCharset;
        self::$aopClient->format = $this->format;
    }


    private function getConfig()
    {
        if (empty(self::$config)) {
            self::$config = [
                "ali_app_id" => $this->appId,
                "ali_private_key" => $this->privateKey,
                "ali_public_key" => $this->alipayPublicKey
            ];
        }
    }

    /**
     * alipay.planet.ecocampus.api.roster.signUpInfo(签约信息同步)
     * @param $face_uid string 学生刷脸编号（不可修改）
     * @param $parent_uid string 家长uid（可修改）	2088122417040252
     * @param $parent_logon_id string 家长支付宝账号脱敏信息（可修改）
     * @param $roster_name string 学生姓名（可修改）
     * @param $out_roster_code string 外部花名册编号（不可修改）
     * @param $school_code string 学校内标（不可修改）
     * @param $school_name string 学校名称（不可修改）
     * @param $face_open_status string 刷脸开通状态（可修改）
     * @param $scan_face_pay_status string 刷脸支付开通状态（可修改）
     * @return array
     */

    public function alipayPlanetEcocampusApiRosterSignUpInfo($face_uid, $parent_uid, $parent_logon_id, $roster_name, $out_roster_code, $school_code, $school_name, $face_open_status = "OFF", $scan_face_pay_status = "OFF"): array
    {
        try {
            $request = new \AlipayPlanetEcocampusApiRosterSignUpInfo();
            $params = [
                "face_uid" => $face_uid,
                "face_open_status" => $parent_uid,
                "parent_uid" => $parent_logon_id,
                "parent_logon_id" => $roster_name,
                "roster_name" => $out_roster_code,
                "out_roster_code" => $school_code,
                "school_code" => $school_name,
                "school_name" => $face_open_status,
                "scan_face_pay_status" => $scan_face_pay_status,
            ];

            $request->setBizContent(json_encode($params));
            $response = self::$aopClient->execute($request);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            dd($responseNode);
            $responseData = json_decode(json_encode($response->$responseNode), true);
            if ($responseData['code'] == 10000) {
                return $this->returnRes(true, $responseData['msg'], $responseData);
            } else {
                return $this->returnRes(false, ($responseData['sub_msg'] ?? $responseData['msg']), $responseData);
            }
        } catch (\Exception $exception) {
            return $this->returnRes(false, $exception->getMessage());
        }
    }

}