diff --git a/KhipuPostBack.php b/KhipuPostBack.php index 0e4c275..c7de944 100644 --- a/KhipuPostBack.php +++ b/KhipuPostBack.php @@ -18,7 +18,7 @@ class KhipuPostback { - const PLUGIN_VERSION = '4.0.13'; + const PLUGIN_VERSION = '4.1'; public function init() { diff --git a/composer.json b/composer.json index f39e946..8ae629e 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { "require": { - "khipu/khipu-api-client": "*" + "khipu/khipu-api-client": "3.0.0.x-dev" } } diff --git a/config.xml b/config.xml index e337e7a..401d8bc 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ khipupayment - + diff --git a/config_es.xml b/config_es.xml index d9b20ea..72ca5c3 100644 --- a/config_es.xml +++ b/config_es.xml @@ -2,7 +2,7 @@ khipupayment - + diff --git a/controllers/front/manual.php b/controllers/front/manual.php index b146c03..9e488cd 100644 --- a/controllers/front/manual.php +++ b/controllers/front/manual.php @@ -28,15 +28,15 @@ public function initContent() $cart = $this->context->cart; $this->module->validateOrder( - (int)self::$cart->id, + (int) $this->context->cart->id, (int)Configuration::get('PS_OS_KHIPU_OPEN'), - (float)self::$cart->getOrderTotal(), + (float)$cart->getOrderTotal(), $this->module->displayName, null, array(), null, false, - self::$cart->secure_key + $cart->secure_key ); $order = new Order(Order::getOrderByCartId($cart->id)); diff --git a/controllers/front/simplified.php b/controllers/front/simplified.php index e0b4d19..54e9868 100644 --- a/controllers/front/simplified.php +++ b/controllers/front/simplified.php @@ -28,15 +28,15 @@ public function initContent() $cart = $this->context->cart; $this->module->validateOrder( - (int)self::$cart->id, + (int) $this->context->cart->id, (int)Configuration::get('PS_OS_KHIPU_OPEN'), - (float)self::$cart->getOrderTotal(), + (float)$cart->getOrderTotal(), $this->module->displayName, null, array(), null, false, - self::$cart->secure_key + $cart->secure_key ); $order = new Order(Order::getOrderByCartId($cart->id)); diff --git a/khipupayment.php b/khipupayment.php index c86c817..9b037aa 100644 --- a/khipupayment.php +++ b/khipupayment.php @@ -36,9 +36,9 @@ public function __construct() { $this->name = 'khipupayment'; $this->tab = 'payments_gateways'; - $this->version = '4.0.13'; + $this->version = '4.1'; $this->apiVersion = '2.0'; - $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); + $this->ps_versions_compliancy = array('min' => '8.0', 'max' => _PS_VERSION_); $this->author = 'Khipu SpA'; $this->controllers = array('validate'); $this->is_eu_compatible = 1; @@ -73,47 +73,130 @@ public function install() } } + private function addOrderStates() { - if (!(Configuration::get('PS_OS_KHIPU_OPEN') > 0)) { - $OrderState = new OrderState(null, Configuration::get('PS_LANG_DEFAULT')); - $OrderState->name = "Esperando pago"; - $OrderState->invoice = false; - $OrderState->send_email = false; - $OrderState->module_name = $this->name; - $OrderState->color = "RoyalBlue"; - $OrderState->unremovable = true; - $OrderState->hidden = false; - $OrderState->logable = false; - $OrderState->delivery = false; - $OrderState->shipped = false; - $OrderState->paid = false; - $OrderState->deleted = false; - $OrderState->template = "order_changed"; - $OrderState->add(); - - Configuration::updateValue("PS_OS_KHIPU_OPEN", $OrderState->id); - - if (file_exists(dirname(dirname(dirname(__file__))) . '/img/os/10.gif')) { - copy( - dirname(dirname(dirname(__file__))) . '/img/os/10.gif', - dirname(dirname(dirname(__file__))) . '/img/os/' . $OrderState->id . '.gif' - ); - } + $khipuOpenOrderStateId = (int) Configuration::get('PS_OS_KHIPU_OPEN'); + + // Verificar si el estado de pedido de Khipu ya existe + if ($khipuOpenOrderStateId > 0) { + return; // El estado de pedido de Khipu ya está configurado } + + // Configuración del nuevo estado de pedido + $orderStateData = array( + 'name' => array(), + 'color' => '#4169e1', + 'send_email' => false, + 'unremovable' => true, + 'hidden' => false, + 'logable' => false, + 'delivery' => false, + 'shipped' => false, + 'paid' => false, + 'deleted' => false, + 'template' => 'order_changed', + 'module_name' => 'khipupayment' + ); + + foreach (Language::getLanguages() as $language) { + $orderStateData['name'][(int)$language['id_lang']] = 'Esperando pago Khipu'; + } + + // Crear el nuevo estado de pedido + $orderState = new OrderState(); + $orderState->hydrate($orderStateData); + $orderState->add(); + + // Actualizar la configuración del estado de pedido de Khipu + Configuration::updateValue('PS_OS_KHIPU_OPEN', (int) $orderState->id); + + // Configurar la imagen del estado de pedido + $orderState->updateImg(dirname(__FILE__) . '/views/img/status.gif'); } + public function uninstall() { - return - parent::uninstall() - && Configuration::deleteByName('KHIPU_MERCHANTID') - && Configuration::deleteByName('KHIPU_SECRETCODE') - && $this->unregisterHook('paymentOptions') - && $this->unregisterHook('paymentReturn'); + try { + $success = true; + + // Eliminar configuraciones del módulo + $success &= Configuration::deleteByName('KHIPU_MERCHANTID'); + $success &= Configuration::deleteByName('KHIPU_SECRETCODE'); + + // Desregistrarse de los hooks + $success &= $this->unregisterHook('paymentOptions'); + $success &= $this->unregisterHook('paymentReturn'); + + // Verificar si la desinstalación se realizó correctamente + if ($success) { + return parent::uninstall(); + } else { + throw new Exception('Error during uninstallation: not all actions were successful.'); + } + } catch (Exception $e) { + PrestaShopLogger::addLog('Error during uninstallation: ' . $e->getMessage(), 3, null, 'Module', (int)$this->id, true); + return false; + } + } + + public function hookPaymentReturn($params) + { + if (!$this->active) { + return; + } + + // Verificar si el pedido se ha completado correctamente + if (isset($params['order']) && Validate::isLoadedObject($params['order'])) { + $order = $params['order']; + + // Obtener el estado del pedido + $orderStatus = $order->getCurrentState(); + + // Verificar si el estado del pedido es el de pago confirmado + if ($orderStatus == Configuration::get('PS_OS_PAYMENT') || $orderStatus == Configuration::get('PS_OS_KHIPU_OPEN')) { + // Obtener el contenido del carrito + $cartContent = array(); + $products = $order->getProducts(); + foreach ($products as $product) { + $cartContent[] = array( + 'name' => $product['product_name'], + 'quantity' => $product['product_quantity'], + 'price' => Tools::displayPrice($product['product_price'], $order->id_currency), + ); + } + + // Obtener los datos del pagador + $customerData = array( + 'first_name' => $order->getCustomer()->firstname, + 'last_name' => $order->getCustomer()->lastname, + 'email' => $order->getCustomer()->email, + // Agrega otros campos según sea necesario + ); + + // Asignar variables para la plantilla + $this->context->smarty->assign(array( + 'status' => 'ok', + 'id_order' => $order->reference, + 'total_to_pay' => Tools::displayPrice($order->total_paid, $order->id_currency), + 'cart_content' => $cartContent, + 'customer_data' => $customerData, + )); + + // Renderizar la plantilla y devolverla como salida + return $this->display(__FILE__, 'views/templates/hook/payment_return.tpl'); + } + } + + // Si el estado del pedido no es el esperado, mostrar un mensaje de error genérico + $this->context->smarty->assign('status', 'failed'); + return $this->display(__FILE__, 'views/templates/hook/payment_return.tpl'); } + + private function getPaymentMethod(Khipu\Model\PaymentMethodsResponse $paymentMethods, $id) { @@ -142,7 +225,7 @@ public function hookPaymentOptions($params) $configuration = new Khipu\Configuration(); $configuration->setSecret(Configuration::get('KHIPU_SECRETCODE')); $configuration->setReceiverId(Configuration::get('KHIPU_MERCHANTID')); - $configuration->setPlatform('prestashop-khipu', $this->module->version); + $configuration->setPlatform('prestashop-khipu', $this->version); $client = new Khipu\ApiClient($configuration); @@ -166,20 +249,35 @@ public function hookPaymentOptions($params) public function cancelExpiredOrders() { - - $result = Db::getInstance()->ExecuteS('SELECT `id_order` FROM `' . _DB_PREFIX_ . 'orders` WHERE DATE_ADD(date_add, INTERVAL +' . Configuration::get('KHIPU_MINUTES_TIMEOUT') . ' MINUTE) < \'' . date('Y-m-d H:i:00', time()) - . '\' AND current_state = ' . Configuration::get('PS_OS_KHIPU_OPEN')); + // Obtén el tiempo de espera desde la configuración del módulo + $waiting_period = (int)Configuration::get('KHIPU_MINUTES_TIMEOUT'); + + // Calcula la fecha límite para cancelar los pedidos + $expiration_date = date('Y-m-d H:i:00', strtotime("-$waiting_period minutes")); + + // Consulta los pedidos que están en estado "Esperando Pago" y han expirado + $result = Db::getInstance()->executeS( + 'SELECT `id_order` + FROM `' . _DB_PREFIX_ . 'orders` + WHERE `date_add` < \'' . pSQL($expiration_date) . '\' + AND `current_state` = ' . (int)Configuration::get('PS_OS_KHIPU_OPEN') + ); if ($result) { - foreach ($result as $orderId) { - $order = new Order(intval($orderId['id_order'])); + foreach ($result as $order_data) { + $order_id = (int)$order_data['id_order']; + $order = new Order($order_id); + + // Verifica si el pedido todavía está en estado "Esperando Pago" if ($order->current_state == (int)Configuration::get('PS_OS_KHIPU_OPEN')) { + // Actualiza el estado del pedido a "Cancelado" $this->setCurrentOrderState($order, (int)Configuration::get('PS_OS_CANCELED')); } } } } + public function getKhipuSimplifiedTransferPayment(Khipu\Model\PaymentMethodItem $paymentMethod) { $simplifiedTransfer = new PaymentOption(); @@ -262,7 +360,7 @@ public function getContent() */ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method = 'Unknown', $message = null, $extra_vars = array(), $currency_special = null, $dont_touch_amount = false, - $secure_key = false, Shop $shop = null) + $secure_key = false, Shop $shop = null, ?string $order_reference = null) { if (self::DEBUG_MODE) { PrestaShopLogger::addLog('PaymentModule::validateOrder - Function called', 1, null, 'Cart', (int)$id_cart, true); diff --git a/vendor/khipu/khipu-api-client/composer.json b/vendor/khipu/khipu-api-client/composer.json index f86bcea..f0a7fcd 100644 --- a/vendor/khipu/khipu-api-client/composer.json +++ b/vendor/khipu/khipu-api-client/composer.json @@ -1,6 +1,6 @@ { "name": "khipu/khipu-api-client", - "version": "2.9.1", + "version": "3.0.0", "description": "", "keywords": [ "khipu", @@ -17,7 +17,7 @@ } ], "require": { - "php": ">=5.3.3", + "php": ">=8.0.0", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*" diff --git a/vendor/khipu/khipu-api-client/lib/Model/AuthorizationError.php b/vendor/khipu/khipu-api-client/lib/Model/AuthorizationError.php index ed4adce..f3c1d8a 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/AuthorizationError.php +++ b/vendor/khipu/khipu-api-client/lib/Model/AuthorizationError.php @@ -149,46 +149,50 @@ public function setMessage($message) $this->message = $message; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/BankItem.php b/vendor/khipu/khipu-api-client/lib/Model/BankItem.php index 0ab3b16..2d13222 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/BankItem.php +++ b/vendor/khipu/khipu-api-client/lib/Model/BankItem.php @@ -277,46 +277,50 @@ public function setParent($parent) $this->parent = $parent; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/ErrorItem.php b/vendor/khipu/khipu-api-client/lib/Model/ErrorItem.php index ae81001..44f2806 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/ErrorItem.php +++ b/vendor/khipu/khipu-api-client/lib/Model/ErrorItem.php @@ -149,46 +149,50 @@ public function setMessage($message) $this->message = $message; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/PaymentMethodItem.php b/vendor/khipu/khipu-api-client/lib/Model/PaymentMethodItem.php index a9e7205..23c3af1 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/PaymentMethodItem.php +++ b/vendor/khipu/khipu-api-client/lib/Model/PaymentMethodItem.php @@ -181,46 +181,50 @@ public function setLogoUrl($logo_url) $this->logo_url = $logo_url; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/PaymentsCreateResponse.php b/vendor/khipu/khipu-api-client/lib/Model/PaymentsCreateResponse.php index 2213fe1..434fb39 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/PaymentsCreateResponse.php +++ b/vendor/khipu/khipu-api-client/lib/Model/PaymentsCreateResponse.php @@ -379,9 +379,10 @@ public function setReadyForTerminal($ready_for_terminal) * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } /** @@ -389,9 +390,10 @@ public function offsetExists($offset) * @param integer $offset Offset * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } /** @@ -400,9 +402,10 @@ public function offsetGet($offset) * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } /** @@ -410,9 +413,10 @@ public function offsetSet($offset, $value) * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/PaymentsResponse.php b/vendor/khipu/khipu-api-client/lib/Model/PaymentsResponse.php index 79444d6..db3379b 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/PaymentsResponse.php +++ b/vendor/khipu/khipu-api-client/lib/Model/PaymentsResponse.php @@ -1333,46 +1333,50 @@ public function setPaymentMethod($payment_method) $this->payment_method = $payment_method; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/vendor/khipu/khipu-api-client/lib/Model/ServiceError.php b/vendor/khipu/khipu-api-client/lib/Model/ServiceError.php index 97e992b..701ca94 100644 --- a/vendor/khipu/khipu-api-client/lib/Model/ServiceError.php +++ b/vendor/khipu/khipu-api-client/lib/Model/ServiceError.php @@ -149,46 +149,50 @@ public function setMessage($message) $this->message = $message; return $this; } - + /** * Returns true if offset exists. False otherwise. - * @param integer $offset Offset + * @param integer $offset Offset * @return boolean */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { - return isset($this->$offset); + return isset($this->data[$offset]); } - + /** * Gets offset. - * @param integer $offset Offset - * @return mixed + * @param integer $offset Offset + * @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { - return $this->$offset; + return isset($this->data[$offset]); } - + /** * Sets value based on offset. - * @param integer $offset Offset + * @param integer $offset Offset * @param mixed $value Value to be set * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->data[$offset]= $value; } - + /** * Unsets offset. - * @param integer $offset Offset + * @param integer $offset Offset * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { - unset($this->$offset); + unset($this->data[$offset]); } /** diff --git a/views/templates/hook/payment_return.tpl b/views/templates/hook/payment_return.tpl new file mode 100644 index 0000000..6bd3bad --- /dev/null +++ b/views/templates/hook/payment_return.tpl @@ -0,0 +1,15 @@ +{if isset($status) && $status == 'ok'} +

Su pago ha sido procesado correctamente. Gracias por su compra.

+

Número de pedido: {$id_order}

+

Total pagado: {$total_to_pay}

+ + {if isset($customer_data)} +

Datos del pagador:

+

Nombre: {$customer_data.first_name} {$customer_data.last_name}

+

Correo electrónico: {$customer_data.email}

+ + {/if} +{else} +

Lo sentimos, ha ocurrido un error durante el procesamiento de su pago.

+

Póngase en contacto con nosotros para obtener ayuda.

+{/if}