* @copyright 2005-2019 PayLane sp. z.o.o. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PayLane sp. z.o.o. */ require_once(dirname(__FILE__).'/../../core/core.php'); require_once(dirname(__FILE__).'/paymentStatus.php'); require_once(_PS_MODULE_DIR_ . 'paylane/paylane.php'); class Paylane3dsValidationModuleFrontController extends ModuleFrontController { protected $orderConfirmationUrl = 'index.php?controller=order-confirmation'; public function isOldPresta() { return version_compare(_PS_VERSION_, '1.7', '<'); } public function postProcess() { if ($this->isOldPresta()) { $this->postProcess16(); return; } $cartId = (int)Tools::getValue('cart_id'); PrestaShopLogger::addLog('process return url', 1, null, 'Cart', $cartId, true); $orderId = Order::getOrderByCartId($cartId); PrestaShopLogger::addLog('order id:', 1, null, 'Order', $orderId, true); $payment = Tools::getValue('payment_method'); if ($payment == 'CREDITCARD') { $payment = 'CreditCard'; } if (isset($payment)) { require_once(_PS_MODULE_DIR_ . 'paylane/class/' . $payment . '.php'); $paylane = new Paylane(); $handler = new $payment($paylane); try { $responseStatus = $this->getResponseStatus(); $result = $handler->handle3DSPayment($responseStatus); if ($result['success']) { $responseStatus['transaction_id'] = $result['id_sale']; if (isset($result['order_status'])) { $orderStatus = $result['order_status']; } else { $orderStatus = 'CLEARED'; } $responseStatus['paylane_status'] = $orderStatus; $responseStatus['status'] = PaylanePaymentCore::paymentStatus($responseStatus['paylane_status']); } else { $errorStatus = PaylanePaymentCore::getErrorMessage( array('error_text' => $result['error']['error_description']) ); $this->redirectError($errorStatus); } } catch (Exception $e) { $errorStatus = PaylanePaymentCore::getErrorMessage(array('error_text' => $e->getMessage())); $this->redirectError($errorStatus); } } else { $responseStatus = $this->getResponseStatus(); } //$responseStatus = $this->getResponseStatus(); //$result = $this->handle3DSPayment($responseStatus); PrestaShopLogger::addLog('Paylane - return url order ID:'. $orderId, 1, null, 'Cart', $cartId, true); $this->checkPaymentStatus($cartId, $responseStatus); //LK if ($orderId) { PrestaShopLogger::addLog('validate order', 1, null, 'Cart', $cartId, true); $this->validateOrder($cartId, $responseStatus['transaction_id']); } else { PrestaShopLogger::addLog('prestashop order not found', 1, null, 'Cart', $cartId, true); //$this->checkPaymentStatus($cartId, $responseStatus); //LK } } protected function getResponseStatus() { $responseStatus = array(); $responseStatus['paylane_status'] = Tools::getValue('status'); $responseStatus['status'] = PaylanePaymentCore::paymentStatus($responseStatus['paylane_status']); $responseStatus['amount'] = Tools::getValue('amount'); $responseStatus['currency'] = Tools::getValue('currency'); $responseStatus['description'] = Tools::getValue('description'); $responseStatus['hash'] = Tools::getValue('hash'); $responseStatus['transaction_id'] = Tools::getValue('id_sale'); $responseStatus['payment_method'] = (Tools::getValue('payment_method')) ? Tools::getValue('payment_method') : Tools::getValue('payment_type'); $responseStatus['error_code'] = Tools::getValue('error_code'); $responseStatus['error_text'] = Tools::getValue('error_text'); $responseStatus['id_3dsecure_auth'] = Tools::getValue('id_3dsecure_auth'); return $responseStatus; } protected function validateOrder($cartId, $transactionId) { $order = $this->module->getOrderByTransactionId($transactionId); PrestaShopLogger::addLog('transaction log order : '.print_r($order, true), 1, null, 'Cart', $cartId, true); if (empty($order) || empty($order['order_status'])) { PrestaShopLogger::addLog('Paylane - status url late', 1, null, 'Cart', $cartId, true); $this->checkPaymentStatus($cartId, $transactionId); } elseif ($order['order_status'] == $this->module->failedStatus) { $paymentResponse = unserialize($order['payment_response']); $errorStatus = PaylanePaymentCore::getErrorMessage($paymentResponse); $this->redirectError($errorStatus); } else { if ($this->context->cart->OrderExists() == false) { $responseStatus = $this->getResponseStatus(); PrestaShopLogger::addLog('Paylane - check order from return url', 1, null, 'Cart', $cartId, true); $this->checkPaymentStatus($cartId, $responseStatus); } else { PrestaShopLogger::addLog( 'Paylane - redirect success validate return url', 1, null, 'Cart', $cartId, true ); $this->redirectSuccess($cartId); } } } protected function checkPaymentStatus($cartId, $responseStatus) { $cart = $this->context->cart; $fieldParams = array(); PrestaShopLogger::addLog('Paylane - check Payment Status', 1, null, 'Cart', $cartId, true); PrestaShopLogger::addLog( 'Paylane - check payment status:'. print_r($responseStatus, true), 1, null, 'Cart', $cartId, true ); if (isset($responseStatus) && $responseStatus['status'] !== '-2') { $PaymentStatus = new PaylanePaymentStatusModuleFrontController(); $isTransactionLogValid = $PaymentStatus->isTransactionLogValid($responseStatus['transaction_id']); if (!$isTransactionLogValid) { $orderTotal = $responseStatus['amount']; $transactionLog = $PaymentStatus->setTransactionLog($orderTotal, $responseStatus); PrestaShopLogger::addLog('Paylane - transactionLog: '. print_r($transactionLog, true), 1, null, 'Cart', $cartId, true); $generatedMd5Sig = $this->module->generateMd5sig($responseStatus); $isPaymentSignatureEqualsGeneratedSignature = $this->module->isPaymentSignatureEqualsGeneratedSignature( $responseStatus['hash'], $generatedMd5Sig ); $generatedAntiFraudHash = $this->module->generateAntiFraudHash( $cartId, $responseStatus['payment_method'], $cart->date_add ); $isFraud = $this->module->isFraud($generatedAntiFraudHash, Tools::getValue('secure_method')); $additionalInformation = $PaymentStatus->getAdditionalInformation( $responseStatus, $isPaymentSignatureEqualsGeneratedSignature, $isFraud ); PrestaShopLogger::addLog( 'Paylane - save transaction log from return URL', 1, null, 'Cart', $cartId, true ); $PaymentStatus->saveTransactionLog($transactionLog, 0, $additionalInformation); $PaymentStatus->validatePayment($cartId, $responseStatus, $responseStatus['status']); } $this->redirectSuccess($cartId); } elseif (isset($responseStatus) && $responseStatus['status'] == '-2') { $PaymentStatus = new PaylanePaymentStatusModuleFrontController(); $currency = $this->context->currency; $customer = new Customer($cart->id_customer); $this->module->validateOrder( (int)$cart->id, $PaymentStatus->getPaymentStatus($responseStatus), $amount = sprintf('%01.2f', $cart->getOrderTotal()), $this->getPaymentName($responseStatus['payment_method']), null, array(), (int)$currency->id, false, $customer->secure_key ); $errorStatus = PaylanePaymentCore::getErrorMessage($responseStatus); $this->redirectError($errorStatus); } else { $this->redirectPaymentReturn(); } } protected function getPaymentName($paymentType) { $paymentMethod = PaylanePaymentCore::getPaymentMethodByPaymentType($paymentType); if ($this->module->l('PAYLANE_FRONTEND_PM_'.$paymentType) == 'PAYLANE_FRONTEND_PM_'.$paymentType) { $paymentName = $paymentMethod['name']; } else { $paymentName = $this->module->l('PAYLANE_FRONTEND_PM_'.$paymentType); } $isPaylane = strpos($paymentName, 'Paylane'); if ($isPaylane === false) { $paymentName = 'Paylane '.$paymentName; } return $paymentName; } protected function redirectError($returnMessage) { $this->errors[] = $returnMessage; $this->redirectWithNotifications($this->context->link->getPageLink('order', true, null, array( 'step' => '3'))); } protected function redirectPaymentReturn() { $url = $this->context->link->getModuleLink('paylane', 'paymentReturn', array( 'secure_key' => $this->context->customer->secure_key), true); PrestaShopLogger::addLog('rediret to payment return : '.$url, 1, null, 'Cart', $this->context->cart->id, true); Tools::redirect($url); exit; } protected function redirectSuccess($cartId) { Tools::redirect( $this->orderConfirmationUrl. '&id_cart='.$cartId. '&id_module='.(int)$this->module->id. '&key='.$this->context->customer->secure_key ); } public function postProcess16() { if (method_exists('Tools', 'getAllValues')) { $params = Tools::getAllValues(); } else { $params = $_POST + $_GET; } if (isset($params['payment']) && isset($params['payment']['additional_information'])) { $paymentParams = $params['payment']['additional_information']; } else { $paymentParams = null; } $idSale = null; $orderStatus = Configuration::get('PAYLANE_PAYMENT_STATUS_FAILED'); $displayName = $this->module->displayName; if (!isset($params['payment_type'])){ $params['payment_type'] = 'CreditCard'; } if (isset($params['payment_type'])) { require_once(_PS_MODULE_DIR_ . 'paylane/class/' . $params['payment_type'] . '.php'); $paylane = Module::getInstanceByName('paylane'); $paymentParams = $params; $handler = new $params['payment_type']($paylane); $result = $handler->handle3DSPayment($paymentParams); if ($result['success']) { $idSale = $result['id_sale']; if (isset($result['order_status'])) { $orderStatus = $result['order_status']; } else { $orderStatus = Configuration::get('PS_OS_PAYMENT'); } } $paymentLabelPath = 'paylane_' . Tools::strtolower($params['payment_type']) . '_label'; $displayName .= ' | ' . Configuration::get($paymentLabelPath); } $cart = $this->context->cart; if (!$this->module->checkCurrency($cart)) { Tools::redirect('index.php?controller=order'); } $customer = new Customer($cart->id_customer); $currency = $this->context->currency; $amount = sprintf('%01.2f', $cart->getOrderTotal()); $extraVars = null; if (!is_null($idSale)) { $extraVars = array( 'transaction_id' => $idSale ); } if (!Validate::isLoadedObject($customer)) { Tools::redirect('index.php?controller=order&step=1'); } $this->module->validateOrder( (int)$cart->id, $orderStatus, $amount, $displayName, null, $extraVars, (int)$currency->id, false, $customer->secure_key ); $redirectUrl = 'index.php?controller=order-confirmation&id_cart='; $redirectUrl .= (int)$cart->id.'&id_module='.(int)$this->module->id; $redirectUrl .= '&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key; Tools::redirect($redirectUrl); } }