_paymentMethods = $config['paymentMethods']; if (count($this->_paymentMethods) > 1) { $this->_dfRelations = $config['dfRelations']; } $this->_termsContainerId = $config['termsContainerId']; $this->setAttrib('id', 'payment-information-form'); parent::init(); } protected function _initElements() { $this->_initPaymentMethod(); return $this; } protected function _initPaymentMethod() { $this->addElement('header', 'paymentMethodHeader', ['label' => 'Payment Method']); $defaultPaymentMethod = key($this->_paymentMethods); if (count($this->_paymentMethods) > 1) { $this->addElement('radio', 'paymentMethod', [ 'label' => 'Payment Method', 'separator' => '   ', 'multiOptions' => $this->_paymentMethods, 'value' => $defaultPaymentMethod, ]); } else { $paymentMethodTitle = current($this->_paymentMethods); $this->addElement('hidden', 'paymentMethod', ['value' => $defaultPaymentMethod]); $this->addElement('html', 'paymentMethodTitle', [ 'html' => htmlspecialchars($paymentMethodTitle), 'decorators' => ['DtDdWrapper'], ]); } if (array_key_exists(Entity::PAYMENT_METHOD_CC, $this->_paymentMethods)) { $this->_initCreditCardElements(); } if (array_key_exists(Entity::PAYMENT_METHOD_CHECK, $this->_paymentMethods)) { $this->_initCheckElements(); } $this->addElement('html', 'paymentSummary', [ 'html' => $this->getPaymentSummary(), 'decorators' => ['DtDdWrapper'], ]); return $this; } protected function _initCreditCardElements() { $this->addElement('tel', 'ccNumber', [ 'label' => 'Credit Card Number', 'required' => $this->isElementRequired('ccNumber'), 'autocomplete' => 'off', 'filters' => ['StringTrim', 'Digits'], 'validators' => ['CreditCard'], ]); $this->addElement('dateExtended', 'ccExpirationDate', [ 'label' => 'Expiration Date', 'showDay' => false, 'required' => $this->isElementRequired('ccExpirationDate'), 'validators' => [ ['Callback', true, [ 'callback' => function ($value) { $value = substr($value, 0, -3); if ($value < date('Y-m')) { return false; } return true; }, 'messages' => 'The credit card has expired.', ]], ], ]); $this->addElement('tel', 'ccSecurityCode', [ 'label' => 'Security Code', 'required' => $this->isElementRequired('ccSecurityCode'), 'autocomplete' => 'off', ]); return $this; } protected function _initCheckElements() { $this->addElement('select', 'checkType', [ 'label' => 'Account Type', 'required' => $this->isElementRequired('checkType'), 'multiOptions' => ['' => 'Select One'] + $this->getConfigArray('checkTypes'), ]); /** @var Qs_Form_Decorator_ViewHelper $viewHelper */ if (($viewHelper = $this->getElement('checkType')->getDecorator('ViewHelper'))) { $viewHelper->setHtmlAfterElement($this->renderCheckTypeTips()); } /* $this->addElement('text', 'accountName', [ 'label' => 'Name on Account', 'required' => $this->isElementRequired('accountName'), 'maxlength' => 255, ]); */ $this->addElement('text', 'routingNumber', [ 'label' => 'Routing Number', 'required' => $this->isElementRequired('routingNumber'), 'maxlength' => 10, ]); $this->addElement('text', 'bank', [ 'label' => 'Bank', 'required' => $this->isElementRequired('bank'), 'maxlength' => 255, ]); $this->addScript('js/app/payment/form/element/routingNumber.js'); $this->addInitObject('app.payment.form.element.RoutingNumber', [[ 'id' => $this->getElement('routingNumber')->getId(), 'apiUrl' => $this->getBankApiUrl(), 'nodes' => [ 'bankName' => '#' . $this->getElement('bank')->getId(), ], ]]); $this->addElement('text', 'accountNumber', [ 'label' => 'Account Number', 'required' => $this->isElementRequired('accountNumber'), 'maxlength' => 10, ]); $this->addElement('text', 'confirmAccountNumber', [ 'label' => 'Confirm Account Number', 'required' => $this->isElementRequired('confirmAccountNumber'), 'maxlength' => 10, 'validators' => [ ['Compare', true, [ 'operator' => '==', 'callback' => [$this->getElement('accountNumber'), 'getValue'], 'messages' => 'Account Numbers don\'t match', ]], ], ]); return $this; } public function getBankApiUrl() { return $this->_bankApiUrl; } public function setBankApiUrl($bankApiUrl) { $this->_bankApiUrl = $bankApiUrl; return $this; } public function isValid($data) { $isValid = parent::isValid($data); return $this->isAgreedWithTerms() && $isValid; } private function isAgreedWithTerms() { if (Entity::PAYMENT_METHOD_CHECK == Qs_Request::getPostValue('paymentMethod') && 'y' != Qs_Request::getPostValue($this->_hiddenInputName) ) { $this->addError('You should agree with terms'); return false; } return true; } private function renderCheckTypeTips() { $html = $this->renderCheckTypeTip(Entity::CHECK_TYPE_PERSONAL_CHECKING); $html .= $this->renderCheckTypeTip(Entity::CHECK_TYPE_BUSINESS_CHECKING); return $html; } private function renderCheckTypeTip($type) { $visible = $this->_getData('checkType') == $type; $image = "images/{$type}.jpg"; $html = Html::renderTag('img', ['class' => $type, 'alt' => ucfirst($type), 'src' => $image]); $html = Html::renderContainer('a', $html, [ 'data-check-type' => $type, 'class' => ($visible ? '' : ' hidden'), 'href' => $image, ]); return $html; } protected function _addResources() { $this->_addDfResources(); $this->getDoc()->addScript('js/app/payment/Form.js'); $this->getDoc()->addScript('js/app/termsDialog/Dialog.js'); $this->getDoc()->addScript('js/fancybox/jquery.fancybox.js'); $this->getDoc()->addStylesheet('css/thirdpart/fancybox/jquery.fancybox.css'); $dialogId = $this->_termsContainerId; $this->getDoc()->addInitObject('app.payment.Form', [[ 'id' => $this->getId(), 'PAYMENT_METHOD_CHECK' => Entity::PAYMENT_METHOD_CHECK, 'nodes' => [ 'paymentMethod' => $this->getPaymentMethodSelector(), 'checkType' => '#' . $this->getElement('checkType')->getId(), 'checkTips' => '#' . $this->getElement('checkType')->getId() . '-element [data-check-type]', ], 'dialog' => [ 'id' => $dialogId, 'hiddenInputName' => $this->_hiddenInputName, 'nodes' => [ 'form' => '#' . $this->getId(), 'agreeButton' => '#' . $dialogId . ' [data-agree]', 'dialogBody' => '#' . $dialogId . ' .modal-body', ], ], ]]); $this->renderTermDialog(); return $this; } public function getPaymentSummary() { return $this->_paymentSummary; } public function setPaymentSummary($paymentSummary) { $this->_paymentSummary = $paymentSummary; return $this; } public function renderTermDialog() { $html = App_Settings_Obj::get('termsAndConditionsHtml'); $html = trim($html); if (!$html) { return; } $item = [ 'id' => $this->_termsContainerId, 'html' => $html, 'title' => 'Terms and Conditions', 'tpl' => 'ViewController/modal-agree-popup.tpl', ]; $this->getDoc()->addItem($item, 'AFTER_BODY'); } public function addButtons() { return $this->addStepButtons(); } private function getPaymentMethodSelector() { return $this->getElement('paymentMethod') instanceof Zend_Form_Element_Hidden ? '[name=paymentMethod]' : '[name=paymentMethod]:checked'; } }