Skip to content

Commit

Permalink
feat: adiciona rota para tratar as notificaçoes
Browse files Browse the repository at this point in the history
  • Loading branch information
valdeir2000 committed Sep 2, 2020
1 parent a52325f commit da37f20
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
41 changes: 41 additions & 0 deletions upload/catalog/controller/extension/payment/pagseguro.php
@@ -0,0 +1,41 @@
<?php

class ControllerExtensionPaymentPagSeguro extends Controller
{
const EXTENSION_PREFIX = 'payment_pagseguro_';

public function callback()
{
$this->load->model('extension/payment/pagseguro');
$this->load->model('checkout/order');

$notificationCode = $this->request->post['notificationCode'] ?? 0;

$transaction = $this->model_extension_payment_pagseguro->checkStatusByNotificationCode($notificationCode);

// Logger

$status = $transaction->getStatus();

$statuses = [
0 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_pending'),
1 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_pending'),
2 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_analysing'),
3 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_paid'),
4 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_available'),
5 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_disputed'),
6 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_returned'),
7 => $this->config->get(self::EXTENSION_PREFIX . 'order_status_cancelled')
];

if (array_key_exists($status, $statuses)) {
$statusId = $statuses[$status];
} else {
$statusId = reset($statuses);
}

$customer_notify = !!$this->config->get(self::EXTENSION_PREFIX . 'customer_notify');

$this->model_checkout_order->addOrderHistory($transaction['order_id'], $statusId, '', $customer_notify);
}
}
31 changes: 31 additions & 0 deletions upload/catalog/model/extension/payment/pagseguro.php
Expand Up @@ -4,6 +4,7 @@

use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Request\Session;
use ValdeirPsr\PagSeguro\Request\Notification;

class ModelExtensionPaymentPagSeguro extends Model
{
Expand Down Expand Up @@ -38,6 +39,36 @@ public function generateSession(): string
return $session->generate();
}

/**
* Captura o status do pedido, de acordo com o código da notificação
*
* @param string $notification_code
*
* @return array|null
*/
public function checkStatusByNotificationCode(string $notification_code): ?array
{
$env = $this->factoryEnvironment();
$request = new Notification($env);
$transaction = $request->capture($notification_code);

$order = $this->db->query('
SELECT
order_id
FROM ' . DB_PREFIX . 'pagseguro_orders
WHERE `code` = "' . $this->db->escape($transaction->getCode()) . '"
');

if (isset($order->row['order_id'])) {
return [
"order_id" => $order->row['order_id'],
"status" => $transaction->getStatus()
];
}

return null;
}

/**
* Captura o valor do frete
*
Expand Down

0 comments on commit da37f20

Please sign in to comment.