Files
interblue.pl/modules/paybynet/paybynet.php
2024-10-25 14:16:28 +02:00

579 lines
22 KiB
PHP

<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
include_once(dirname(__FILE__).'/classes/PaybynetCore.php');
/**
*
*/
class Paybynet extends PaymentModule
{
public function __construct()
{
$this->name = 'paybynet';
$this->tab = 'payments_gateways';
$this->version = '1.0';
$this->author = 'KIR';
$this->display = 'view';
$this->is_eu_compatible = 1;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->controllers = array('payment', 'validation');
$this->bootstrap = true;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
parent::__construct();
$this->displayName = $this->l('PayByNet');
$this->description = $this->l('Paybynet jest jedynym w Polsce systemem do realizacji płatności przez Internet bezpośrednio z konta bankowego klienta na konto sklepu.');
$this->confirmUninstall = $this->l('Are you sure you want to delete your details?');
$this->module_link = $this->context->link->getAdminLink('AdminModules', true).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
}
public function install()
{
if (!parent::install()) {
return false;
}
if (!$this->registerHooks()) {
return false;
}
if (
!Configuration::updateValue('PAYBYNET_NAME', '')
|| !Configuration::updateValue('PAYBYNET_SHOW_TYPE', '')
|| !Configuration::updateValue('PAYBYNET_ZIP_CODE', '')
|| !Configuration::updateValue('PAYBYNET_CITY', '')
|| !Configuration::updateValue('PAYBYNET_STREET', '')
|| !Configuration::updateValue('PAYBYNET_COUNTRY', 'Polska')
|| !Configuration::updateValue('PAYBYNET_ACCOUNT', '')
|| !Configuration::updateValue('PAYBYNET_CURRENCY', 'PLN')
|| !Configuration::updateValue('PAYBYNET_DATE_VALID', '30')
|| !Configuration::updateValue('PAYBYNET_ID_CLIENT', '')
|| !Configuration::updateValue('PAYBYNET_PASSWORD', '')
|| !Configuration::updateValue('PAYBYNET_SHOW_BANKS', '0')
|| !Configuration::updateValue('PAYBYNET_CC_ACTIVE', '0')
|| !Configuration::updateValue('PAYBYNET_SANDBOX', '1')
|| !Configuration::updateValue('PAYBYNET_SORAGE_LOGS', '1')
) {
return false;
}
return true;
}
private function registerHooks()
{
if (!$this->registerHook('paymentOptions')
|| !$this->registerHook('displayOrderConfirmation')
|| !$this->registerHook('paymentReturn')
|| !$this->registerHook('displayAdminOrder')
|| !$this->registerHook('actionOrderStatusPostUpdate')
|| !$this->registerHook('actionOrderStatusUpdate')
|| !$this->registerHook('actionAdminControllerSetMedia')
|| !$this->registerHook('backOfficeHeader')
|| !$this->registerHook('header')
|| !$this->_crateOrderStates()) {
return false;
}
return true;
}
public function uninstall()
{
if (
!Configuration::deleteByName('PAYBYNET_NAME')
|| !Configuration::deleteByName('PAYBYNET_SHOW_TYPE')
|| !Configuration::deleteByName('PAYBYNET_ZIP_CODE')
|| !Configuration::deleteByName('PAYBYNET_CITY')
|| !Configuration::deleteByName('PAYBYNET_STREET')
|| !Configuration::deleteByName('PAYBYNET_COUNTRY')
|| !Configuration::deleteByName('PAYBYNET_ACCOUNT')
|| !Configuration::deleteByName('PAYBYNET_CURRENCY')
|| !Configuration::deleteByName('PAYBYNET_DATE_VALID')
|| !Configuration::deleteByName('PAYBYNET_ID_CLIENT')
|| !Configuration::deleteByName('PAYBYNET_PASSWORD')
|| !Configuration::deleteByName('PAYBYNET_SHOW_BANKS')
|| !Configuration::deleteByName('PAYBYNET_CC_ACTIVE')
|| !Configuration::deleteByName('PAYBYNET_SANDBOX')
|| !Configuration::deleteByName('PAYBYNET_SORAGE_LOGS')
|| !parent::uninstall()
) {
return false;
}
return true;
}
private function _crateOrderStates()
{
if (!Configuration::get('PAYBYNET_ORDERSTATE_WAITING_PAYMENT'))
{
$order_state = new OrderState();
$order_state->name = array();
foreach (Language::getLanguages(false) as $language)
if (Tools::strtolower($language['iso_code']) == 'pl') {
$order_state->name[(int)$language['id_lang']] = 'Oczekiwanie na płatność PayByNet';
} else {
$order_state->name[(int)$language['id_lang']] = 'Pending payment PayByNet';
}
$order_state->color = '#4169E1';
$order_state->hidden = false;
$order_state->send_email = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->invoice = false;
$order_state->unremovable = true;
if ($order_state->add()) {
Configuration::updateValue('PAYBYNET_ORDERSTATE_WAITING_PAYMENT', $order_state->id);
copy($this->_path.'logo.gif', $this->_path.'../../img/os/'.(int)$order_state->id.'.gif');
} else {
return false;
}
}
if (!Configuration::get('PAYBYNET_ORDERSTATE_EXPIRED_PAYMENT'))
{
$order_state = new OrderState();
$order_state->name = array();
foreach (Language::getLanguages(false) as $language)
if (Tools::strtolower($language['iso_code']) == 'pl') {
$order_state->name[(int)$language['id_lang']] = 'Płatność przeterminowana PayByNet';
} else {
$order_state->name[(int)$language['id_lang']] = 'Payment Expired PayByNet';
}
$order_state->color = '#8f0621';
$order_state->hidden = false;
$order_state->send_email = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->invoice = false;
$order_state->unremovable = true;
if ($order_state->add()) {
Configuration::updateValue('PAYBYNET_ORDERSTATE_EXPIRED_PAYMENT', $order_state->id);
copy($this->_path.'logo.gif', $this->_path.'../../img/os/'.(int)$order_state->id.'.gif');
} else {
return false;
}
}
if (!Configuration::get('PAYBYNET_ORDERSTATE_REJECTED_PAYMENT'))
{
$order_state = new OrderState();
$order_state->name = array();
foreach (Language::getLanguages(false) as $language)
if (Tools::strtolower($language['iso_code']) == 'pl') {
$order_state->name[(int)$language['id_lang']] = 'Płatność odrzucona PayByNet';
} else {
$order_state->name[(int)$language['id_lang']] = 'Payment Rejected PayByNet';
}
$order_state->color = '#8f0621';
$order_state->hidden = false;
$order_state->send_email = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->invoice = false;
$order_state->unremovable = true;
if ($order_state->add()) {
Configuration::updateValue('PAYBYNET_ORDERSTATE_REJECTED_PAYMENT', $order_state->id);
copy($this->_path.'logo.gif', $this->_path.'../../img/os/'.(int)$order_state->id.'.gif');
} else {
return false;
}
}
if (!Configuration::get('PAYBYNET_ORDERSTATE_ACCEPTED_PAYMENT'))
{
$order_state = new OrderState();
$order_state->name = array();
foreach (Language::getLanguages(false) as $language)
if (Tools::strtolower($language['iso_code']) == 'pl') {
$order_state->name[(int)$language['id_lang']] = 'Płatność przyjęta PayByNet';
} else {
$order_state->name[(int)$language['id_lang']] = 'Payment Accepted PayByNet';
}
$order_state->color = '#32CD32';
$order_state->hidden = false;
$order_state->send_email = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->unremovable = true;
if ($order_state->add()) {
Configuration::updateValue('PAYBYNET_ORDERSTATE_ACCEPTED_PAYMENT', $order_state->id);
copy($this->_path.'logo.gif', $this->_path.'../../img/os/'.(int)$order_state->id.'.gif');
} else {
return false;
}
}
return true;
}
public function hookPaymentOptions($params)
{
$payment_options = new PaymentOption();
$action_text = $this->l('PayByNet');
$payments = $this->paymentOptionsByBank();
$paymentBanks = array();
if (Configuration::get('PAYBYNET_SHOW_TYPE') == 2) {
if ($payments['banks_acvite']) {
foreach ($payments['banks'] as $bank) {
$payment_option = new PaymentOption();
$payment_option->setModuleName($this->l('PayByNet'));
$additionalInfo = $this->l('Payment "%s" is supported by: %s');
$additionalInfo = sprintf($additionalInfo, '<strong>' . $bank['name'] . '</strong>', '<strong>' . $this->l('PayByNet') . '</strong>');
$payment_option->setAdditionalInformation('<section><p>' . $additionalInfo . '</strong></p></section><br>');
$payment_option->setLogo(Media::getMediaPath($payments['icon_path'] . $bank['image']));
$payment_option->setAction($this->context->link->getModuleLink($this->name, 'submit', array('bank_id' => $bank['id']), true));
$paymentBanks[] = $payment_option;
}
}
} elseif (Configuration::get('PAYBYNET_SHOW_TYPE') == 1) {
$payment_option = new PaymentOption();
$payment_option->setModuleName($this->l('PayByNet'));
$payment_option->setLogo(Media::getMediaPath($payments['icon_path'] . 'paybynet_logo_banks.jpg'));
$payment_option->setAction($this->context->link->getModuleLink($this->name, 'submit', array(), true));
$paymentBanks[] = $payment_option;
} else {
$payment_option = new PaymentOption();
$payment_option->setModuleName($this->l('PayByNet'));
$payment_option->setLogo(Media::getMediaPath($payments['icon_path'] . 'paybynet_logo.jpg'));
$payment_option->setAction($this->context->link->getModuleLink($this->name, 'submit', array(), true));
$paymentBanks[] = $payment_option;
}
return $paymentBanks;
}
public function hookHeader()
{
if (Tools::getValue('controller') == 'order') {
$this->context->controller->addCSS($this->_path . '/views/css/payment-options.css', 'all');
$this->context->controller->addJS($this->_path . '/views/js/payment-options.js', 'all');
}
}
private function paymentOptionsByBank()
{
$validation_configuration = $this->validateFormConfiguration();
$validation_data = $this->validateFormData();
if ($validation_configuration['status'] || $validation_data['status']) {
return;
}
$banks = '';
$icon_path = _PS_MODULE_DIR_ . $this->name . '/';
$config = array(
'sandbox' => Configuration::get('PAYBYNET_SANDBOX'),
'log' => Configuration::get('PAYBYNET_SORAGE_LOGS')
);
if (Configuration::get('PAYBYNET_SHOW_TYPE') == 2) {
$paybynet_core = new PaybynetCore($config);
$banks = $paybynet_core->getBanksArray(Configuration::get('PAYBYNET_CC_ACTIVE'));
$icon_path .= 'cache/images/';
if (Configuration::get('PAYBYNET_SANDBOX')) {
$icon_path .= 'cache/images_test/';
}
}
if (_PS_VERSION_ < '1.5'){
$redirect_url = Tools::getShopDomainSsl(true).__PS_BASE_URI__.'modules/'.$this->name.'/payment.php';
} else {
$redirect_url = Context::getContext()->link->getModuleLink($this->name, 'payment');
}
return array(
// 'banks_acvite' => Configuration::get('PAYBYNET_SHOW_BANKS'),
'banks_acvite' => 1,
'banks' => $banks,
'icon_path' => $icon_path,
'module_path' => $this->_path,
'icon_path' => $icon_path,
'redirect_url' => $redirect_url
);
//
$this->smarty->assign(array(
// 'banks_acvite' => Configuration::get('PAYBYNET_SHOW_BANKS'),
'banks_acvite' => 0,
'banks' => $banks,
'icon_path' => $icon_path,
'module_path' => $this->_path,
'redirect_url' => $redirect_url
));
if (Configuration::get('PAYBYNET_SHOW_TYPE') == 2) {
return $this->fetch('module:paybynet/views/templates/hook/payment.tpl');
}
if (Configuration::get('PAYBYNET_SHOW_TYPE') == 1) {
return $this->fetch('module:paybynet/views/templates/hook/payment_banner_with_banks.tpl');
}
return $this->fetch('module:paybynet/views/templates/hook/payment_banner_pbn.tpl');
}
public function hookActionAdminControllerSetMedia()
{
if (Tools::getValue('controller') == "AdminOrders" && Tools::getValue('id_order')) {
}
}
public function hookPayment($params)
{
$validation_configuration = $this->validateFormConfiguration();
$validation_data = $this->validateFormData();
if ($validation_configuration['status'] || $validation_data['status']) {
return;
}
$banks = '';
$config = array(
'sandbox' => Configuration::get('PAYBYNET_SANDBOX'),
'log' => Configuration::get('PAYBYNET_SORAGE_LOGS')
);
$paybynet_core = new PaybynetCore($config);
$banks = $paybynet_core->getBanksArray(Configuration::get('PAYBYNET_CC_ACTIVE'));
$icon_paht = $this->_path.'cache/images/';
if (Configuration::get('PAYBYNET_SANDBOX')) {
$icon_paht = $this->_path.'cache/images_test/';
}
if (_PS_VERSION_ < '1.5'){
$redirect_url = Tools::getShopDomainSsl(true).__PS_BASE_URI__.'modules/'.$this->name.'/payment.php';
} else {
$redirect_url = Context::getContext()->link->getModuleLink($this->name, 'payment');
}
$this->smarty->assign(array(
'banks_acvite' => 1,
'banks' => $banks,
'icon_path' => $icon_paht,
'module_path' => $this->_path,
'redirect_url' => $redirect_url
));
return $this->display($this->_path, '/views/templates/hook/payment.tpl');
}
public function hookBackOfficeHeader()
{
if (Tools::getValue('configure') != 'paybynet') {
return;
}
$this->context->controller->addCSS($this->_path.'views/css/admin-theme.css', 'all');
}
public function getContent()
{
if (Tools::isSubmit('save_configuration'))
{
Configuration::updateValue('PAYBYNET_DATE_VALID', trim(Tools::getValue('paybynet_date_valid')));
Configuration::updateValue('PAYBYNET_SHOW_TYPE', trim(Tools::getValue('paybynet_show_type')));
Configuration::updateValue('PAYBYNET_SHOW_BANKS', Tools::getValue('paybynet_show_banks'));
Configuration::updateValue('PAYBYNET_CC_ACTIVE', Tools::getValue('paybynet_cc_active'));
Configuration::updateValue('PAYBYNET_SANDBOX', Tools::getValue('paybynet_sandbox'));
Configuration::updateValue('PAYBYNET_SORAGE_LOGS', Tools::getValue('paybynet_sorage_logs'));
}
if (Tools::isSubmit('save_data'))
{
Configuration::updateValue('PAYBYNET_NAME', trim(Tools::getValue('paybynet_name')));
Configuration::updateValue('PAYBYNET_ZIP_CODE', trim(Tools::getValue('paybynet_zip_code')));
Configuration::updateValue('PAYBYNET_CITY', trim(Tools::getValue('paybynet_city')));
Configuration::updateValue('PAYBYNET_STREET', trim(Tools::getValue('paybynet_street')));
Configuration::updateValue('PAYBYNET_COUNTRY', trim(Tools::getValue('paybynet_country')));
Configuration::updateValue('PAYBYNET_ACCOUNT', str_replace(array('-',' '), array('','',), trim(Tools::getValue('paybynet_account'))));
Configuration::updateValue('PAYBYNET_CURRENCY', trim(Tools::getValue('paybynet_currency')));
Configuration::updateValue('PAYBYNET_ID_CLIENT', str_replace(array('-',' '), array('','',), trim(Tools::getValue('paybynet_id_client'))));
Configuration::updateValue('PAYBYNET_PASSWORD', trim(Tools::getValue('paybynet_password')));
}
$config = array(
'sandbox' => Configuration::get('PAYBYNET_SANDBOX'),
'log' => Configuration::get('PAYBYNET_SORAGE_LOGS')
);
$pbn = new PaybynetCore($config);
if (Tools::isSubmit('bank_list_refresh')) {
$pbn->removeBankFile();
}
$bank_list = $pbn->getBanksArray();
$bank_file_info = $pbn->getBnaksFileTime();
$bank_image_path = $this->_path.'cache/images/';
if ($config['sandbox'] == 1) {
$bank_image_path = $this->_path.'cache/images_test/';
}
$this->smarty->assign(array(
'path' => $this->_path,
'index' => 'index.php?controller='.Tools::safeOutput(Tools::getValue('controller')),
'module' => $this->name,
'token' => Tools::safeOutput(Tools::getValue('token')),
'lang' => Context::getContext()->language->iso_code,
'form_values' => $this->getFormValues(),
'validate_configuration' => $this->validateFormConfiguration(),
'validate_data' => $this->validateFormData(),
'update_order_status_link' => _PS_BASE_URL_.'/modules/paybynet/update_transaction.php',
'file_info' => $bank_file_info,
'bank_list' => $bank_list,
'bank_image_path' => $bank_image_path
));
return $this->display($this->_path, 'views/templates/admin/configuration.tpl');
}
private function getFormValues()
{
$form_values = array();
$form_values['paybynet_name'] = Configuration::get('PAYBYNET_NAME');
$form_values['paybynet_show_type'] = Configuration::get('PAYBYNET_SHOW_TYPE');
$form_values['paybynet_zip_code'] = Configuration::get('PAYBYNET_ZIP_CODE');
$form_values['paybynet_city'] = Configuration::get('PAYBYNET_CITY');
$form_values['paybynet_street'] = Configuration::get('PAYBYNET_STREET');
$form_values['paybynet_country'] = Configuration::get('PAYBYNET_COUNTRY');
$form_values['paybynet_account'] = Configuration::get('PAYBYNET_ACCOUNT');
$form_values['paybynet_currency'] = Configuration::get('PAYBYNET_CURRENCY');
$form_values['paybynet_date_valid'] = Configuration::get('PAYBYNET_DATE_VALID');
$form_values['paybynet_id_client'] = Configuration::get('PAYBYNET_ID_CLIENT');
$form_values['paybynet_password'] = Configuration::get('PAYBYNET_PASSWORD');
$form_values['paybynet_show_banks'] = 0;
$form_values['paybynet_cc_active'] = Configuration::get('PAYBYNET_CC_ACTIVE');
$form_values['paybynet_sandbox'] = Configuration::get('PAYBYNET_SANDBOX');
$form_values['paybynet_sorage_logs'] = Configuration::get('PAYBYNET_SORAGE_LOGS');
return $form_values;
}
private function validateFormData()
{
$form_values = $this->getFormValues();
$error = array();
$error['status'] = false;
if (strlen(trim($form_values['paybynet_name'])) < 3) {
$error['status'] = true;
$error['message']['paybynet_name'] = $this->l('Field is required and must have more then 3 characters.');
}
if (strlen(trim($form_values['paybynet_zip_code'])) < 5 || strlen(trim($form_values['paybynet_zip_code'])) > 6) {
$error['status'] = true;
$error['message']['paybynet_zip_code'] = $this->l('Field is required and must be between 5 and 6 characters.');
}
if (strlen(trim($form_values['paybynet_city'])) < 3) {
$error['status'] = true;
$error['message']['paybynet_city'] = $this->l('Field is required and must have more then 3 characters.');
}
if (strlen(trim($form_values['paybynet_street'])) < 3) {
$error['status'] = true;
$error['message']['paybynet_street'] = $this->l('Field is required and must have more then 3 characters.');
}
if (strlen(trim($form_values['paybynet_country'])) < 3) {
$error['status'] = true;
$error['message']['paybynet_country'] = $this->l('Field is required and must have more then 3 characters.');
}
if (strlen(str_replace(array('-',' '), array('','',), trim($form_values['paybynet_account']))) != 26) {
$error['status'] = true;
$error['message']['paybynet_account'] = $this->l('Field is required and must have 26 characters. (Without spaces and "-")');
}
if (strlen(trim($form_values['paybynet_currency'])) < 2) {
$error['status'] = true;
$error['message']['paybynet_currency'] = $this->l('Field is required and must have more then 1 characters.');
}
if (strlen(trim($form_values['paybynet_id_client'])) < 10 || strlen(trim($form_values['paybynet_id_client'])) > 11) {
$error['status'] = true;
$error['message']['paybynet_id_client'] = $this->l('Field is required and must be between 10 and 12 characters.');
}
if (strlen(trim($form_values['paybynet_password'])) < 1) {
$error['status'] = true;
$error['message']['paybynet_password'] = $this->l('Field is required.');
}
return $error;
}
private function validateFormConfiguration()
{
$error['message'] = array();
$error['status'] = false;
if ((int)Configuration::get('PAYBYNET_DATE_VALID') <= 0) {
$error['status'] = true;
$error['message']['paybynet_date_valid'] = $this->l('Field is required and the value have to be greater then 0.');
}
return $error;
}
public function updateTransaction()
{
$newStatus = Tools::getValue('newStatus');
$transAmount = Tools::getValue('transAmount');
$paymentId = Tools::getValue('paymentId');
$hash = Tools::getValue('hash');
$passwrod = Configuration::get('PAYBYNET_PASSWORD');
$valid_hash = sha1($newStatus.$transAmount.$paymentId.$passwrod);
$config = array(
'sandbox' => Configuration::get('PAYBYNET_SANDBOX'),
'log' => 1
);
$pbn = new PaybynetCore($config);
$pbn->logOperation('Update status: Start operation with parameters:'.print_r(Tools::getAllValues(), true), 'Error');
if ($hash != $valid_hash) {
$pbn->logOperation('Update status: hash not valid.', 'Error');
return 'Error: hash not valid.';
}
//Format paymentId to Int after hash validation
$order = new Order((int)$paymentId);
$order_history = new OrderHistory();
$order_history->id_order = (int)$order->id;
if ($order) {
$status_type = $pbn->transactionStatus($newStatus);
$new_order_status_id = 0;
switch ($status_type) {
case 'approved':
$new_order_status_id = Configuration::get('PAYBYNET_ORDERSTATE_ACCEPTED_PAYMENT');
break;
case 'rejected':
$new_order_status_id = Configuration::get('PAYBYNET_ORDERSTATE_REJECTED_PAYMENT');
break;
case 'expired':
$new_order_status_id = Configuration::get('PAYBYNET_ORDERSTATE_EXPIRED_PAYMENT');
break;
}
if ($new_order_status_id > 0) {
$order_history->changeIdOrderState((int)($new_order_status_id), (int)($order->id));
return 'OK';
} else {
$pbn->logOperation('Update status: Status didn\'t match: ' . $newStatus . '.', 'Error');
return 'Error: Status didn\'t match.';
}
} else {
$pbn->logOperation('Update status: Order not found ID: ' . (int)$paymentId . '.', 'Error');
return 'Error: order not found.';
}
}
}