<?php
namespace Modules\RemoteData\CreditCardValidation;

class Controller extends \App\Mvc\ControllerAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize() 
	 */
	protected function initialize()
	{
		parent::initialize();
		$this->actionsCfg['CreditCardValidation'] = [ 'checkOption' => false, 'checkSection' => false ];
	}

	/**
	 * Validate credit card number
	 * @param array $R
	 * @return \Engine\Mvc\Response\Json
	 */
	protected function actionCreditCardValidation($R)
	{
		if (!$R['value']) {
			return;
		}

		$responseObj = new \stdClass();
		$responseObj->status = 	'skip';
		$responseObj->message = '';

		// check for validations
		$ccNumber = $R['value'];
		$ccDetails = \App\Utils\CreditCard\NumberDetails::create($ccNumber)->getDetails();
		
		if ($ccDetails['valid']) {
			$responseObj->status = 	'ok';
			
			if ($ccDetails['companyName']) {
				$msgText = sprintf($this->lang['msgTextOKForCompany'], $ccDetails['companyName']);
			}
			else {
				$msgText = $this->lang['msgTextOK'];
			}
			$responseObj->message = $msgText;
		}
		else {
			$responseObj->status = 	'fail';
			$responseObj->message = $this->lang['msgTextFail'];
		}

		return new \Engine\Mvc\Response\Json($responseObj);
	}
}
