MinPayment.php 2.0 KB
<?php

namespace Lackoxygen\MinPayment;

use Lackoxygen\MinPayment\Contract\ResponseInterface;
use Lackoxygen\MinPayment\Request\CustomsOrder;
use Lackoxygen\MinPayment\Request\CustomsQuery;
use Lackoxygen\MinPayment\Request\IdentifyOrder;
use Lackoxygen\MinPayment\Utils\Client;
use Lackoxygen\MinPayment\Utils\Signature;

class MinPayment
{
    /**
     * @var Client $client
     */
    protected $client;

    private $config = [];


    public function __construct(array $config = [])
    {
        $this->config = $config + (array)\config('min-payment');
        $this->client = new Client($this->config);
    }

    /**
     * @param callable $options
     *
     * @return ResponseInterface
     * @throws Exception\Exception
     */
    public function identifyOrder(callable $options): ResponseInterface
    {
        $identifyOrder = new IdentifyOrder;

        $options($identifyOrder);

        $client = $this->client;

        return $client($identifyOrder);
    }


    /**
     * @param callable $options
     *
     * @return ResponseInterface
     * @throws Exception\Exception
     */
    public function customsOrder(callable $options): ResponseInterface
    {
        $customsOrder = new CustomsOrder;

        $options($customsOrder);

        $client = $this->client;

        return $client($customsOrder);
    }

    /**
     * @param callable $options
     *
     * @return ResponseInterface
     * @throws Exception\Exception
     */
    public function customsQuery(callable $options): ResponseInterface
    {
        $customsQuery = new CustomsQuery;

        $options($customsQuery);

        $client = $this->client;

        return $client($customsQuery);
    }


    /**
     * @param array  $input
     * @param string $signName
     *
     * @return bool
     */
    public function verify(array $input, string $signName = 'sign'): bool
    {
        $sign = \Arr::get($input, $signName);
        unset($input[$signName]);
        $secret = \Arr::get($this->config, 'secret');

        return Signature::make($input, $secret) === $sign;
    }
}