-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
executable file
·74 lines (64 loc) · 2.43 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* @package Checkout Lightbox with Pagar.me
* @author Deividson Developer
* @copyright 2017 Deividson (deividson.net)
* @license GNU/GPLv3 http:https://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die;
class ModCheckoutLightboxPagarMeHelper
{
/**
* Efetua a captura da transação na API do Pagar.me e retorna o resultado.
*
* @return array
*/
public static function efetuarCapturaAjax()
{
$session = JFactory::getSession();
$jinput = JFactory::getApplication()->input;
$module_id = $jinput->get('id', 0, 'INT');
$quantity = $jinput->get('quantity', 0, 'INT');
$token = $jinput->get('token', null);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('params'));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('id') . ' = '. strval($module_id));
$db->setQuery($query);
$result = json_decode($db->loadResult());
if($quantity == 0) {
$quantity = intval($result->quantity);
}
$result->amount = floatval(str_replace(',', '', $result->amount)); /* O Valor para API tem que ser sem separador decimal. */
$content = array();
$content['amount'] = strval($result->amount * $quantity); /* Valor total. */
$content['api_key'] = $result->api_key; /* Chave de API. */
$content['metadata']['product_code'] = $result->product_code; /* Identificador do item. */
$ch = curl_init("https://api.pagar.me/1/transactions/" . $token . "/capture");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if(!empty($error)) {
throw new Exception(JText::_('MOD_LIGHTBOXPAGARME_ERROR_CHECKOUT').$error);
}
$result = json_decode($json);
/* Retorna as mensagens de erro vindas da API. */
if(property_exists($result, "errors")) {
throw new Exception(JText::_('MOD_LIGHTBOXPAGARME_ERROR_CHECKOUT').$result->errors[0]->message);
}
$return['payment_method'] = $result->payment_method;
$return['status'] = $result->status;
if($result->payment_method == "credit_card") {
return $return;
}
else if($result->payment_method == "boleto") {
$return['boleto_url'] = $result->boleto_url;
return $return;
}
}
}