<?php 
namespace PortalPeople\redsys\models;

class Redsys extends \common\models\DynamicModel
{

    const sandboxUrl = 'https://sis-t.redsys.es:25443/sis/rest/trataPeticionREST';
    const productionUrl = 'https://sis.redsys.es/sis/rest/trataPeticionREST';
    const Ds_SignatureVersion = 'HMAC_SHA512_V2';
    const HMAC_KEY = 'sq7HjrUOBfKmC576ILgskD5srU870gJ7';

    public static function localAttributes()
    {
        return [
            "DS_MERCHANT_ORDER" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_MERCHANTCODE" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_TERMINAL" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_CURRENCY" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_TRANSACTIONTYPE" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_AMOUNT" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_IDENTIFIER" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_COF_INI" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_DIRECTPAYMENT" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
            "DS_MERCHANT_EXCEP_SCA" => [
                'rules' => ['string'],
                'label' => '',
                'input' => 'default',
            ]
        ];
    }

    public function getDsSignatureVersion()
    {
        return self::Ds_SignatureVersion;
    }
    public function getDs_MerchantParameters(){
        return [
            'DS_MERCHANT_ORDER' => $this->DS_MERCHANT_ORDER,
            'DS_MERCHANT_MERCHANTCODE' => $this->DS_MERCHANT_MERCHANTCODE,
            'DS_MERCHANT_TERMINAL' => $this->DS_MERCHANT_TERMINAL,
            'DS_MERCHANT_CURRENCY' => $this->DS_MERCHANT_CURRENCY,
            'DS_MERCHANT_TRANSACTIONTYPE' => $this->DS_MERCHANT_TRANSACTIONTYPE,
            'DS_MERCHANT_AMOUNT' => $this->DS_MERCHANT_AMOUNT,
            'DS_MERCHANT_IDENTIFIER' => $this->DS_MERCHANT_IDENTIFIER,
            'DS_MERCHANT_COF_INI' => $this->DS_MERCHANT_COF_INI,
            'DS_MERCHANT_DIRECTPAYMENT' => $this->DS_MERCHANT_DIRECTPAYMENT,
            'DS_MERCHANT_EXCEP_SCA' => $this->DS_MERCHANT_EXCEP_SCA,
            
            "DS_MERCHANT_MERCHANTURL" => $url,
            "DS_MERCHANT_URLOK" => $urlOKKO,
            "DS_MERCHANT_URLKO" => $urlOKKO
        ];
    }   
    public function getDs_Signature(){
        return 
    }

    public function getPayload()
    {
        $kc = self::HMAC_KEY;
        $order = $this->DS_MERCHANT_ORDER;
        $params = self::base64_url_encode_safe(json_encode($this->getDs_MerchantParameters()));
        $signature = self::createMerchantSignature($kc, $params, $order);
        return json_encode([
            'Ds_SignatureVersion' => $this->getDsSignatureVersion(),
            'Ds_MerchantParameters' => $params,
            'Ds_Signature' => $signature,
        ]);
    }

    /******  AES Function  ******/
	static function encrypt_AES($data, $key) {
		$fixedKey = str_pad(substr($key, 0, 16), 16, "0");
		$firma = base64_encode(openssl_encrypt($data, "aes-128-cbc", $fixedKey, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"));

		return $firma;
	}

	static function mac512($data, $key) {
		$sha = hash_hmac('sha512', $data, $key, true);
		return $sha;
	}

	static function createMerchantSignature($key, $data, $diversifyingFactor) {
        // The key is diversified with the Order Number
		$key = self::encrypt_AES($diversifyingFactor, $key);

        // MAC512 of the Ds_Parameters parameter sent by Redsys
		$res = self::mac512($data, $key);

        // The data is encoded in Base64
		return Utils::base64_url_encode_safe($res);
	}

	static function checkSignatures($sig1, $sig2) {
		if (strcasecmp($sig1, $sig2) !== 0) {
			throw new Exception("Integrity failure. The received signature does not match the calculated one.");
		}
	}

    	/******  Base64 Functions  ******/
	static function base64_url_encode($input) {
		return strtr(base64_encode($input), '+/', '-_');
	}

	static function base64_url_decode($input) {
		return base64_decode(strtr($input, '-_', '+/'));
	}

	static function base64_url_encode_safe($input) {
		return str_replace("=", "", strtr(base64_encode($input), '+/', '-_'));
	}

	static function base64_url_decode_safe($input) {
		$str = str_pad($input, strlen($input) + (4 - strlen($input) % 4) % 4, '=', STR_PAD_RIGHT);
		return base64_decode(strtr($str, '-_', '+/'));
	}
}