Skip to content

Commit

Permalink
feat: adiciona suporte ao download da segunda via
Browse files Browse the repository at this point in the history
Download realizado atraves da pagina de detalhes
do pedido, do usuario
  • Loading branch information
valdeir2000 committed Sep 10, 2020
1 parent 95dbe40 commit 35c6074
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
23 changes: 23 additions & 0 deletions install.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>PagSeguro Checkout Transparente</name>
<code>valdeir_pagseguro_checkout_transparente</code>
<version>2.0</version>
<author>Valdeir S.</author>
<link>https://valdeir.dev</link>

<file path="catalog/view/theme/default/template/account/order_info.twig">
<operation>
<search>
<![CDATA[{{ continue }}]]>
</search>
<add position="before">
<![CDATA[
{% if boleto %}
<div class="pull-left"><a href="{{ boleto }}" class="btn btn-primary" target="_blank">{{ button_download_boleto }}</a></div>
{% endif %}
]]>
</add>
</operation>
</file>
</modification>
29 changes: 29 additions & 0 deletions upload/catalog/controller/event/extension/payment/pagseguro.php
@@ -0,0 +1,29 @@
<?php

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

/**
* Evento
* trigger: catalog/view/account/order_info/before
*/
public function boleto2(&$route, &$data)
{
$order_id = $this->request->get['order_id'] ?? 0;

$this->load->model('extension/payment/pagseguro');

$info = $this->model_extension_payment_pagseguro->getTransactionInfo($order_id, [
'payment_link',
'o.order_status_id'
]);

if (
isset($info['payment_link']) &&
$this->config->get(self::EXTENSION_PREFIX . 'order_status_pending') == $info['order_status_id']
) {
$data['boleto'] = $info['payment_link'];
}
}
}
19 changes: 16 additions & 3 deletions upload/catalog/controller/extension/payment/pagseguro_boleto.php
Expand Up @@ -192,13 +192,26 @@ public function transaction()
*/
public function download()
{
if (isset($this->session->data['order_id'])) {
$url = base64_decode($this->request->get['url']);
$order_id = $this->session->data['order_id'] ?? $this->request->get['order_id'] ?? null;

if ($order_id) {
if (isset($this->request->get['url'])) {
$url = base64_decode($this->request->get['url']);
} elseif (isset($this->request->get['transaction_id'])) {
$this->load->model('extension/payment/pagseguro');
$env = $this->model_extension_payment_pagseguro->factoryEnvironment();
$sale = new Sale($env);
$transaction = $sale->info($this->request->get['transaction_id']);
$url = $transaction->getPayment()->getPaymentLink();
} else {
die('PSR');
}

$url = str_replace('print.jhtml', 'print_pdf.jhtml', $url);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=psr_pedido_' . $this->session->data['order_id'] . '.pdf');
header('Content-Disposition: attachment; filename=psr_pedido_' . $order_id . '.pdf');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
Expand Down
51 changes: 51 additions & 0 deletions upload/catalog/model/extension/payment/pagseguro.php
Expand Up @@ -174,4 +174,55 @@ public function factoryEnvironment(): Environment

return Environment::production($email, $token);
}

/**
* Captura as informações de uma transação
*
* @param string $order_id_or_code
* @param array $columns `null` para todas, exceto includeRaw
* @param bool $includeRaw
*
* @return array
*/
public function getTransactionInfo($order_id_or_code, $columns = null, bool $includeRaw = false)
{
$columns_default = [
'code',
'order_id',
'last_event_date',
'payment_method',
'payment_link',
'gross_amount',
'discount_amount',
'fee_amount',
'net_amount',
'extra_amount',
];

if ($columns === null && $includeRaw === true) {
array_push($columns_default, 'raw');
}

if ($columns === null) {
$columns = $columns_default;
}

$columns = array_map(function ($item) {
return (strpos($item, '.') !== false) ? $item : 'po.' . $item;
}, $columns);

$id = $this->db->escape($order_id_or_code);

$query = $this->db->query('
SELECT ' . implode(',', $columns) . '
FROM ' . DB_PREFIX . 'pagseguro_orders po
LEFT JOIN ' . DB_PREFIX . 'order o
on (o.order_id = po.order_id)
WHERE
po.`code` = "' . $id . '"
OR po.`order_id` = "' . $id . '"
');

return $query->row;
}
}

0 comments on commit 35c6074

Please sign in to comment.