| 1 |
<?php |
| 2 |
|
| 3 |
namespace SuperFrete_API\Admin; |
| 4 |
|
| 5 |
use SuperFrete_API\Http\Request; |
| 6 |
use SuperFrete_API\Helpers\Logger; |
| 7 |
use WC_Order; |
| 8 |
|
| 9 |
if (!defined('ABSPATH')) |
| 10 |
exit; // Segurança |
| 11 |
|
| 12 |
class SuperFrete_OrderActions |
| 13 |
{ |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_action('add_meta_boxes', [$this, 'add_superfrete_metabox']); |
| 18 |
add_action('admin_post_superfrete_resend_order', [$this, 'resend_order_to_superfrete']); |
| 19 |
add_action('admin_post_superfrete_pay_ticket', [$this, 'pay_ticket_superfrete']); |
| 20 |
|
| 21 |
// Adiciona o AJAX para verificar o status da etiqueta |
| 22 |
add_action('wp_ajax_check_superfrete_status', [$this, 'check_superfrete_status']); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Helper method to safely get order meta data (HPOS compatible) |
| 27 |
*/ |
| 28 |
private function get_order_meta($order_id, $meta_key, $single = true) |
| 29 |
{ |
| 30 |
$order = wc_get_order($order_id); |
| 31 |
if (!$order) { |
| 32 |
return $single ? '' : []; |
| 33 |
} |
| 34 |
return $order->get_meta($meta_key, $single); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Helper method to safely update order meta data (HPOS compatible) |
| 39 |
*/ |
| 40 |
private function update_order_meta($order_id, $meta_key, $meta_value) |
| 41 |
{ |
| 42 |
$order = wc_get_order($order_id); |
| 43 |
if (!$order) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
$order->update_meta_data($meta_key, $meta_value); |
| 47 |
$order->save(); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Adiciona a metabox na lateral da tela de edição do pedido |
| 53 |
*/ |
| 54 |
public function add_superfrete_metabox() |
| 55 |
{ |
| 56 |
$screen = 'shop_order'; |
| 57 |
|
| 58 |
if (defined('WC_VERSION') && version_compare(WC_VERSION, '7.1', '>=')) { |
| 59 |
$hpos_enabled = wc_get_container()->get(\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled(); |
| 60 |
$screen = $hpos_enabled ? wc_get_page_screen_id('shop-order') : 'shop_order'; |
| 61 |
} |
| 62 |
|
| 63 |
add_meta_box( |
| 64 |
'wc-superfrete_metabox', |
| 65 |
'SuperFrete', |
| 66 |
array($this, 'display_superfrete_metabox'), |
| 67 |
$screen, |
| 68 |
'side', |
| 69 |
'high' |
| 70 |
); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Exibe a metabox na tela de edição do pedido |
| 76 |
*/ |
| 77 |
public function display_superfrete_metabox($post) |
| 78 |
{ |
| 79 |
$order = ($post instanceof WP_Post) ? wc_get_order($post->ID) : $post; |
| 80 |
$order_id = is_callable(array($order, 'get_id')) ? $order->get_id() : $order->ID; |
| 81 |
|
| 82 |
$order = wc_get_order($order_id); |
| 83 |
$methods = $order->get_shipping_methods(); |
| 84 |
|
| 85 |
foreach ($methods as $method) { |
| 86 |
$method_id = $method->get_method_id(); // Forma correta de obter o method_id |
| 87 |
|
| 88 |
|
| 89 |
if (strpos($method_id, 'superfrete') !== false) { |
| 90 |
|
| 91 |
echo '<input type="hidden" name="_ajax_nonce" value="' . esc_attr(wp_create_nonce('check_superfrete_status_nonce')) . '">'; |
| 92 |
echo '<button id="verificar_etiqueta" class="button button-primary" data-order-id="' . esc_attr($order_id) . '">' . esc_html__('Verificar Etiqueta', 'superfrete') . '</button>'; |
| 93 |
echo '<div id="superfrete_status_container"></div>'; |
| 94 |
|
| 95 |
// Script JS para processar o clique do botão |
| 96 |
?> |
| 97 |
<script type="text/javascript"> |
| 98 |
jQuery(document).ready(function ($) { |
| 99 |
$('#verificar_etiqueta').on('click', function (event) { |
| 100 |
event.preventDefault(); |
| 101 |
var order_id = $(this).data('order-id'); |
| 102 |
|
| 103 |
$('#superfrete_status_container').html('<p>Verificando status...</p>'); |
| 104 |
|
| 105 |
$.ajax({ |
| 106 |
url: ajaxurl, |
| 107 |
type: 'POST', |
| 108 |
data: { |
| 109 |
action: 'check_superfrete_status', |
| 110 |
order_id: order_id, |
| 111 |
|
| 112 |
}, |
| 113 |
success: function (response) { |
| 114 |
$('#superfrete_status_container').html(response); |
| 115 |
}, |
| 116 |
error: function () { |
| 117 |
$('#superfrete_status_container').html('<p style="color: red;">Erro na API, Verifique os Logs</p>'); |
| 118 |
} |
| 119 |
}); |
| 120 |
}); |
| 121 |
}); |
| 122 |
</script> |
| 123 |
<?php |
| 124 |
|
| 125 |
} else { |
| 126 |
echo '<strong>Esse pedido não foi feito utilizando SuperFrete</strong>'; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* AJAX para buscar o status da etiqueta |
| 133 |
*/ |
| 134 |
public function check_superfrete_status() |
| 135 |
{ |
| 136 |
|
| 137 |
if (!(!isset($_POST['_ajax_nonce']) || !wp_verify_nonce($_POST['_ajax_nonce'], 'check_superfrete_status_nonce')) && !isset($_POST['order_id']) || !current_user_can('manage_woocommerce')) { |
| 138 |
wp_die(esc_html__('Permissão negada.', 'superfrete')); |
| 139 |
} |
| 140 |
|
| 141 |
$order_id = intval($_POST['order_id']); |
| 142 |
$etiqueta_id = $this->get_order_meta($order_id, '_superfrete_id', true); |
| 143 |
|
| 144 |
if (!$etiqueta_id) { |
| 145 |
echo '<p>' . esc_html__('Status da Etiqueta: ', 'superfrete') . ' <strong>' . esc_html__('Erro ao Enviar, Verifique o Log', 'superfrete') . '</strong></p>'; |
| 146 |
echo '<a href="' . esc_url(wp_nonce_url(admin_url('admin-post.php?action=superfrete_resend_order&order_id=' . $order_id), 'superfrete_resend_order')) . '" class="button button-primary">' . esc_html__('Reenviar Pedido', 'superfrete') . '</a>'; |
| 147 |
wp_die(); |
| 148 |
} |
| 149 |
|
| 150 |
$saldo = $this->get_superfrete_balance(); |
| 151 |
$superfrete_status = $this->get_superfrete_data($etiqueta_id)['status']; |
| 152 |
$superfrete_tracking = $this->get_superfrete_data($etiqueta_id)['tracking']; |
| 153 |
|
| 154 |
$valor_frete = floatval($this->get_order_meta($order_id, '_superfrete_price', true)); |
| 155 |
|
| 156 |
echo "<p><strong>" . esc_html__('Saldo na SuperFrete:', 'superfrete') . "</strong> R$ " . esc_html(number_format($saldo, 2, ',', '.')) . "</p>"; |
| 157 |
echo "<p><strong>" . esc_html__('Valor da Etiqueta:', 'superfrete') . "</strong> R$ " . esc_html(number_format($valor_frete, 2, ',', '.')) . "</p>"; |
| 158 |
if ($superfrete_status == 'released') { |
| 159 |
echo '<p>' . esc_html__('Status da Etiqueta: ', 'superfrete') . ' <strong>' . esc_html__('Emitida', 'superfrete') . '</strong></p>'; |
| 160 |
echo '<a href="' . esc_url($this->get_ticket_superfrete($order_id)) . '" target="_blank" class="button button-secondary">' . esc_html__('Imprimir Etiqueta', 'superfrete') . '</a>'; |
| 161 |
} elseif ($superfrete_status == 'pending') { |
| 162 |
|
| 163 |
echo '<p>' . esc_html__('Status da Etiqueta: ', 'superfrete') . ' <strong>' . esc_html__('Pendente Pagamento', 'superfrete') . '</strong></p>'; |
| 164 |
if ($saldo < $valor_frete) { |
| 165 |
$web_url = $this->get_superfrete_web_url(); |
| 166 |
echo '<a href="' . esc_url($web_url . '/#/account/credits') . '" target="_blank" class="button button-primary">' . esc_html__('Adicionar Saldo', 'superfrete') . '</a>'; |
| 167 |
echo '<p style="color: red;">' . esc_html__('Saldo insuficiente para pagamento da etiqueta.', 'superfrete') . '</p>'; |
| 168 |
} else { |
| 169 |
echo '<a href="' . esc_url(wp_nonce_url(admin_url('admin-post.php?action=superfrete_pay_ticket&order_id=' . $order_id), 'superfrete_pay_ticket')) . '" class="button button-primary">' . esc_html__('Pagar Etiqueta', 'superfrete') . '</a>'; |
| 170 |
} |
| 171 |
} else if ($superfrete_status == 'canceled') { |
| 172 |
echo '<p>' . esc_html__('Status da Etiqueta: ', 'superfrete') . ' <strong>' . esc_html__('Cancelada', 'superfrete') . '</strong></p>'; |
| 173 |
echo '<a href="' . esc_url(wp_nonce_url(admin_url('admin-post.php?action=superfrete_resend_order&order_id=' . $order_id), 'superfrete_resend_order')) . '" class="button button-primary">' . esc_html__('Reenviar Pedido', 'superfrete') . '</a>'; |
| 174 |
|
| 175 |
|
| 176 |
} else if ($superfrete_status == 'posted') { |
| 177 |
echo '<p>' . esc_html__('Status do Pedido: ', 'superfrete') . ' <strong>' . esc_html__('Postado', 'superfrete') . '</strong></p>'; |
| 178 |
|
| 179 |
// Show webhook tracking info if available |
| 180 |
$tracking_code = $this->get_order_meta($order_id, '_superfrete_tracking_code', true); |
| 181 |
$tracking_url = $this->get_order_meta($order_id, '_superfrete_tracking_url', true); |
| 182 |
$posted_at = $this->get_order_meta($order_id, '_superfrete_posted_at', true); |
| 183 |
|
| 184 |
if ($tracking_code) { |
| 185 |
echo '<p><strong>' . esc_html__('Código de Rastreamento:', 'superfrete') . '</strong> ' . esc_html($tracking_code) . '</p>'; |
| 186 |
} |
| 187 |
if ($posted_at) { |
| 188 |
echo '<p><strong>' . esc_html__('Data de Postagem:', 'superfrete') . '</strong> ' . esc_html(wp_date('d/m/Y H:i', strtotime($posted_at))) . '</p>'; |
| 189 |
} |
| 190 |
|
| 191 |
$tracking_base_url = $this->get_superfrete_tracking_url(); |
| 192 |
$tracking_link = $tracking_url ?: "{$tracking_base_url}/#/tracking/{$superfrete_tracking}"; |
| 193 |
echo '<a href="' . esc_url($tracking_link) . '" target="_blank" class="button button-primary">' . esc_html__('Rastrear Pedido', 'superfrete') . '</a>'; |
| 194 |
|
| 195 |
} else if ($superfrete_status == 'delivered') { |
| 196 |
echo '<p>' . esc_html__('Status do Pedido: ', 'superfrete') . ' <strong>' . esc_html__('Entregue', 'superfrete') . '</strong></p>'; |
| 197 |
|
| 198 |
// Show delivery info if available |
| 199 |
$delivered_at = $this->get_order_meta($order_id, '_superfrete_delivered_at', true); |
| 200 |
$tracking_code = $this->get_order_meta($order_id, '_superfrete_tracking_code', true); |
| 201 |
|
| 202 |
if ($delivered_at) { |
| 203 |
echo '<p><strong>' . esc_html__('Data de Entrega:', 'superfrete') . '</strong> ' . esc_html(wp_date('d/m/Y H:i', strtotime($delivered_at))) . '</p>'; |
| 204 |
} |
| 205 |
if ($tracking_code) { |
| 206 |
echo '<p><strong>' . esc_html__('Código de Rastreamento:', 'superfrete') . '</strong> ' . esc_html($tracking_code) . '</p>'; |
| 207 |
} |
| 208 |
|
| 209 |
} else { |
| 210 |
|
| 211 |
echo '<p>' . esc_html__('Status da Etiqueta: ', 'superfrete') . ' <strong>' . esc_html__('Erro ao Enviar', 'superfrete') . '</strong></p>'; |
| 212 |
echo '<a href="' . esc_url(wp_nonce_url(admin_url('admin-post.php?action=superfrete_resend_order&order_id=' . $order_id), 'superfrete_resend_order')) . '" class="button button-primary">' . esc_html__('Reenviar Pedido', 'superfrete') . '</a>'; |
| 213 |
} |
| 214 |
|
| 215 |
wp_die(); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Obtém o saldo do usuário na SuperFrete |
| 220 |
*/ |
| 221 |
private function get_superfrete_balance() |
| 222 |
{ |
| 223 |
$request = new Request(); |
| 224 |
$response = $request->call_superfrete_api('/api/v0/user', 'GET', [], true); |
| 225 |
return isset($response['balance']) ? floatval($response['balance']) : 0; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the correct web URL based on environment |
| 230 |
*/ |
| 231 |
private function get_superfrete_web_url() |
| 232 |
{ |
| 233 |
$use_dev_env = get_option('superfrete_sandbox_mode') === 'yes'; |
| 234 |
return $use_dev_env ? 'https://sandbox.superfrete.com' : 'https://web.superfrete.com'; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get the correct tracking URL based on environment |
| 239 |
*/ |
| 240 |
private function get_superfrete_tracking_url() |
| 241 |
{ |
| 242 |
$use_dev_env = get_option('superfrete_sandbox_mode') === 'yes'; |
| 243 |
return $use_dev_env ? 'https://sandbox.superfrete.com' : 'https://rastreio.superfrete.com'; |
| 244 |
} |
| 245 |
|
| 246 |
private function get_superfrete_data($id) |
| 247 |
{ |
| 248 |
$request = new Request(); |
| 249 |
$response = $request->call_superfrete_api('/api/v0/order/info/' . $id, 'GET', [], true); |
| 250 |
return $response; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Obtém o link de impressão da etiqueta |
| 255 |
*/ |
| 256 |
private function get_ticket_superfrete($order_id) |
| 257 |
{ |
| 258 |
$etiqueta_id = $this->get_order_meta($order_id, '_superfrete_id', true); |
| 259 |
if (!$etiqueta_id) { |
| 260 |
return ''; |
| 261 |
} |
| 262 |
|
| 263 |
$request = new Request(); |
| 264 |
$response = $request->call_superfrete_api('/api/v0/tag/print', 'POST', ['orders' => [$etiqueta_id]], true); |
| 265 |
|
| 266 |
if (isset($response['url'])) { |
| 267 |
$this->update_order_meta($order_id, '_superfrete_status', 'success'); |
| 268 |
return $response['url']; |
| 269 |
} |
| 270 |
|
| 271 |
return ''; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Reenvia o pedido para a API SuperFrete |
| 276 |
*/ |
| 277 |
public function resend_order_to_superfrete() |
| 278 |
{ |
| 279 |
if (!isset($_GET['order_id']) || !current_user_can('manage_woocommerce')) { |
| 280 |
wp_die(esc_html__('Permissão negada.', 'superfrete')); |
| 281 |
} |
| 282 |
|
| 283 |
$order_id = intval($_GET['order_id']); |
| 284 |
check_admin_referer('superfrete_resend_order'); |
| 285 |
$order = wc_get_order($order_id); |
| 286 |
if (!$order) { |
| 287 |
wp_die(esc_html__('Pedido inválido.', 'superfrete')); |
| 288 |
} |
| 289 |
|
| 290 |
Logger::log('SuperFrete', "Reenviando pedido #{$order_id} para a API..."); |
| 291 |
|
| 292 |
$controller = new \SuperFrete_API\Controllers\SuperFrete_Order(); |
| 293 |
$response = $controller->send_order_to_superfrete($order_id); |
| 294 |
|
| 295 |
if (isset($response['status']) && $response['status'] === 'pending') { |
| 296 |
$this->update_order_meta($order_id, '_superfrete_status', 'pending-payment'); |
| 297 |
Logger::log('SuperFrete', "Pedido #{$order_id} enviado com sucesso."); |
| 298 |
} else { |
| 299 |
$this->update_order_meta($order_id, '_superfrete_status', 'erro'); |
| 300 |
Logger::log('SuperFrete', "Erro ao reenviar pedido #{$order_id}."); |
| 301 |
} |
| 302 |
|
| 303 |
wp_redirect(admin_url('post.php?post=' . $order_id . '&action=edit')); |
| 304 |
exit; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Paga a etiqueta da SuperFrete |
| 309 |
*/ |
| 310 |
public function pay_ticket_superfrete() |
| 311 |
{ |
| 312 |
if (!isset($_GET['order_id']) || !current_user_can('manage_woocommerce')) { |
| 313 |
wp_die(esc_html__('Permissão negada.', 'superfrete')); |
| 314 |
} |
| 315 |
|
| 316 |
$order_id = intval($_GET['order_id']); |
| 317 |
check_admin_referer('superfrete_pay_ticket'); |
| 318 |
$order = wc_get_order($order_id); |
| 319 |
if (!$order) { |
| 320 |
wp_die(esc_html__('Pedido inválido.', 'superfrete')); |
| 321 |
} |
| 322 |
|
| 323 |
Logger::log('SuperFrete', "Pagando Etiqueta #{$order_id}..."); |
| 324 |
|
| 325 |
$etiqueta_id = $this->get_order_meta($order_id, '_superfrete_id', true); |
| 326 |
|
| 327 |
$request = new Request(); |
| 328 |
$response = $request->call_superfrete_api('/api/v0/checkout', 'POST', ['orders' => [$etiqueta_id]], true); |
| 329 |
|
| 330 |
if ($response == 409) { |
| 331 |
$this->update_order_meta($order_id, '_superfrete_status', 'success'); |
| 332 |
wp_redirect(admin_url('post.php?post=' . $order_id . '&action=edit')); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
if (isset($response['status']) && $response['status'] === 'pending') { |
| 337 |
Logger::log('SuperFrete', "Etiqueta Paga #{$order_id}..."); |
| 338 |
$this->update_order_meta($order_id, '_superfrete_status', 'pending-payment'); |
| 339 |
Logger::log('SuperFrete', "Pedido #{$order_id} enviado com sucesso."); |
| 340 |
} else { |
| 341 |
$this->update_order_meta($order_id, '_superfrete_status', 'aguardando'); |
| 342 |
|
| 343 |
Logger::log('SuperFrete', "Erro ao tentar pagar o ticket do pedido #{$order_id}."); |
| 344 |
} |
| 345 |
|
| 346 |
wp_redirect(admin_url('post.php?post=' . $order_id . '&action=edit')); |
| 347 |
exit; |
| 348 |
} |
| 349 |
} |
| 350 |
|