name = 'imoje'; $this->displayName = 'imoje'; $this->tab = 'payments_gateways'; $this->version = '1.3.4'; $this->author = 'imoje'; $this->need_instance = 1; $this->currencies = true; $this->currencies_mode = 'checkbox'; $this->ps_versions_compliancy = [ 'min' => '1.7', 'max' => '1.7', ]; $this->is_eu_compatible = 1; parent::__construct(); $this->displayName = $this->l('imoje'); $this->description = $this->l('Visa, MasterCard, Blik ect.'); $this->confirm_uninstall = $this->l('Are you sure you want to uninstall imoje module?'); } /** * @param $name * @param $type * * @return string */ public static function buildTemplatePath($name, $type) { return 'module:imoje/views/templates/' . $type . '/' . $name . '.tpl'; } /** * This function installs the imoje Module * * @return boolean * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public function install() { if(!( parent::install() && $this->registerHook('paymentOptions') && $this->registerHook('orderConfirmation') && $this->registerHook('paymentReturn') && $this->registerHook('displayBackOfficeHeader') && $this->registerHook('displayHeader') && Configuration::updateValue('IMOJE_SANDBOX', 0) && Configuration::updateValue('IMOJE_SERVICE_ID', '') && Configuration::updateValue('IMOJE_SERVICE_KEY', '') && Configuration::updateValue('IMOJE_MERCHANT_ID', '') && Configuration::updateValue('IMOJE_PAYMENT_METHODS', '') && Configuration::updateValue('IMOJE_GA_KEY', '') && Configuration::updateValue('IMOJE_IMOJE_BUTTON', 1) && Configuration::updateValue('IMOJE_PAYMENT_TITLE', '') && Configuration::updateValue('IMOJE_TWISTO_BUTTON', 0) && Configuration::updateValue('IMOJE_TWISTO_ENABLED', 0) && Configuration::updateValue('IMOJE_TWISTO_PAYMENT_TITLE', '') && Configuration::updateValue('IMOJE_BLIK_BUTTON', 0) && Configuration::updateValue('IMOJE_BLIK_PAYMENT_TITLE', '') && Configuration::updateValue('IMOJE_TOKEN', '') )) { return false; } if(Validate::isInt(Configuration::get('PAYMENT_IMOJE_NEW_STATUS')) XOR (Validate::isLoadedObject($orderStateNew = new OrderState(Configuration::get('PAYMENT_IMOJE_NEW_STATUS'))))) { $orderStateNew = new OrderState(); $missingLang = true; $langs = [ 'en' => 'Payment imoje: awaiting for confirmation', 'pl' => 'Płatność imoje: oczekuje na potwierdzenie', ]; foreach($langs as $lang => $message) { $langId = Language::getIdByIso($lang); if(isset($langId)) { $orderStateNew->name[$langId] = $message; $missingLang = false; } } if($missingLang) { $langId = (int) $this->context->language->id; $orderStateNew->name[$langId] = $langs['en']; } $orderStateNew->send_email = false; $orderStateNew->invoice = false; $orderStateNew->unremovable = false; $orderStateNew->color = "lightblue"; if(!$orderStateNew->add()) { $this->_errors[] = $this->l('There was an Error installing the module. Cannot add new order state.'); return false; } if(!Configuration::updateValue('PAYMENT_IMOJE_NEW_STATUS', $orderStateNew->id)) { $this->_errors[] = $this->l('There was an Error installing the module. Cannot update new order state.'); return false; } copy(_PS_MODULE_DIR_ . $this->name . '/imoje.gif', _PS_IMG_DIR_ . 'os/' . $orderStateNew->id . '.gif'); } return true; } /** * This function uninstalls the imoje Module * * @return boolean */ public function uninstall() { return Configuration::deleteByName('IMOJE_SANDBOX') && Configuration::deleteByName('IMOJE_SERVICE_ID') && Configuration::deleteByName('IMOJE_SERVICE_KEY') && Configuration::deleteByName('IMOJE_MERCHANT_ID') && Configuration::deleteByName('IMOJE_PAYMENT_METHODS') && Configuration::deleteByName('IMOJE_TWISTO_ENABLED') && Configuration::deleteByName('IMOJE_GA_KEY') && Configuration::deleteByName('IMOJE_IMOJE_BUTTON') && Configuration::deleteByName('IMOJE_PAYMENT_TITLE') && Configuration::deleteByName('IMOJE_TWISTO_BUTTON') && Configuration::deleteByName('IMOJE_TWISTO_PAYMENT_TITLE') && Configuration::deleteByName('IMOJE_BLIK_BUTTON') && Configuration::deleteByName('IMOJE_BLIK_PAYMENT_TITLE') && Configuration::deleteByName('IMOJE_TOKEN') && parent::uninstall(); } /** * Display configuration form. * * @return mixed */ public function getContent() { $msg = ''; $paymentMethods = json_decode(Configuration::get('IMOJE_PAYMENT_METHODS'), true); if(isset($paymentMethods['errorTmp'])) { $msg = [ 'type' => 'error', 'message' => $this->l('Couldn\'t connect to imoje to get the Twisto configuration. Please try again later!'), ]; unset($paymentMethods['errorTmp']); Configuration::updateValue('IMOJE_PAYMENT_METHODS', json_encode($paymentMethods)); }; if(isset($paymentMethods['errorCurl'])) { $msg = [ 'type' => 'error', 'message' => $this->l('Curl extension is not installed on server.'), ]; unset($paymentMethods['errorCurl']); Configuration::updateValue('IMOJE_PAYMENT_METHODS', json_encode($paymentMethods)); }; if(isset($paymentMethods['successTmp'])) { $msg = [ 'type' => 'success', 'message' => $this->l('Configuration was successfully downloaded.'), ]; if(isset($paymentMethods['noTwisto'])) { $msg = [ 'type' => 'success', 'message' => $this->l('Twisto is not activated in your shop. Please contact our Customer Service.'), ]; unset($paymentMethods['noTwisto']); } unset($paymentMethods['successTmp']); Configuration::updateValue('IMOJE_PAYMENT_METHODS', json_encode($paymentMethods)); }; if(Tools::isSubmit('submitiMoje')) { if($this->saveConfiguration($paymentMethods)) { $msg = [ 'type' => 'success', 'message' => $this->l('Configuration updated successfully'), ]; } else { $msg = [ 'type' => 'error', 'message' => $this->l('There was an error saving your configuration'), ]; } } $this->context->smarty->assign([ 'imoje_form' => './index.php?tab=AdminModules&configure=imoje&token=' . Tools::getAdminTokenLite('AdminModules') . '&tab_module=' . $this->tab . '&module_name=imoje', 'imoje_enabled' => Configuration::get('IMOJE_ENABLED'), 'imoje_token' => Configuration::get('IMOJE_TOKEN'), 'imoje_sandbox' => Configuration::get('IMOJE_SANDBOX'), 'imoje_service_id' => Configuration::get('IMOJE_SERVICE_ID'), 'imoje_service_key' => Configuration::get('IMOJE_SERVICE_KEY'), 'imoje_merchant_id' => Configuration::get('IMOJE_MERCHANT_ID'), 'imoje_twisto' => Configuration::get('IMOJE_TWISTO_ENABLED'), 'imoje_payment_methods' => $paymentMethods, 'imoje_payment_methods_url' => $this->context->link->getModuleLink('imoje', 'checkmethods'), 'imoje_notification_url' => $this->context->link->getModuleLink('imoje', 'notification'), 'imoje_msg' => $msg, 'imoje_ga_key' => Configuration::get('IMOJE_GA_KEY'), 'imoje_imoje_button' => Configuration::get('IMOJE_IMOJE_BUTTON'), 'imoje_payment_title' => Configuration::get('IMOJE_PAYMENT_TITLE'), 'imoje_payment_title_default' => $this->getPaymentTitleDefault($paymentMethods, Configuration::get('IMOJE_TWISTO_ENABLED')), 'imoje_twisto_button' => Configuration::get('IMOJE_TWISTO_BUTTON'), 'imoje_twisto_payment_title' => Configuration::get('IMOJE_TWISTO_PAYMENT_TITLE'), 'imoje_twisto_payment_title_default' => $this->getTwistoPaymentTitleDefault(), 'imoje_blik_button' => Configuration::get('IMOJE_BLIK_BUTTON'), 'imoje_blik_payment_title' => Configuration::get('IMOJE_BLIK_PAYMENT_TITLE'), 'imoje_blik_payment_title_default' => $this->getBlikPaymentTitleDefault(), ]); return $this->fetchTemplate('admin.tpl'); } /** * @param string $name * * @return mixed */ public function fetchTemplate($name) { return $this->display(__FILE__, $name); } /** * Hook that run on mobile version of Prestashop. */ public function hookDisplayMobileHeader() { $this->hookHeader(); } /** * Hook that adds css for old versions of Prestashop. */ public function hookHeader() { $this->context->controller->addCSS(($this->_path) . 'assets/css/imoje-legacy-front.min.css', 'all'); } /** * Hook that adds css and js in admin panel. For newer version of PrestaShop. */ public function hookDisplayBackOfficeHeader() { $this->context->controller->addJquery(); $this->context->controller->addCSS($this->_path . 'assets/css/imoje-admin.min.css', 'all'); $this->context->controller->addCSS($this->_path . 'assets/css/font-awesome.min.css', 'all'); } /** * Hook that adds css and js in admin panel. */ public function hookBackOfficeFooter() { echo ''; echo ''; echo ''; } /** * @param array $params * * @return array|bool * @throws SmartyException * @throws Exception */ public function hookPaymentOptions($params) { $cart = new Cart($params['cart']->id); $currency = Currency::getCurrency($params['cart']->id_currency); if( !Configuration::get('IMOJE_MERCHANT_ID') || !Configuration::get('IMOJE_SERVICE_ID') || !Configuration::get('IMOJE_SERVICE_KEY') || !$this->active || !Util::canUseForCurrency($currency['iso_code']) ) { return false; } $paymentMethods = json_decode(Configuration::get('IMOJE_PAYMENT_METHODS'), true); $twistoPayload = ''; $isTwisto = Configuration::get('IMOJE_TWISTO_ENABLED'); if(Twisto::isActive( $paymentMethods, $isTwisto )) { $customer = new Customer((int) $cart->id_customer); $twistoPayload = Twisto::getPayload( $paymentMethods['twisto']['sk'], $cart->getOrderTotal(true), $this->getCart($cart), $customer->email, DateTime::createFromFormat('Y-m-d H:i:s', $cart->date_upd)); } $environment = Configuration::get('IMOJE_SANDBOX') == 1 ? Util::ENVIRONMENT_SANDBOX : Util::ENVIRONMENT_PRODUCTION; $link = $this->context->link->getModuleLink('imoje', 'payment'); $linkTwisto = $this->context->link->getModuleLink('imoje', 'twisto'); $this->context->smarty->assign([ 'imoje_ps_version' => _PS_VERSION_, 'imoje_enabled' => Configuration::get('IMOJE_ENABLED'), 'imoje_sandbox' => Configuration::get('IMOJE_SANDBOX'), 'imoje_twisto_payload' => $twistoPayload, 'imoje_logo' => Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/assets/img/logo.png'), 'blik_logo' => Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/assets/img/blik.png'), 'twisto_logo' => Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/twisto.svg'), 'loading_gif' => Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/assets/img/loading.gif'), 'imoje_payment_url' => $link, 'imoje_payment_methods' => $paymentMethods, 'imoje_twisto_script_url' => Twisto::getUrlScript( $environment ), 'imoje_link_twisto' => $linkTwisto, 'twisto_hint' => $this->l('Please wait, the connection with the Twisto is being established'), ]); $paymentOptions = []; $isBlikButton = Configuration::get('IMOJE_BLIK_BUTTON'); if($isBlikButton) { $paymentOptionBlik = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $paymentOptions[] = $paymentOptionBlik->setCallToActionText( (Configuration::get('IMOJE_BLIK_PAYMENT_TITLE') ?: $this->getBlikPaymentTitleDefault()) . ($environment === Util::ENVIRONMENT_SANDBOX ? ' ' . $this->l('Sandbox is enabled.') : '')) ->setModuleName($this->name) ->setAction($this->context->link->getModuleLink('imoje', 'paymentblik')) ->setAdditionalInformation($this->context->smarty->fetch('module:imoje/views/templates/hook/payment_blik.tpl')); } if(Configuration::get('IMOJE_IMOJE_BUTTON') || (!$isTwisto && !$isBlikButton)) { $paymentOptionImoje = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $paymentOptions[] = $paymentOptionImoje->setCallToActionText( (Configuration::get('IMOJE_PAYMENT_TITLE') ?: $this->getPaymentTitleDefault($paymentMethods)) . ($environment === Util::ENVIRONMENT_SANDBOX ? ' ' . $this->l('Sandbox is enabled.') : '')) ->setModuleName($this->name) ->setAction($link) ->setAdditionalInformation($this->context->smarty->fetch('module:imoje/views/templates/hook/payment_imoje.tpl')); } if(Configuration::get('IMOJE_TWISTO_BUTTON') && $isTwisto) { $paymentOptionTwisto = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $paymentOptions[] = $paymentOptionTwisto->setCallToActionText( (Configuration::get('IMOJE_TWISTO_PAYMENT_TITLE') ?: $this->getTwistoPaymentTitleDefault()) . ($environment === Util::ENVIRONMENT_SANDBOX ? ' ' . $this->l('Sandbox is enabled.') : '')) ->setModuleName('twisto') ->setAction($linkTwisto) ->setAdditionalInformation($this->context->smarty->fetch('module:imoje/views/templates/hook/payment_twisto.tpl')); } return $paymentOptions; } /** * @param Cart $cart * * @return array * @throws Exception */ public function getCart($cart) { $items = []; foreach($cart->getProducts() as $product) { $items[] = [ 'name' => $product['name'], 'productId' => $product['id_product'] . (isset($product['attributes_small']) && !empty($product['attributes_small']) ? ' - ' . $product['attributes_small'] : ''), 'quantity' => (float) $product['quantity'], 'amount' => round($product['total_wt'], 2) / $product['quantity'], 'vatRate' => (float) $product['rate'], ]; } $carrier = new Carrier($cart->id_carrier); $addressBilling = new Address((int) $cart->id_address_invoice); $addressDelivery = new Address((int) $cart->id_address_delivery); $data = [ 'shipping' => [ 'amount' => $cart->getTotalShippingCost(), 'name' => $carrier->name, 'vatRate' => (float) $carrier->getTaxesRate(new Address((int) $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')})), ], 'items' => $items, 'addressBilling' => Util::formatAddress( $addressBilling->firstname . ' ' . $addressBilling->lastname, empty($addressBilling->address2) ? $addressBilling->address1 : $addressBilling->address1 . ', ' . $addressBilling->address2, $addressBilling->city, $addressBilling->postcode, CountryCore::getIsoById($addressBilling->id_country), empty($addressBilling->phone_mobile) ? (empty($addressBilling->phone) ? '' : $addressBilling->phone) : $addressBilling->phone_mobile), 'addressDelivery' => Util::formatAddress( $addressDelivery->firstname . ' ' . $addressDelivery->lastname, empty($addressDelivery->address2) ? $addressDelivery->address1 : $addressDelivery->address1 . ', ' . $addressDelivery->address2, $addressDelivery->city, $addressDelivery->postcode, CountryCore::getIsoById($addressDelivery->id_country), empty($addressDelivery->phone_mobile) ? (empty($addressDelivery->phone) ? '' : $addressDelivery->phone) : $addressDelivery->phone_mobile), ]; $discount = $cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS); if($discount > 0) { $data['discount'] = [ 'amount' => $discount, 'name' => 'discount', 'vatRate' => 0, ]; } return $data; } /** * @return mixed */ public function hookPaymentReturn() { return $this->display(__FILE__, 'tpl/confirmation.tpl'); } /** * Prepares data for form that is send to imoje. * * @param Cart $cart * @param string $twistoData * * @return array * @throws Exception */ public function getDataForRequestToPaywall($cart, $twistoData = '') { $cartId = $cart->id; $customer = new Customer(intval($cart->id_customer)); $addressBilling = new Address((int) $cart->id_address_invoice); $addressDelivery = new Address((int) $cart->id_address_delivery); $currencyInfo = Currency::getCurrency($cart->id_currency); if(Imoje\Payment\Twisto::isActive( json_decode(Configuration::get('IMOJE_PAYMENT_METHODS'), true), Configuration::get('IMOJE_TWISTO_ENABLED') ) && Configuration::get('IMOJE_SANDBOX')) { $twistoData = Twisto::getDataSandbox(); } return Util::prepareData( Util::convertAmountToFractional(Context::getContext()->cart->getOrderTotal()), $currencyInfo['iso_code'], intval($cartId), null, $customer->firstname, $customer->lastname, $customer->email, ($addressDelivery->phone_mobile ?: ($addressBilling->phone_mobile ?: '')), Configuration::get('IMOJE_GA_KEY') ? $this->context->link->getModuleLink('imoje', 'success', [ 'ga_cart_id' => $cart->id, 'ga_hash' => hash('sha256', $cart->id . $cart->secure_key), ]) : $this->context->link->getModuleLink('imoje', 'success'), $this->context->link->getModuleLink('imoje', 'failure'), $this->context->link->getModuleLink('imoje', 'confirmation'), $this->version . ';prestashop_' . _PS_VERSION_, $twistoData ); } /** * @param array $paymentMethods * @param bool $isTwisto * * @return string */ private function getPaymentTitleDefault($paymentMethods, $isTwisto = false) { $paymentDefaultTitle = $this->l('Pay for purchases using the most convenient online payment methods - PayByLink, cards, BLIK.'); if(Twisto::isActive( $paymentMethods, $isTwisto ) && !Configuration::get('IMOJE_TWISTO_BUTTON')) { $paymentDefaultTitle = $paymentDefaultTitle . ' ' . $this->getTwistoPaymentTitleDefault(); } return $paymentDefaultTitle; } /** * @return string */ private function getTwistoPaymentTitleDefault() { return $this->l('Order now, pay in 21 days with Twisto'); } /** * @return string */ private function getBlikPaymentTitleDefault() { return $this->l('BLIK'); } /** * @param array $paymentMethods * * @return bool */ private function saveConfiguration(&$paymentMethods) { if(isset($paymentMethods['twisto'])) { Configuration::updateValue('IMOJE_TWISTO_ENABLED', pSQL(Tools::getValue('imoje_twisto'))); } Configuration::updateValue('IMOJE_SANDBOX', pSQL(Tools::getValue('imoje_sandbox'))); Configuration::updateValue('IMOJE_SERVICE_ID', pSQL(Tools::getValue('imoje_service_id'))); Configuration::updateValue('IMOJE_SERVICE_KEY', pSQL(Tools::getValue('imoje_service_key'))); Configuration::updateValue('IMOJE_MERCHANT_ID', pSQL(Tools::getValue('imoje_merchant_id'))); Configuration::updateValue('IMOJE_TOKEN', pSQL(Tools::getValue('imoje_token'))); Configuration::updateValue('IMOJE_PAYMENT_METHODS', ($paymentMethods === '' ? $paymentMethods : json_encode($paymentMethods))); Configuration::updateValue('IMOJE_GA_KEY', pSQL(Tools::getValue('imoje_ga_key'))); Configuration::updateValue('IMOJE_IMOJE_BUTTON', pSQL(Tools::getValue('imoje_imoje_button'))); Configuration::updateValue('IMOJE_PAYMENT_TITLE', pSQL(Tools::getValue('imoje_payment_title'))); Configuration::updateValue('IMOJE_TWISTO_BUTTON', pSQL(Tools::getValue('imoje_twisto_button'))); Configuration::updateValue('IMOJE_TWISTO_PAYMENT_TITLE', pSQL(Tools::getValue('imoje_twisto_payment_title'))); Configuration::updateValue('IMOJE_BLIK_BUTTON', pSQL(Tools::getValue('imoje_blik_button'))); Configuration::updateValue('IMOJE_BLIK_PAYMENT_TITLE', pSQL(Tools::getValue('imoje_blik_payment_title'))); return true; } }