<?php

namespace SRVX\Api\Controllers\Alipay;

use App\constants\Common as ICommon;
use SRVX\Api\Controllers\ControllerBase;
use SRVX\Msg;
use SRVX\Service\Pay\AlipayService;
use SRVX\Service\Pay\EasyAlipayService;
use SRVX\Service\Pay\PayService;


class SystemOauthController extends ControllerBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function authorizationCodeAction()
    {
        if ($this->request->isPost()) {
            $args = $this->request->getJsonRawBody(true);
            if (empty($args["code"])) {
                $this->api(Msg::NewError(500, '缺少必要參數！'));
            }
            $code        = $args["code"];
            $config = [
                "pay_ali_app_id"      => "2021002188623414",
                "pay_ali_public_key"  => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzEC7+3O/Ej/gkXug6EgcCM3LjWzz11RR6ESo0KshGg3ODgM8DfdKDHD5b1T6VbcRGFobXQBqVtW008ocUnSOZzE67a73l6mKT3FbmhRsbrgwcUuOHEJUZaLFxPibJgFl7fkOjBcIiz+Wc+9VDTt8O4YxIZhAL6L6ppXcrVrCkOaiIiuHGyp7FjQ+5s3Lk3NLSO3TfXw1WEX2F+IHnw32ZN/FlC8rF8HVlgM0w/PZJ9RuadoLqn5H6z/iqrurbtxscnx0Qbdt8CBzREabJnWxHjxO936AXu9njIPvsXkTfaJJIXxRGGb0b8peSiCljwi8UEODe8mjl2c/LzLel6RuLQIDAQAB",
                "pay_ali_private_key" => "MIIEowIBAAKCAQEAoZkzOwPfAFEjsBfOKu4vtI9Rf/pu6lhGov5AfVPi0iteHaXJ3+0kKZxWovxIdRQB4LjU0pegWKWECDAUVXT5w6lustdkglRgGKy6MKNi6B4k0FZDdBHbc2c3AW53hUap55kHrY9wXdQJJHp1wCvOO0zaRZ+GEoNHI4+9WO+NaCJ91t6V+p54PvIAubVe6nqlB9qq6VK3sISitu2BtRYkGvj2E7sOj7dYFjwGRSLKjxF1W2/USw/zpOW0wJWPzuXonWM4FztvYrmQD7vyvEgbqSHplitXVCUiLrNNVRI0Zl24P1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Y9Tz8dXUBQKBgCUrvC3WcnErwzK6RmayjMAA5G15nKgMdeQU+SZXQih8DHx0peCsmJN1LJdiggZqxu5zB+L6v77kiN3rRBs5p8cb7izWQWAn4J8/504tn/oyvFSyBxLIlLByBqcr35QtWb5qgJPlIj9COGAsVagXr3KA/weq1rnbX1LGf2ww1ONi",
            ];

            $app_auth_token =  (isset($args["app_auth_token"]) AND !empty($args["app_auth_token"])) ? $args["app_auth_token"] :"202111BB0c1ab02eb52f4d07a7b842d7ee7a3C30";
            $result         = (new EasyAlipayService($config))->systemOauthTokenOrRefreshToken($app_auth_token, $code);
            if (!$result["status"]) {
                //调用失败
                $this->api(Msg::NewError(500, $result["msg"] ?? $result["sub_msg"]));
            }
            $this->api($result);
        }
        $this->api(Msg::NewError(500, '提交方式错误！'));
    }

    public function refreshTokenAction()
    {
        if ($this->request->isPost()) {
            $args = $this->request->getJsonRawBody(true);
            if (empty($args["refresh_token"])) {
                $this->api(Msg::NewError(500, '缺少必要參數！'));
            }

            $refresh_token        = $args["refresh_token"];
            $config = [
                "pay_ali_app_id"      => "2021002188623414",
                "pay_ali_public_key"  => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzEC7+3O/Ej/gkXug6EgcCM3LjWzz11RR6ESo0KshGg3ODgM8DfdKDHD5b1T6VbcRGFobXQBqVtW008ocUnSOZzE67a73l6mKT3FbmhRsbrgwcUuOHEJUZaLFxPibJgFl7fkOjBcIiz+Wc+9VDTt8O4YxIZhAL6L6ppXcrVrCkOaiIiuHGyp7FjQ+5s3Lk3NLSO3TfXw1WEX2F+IHnw32ZN/FlC8rF8HVlgM0w/PZJ9RuadoLqn5H6z/iqrurbtxscnx0Qbdt8CBzREabJnWxHjxO936AXu9njIPvsXkTfaJJIXxRGGb0b8peSiCljwi8UEODe8mjl2c/LzLel6RuLQIDAQAB",
                "pay_ali_private_key" => "MIIEowIBAAKCAQEAoZkzOwPfAFEjsBfOKu4vtI9Rf/pu6lhGov5A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dowX/IuJE8l7l5SUnwlX+Q1S/SnfqdtoakLUy9kdwZG21/KJ+WAMKXlfMYgVFs1itzXO6WvQKaHepWLfH5jxIgSRbGRh9N0qo+QKBgQCYQmVwMYCfkwz1DUqe1reMVNSKqIcdeTiFEKwV5t2oaQgsrcD2zbH5AXmwzEaUktbKckwv30oveOn582C9fxex+1EOmBD+fqew40dQQURSsIaHZJdXBc/NcblBuUjxk6goiXQdWMqDclKsYqk/i/xvdzwvEXAVVQ7gY9Tz8dXUBQKBgCUrvC3WcnErwzK6RmayjMAA5G15nKgMdeQU+SZXQih8DHx0peCsmJN1LJdiggZqxu5zB+L6v77kiN3rRBs5p8cb7izWQWAn4J8/504tn/oyvFSyBxLIlLByBqcr35QtWb5qgJPlIj9COGAsVagXr3KA/weq1rnbX1LGf2ww1ONi",
            ];
            $app_auth_token = "202111BB0c1ab02eb52f4d07a7b842d7ee7a3C30";
            $result         = (new EasyAlipayService($config))->systemOauthTokenOrRefreshToken($app_auth_token, "", $refresh_token,"refresh_token");
            if (!$result["status"]) {
                //调用失败
                $this->api(Msg::NewError(500, $result["msg"] ?? $result["sub_msg"]));
            }
            $this->api($result);
        }
        $this->api(Msg::NewError(500, '提交方式错误！'));
    }

}
