307 lines
10 KiB
PHP
307 lines
10 KiB
PHP
<?php
|
|
|
|
use KupShop\AdminBundle\Util\ActivityLog;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\OrderingBundle\Exception\PaymentException;
|
|
use KupShop\OrderingBundle\Util\Order\OrderInfo;
|
|
|
|
$main_class = 'OrderPayment';
|
|
|
|
class OrderPayment extends Window
|
|
{
|
|
use DatabaseCommunication;
|
|
|
|
protected $tableName = 'order_payments';
|
|
protected $nameField = 'note';
|
|
protected $defaults = ['method' => '6'];
|
|
|
|
public function get_vars()
|
|
{
|
|
$vars = parent::get_vars();
|
|
$acn = getVal('acn');
|
|
|
|
if ($acn != 'add') {
|
|
$vars['body']['closed'] = $this->isClosed($vars['body']['data']['date']);
|
|
|
|
$vars['body']['data']['data'] = $vars['body']['data']['payment_data'];
|
|
|
|
$this->unserializeCustomData($vars['body']['data']);
|
|
|
|
if (!empty($vars['body']['data']['data'])) {
|
|
$vars['body']['data']['transactionID'] = $vars['body']['data']['data']['transactionID'] ?? $vars['body']['data']['data']['session'] ?? '';
|
|
}
|
|
}
|
|
|
|
$vars['admins'] = $this->getQueryBuilder()->select('*')->from('admins')->execute()->fetchAll();
|
|
|
|
global $adminID;
|
|
|
|
$vars['login_admin'] = $adminID;
|
|
|
|
if (findModule(\Modules::EET)) {
|
|
$vars['body']['eet'] = sqlQueryBuilder()->select('*')->from('eet_sent_payment')
|
|
->where(\Query\Operator::equals(['id_payment' => $this->getID()]))
|
|
->execute()->fetch();
|
|
}
|
|
|
|
if (findModule(\Modules::SKEET)) {
|
|
$vars['body']['skeet']['methods'] = [
|
|
\Payment::METHOD_EET_INVOICE_CASH => 'Úhrada faktury - hotově',
|
|
\Payment::METHOD_EET_INVOICE_CARD => 'Úhrada faktury - kartou',
|
|
];
|
|
}
|
|
|
|
$vars['type'] = 'orderPayment';
|
|
|
|
if (findModule('replacement') && getVal('replacement')) {
|
|
$order = new Order();
|
|
$order->createFromDB($vars['body']['data']['id_order']);
|
|
$originalOrderId = $order->getData('replacement');
|
|
$orderInfo = ServiceContainer::getService(OrderInfo::class);
|
|
if ($orderInfo->isOrderInPerson($originalOrderId)) {
|
|
$vars['body']['data']['method'] = Payment::METHOD_CASH;
|
|
}
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
|
|
public function getData()
|
|
{
|
|
$data = parent::getData();
|
|
if (getVal('Submit')) {
|
|
$data['date'] = $this->prepareDateTime($data['date']);
|
|
|
|
if (!empty($data['inserted'])) {
|
|
$data['price'] = $this->preparePrice($data['inserted']);
|
|
} elseif (!empty($data['choosed'])) {
|
|
$data['price'] = $this->preparePrice($data['choosed']) * (-1);
|
|
}
|
|
|
|
if (empty($data['id_order'])) {
|
|
$data['id_order'] = null;
|
|
}
|
|
|
|
if (!empty($data['price']) && empty($data['method'])) {
|
|
$data['method'] = ($data['price'] > 0) ? 4 : 5;
|
|
}
|
|
|
|
$this->serializeCustomData($data);
|
|
$data['payment_data'] = $data['data'];
|
|
unset($data['data']);
|
|
} elseif ($return_payment_id = getVal('return_payment_id', $data)) {
|
|
if ($orderPayment = $this->getOrderPayment($return_payment_id)) {
|
|
$data['note'] = "Vrácení platby: objednávka {$orderPayment['order']->order_no}, číslo platby: {$return_payment_id}";
|
|
$data['method'] = $orderPayment['method'];
|
|
$data['price'] = $orderPayment['price'] * (-1);
|
|
$data['return_payment'] = $orderPayment;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function createObject()
|
|
{
|
|
$data = parent::createObject();
|
|
|
|
if (empty($data['date'])) {
|
|
$data['date'] = date('d-m-Y H:i:s');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function handleReturn()
|
|
{
|
|
$paymentId = getVal('payment_id');
|
|
$this->setID($paymentId);
|
|
|
|
if (!$paymentId) {
|
|
$this->action = 'add';
|
|
$this->returnOK();
|
|
}
|
|
|
|
if (!($orderPayment = $this->getOrderPayment($paymentId))) {
|
|
$this->action = 'add';
|
|
$this->returnOK();
|
|
}
|
|
|
|
if (!$orderPayment['autoReturn']) {
|
|
$this->action = 'add';
|
|
$this->handle();
|
|
}
|
|
|
|
$order = $orderPayment['order'];
|
|
$data = $this->getData();
|
|
|
|
$contextManager = ServiceContainer::getService(ContextManager::class);
|
|
$contextManager->activateOrder($order, function () use ($order, $orderPayment, $data, $paymentId) {
|
|
$newId = null;
|
|
$returnPrice = $data['price'];
|
|
try {
|
|
$payment_class = $orderPayment['payment_class'];
|
|
$payment_class->loadConfig($order->getLanguage());
|
|
$payment_class->setOrder($order);
|
|
$result = $payment_class->returnPayment($returnPrice, $paymentId, $newId);
|
|
$message = translate('returnSucceed', 'orderPayment');
|
|
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_COMMUNICATION, $message,
|
|
['amount' => $returnPrice, 'payment_id' => $paymentId, 'order_id' => $order->id, 'result' => $result]);
|
|
$newId = $order->insertPayment($returnPrice, $data['note'], null, true, $orderPayment['method']);
|
|
$result['paymentClass'] = $payment_class->class;
|
|
$session = $orderPayment['payment_data']['session'] ?? '';
|
|
$result['session'] = ($result['id'] ?? $session).'_'.$newId;
|
|
$this->updateSQL('order_payments', ['payment_data' => json_encode($result)], ['id' => $newId]);
|
|
$order->updatePayments();
|
|
} catch (PaymentException $e) {
|
|
if ($e->getShortMessage() == 'Adyen') {
|
|
// the refund request was successfully received by Adyen - no result yet,
|
|
// because you receive the outcome of the refund request asynchronously, in a REFUND webhook.
|
|
addActivityLog(ActivityLog::SEVERITY_SUCCESS, ActivityLog::TYPE_COMMUNICATION, $e->getMessage(),
|
|
['amount' => $returnPrice, 'payment_id' => $paymentId, 'order_id' => $order->id]);
|
|
$this->returnOK($e->getMessage());
|
|
}
|
|
|
|
$this->returnError($e->getMessage(), true, $newId);
|
|
}
|
|
});
|
|
|
|
$this->action = 'add';
|
|
$this->activityMessage($data[$this->nameField]);
|
|
|
|
$this->returnOK();
|
|
}
|
|
|
|
public function handleUpdate()
|
|
{
|
|
$SQL = parent::handleUpdate();
|
|
$data = $this->getData();
|
|
if ($SQL && !empty($data['id_order'])) {
|
|
$order = new Order();
|
|
$order->createFromDB($data['id_order']);
|
|
$order->updatePayments();
|
|
}
|
|
|
|
return $SQL;
|
|
}
|
|
|
|
public function handleDelete()
|
|
{
|
|
$ID = $this->getID();
|
|
|
|
if (!findRight('ORDER')) {
|
|
redirect('launch.php?s=error.php&id=1');
|
|
}
|
|
|
|
$id_order = returnSQLResult('SELECT id_order FROM '.getTableName('order_payments')." WHERE id='{$ID}'");
|
|
|
|
// smazat
|
|
sqlQuery('DELETE FROM '.getTableName('order_payments')." WHERE id='{$ID}' ");
|
|
|
|
if (!empty($id_order)) {
|
|
$order = new Order();
|
|
$order->createFromDB($id_order);
|
|
$order->updatePayments();
|
|
}
|
|
|
|
writeDownActivity(sprintf(translate('activityDeleted'), $ID));
|
|
redirect('launch.php?s=orderPayment.php&acn=erased2');
|
|
}
|
|
|
|
public function isClosed($date_handle)
|
|
{
|
|
$dbcfg = Settings::getDefault();
|
|
|
|
if (empty($dbcfg->shop_orders_finished) || empty($date_handle)) {
|
|
return false;
|
|
}
|
|
|
|
$date_finished = new DateTime($dbcfg->shop_orders_finished);
|
|
$date_handle = new DateTime($date_handle);
|
|
|
|
return $date_handle < $date_finished;
|
|
}
|
|
|
|
public function tryRights($acn = '')
|
|
{
|
|
}
|
|
|
|
public function getOrderPayment($id)
|
|
{
|
|
$orderPayment = $this->selectSQL('order_payments', ['id' => $id])->fetch();
|
|
if ($orderPayment) {
|
|
$orderPayment['payment_data'] = json_decode($orderPayment['payment_data'] ?? '', true);
|
|
$order = new Order();
|
|
$order->createFromDB($orderPayment['id_order']);
|
|
|
|
$contextManager = ServiceContainer::getService(ContextManager::class);
|
|
$contextManager->activateOrder($order, function () use ($order, &$orderPayment) {
|
|
$orderPayment['order'] = $order;
|
|
if (($delivery_type = $order->getDeliveryType()) && !empty($delivery_type->payment_class)) {
|
|
$orderPayment['payment_class'] = $delivery_type->payment_class;
|
|
$orderPayment['autoReturn'] = $delivery_type->payment_class->enabledReturnPayment();
|
|
}
|
|
});
|
|
|
|
return $orderPayment;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getOrderNo($id)
|
|
{
|
|
$order = new Order();
|
|
$order->createFromDB($id);
|
|
|
|
return $order->order_no;
|
|
}
|
|
|
|
public function getOrder()
|
|
{
|
|
$id_order = returnSQLResult('SELECT id_order FROM order_payments WHERE id=:id', ['id' => $this->getID()]);
|
|
|
|
$order = new Order();
|
|
$order->createFromDB($id_order);
|
|
|
|
return $order;
|
|
}
|
|
|
|
public function handleSendEETReceipt()
|
|
{
|
|
$order = $this->getOrder();
|
|
|
|
$order->sendPaymentReceipt($this->getID());
|
|
|
|
$this->returnOK('Email s potvrzením platby odeslán.');
|
|
}
|
|
|
|
public function hasRights($name = null)
|
|
{
|
|
switch ($name) {
|
|
case Window::RIGHT_SAVE:
|
|
if (!getVal('closed') && ((findRight('POS_EDIT') && getVal('acn') == 'edit')
|
|
|| (findRight('POS_ADD') && getVal('acn') == 'add'))) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
case Window::RIGHT_DUPLICATE:
|
|
if (getVal('acn') == 'edit' && !getVal('closed') && findRight('POS_ADD')) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
case Window::RIGHT_DELETE:
|
|
if (findRight('POS_ERASE') && getVal('acn') == 'edit') {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
default:
|
|
return parent::hasRights($name);
|
|
}
|
|
}
|
|
}
|