* @copyright 1997-2016 Quadra Informatique * @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ require_once(_PS_MODULE_DIR_.'be2bill/be2bill.php'); require_once(_PS_MODULE_DIR_.'be2bill/classes/Be2billAlias.php'); require_once(_PS_MODULE_DIR_.'be2bill/classes/Be2billExeccode.php'); require_once(_PS_MODULE_DIR_.'be2bill/classes/Be2billExeccodeHistory.php'); require_once(_PS_MODULE_DIR_.'be2bill/classes/Be2billTransaction.php'); require_once(_PS_MODULE_DIR_.'be2bill/models/MerchandConfiguration.php'); class Be2billNotification { /** * Manage the payment * @return message if something went wrong */ public function notify($module_name = 'be2bill', $id_account = 0, $option_code = 'standard') { if (!Tools::getIsset('ORDERID') || !Tools::getIsset('EXECCODE') || !Tools::getIsset('HASH')) { die('Missing parameters (ORDERID, EXECCODE and/or HASH)!'); } // Log payment platform return if ((int)Configuration::getGlobalValue('BE2BILL_LOGS')) { $this->logPaymentPlatformReturn(); } // Hash verification if (!$this->validateHash()) { die('Corrupt received data!'); } // Get first order by reference (ORDERID) // if the orderid is an int, then it's means that it's an order from the module 1.0 if (is_int(Tools::getValue('ORDERID')) == true) { $id_cart = Tools::getValue('ORDERID'); } else { $id_cart = Be2billOrderReference::getIdCartByReference(Tools::getValue('ORDERID')); } // For older orders Be2bill (they aren't in the new table) if (empty($id_cart)) { $order = Order::getByReference(Tools::getValue('ORDERID'))->getFirst(); $id_cart = $order->id_cart; } $cart = new Cart($id_cart); Context::getContext()->cart = $cart; if (empty($cart->id)) { die('Cart not found!'); } $is_already_order = Be2billOrderReference::isOrderByReference(Tools::getValue('ORDERID')); $this->module_name = $module_name; switch (Tools::getValue('OPERATIONTYPE')) { case Be2billTransaction::TYPE_PAYMENT: if ($option_code == MerchandConfigurationOptions::TYPE_NTIMES) { if ($is_already_order == true) { $this->paymentNtimes($cart, $id_account); } else { $this->createOrderNtimes($cart, $id_account); } } else { if ($is_already_order == true) { $this->payment($cart, $id_account); } else { $this->createOrder($cart, $id_account); } } break; case Be2billTransaction::TYPE_AUTHORIZATION: if ($is_already_order == true) { $this->authorize($cart, $id_account); } else { if ($option_code == MerchandConfigurationOptions::TYPE_DELIVERY) { $this->createOrderDelivery($cart, $id_account); } else { $this->createOrderDefered($cart, $id_account); } } break; case Be2billTransaction::TYPE_CAPTURE: $this->capture($id_account); break; case Be2billTransaction::TYPE_REFUND: $this->refund($id_account); break; } } /** * set schedule payment in ntimes * * @return void */ protected function paymentNtimes($cart, $id_account) { $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $cart->id_currency); $id_order = Order::getOrderByCartId($cart->id); $order = new Order((int)$id_order); // Payment success if (Tools::getValue('EXECCODE') == '0000') { if ($order->current_state == _PS_OS_ERROR_) { $order->setCurrentState((int)Configuration::get('BE2BILLNX_OS_PAYMENT')); } $this->addOrderPayment($order, $amount, $id_account); $this->addSchedule($order); } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$order->id_customer, $id_account); } /** * Create the order or add the payment */ protected function payment($cart, $id_account) { if (!$cart) { return; } $be2bill_amount = Tools::getValue('AMOUNT') / 100; $id_order = Order::getOrderByCartId((int)$cart->id); $order = new Order((int)$id_order); $amount = $this->getConvertedPrice($be2bill_amount, $order->id_currency); // Payment success if (Tools::getValue('EXECCODE') === '0000' && $order->current_state === _PS_OS_ERROR_) { $this->addOrderPayment($order, $amount, $id_account); if ($order->total_paid_real == $order->total_paid) { $order->setCurrentState(_PS_OS_PAYMENT_); } } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$cart->id_customer, $id_account); } /** * Create the order */ protected function createOrder($cart, $id_account) { if (!$cart) { return; } $payment_account = new MerchandConfiguration($id_account); $name = Module::getModuleName($this->module_name); if ($payment_account->id) { $name = $payment_account->b2b_xml_account_type_code; } $module = Module::getInstanceByName($this->module_name); $this->context = Context::getContext(); $customer = new Customer($cart->id_customer); $currency = new Currency($cart->id_currency); $this->context->cart = $cart; $this->context->currency = $currency; $this->context->customer = $customer; $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $currency->id); if (empty($module)) { die('No module founded!'); } // Payment success if (Tools::getValue('EXECCODE') == '0000' && $amount == $this->context->cart->getOrderTotal()) { $id_state = _PS_OS_PAYMENT_; if (empty($id_state)) { die('No Payment Statut Found!'); } $module->validateOrder( (int)$cart->id, $id_state, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); $id_order = Be2billApi::getOrderByCartId($cart->id, $cart->id_shop); $order = new Order((int)$id_order); } elseif (Tools::getValue('EXECCODE') == '0000' && $amount != $this->context->cart->getOrderTotal()) { $module->validateOrder( (int)$cart->id, (int)_PS_OS_ERROR_, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); $id_order = Be2billApi::getOrderByCartId($cart->id, $cart->id_shop); $order = new Order((int)$id_order); } else { // Payment error, create the order as error payment and duplicate the customer cart $module->validateOrder( (int)$cart->id, (int)_PS_OS_ERROR_, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); // Duplicate the cart if error $new_cart = $cart->duplicate(); Context::getContext()->cart = $new_cart; $id_order = Be2billApi::getOrderByCartId($cart->id, $cart->id_shop); $order = new Order((int)$id_order); } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$cart->id_customer, $id_account); } /** * Create the order or add the payment in n times */ protected function createOrderNtimes($cart, $id_account) { $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $cart->id_currency); //$transaction_id = (Tools::getIsset('TRANSACTIONID')) ? Tools::getValue('TRANSACTIONID') : null; $customer = new Customer($cart->id_customer); $payment_account = new MerchandConfiguration($id_account); $name = Module::getModuleName($this->module_name); if ($payment_account->id) { $name = $payment_account->b2b_xml_account_type_code; } $module = Module::getInstanceByName($this->module_name); // Payment success if (Tools::getValue('EXECCODE') == '0000') { $id_state = Configuration::get('BE2BILLNX_OS_PAYMENT'); $context = Context::getContext(); // !IMPORTANT! Sometime, the currency context is deleted.. $context->currency = new Currency($cart->id_currency); $be2bill_amount = $cart->getOrderTotal(); if (empty($id_state)) { die('No Payment Statut Found!'); } $module->validateOrder( (int)$cart->id, $id_state, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); $id_order = Order::getOrderByCartId($cart->id); $order = new Order((int)$id_order); // Register schedules if ($order->module == 'be2bill') { $be2bill_schedules = Be2billSchedule::getByIdOrder((int)$order->id); // Create schedules if (!count($be2bill_schedules)) { $iterator = 1; $schedules = Be2billSchedule::getPaymentSchedule($order->getOrdersTotalPaid() * 100, $id_account); foreach ($schedules as $date => $scheduled_amount) { if ($iterator == 1) { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, $date, 1, MerchandConfigurationOptions::TYPE_NTIMES ); } else { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, null, 0, MerchandConfigurationOptions::TYPE_NTIMES ); } $iterator++; } } } // Add payment // $use_existing_payment = false; if (Tools::version_compare(_PS_VERSION_, '1.5.5.0', '>=') || $order->module == 'be2bill') { if ($order->total_paid_tax_incl > $order->total_paid_real) { //$use_existing_payment = true; if ($amount > $order->total_paid_tax_incl) { $this->addOrderPayment($order, $order->total_paid_tax_incl, $id_account); $amount -= $order->total_paid_tax_incl; } else { $this->addOrderPayment($order, $amount, $id_account); $amount = 0; } } } } else { // Payment error $module->validateOrder( (int)$cart->id, (int)_PS_OS_ERROR_, $be2bill_amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); // Duplicate the cart if error $new_cart = $cart->duplicate(); Context::getContext()->cart = $new_cart; $id_order = Order::getOrderByCartId($cart->id); $order = new Order((int)$id_order); // Register schedules if ($order->module == 'be2bill') { $be2bill_schedules = Be2billSchedule::getByIdOrder((int)$order->id); // Create schedules if (!count($be2bill_schedules)) { $iterator = 1; $schedules = Be2billSchedule::getPaymentSchedule($order->getOrdersTotalPaid() * 100, $id_account); foreach ($schedules as $date => $scheduled_amount) { if ($iterator == 1) { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, null, 0, MerchandConfigurationOptions::TYPE_NTIMES ); } else { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, null, 0, MerchandConfigurationOptions::TYPE_NTIMES ); } $iterator++; } } } } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$order->id_customer, $id_account); } /** * Create the Order * @param type $cart */ protected function createOrderDefered($cart, $id_account) { $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $cart->id_currency); $customer = new Customer($cart->id_customer); $payment_account = new MerchandConfiguration($id_account); $name = Module::getModuleName($this->module_name); if ($payment_account->id) { $name = $payment_account->b2b_xml_account_type_code; } $module = Module::getInstanceByName($this->module_name); // Payment success if (Tools::getValue('EXECCODE') == '0000') { $id_state = Configuration::get('BE2BILLDEF_OS_PAYMENT'); $context = Context::getContext(); // !IMPORTANT! Sometime, the currency context is deleted.. $context->currency = new Currency($cart->id_currency); $be2bill_amount = $cart->getOrderTotal(); if (empty($id_state)) { die('No Payment Statut Found!'); } $module->validateOrder( (int)$cart->id, $id_state, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); $id_order = Order::getOrderByCartId($cart->id); $order = new Order((int)$id_order); //Schedule capture // set to default value 7 $days = 7; if ($id_account) { $days = MerchandConfiguration::getDeferedDays($id_account); } $date_scheduled = null; $datetime = new DateTime(); $datetime->add(new DateInterval('P'.(int)$days.'D')); $date_scheduled = $datetime->format('Y-m-d H:i:s'); $this->registerSchedule( 0, (int)$order->id, (float)$amount, $date_scheduled, null, 0, MerchandConfigurationOptions::TYPE_DEFERED ); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Change order status $this->changeOrderState( (int)$brother_order->id, (int)Configuration::get(Tools::strtoupper($brother_order->module).'_OS_PAYMENT') ); } } else { // Payment error $module->validateOrder( (int)$cart->id, (int)_PS_OS_ERROR_, $be2bill_amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); // Duplicate the cart if error $new_cart = $cart->duplicate(); Context::getContext()->cart = $new_cart; $id_order = Order::getOrderByCartId($cart->id); $order = new Order((int)$id_order); // Register schedules if ($order->module == 'be2bill') { $be2bill_schedules = Be2billSchedule::getByIdOrder((int)$order->id); // Create schedules if (!count($be2bill_schedules)) { $iterator = 1; $schedules = Be2billSchedule::getPaymentSchedule($order->getOrdersTotalPaid() * 100, $id_account); foreach ($schedules as $date => $scheduled_amount) { if ($iterator == 1) { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, null, 0, MerchandConfigurationOptions::TYPE_DEFERED ); } else { $this->registerSchedule( 0, (int)$order->id, (float)$scheduled_amount / 100, $date, null, 0, MerchandConfigurationOptions::TYPE_DEFERED ); } $iterator++; } } } } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$order->id_customer, $id_account); } /** * Create the delivery order */ protected function createOrderDelivery($cart, $id_account) { $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $cart->id_currency); //$transaction_id = (Tools::getIsset('TRANSACTIONID')) ? Tools::getValue('TRANSACTIONID') : null; $customer = new Customer($cart->id_customer); $payment_account = new MerchandConfiguration($id_account); $name = Module::getModuleName($this->module_name); if ($payment_account->id) { $name = $payment_account->b2b_xml_account_type_code; } $module = Module::getInstanceByName($this->module_name); // Payment success if (Tools::getValue('EXECCODE') == '0000') { $id_state = Configuration::get('BE2BILLSHIP_OS_PAYMENT'); $context = Context::getContext(); // !IMPORTANT! Sometime, the currency context is deleted.. $context->currency = new Currency($cart->id_currency); $be2bill_amount = $cart->getOrderTotal(); if (empty($id_state)) { die('No Payment Statut Found!'); } $module->validateOrder( (int)$cart->id, $id_state, $amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); $id_order = Be2billApi::getOrderByCartId($cart->id, $cart->id_shop); $order = new Order((int)$id_order); //Schedule capture $date_scheduled = null; $this->registerSchedule( 0, (int)$order->id, (float)$amount, $date_scheduled, null, 0, MerchandConfigurationOptions::TYPE_DELIVERY ); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Change order status $this->changeOrderState( (int)$brother_order->id, (int)Configuration::get(Tools::strtoupper($brother_order->module).'_OS_PAYMENT') ); } } else { // Payment error $module->validateOrder( (int)$cart->id, (int)_PS_OS_ERROR_, $be2bill_amount, $name, null, null, (int)$cart->id_currency, false, $customer->secure_key ); // Duplicate the cart if error $new_cart = $cart->duplicate(); Context::getContext()->cart = $new_cart; $id_order = Be2billApi::getOrderByCartId($cart->id, $cart->id_shop); $order = new Order((int)$id_order); } // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Register alias $this->registerAlias((int)$order->id_customer, $id_account); } /** * Create the authorization */ protected function authorize($cart, $id_account) { $id_order = Order::getOrderByCartId((int)$cart->id); $order = new Order((int)$id_order); $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $order->id_currency); // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Authorization success if (Tools::getValue('EXECCODE') == '0000') { //Schedule capture $date_scheduled = null; // set to default value 7 $days = 7; if ($id_account) { $days = MerchandConfiguration::getDeferedDays($id_account); } $datetime = new DateTime(); $datetime->add(new DateInterval('P'.(int)$days.'D')); $date_scheduled = $datetime->format('Y-m-d H:i:s'); $this->registerSchedule( 0, (int)$order->id, (float)$amount, $date_scheduled, null, 0, MerchandConfigurationOptions::TYPE_DEFERED ); // Change order status $this->changeOrderState( (int)$order->id, (int)Configuration::get(Tools::strtoupper($order->module).'_OS_PAYMENT') ); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Change order status $this->changeOrderState( (int)$brother_order->id, (int)Configuration::get(Tools::strtoupper($brother_order->module).'_OS_PAYMENT') ); } } else { // Authorization error // Change order status $this->changeOrderState((int)$order->id, (int)_PS_OS_ERROR_); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Change order status $this->changeOrderState((int)$brother_order->id, (int)_PS_OS_ERROR_); } } } /** * Add the capture */ protected function capture($id_account) { //$extradata = explode(' ', Tools::getValue('EXTRADATA')); $refund_info = explode(':', Tools::getValue('EXTRADATA')); $extradata = explode(' ', $refund_info[1]); if (!count($extradata)) { return false; } $id_order = 0; $id_schedule = 0; foreach ($extradata as $data) { if ($res = preg_split('/#/', $data)) { if ($res[0] == 'ORDER') { $id_order = (int)$res[1]; } elseif ($res[0] == 'SCHEDULE') { $id_schedule = (int)$res[1]; } } } // if empty extradata, means it come from Be2bill if (!$id_order || !$id_schedule) { $id_cart = Be2billOrderReference::getIdCartByReference(Tools::getValue('ORDERID')); // For older orders Be2bill (they aren't in the table be2bill_orderreference) if (empty($id_cart)) { $order = Order::getByReference(Tools::getValue('ORDERID'))->getFirst(); $id_cart = $order->id_cart; } $cart = new Cart($id_cart); $id_order = Order::getOrderByCartId((int)$cart->id); $order = new Order((int)$id_order); $id_schedule = Be2billSchedule::getNextOrderIdScheduleToCapture($order->reference); } $order = new Order((int)$id_order); if ($order->id) { $be2bill_amount = Tools::getValue('AMOUNT') / 100; $amount = $this->getConvertedPrice($be2bill_amount, $order->id_currency); // Register transaction $this->registerTransaction((int)$order->id, $id_account); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); if (Tools::getValue('EXECCODE') == '0000') { // Update schedule $datetime = new DateTime(); $this->registerSchedule( (int)$id_schedule, (int)$order->id, (float)$amount, null, $datetime->format('Y-m-d H:i:s'), 1, MerchandConfigurationOptions::TYPE_DEFERED ); // Add payment if ($order->total_paid_tax_incl > $order->total_paid_real) { if ($amount > $order->total_paid_tax_incl) { $this->addOrderPayment($order, $order->total_paid_tax_incl, $id_account); $amount -= $order->total_paid_tax_incl; } else { $this->addOrderPayment($order, $amount, $id_account); $amount = 0; } } // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Add payment if ($amount > 0 && $brother_order->total_paid_tax_incl > $brother_order->total_paid_real) { if ($amount > $brother_order->total_paid_tax_incl) { $this->addOrderPayment($brother_order, $brother_order->total_paid_tax_incl, $id_account); $amount -= $brother_order->total_paid_tax_incl; } else { $this->addOrderPayment($brother_order, $amount, $id_account); $amount = 0; } } } } else { // Payment error // Update schedule $datetime = new DateTime(); $this->registerSchedule( (int)$id_schedule, (int)$order->id, (float)$amount, null, $datetime->format('Y-m-d H:i:s'), 0, MerchandConfigurationOptions::TYPE_DEFERED ); // Change order status $this->changeOrderState((int)$order->id, (int)_PS_OS_ERROR_); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); // Change order status $this->changeOrderState((int)$brother_order->id, (int)_PS_OS_ERROR_); } } } } /** * Treat refund notification */ protected function refund($id_account) { $id_order_slip = 0; $reference = Be2billOrderReference::getReferenceByBe2billReference(Tools::getValue('ORDERID')); if (!Tools::isEmpty(Tools::getValue('EXTRADATA')) && Tools::getValue('EXTRADATA') != '') { $id_order = 0; $refund_info = explode(':', Tools::getValue('EXTRADATA')); $extradata = explode(' ', $refund_info[1]); foreach ($extradata as $data) { if ($res = preg_split('/#/', $data)) { if ($res[0] == 'ORDER') { $id_order = (int)$res[1]; } elseif ($res[0] == 'SLIP') { $id_order_slip = (int)$res[1]; } } } if (!$id_order || !$id_order_slip) { return; } $order = new Order((int)$id_order); } else { $order = Order::getByReference($reference)->getFirst(); } if ($order->id) { // Register transaction $this->registerTransaction((int)$order->id, $id_account, (int)$id_order_slip); // Register execode history $this->registerExeccodeHistory((int)Db::getInstance()->Insert_ID()); // Add private message $this->addNewPrivateMessage((int)$order->id); // Treat brother (multishipping) foreach ($order->getBrother() as $brother_order) { // Add private message $this->addNewPrivateMessage((int)$brother_order->id); } } } /** * Change order state * @param int $id_order * @param int $id_state * @return boolean */ protected function changeOrderState($id_order, $id_state, $use_existing_payment = false) { if (!(bool)$id_order || !(bool)$id_state) { return false; } $order = new Order($id_order); if ($order->getCurrentState() != Configuration::get('BE2BILL_OS_PENDING') && $order->getCurrentState() != _PS_OS_ERROR_) { return true; } $history = new OrderHistory(); $history->id_order = (int)$id_order; $history->changeIdOrderState((int)$id_state, $history->id_order, $use_existing_payment); $history->addWithemail(); return $history->save(); } /** * Add private message to order * @param int $id_order * @return boolean */ protected function addNewPrivateMessage($id_order) { if (!(bool)$id_order) { return false; } // Format private message $data = $_GET; unset($data['fc']); unset($data['module']); unset($data['controller']); unset($data['id_lang']); $message = Be2billExeccode::getMessageByExeccode($data['EXECCODE'])."\n"; foreach ($data as $key => $value) { $message .= $key.': '.$value."\n"; } $new_message = new Message(); $message = strip_tags($message, '
'); if (!Validate::isCleanHtml($message)) { $message = $this->l('Payment message is not valid, please check your module.'); } $new_message->message = $message; $new_message->id_order = (int)$id_order; $new_message->private = 1; return $new_message->add(); } /** * Save the be2bill transaction * @param int $id_order * @param int $id_slip * @return boolean */ protected function registerTransaction($id_order, $id_account = 0, $id_slip = 0) { if (!(bool)$id_order) { return false; } $already_exist = Be2billTransaction::getByTransactionId(Tools::getValue('TRANSACTIONID')); if (empty($already_exist)) { $transaction = new Be2billTransaction(); $transaction->id_order = (int)pSQL($id_order); $transaction->id_order_slip = (int)pSQL($id_slip); $transaction->transaction_id = pSQL(Tools::getValue('TRANSACTIONID')); $transaction->transaction_type = pSQL(Tools::strtoupper(Tools::getValue('OPERATIONTYPE'))); $transaction->amount = (float)pSQL(Tools::getValue('AMOUNT') / 100); $transaction->id_merchand_account = (int)$id_account; } else { die('Transaction: '.Tools::getValue('TRANSACTIONID').' already exist!'); } return $transaction->save(); } /** * Save the be2bill execcode history * @param int $id_transaction * @return boolean */ protected function registerExeccodeHistory($id_transaction) { if (!(bool)$id_transaction) { return false; } $history = new Be2billExeccodeHistory(); $history->id_b2b_execcode = pSQL(Tools::getValue('EXECCODE')); $history->id_b2b_transaction = (int)pSQL($id_transaction); return $history->save(); } /** * Save the be2bill alias of the customer * @param int $id_customer * @return boolean */ protected function registerAlias($id_customer, $id_account) { // Register alias if necessary $alias_value = Tools::getValue('ALIAS'); $card_type = preg_replace('/\+/', ' ', Tools::getValue('CARDTYPE')); $card_code = Tools::getValue('CARDCODE'); $cvv_card = Tools::getValue('CARDVALIDITYDATE'); if (isset($alias_value) && !empty($alias_value) && isset($card_type) && isset($card_code) && isset($cvv_card)) { $id_alias = 0; $card_exp = explode('-', $cvv_card); $datetime = new DateTime(); $result = Be2billAlias::getByIdCustomer((int)$id_customer, (int)$id_account); if ($result) { $id_alias = $result['id_b2b_alias']; if ($alias_value == $result['alias']) { return true; } } $alias = new Be2billAlias((int)$id_alias); $alias->id_customer = (int)pSQL($id_customer); $alias->id_merchand_account = (int)pSQL($id_account); $alias->alias = pSQL($alias_value); $alias->card_type = pSQL($card_type); $alias->card_number = pSQL($card_code); $alias->date_add = pSQL($datetime->format('Y-m-d H:i:s')); $alias->date_end = pSQL($card_exp[1].'-'.$card_exp[0].'-01'); return $alias->save(); } return true; } /** * Create the schedule for defered payment */ protected function registerSchedule( $id_be2bill_schedule, $id_order, $amount, $date_scheduled = null, $date_done = null, $is_done = 0, $option = '' ) { if (!(bool)$id_order) { return false; } $schedule = new Be2billSchedule((int)$id_be2bill_schedule); $schedule->id_order = (int)pSQL($id_order); $schedule->transaction_id = pSQL(Tools::getValue('TRANSACTIONID')); $schedule->is_done = (int)pSQL($is_done); $schedule->option = pSQL($option); if ($amount > 0) { $schedule->amount = (float)pSQL($amount); } if (Tools::getIsset('SCHEDULEID')) { $schedule->schedule_id = pSQL(Tools::getValue('SCHEDULEID')); } if (!Tools::isEmpty($date_scheduled)) { $schedule->date_scheduled = pSQL($date_scheduled); } if (!Tools::isEmpty($date_done)) { $schedule->date_done = pSQL($date_done); } return $schedule->save(); } /** * add the payment */ protected function addOrderPayment($order, $amount, $id_account = 0) { $currency = new Currency((int)$order->id_currency); $datetime = new DateTime(); $module = Module::getInstanceByName($order->module); $transaction_id = (Tools::getIsset('TRANSACTIONID')) ? Tools::getValue('TRANSACTIONID') : null; $payment_account = new MerchandConfiguration($id_account); $name = $module->displayName; if ($payment_account->id) { $name = $payment_account->b2b_xml_account_type_code; } if (!$order->hasInvoice()) { $order->setInvoice(false); } $invoice = null; foreach ($order->getInvoicesCollection() as $inv) { if ($inv->isPaid()) { continue; } $invoice = $inv; break; } foreach ($order->getOrderPayments() as $payment) { if ($payment->transaction_id == $transaction_id) { die('Payment already added'); } } $order->addOrderPayment( $amount, $name, $transaction_id, $currency, $datetime->format('Y-m-d H:i:s'), $invoice ); } /** * add the payment schedule */ protected function addSchedule($order) { // Register schedules if ($order->module == 'be2bill') { $be2bill_schedules = Be2billSchedule::getByIdOrder((int)$order->id); // Create schedules if (count($be2bill_schedules)) { foreach ($be2bill_schedules as $schedule) { if ($schedule['is_done'] == 1) { continue; } $schedule_id = $schedule['id_be2bill_schedule']; $datetime = new DateTime(); $this->registerSchedule( (int)$schedule_id, (int)$order->id, 0, null, $datetime->format('Y-m-d H:i:s'), 1, MerchandConfigurationOptions::TYPE_NTIMES ); break; } } } } /** * Log payment return */ private function logPaymentPlatformReturn() { // Unset prestashop data $data = $_GET; unset($data['fc']); unset($data['module']); unset($data['controller']); unset($data['id_lang']); // Log data for debug if ($data['EXECCODE'] == '0000') { Be2bill::log('[OK] '.Tools::strtoupper(Be2billExeccode::getMessageByExeccode($data['EXECCODE']))); } else { Be2bill::log('[KO] '.Tools::strtoupper(Be2billExeccode::getMessageByExeccode($data['EXECCODE']))); } $message = array(); foreach ($data as $key => $value) { $message[] = $key.': '.$value; } Be2bill::log('[INFO] '.implode($message, "\n\t\t\t\t\t\t")); } /** * Validate hash data * @return boolean */ private function validateHash() { $parameters = $_GET; // Unset prestashop data unset($parameters['fc']); unset($parameters['module']); unset($parameters['controller']); unset($parameters['id_lang']); unset($parameters['isolang']); // Extract hash value $hash = $parameters['HASH']; unset($parameters['HASH']); if (isset($parameters['EXTRADATA'])) { $account = $parameters['EXTRADATA']; $account = explode('-', $account); } if (!$account) { $id_account = (int)MerchandConfiguration::getIdByReference($parameters['ORDERID']); $option = $parameters['OPERATIONTYPE']; } else { $id_account = $account[1]; $option = $account[2]; } // Validate hash return ($hash == Be2billApi::buildHash($parameters, $id_account)); } /** * Convert be2bill amount in order currency * @param float $price * @param int $order_currency * @return float */ public function getConvertedPrice($price, $order_currency) { /* not implented yet */ return $price; /*$id_currency = Currency::getIdByIsoCode('EUR'); if ($order_currency == $id_currency) { return $price; } if ($id_currency == (int)Configuration::get('PS_CURRENCY_DEFAULT')) { return Tools::ps_round(Tools::convertPrice($price, $order_currency, true), 2); } else { return Tools::ps_round(Tools::convertPrice($price, $id_currency, false), 2); }*/ } }