-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cria funçoes base no model para gerenciar transaçao
- Loading branch information
1 parent
80d4767
commit a52325f
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,94 @@ | ||
<?php | ||
|
||
require_once DIR_SYSTEM . 'library/PagSeguro/vendor/autoload.php'; | ||
|
||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
use ValdeirPsr\PagSeguro\Request\Session; | ||
|
||
class ModelExtensionPaymentPagSeguro extends Model | ||
{ | ||
const EXTENSION_PREFIX = 'payment_pagseguro_'; | ||
|
||
public function getMethod($address, $total) | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Captura os campos personalizados | ||
* | ||
* @return array | ||
*/ | ||
public function getCustomFieldValues(int $order_id) | ||
{ | ||
$query = $this->db->query('SELECT custom_field FROM ' . DB_PREFIX . 'order WHERE order_id = ' . $order_id); | ||
|
||
return json_decode($query->row['custom_field'], true); | ||
} | ||
|
||
/** | ||
* Gera uma nova sessão para o pedido | ||
* | ||
* @return string | ||
*/ | ||
public function generateSession(): string | ||
{ | ||
$env = $this->factoryEnvironment(); | ||
$session = new Session($env); | ||
return $session->generate(); | ||
} | ||
|
||
/** | ||
* Captura o valor do frete | ||
* | ||
* @return float | ||
*/ | ||
public function getShippingCost($totals): float | ||
{ | ||
$result = array_filter($totals, function ($item) { | ||
return $item['code'] === 'shipping'; | ||
}); | ||
|
||
$result = array_reduce($result, function ($sum, $item) { | ||
return $sum += $item['value']; | ||
}, 0); | ||
|
||
return floatval($result); | ||
} | ||
|
||
/** | ||
* Captura os valores extras | ||
* | ||
* @return float | ||
*/ | ||
public function getExtraAmount($totals): float | ||
{ | ||
$result = array_filter($totals, function ($item) { | ||
return !in_array($item['code'], ['sub_total', 'shipping', 'total']); | ||
}); | ||
|
||
$result = array_reduce($result, function ($sum, $item) { | ||
return $sum += $item['value']; | ||
}, 0); | ||
|
||
return floatval($result); | ||
} | ||
|
||
/** | ||
* Instancia um objeto do tipo Environment | ||
* conforme a configuração de sandbox. | ||
* | ||
* @return Environment | ||
*/ | ||
public function factoryEnvironment(): Environment | ||
{ | ||
$email = $this->config->get(self::EXTENSION_PREFIX . 'email'); | ||
$token = $this->config->get(self::EXTENSION_PREFIX . 'token'); | ||
|
||
if ($this->config->get(self::EXTENSION_PREFIX . 'sandbox')) { | ||
return Environment::sandbox($email, $token); | ||
} | ||
|
||
return Environment::production($email, $token); | ||
} | ||
} |