<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

if ($this->getRequest()->isPost()) {

	$settings = Engine_Api::_()->getApi('settings', 'core');
	$coreApi  = Engine_Api::_()->core();

	//Domain Name
	if (engine_in_array($_SERVER['SERVER_NAME'], ['localhost', '127.0.0.1'])) {
		$domainName = $_POST['domain_name'];
	} else {
		$domainName = str_replace(array('http://', 'https://', 'www.'), array('', '', ''), $_SERVER['HTTP_HOST']);
	}

	//License Key
	$licenseKey = $values['coinpayments_licensekey'];

	//Verify License Key
	$valid = Engine_Api::_()->getApi('verify', 'core')->verifyKey($licenseKey, $domainName);

	if ($valid['status'] == 'Active') {
		if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('coinpayments.pluginactivated')) {

			$db = Zend_Db_Table_Abstract::getDefaultAdapter();

			$db->query('INSERT IGNORE INTO `engine4_payment_gateways` ( `title`, `description`, `enabled`, `plugin`, `test_mode`) VALUES ("Cryptocurrency", NULL, 0, "Payment_Plugin_Gateway_Coinpayment", 0);');

			$db->query('UPDATE `engine4_payment_gateways` SET `default_icon` = \'<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0_2824_18848)"><path d="M14.648 14.423L14.651 14.419C14.5585 14.6856 14.3843 14.9163 14.153 15.078C13.884 15.267 13.506 15.416 12.965 15.442L10.975 15.446V12.516C11.263 12.524 12.54 12.503 13.094 12.531C13.816 12.566 14.265 12.852 14.504 13.199C14.766 13.55 14.797 14.019 14.648 14.423ZM12.519 11.162C13.022 11.138 13.371 11 13.62 10.826C13.834 10.68 13.995 10.459 14.08 10.215C14.214 9.84 14.187 9.405 13.944 9.08C13.721 8.761 13.306 8.496 12.638 8.464C12.143 8.438 11.225 8.461 10.974 8.454V11.163C10.999 11.167 12.513 11.162 12.519 11.162ZM24 12C24 18.627 18.627 24 12 24C5.373 24 0 18.627 0 12C0 5.373 5.373 0 12 0C18.627 0 24 5.373 24 12ZM17.35 14.142C17.372 12.665 16.11 11.81 15.442 11.57C16.157 11.079 16.648 10.527 16.648 9.485C16.648 7.83 15.002 7.055 14.001 6.956C13.919 6.947 13.691 6.943 13.691 6.943V5.361H12.058L12.062 6.944H10.97V5.367H9.31V6.936C9.018 6.943 7.261 6.942 7.261 6.942V8.343H7.832C8.433 8.359 8.654 8.705 8.63 9.02V15.061C8.62627 15.1601 8.58654 15.2545 8.51826 15.3265C8.44997 15.3985 8.35781 15.4431 8.259 15.452C8.01 15.463 7.638 15.452 7.638 15.452L7.318 17.04H9.314V18.64H10.975V17.049H12.066V18.643H13.69V17.055C15.589 17.105 17.333 15.984 17.35 14.142Z" fill="currentColor"/></g><defs><clipPath id="clip0_2824_18848"><rect width="24" height="24" fill="white"/></clipPath></defs></svg>\' WHERE `engine4_payment_gateways`.`plugin` = "Payment_Plugin_Gateway_Coinpayment";');

			$settings->setSetting('coinpayments.licensekey', $licenseKey);
			$settings->setSetting('core.license.domain', $domainName);
			$settings->setSetting('coinpayments.pluginactivated', 1);
		}

		// Update cached status
		$cachedState = isset($valid['status']) ? $valid['status'] : 'Inactive';
		$settings->setSetting('coinpayments.license.cachedstatus', $cachedState);
		$settings->setSetting('coinpayments.license.lastcheck', time());

		$settings->setSetting('coinpayments.license.keyauth', @base64_encode($licenseKey));
		$settings->setSetting('core.license.domainauth', @base64_encode($domainName));
		$error = 1;
	} else {
		$error = $this->view->translate('Please enter correct License key for this product.');
		$error = Zend_Registry::get('Zend_Translate')->_($error);
		$form->getDecorator('errors')->setOption('escape', false);
		$form->addError($error);
		$error = 0;
		$settings->setSetting('coinpayments.licensekey', $_POST['coinpayments_licensekey']);
		return;
		$this->_helper->redirector->gotoRoute(array());
	}
}