_initAction(); if ($this->step != self::STEP_FINAL) { try { $this->validateTribeLicense($this->getTribeLicenseKey()); } catch (Exception $e) { $this->_setBackUrl(LicenseUserView::getPage('url')); $this->_setBackError($e->getMessage()); $this->_doBack(); } } $licenseId = $this->getTribeLicenseKey()['licenseId']; $this->_getDataObj()->setPrimaryKey($licenseId); $result = $this->_callAction(); $parentPage = Qs_SiteMap::findFirst(['id' => $this->_doc->getOption('idParent')]); $this->_doc->setHeader(Qs_String::fill($parentPage['header'], ['id' => $licenseId])); $placeholders = [ 'stepProgress' => $this->renderProgressBar(), 'stepTitleBar' => $this->renderStepTitle(), ]; if (is_string($result)) { $placeholders['stepContent'] = $result; } elseif (is_array($result)) { $placeholders = array_merge($placeholders, $result); } else { throw new Exception('Unsupported action result'); } $this->_doc->setModulePlaceholders($this->getOption('type'), $placeholders); return $this; } public static function getStepPage($step, $field = null) { if (false === ($page = Qs_SiteMap::findFirst(['xStep' => $step], ['type' => 'LicensePremium\\']))) { throw new Exception('Can not find "' . $step . '" step page'); } return Qs_Array::get($page, $field); } protected function getSteps() { if (null === $this->steps) { $this->getTraitSteps(); $disabledSteps = []; if ($this->_action == self::STEP_FINAL) { $disabledSteps = [self::STEP_PAYMENT, self::STEP_VERIFICATION, self::STEP_FINAL]; } foreach ($this->steps as $index => &$step) { $step['enabled'] = !in_array($step['name'], $disabledSteps); $step['title'] = self::getStepPage($step['name'], 'menuTitle'); $step['url'] .= '/' . $this->_restAlias; } } return $this->steps; } public static function getStepUrl($step) { return self::getStepPage($step, 'url'); } protected function _initAction() { $this->step = $this->_action = $this->_doc->getOption('xStep'); return parent::_initAction(); } protected function _doPaymentAjax() { $form = $this->getPaymentForm(); $response = $form->validateAjax(); $this->_displayJson($response); } protected function _doPayment() { $form = $this->getPaymentForm(); if (Qs_Request::isPost()) { if ($form->validate()) { $this->saveStepData($this->step, $form->getValues()); $url = self::getStepUrl(self::STEP_VERIFICATION) . '/' . $this->_restAlias; $this->redirect($url); } } else { $form->setDefaults($this->getStepData()); } return $form->render(); } protected function getPaymentForm() { $options = [ 'bankApiUrl' => BankApiView::getPage('fullAlias'), 'userId' => $this->_doc->getAuthData('id'), 'attribs' => ['class' => 'stepPayment'], ]; $form = new PaymentForm($options); return $form; } protected function getVerificationForm() { $options = []; $options['payment'] = $this->getStepData(self::STEP_PAYMENT); $options['payment']['summary']['total'] = $this->getTribeLicense('surcharge'); $options['cancelUrl'] = self::getStepUrl(self::STEP_PAYMENT) . '/' . $this->_restAlias; $form = new VerificationForm($options); return $form; } protected function renderStepTitle() { $step = $this->getCurrentStep(); $item = [ 'currentStep' => $step, 'currentStepTitle' => $step['title'], 'stepsCount' => count($this->getSteps()), 'tpl' => $this->getTemplate('step-title-bar.tpl'), ]; return $this->_doc->fetchItem($item); } public function setTribeLicenseKey(array $key) { $this->validateTribeLicense($key); $this->getSession()->tribeLicenseKey = $key; return $this; } public function getTribeLicenseKey() { return $this->getSession()->tribeLicenseKey; } private function validateTribeLicense(array $key) { if (empty($key['licenseId'])) { throw new Exception('License ID is not specified'); } if (empty($key['tribeId'])) { throw new Exception('Tribe ID is not specified'); } if (count($key) > 2) { throw new Exception('Invalid tribe license key'); } $licenseObj = new LicenseUserObj(); if (!$licenseObj->hasAccessToLicense($this->_doc->getAuth()->getData('vendorId'), $key['licenseId'])) { throw new NotFoundException('Invalid License ID'); } if (!($tribeLicense = (new Qs_Db_Table('LicenseTribe'))->searchBy($key))) { throw new Exception('Invalid Tribe License'); } if ($tribeLicense['status'] != LicenseEntity::TRIBE_STATUS_APPROVED_PENDING) { throw new Exception('Invalid License Status: ' . $tribeLicense['status']); } if (empty($tribeLicense['surcharge'])) { throw new Exception('Premium amount is not defined'); } } private function getTribeLicense($field = null, $default = null) { if (null === $this->tribeLicense) { if (!($key = $this->getTribeLicenseKey())) { throw new Exception('Tribe License Key is undefined'); } if (!($tribeLicense = (new Qs_Db_Table('LicenseTribe'))->searchBy($key))) { throw new Exception('Invalid Tribe License'); } $this->tribeLicense = $tribeLicense; } return Qs_Array::get($this->tribeLicense, $field, $default); } private function getTribe($field = null, $default = null) { if (null === $this->tribe) { if (!($key = $this->getTribeLicenseKey())) { throw new Exception('Tribe License Key is undefined'); } if (!($tribe = (new Qs_Db_Table('Tribe'))->search($key['tribeId']))) { throw new Exception('Invalid Tribe ID'); } $this->tribe = $tribe; } return Qs_Array::get($this->tribe, $field, $default); } protected function _doVerificationAjax() { $form = $this->getVerificationForm(); $response = $form->validateAjax(); $this->_displayJson($response); } private function isRenew() { return (bool) $this->getTribeLicense('surchargeExpiredOn'); } protected function _doVerification() { $form = $this->getVerificationForm(); $tribeLicense = $this->getTribeLicense(); $isRenewing = $this->isRenew(); if (Qs_Request::isPost() && $form->validate()) { Qs_Db::getInstance()->beginTransaction(); try { $user = $this->_doc->getAuthData(); $payment = $this->getStepData(self::STEP_PAYMENT); $payment['total'] = $tribeLicense['surcharge']; $payment['userId'] = $user['id']; if (($merchantId = $this->getTribe('merchantId'))) { $payment['merchantId'] = $merchantId; } $vendor = $this->_getDataObj()->getVendorModel()->get($user['vendorId']); $payment['transactionId'] = $this->processPayment( 'PrmL#' . $tribeLicense['licenseId'], $payment, $user, $vendor ); $payment['ip'] = Qs_Request::getClientIP(); $this->maskPaymentData($payment); $newExpiredOn = $this->getExpiredOn(); $payment['description'] = "License #{$tribeLicense['licenseId']} Premium " . ($isRenewing ? 'renew': 'payment') . ' ' . 'for "' . $this->getTribe('title') . '" tribe ' . 'up to ' . $newExpiredOn; $paymentId = (new Qs_Db_Table('Payment'))->insert($payment); (new Qs_Db_Table('LicenseTribe'))->updateBy([ 'status' => LicenseEntity::TRIBE_STATUS_APPROVED, 'surchargePaid' => date('Y-m-d H:i:s'), 'surchargeExpiredOn' => $newExpiredOn, 'paymentId' => $paymentId, ], $this->getTribeLicenseKey()); (new PdfModel())->removePremiumReceipt($tribeLicense['licenseId'], $tribeLicense['tribeId']); Qs_Db::getInstance()->commit(); if ($isRenewing) { Mail::sendPremiumRenewedToUser($tribeLicense['licenseId'], $tribeLicense['tribeId']); Mail::sendPremiumRenewedToAdmin($tribeLicense['licenseId'], $tribeLicense['tribeId']); LibraryMail::sendLibraryUpdatedToUser( LibraryObj::RECORD_TYPE_PREMIUM, $tribeLicense['licenseId'], null, [$tribeLicense['tribeId']]); } else { Mail::sendPremiumPaidToUser($tribeLicense['licenseId'], $tribeLicense['tribeId']); Mail::sendPremiumPaidToAdmin($tribeLicense['licenseId'], $tribeLicense['tribeId']); LibraryMail::sendLibraryUpdatedToUser( LibraryObj::RECORD_TYPE_PREMIUM, $tribeLicense['licenseId'], null, [$tribeLicense['tribeId']]); } $this->clearStepData([self::STEP_PAYMENT, self::STEP_VERIFICATION]); $url = self::getStepUrl(self::STEP_FINAL); $this->redirect($url); } catch (IntegrityPaymentException $e) { $this->_setBackUrl(self::getStepUrl(self::STEP_PAYMENT)); $this->_setBackError($e->getMessage()); } catch (SpsException $e) { $this->_setBackUrl(self::getStepUrl(self::STEP_PAYMENT)); $this->_setBackError('SPS Error: ' . $e->getMessage()); } Qs_Db::getInstance()->rollBack(); $this->_doBack(); } Qs_View::getInstance()->addHelperPath('App/License/View/Helper/', 'App\License\View\Helper\\'); $item = $this->_getDataObj()->getData(); $item['vendor'] = $this->_getDataObj()->getVendorModel()->get($this->_doc->getAuthData('vendorId')); $item['tribe'] = Qs_Array::find($item['tribes'], ['tribeId' => $this->getTribeLicenseKey()['tribeId']]); $item['total'] = $tribeLicense['surcharge']; $item['tpl'] = $this->getTemplate('verification.tpl'); return $this->_doc->fetchItem($item) . $form->render(); } private function getExpiredOn() { if (($expiredOn = $this->getTribeLicense('surchargeExpiredOn'))) { $startTime = strtotime($expiredOn); } else { $startTime = time(); } $newExpiredOn = date('Y-m-d', strtotime('+' . App_Settings_Obj::get('licenseDuration') . ' months', $startTime)); return $newExpiredOn; } protected function _getDataObjClass() { return LicenseUserObj::class; } protected function _doFinal() { return [ 'id' => $this->getTribeLicense('licenseId'), ]; } public static function getPremiumPaymentPage($licenseId, $tribeId) { return LicenseUserView::getPage('url') . '?action=becomePremium&licenseId=' . $licenseId . '&tribeId=' . $tribeId; } }