我在许多 Prestashop 平台中发现以下内容:
我们有一个红绿灯系统来阻止订单,但是当订单开具发票时又回到了以前的状态
我尝试在下面编写这个插件,但它没有记录 api 调用来监视哪个 ip/系统进行了此修改。 订单状态设置如下:
2-处理(付费)
3-出口(开具发票)
4-出货准备(物流API取订单详情)
5-完成(发送跟踪代码时物流API关闭订单)
门户网站能够将订单设置为导出(laravel cron 作业)。
许多已交付(完成)的订单现在恢复到已导出状态。
是物流还是门户?
是缓存吗?
Web 服务的覆盖不起作用,它在订单中显示只是一个空白部分。 我已经在邮递员中发送了请求:
a) 首先我得到了所有的订单字段 MYSITE/api/orders/258(258是我修改的一个订单的id b) 我从 get 中复制了相同的主体并仅修改了订单状态 并拨打了电话
MYSITE/api/orders/258?ws_key=MYWEBSERVICEKEY
//order_history_api//orderhistoryapi//orderhistoryapi.php:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
$autoloadPath = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloadPath)) {
require_once $autoloadPath;
}
use PrestaShop/PrestaShop/Core/Module/WidgetInterface;
class OrderHistoryAPI extends Module
{
public function __construct()
{
$this->name = 'orderhistoryapi';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Enrico Bisco';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Order History API');
$this->description = $this->l('Displays API users in the order history section');
}
public function install()
{
$prefix = ""._DB_PREFIX_. "";
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'api_logs` (
`id_api_log` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`api_key` VARCHAR(32) NOT NULL,
`request` TEXT NOT NULL,
`caller_ip` TEXT NOT NULL,
`date_add` DATETIME NOT NULL,
`order_id` INT(10) UNSIGNED DEFAULT NULL,
`order_status` INT(10) UNSIGNED DEFAULT NULL,
PRIMARY KEY (`id_api_log`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8' ;
//$sql = str_replace('PREFIX_', _DB_PREFIX_, $sql);
$result = Db::getInstance()->execute($sql);
return parent::install() &&
$this->registerHook('displayAdminOrder') &&
$result;
}
public function uninstall()
{
// Elimina la tabella 'api_logs'
$sql = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'api_logs`;';
$result = Db::getInstance()->execute($sql);
// Esegui la disinstallazione del modulo genitore e verifica l'esito della cancellazione della tabella
return parent::uninstall() && $result;
}
public function getApiUserInfo($order_id)
{
$db = Db::getInstance();
$query = new DbQuery();
$query->select('al.api_key, al.request, al.caller_ip, al.date_add, al.order_status, wa.key, wa.description, wp.resource, wp.method, al.order_id')
->from('api_logs', 'al')
->leftJoin('webservice_account', 'wa', 'wa.key = al.api_key')
->leftJoin('webservice_permission', 'wp', 'wp.id_webservice_account = wa.id_webservice_account')
->where('al.order_id = ' . (int)$order_id);
return $db->executeS($query);
}
public function hookDisplayAdminOrder($params)
{
$order_id = $params['id_order'];
$api_info = $this->getApiUserInfo($order_id);
$this->context->smarty->assign('api_info', $api_info);
return $this->display(__FILE__, 'views/templates/hook/order_history_api.tpl');
}
}
//order_history_api//orderhistoryapi//config.xml:
<?xml version="1.0" encoding="UTF-8"?>
orderhistoryapi
<![CDATA[Order History API]]>
<![CDATA[your_module_version]]>
<![CDATA[Displays API users in the order history section.]]>
<![CDATA[Your Name]]>
<![CDATA[administration]]>
0
0
orderhistoryapi//views//templates//hook//order_history_api.tpl:
<h3>
{l s='Chiamate API esegite' mod='orderfiles'}
</h3>
<h3>{l s='Chiamate API per modificare Stato dell/'ordine' d='Shop.Theme.Customeraccount'}</h3>
{l s='Date' d='Shop.Theme.Global'}
{l s='Status' d='Shop.Theme.Global'}
{l s='Info' d='Shop.Theme.Global'}
{foreach from=$order.history item=state}
{$state.history_date}
{$state.ostate_name}
{assign var='api_order_info' value=$api_info|@array_filter|@reset}
{if $api_order_info && $api_order_info.date_add|date_format:'%Y-%m-%d %H:%M' == $state.history_date|date_format:'%Y-%m-%d %H:%M'}
API Key usata: {$api_order_info.api_key}<br>
IP: {$api_order_info.caller_ip}<br>
Risorsa: {$api_order_info.resource}<br>
Metodo: {$api_order_info.method}<br>
Key usata: {$api_order_info.key}<br>
Nome webservice: {$api_order_info.description}<br>
{else}
Nessuna API call
{/if}
{/foreach}
{foreach from=$order.history item=state}
{$state.history_date}
{$state.ostate_name}
{/foreach}
orderhistoryapi//override//classes//WebserviceRequest.php:
<?php
class WebserviceRequest extends WebserviceRequestCore
{
public function executeRequest()
{
$response = parent::executeRequest();
$db = Db::getInstance();
$caller_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Unknown';
$is_order_update_api_call = $this->checkIfOrderUpdateApiCall($this->url, $this->method);
$order_id = null;
$order_status = null;
if ($is_order_update_api_call) {
$url_parts = explode('/', $this->url);
$order_id = (int)$url_parts[array_search('orders', $url_parts) + 1];
$order = new Order((int)$order_id);
$order_status = $order->current_state;
}
$db->insert('api_logs', [
'api_key' => pSQL($this->_key),
'request' => pSQL($this->url),
'caller_ip' => pSQL($caller_ip),
'date_add' => pSQL(date('Y-m-d H:i:s')),
'order_id' => pSQL($order_id),
'order_status' => pSQL($order_status)
]);
return $response;
}
private function checkIfOrderUpdateApiCall($url, $method)
{
if ($method === 'PUT' || $method === 'POST') {
if (strpos($url, '/orders/') !== false) {
return true;
}
}
return false;
}
}