<?php

class OmnipayNestpay extends \KupShop\OrderingBundle\OmniPay
{
    public static $name = 'Nestpay platební brána';

    public $template = 'payment.OmnipayNestpay.tpl';
    // protected $templateCart = 'payment.OmnipayNestpay.cart.tpl';

    public $class = 'OmnipayNestpay';

    public $method;

    protected $pay_method = Payment::METHOD_ONLINE;

    public static function getOmnipayName(): string
    {
        return 'Nestpay';
    }

    public function configureGateway(Omnipay\Common\GatewayInterface $gateway, Order $order): Omnipay\Common\GatewayInterface
    {
        $gateway->setBank($this->config['bank']);
        $gateway->setClientId($this->config['clientId']);
        $gateway->setStoreKey($this->config['storeKey']);

        return $gateway;
    }

    public function getGatewayOptions(Order $order): array
    {
        $returnUrl = $this->getGenericPaymentUrl(5);

        $options = [
            'amount' => $this->order->convertPriceToEUR(roundPrice($this->order->getRemainingPayment())->asFloat()),
            'currency' => $this->config['currency'],
            'orderid' => $this->order->order_no,
            'returnUrl' => $returnUrl,
            'cancelUrl' => $returnUrl,
            'shopUrl' => $returnUrl,
            'lang' => $this->config['lang'],
        ];

        if (!empty($this->order->invoice_name)) {
            $options['billToName'] = $this->order->invoice_name.' '.$this->order->invoice_surname;
        }
        if (!empty($this->order->invoice_firm)) {
            $options['billToCompany'] = $this->order->invoice_firm;
        }

        return $options;
    }

    protected function createPaymentFromResponse(Omnipay\Common\Message\ResponseInterface $response)
    {
        $this->createPayment(
            $response->getTransactionReference(),
            $this->order->convertPriceFromEUR($response->getData()['amount']),
            []
        );
    }
}
