Signature.php 1.4 KB
<?php


namespace Lackoxygen\GzCbec\Utils;


use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;

class Signature
{
    /**
     * @var string
     */
    protected $uri;

    protected $client;

    /**
     * Signature constructor.
     *
     * @param string $uri
     */
    public function __construct(string $uri)
    {
        $this->uri    = $uri;
        $this->client = new Client;
    }

    /**
     * @param string $signXml
     *
     * @return ResponseInterface
     */
    protected function execute(string $signXml): ResponseInterface
    {
        return $this->client->post($this->uri, [
            RequestOptions::FORM_PARAMS => [
                'xml' => base64_encode($signXml)
            ],
            RequestOptions::TIMEOUT     => 60
        ]);
    }

    /**
     * @param string $signXml
     *
     * @return string
     */
    public function __invoke(string $signXml): string
    {
        $content = '';
        try {
            $response = $this->execute($signXml);
            $content  = $response->getBody()->getContents();
            $array    = \json_decode($content, true);
            $data     = \Arr::get($array, 'data');
            if (!empty($data)) {
                $content = html_entity_decode(base64_decode($data));
            }
        } catch (\Throwable $throwable) {
        }
        file_put_contents('tran.xml', $content);
        return $content;
    }
}