2015-01-23 13:09:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn;
|
|
|
|
|
|
|
|
require_once ('vn/hedera/init.php');
|
|
|
|
require_once ('vn/db/db.php');
|
|
|
|
require_once ('vn/tpv/soap-method.php');
|
|
|
|
|
|
|
|
use Vn\Lib\Log;
|
|
|
|
|
|
|
|
class Tpv
|
|
|
|
{
|
|
|
|
static $conn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the class.
|
|
|
|
**/
|
|
|
|
static function init ()
|
|
|
|
{
|
|
|
|
global $conf;
|
2015-08-27 14:04:34 +00:00
|
|
|
|
2015-08-28 00:44:34 +00:00
|
|
|
openlog ('hedera-web', LOG_ODELAY, LOG_LOCAL0);
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
self::$conn = new Db\Conn ();
|
|
|
|
self::$conn->open (
|
|
|
|
$conf['db']['host']
|
|
|
|
,$conf['db']['user']
|
|
|
|
,base64_decode ($conf['db']['pass'])
|
|
|
|
,$conf['db']['schema']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deinitializes the class.
|
|
|
|
**/
|
|
|
|
static function deinit ()
|
|
|
|
{
|
|
|
|
self::$conn->close ();
|
2015-08-28 00:44:34 +00:00
|
|
|
closelog ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets transaction confirmations from the IMAP mailbox.
|
|
|
|
**/
|
|
|
|
static function getFromMailbox ()
|
|
|
|
{
|
|
|
|
self::init ();
|
|
|
|
|
|
|
|
$imap = NULL;
|
|
|
|
|
|
|
|
if (self::$conn->isOpen ())
|
|
|
|
{
|
|
|
|
$imapConf = self::$conn->getRow (
|
|
|
|
'SELECT host, user, pass, clean_period, success_folder, error_folder '
|
|
|
|
.'FROM tpv_imap_config'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($imapConf)
|
|
|
|
{
|
|
|
|
$mailbox = sprintf ('{%s/imap/ssl/novalidate-cert}INBOX',
|
|
|
|
$imapConf['host']);
|
|
|
|
|
|
|
|
$imap = imap_open ($mailbox
|
|
|
|
,$imapConf['user']
|
|
|
|
,base64_decode ($imapConf['pass'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($imap)
|
|
|
|
{
|
|
|
|
// Fetchs and confirms new transaction mails
|
|
|
|
|
|
|
|
$inbox = imap_search ($imap, 'ALL');
|
|
|
|
|
|
|
|
if ($inbox)
|
|
|
|
foreach ($inbox as $msg)
|
|
|
|
{
|
|
|
|
// Decodes the mail body
|
|
|
|
|
|
|
|
$params = [];
|
|
|
|
$body = imap_fetchbody ($imap, $msg, '1');
|
|
|
|
$strings = explode (';', $body);
|
|
|
|
|
|
|
|
foreach ($strings as $string)
|
|
|
|
{
|
|
|
|
$x = explode (':', $string);
|
|
|
|
$params[trim ($x[0])] = trim ($x[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirms the transaction
|
|
|
|
|
|
|
|
$success = self::confirm ($params);
|
|
|
|
|
|
|
|
// Moves the processed mail to another folder
|
|
|
|
|
|
|
|
if ($success)
|
|
|
|
$folder = $imapConf['success_folder'];
|
|
|
|
else
|
|
|
|
$folder = $imapConf['error_folder'];
|
|
|
|
|
|
|
|
$folder = sprintf ('INBOX.%s', $folder);
|
|
|
|
|
|
|
|
if (!imap_mail_move ($imap, $msg, $folder))
|
2015-09-11 09:37:16 +00:00
|
|
|
trigger_error (sprintf ('TPV: IMAP: Can\'t move message to %s: %s'
|
2015-01-23 13:09:30 +00:00
|
|
|
,$folder
|
|
|
|
,imap_last_error ()
|
2015-09-11 09:37:16 +00:00
|
|
|
), E_USER_WARNING);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($inbox && ($count = count ($inbox)) > 0)
|
2015-09-11 09:37:16 +00:00
|
|
|
trigger_error (sprintf ('TPV: %d mails processed.', $count));
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
imap_expunge ($imap);
|
|
|
|
|
|
|
|
// Cleans the old mails
|
|
|
|
|
|
|
|
if (rand (1, 20) == 1)
|
|
|
|
{
|
|
|
|
$folders = array (
|
|
|
|
$imapConf['success_folder']
|
|
|
|
,$imapConf['error_folder']
|
|
|
|
);
|
|
|
|
|
|
|
|
$deleted = 0;
|
2015-01-31 01:05:12 +00:00
|
|
|
$date = new \DateTime (NULL);
|
|
|
|
$date->sub (new \DateInterval ($imapConf['clean_period']));
|
2015-01-23 13:09:30 +00:00
|
|
|
$filter = sprintf ('BEFORE "%s"', $date->format('D, j M Y'));
|
|
|
|
|
|
|
|
foreach ($folders as $folder)
|
|
|
|
if (imap_reopen ($imap, $mailbox.'.'.$folder))
|
|
|
|
if ($messages = imap_search ($imap, $filter))
|
|
|
|
{
|
|
|
|
foreach ($messages as $message)
|
|
|
|
imap_delete ($imap, $message);
|
|
|
|
|
|
|
|
imap_expunge ($imap);
|
|
|
|
$deleted += count ($messages);
|
|
|
|
}
|
|
|
|
|
2015-09-11 09:37:16 +00:00
|
|
|
trigger_error (sprintf ('TPV: Cleaner: %d mails deleted.', $deleted));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imap_close ($imap);
|
|
|
|
}
|
|
|
|
else
|
2015-09-11 09:37:16 +00:00
|
|
|
trigger_error (sprintf ('TPV: IMAP: %s', imap_last_error ()), E_USER_ERROR);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
self::deinit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets transaction confirmation from HTTP POST.
|
|
|
|
**/
|
|
|
|
static function getFromPost ()
|
|
|
|
{
|
|
|
|
self::init ();
|
|
|
|
|
|
|
|
if (self::$conn->isOpen ())
|
|
|
|
self::confirm ($_POST);
|
|
|
|
|
|
|
|
self::deinit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets transaction confirmation from SOAP service.
|
|
|
|
**/
|
|
|
|
static function getFromSoap ()
|
|
|
|
{
|
|
|
|
ini_set ('soap.wsdl_cache_enabled', FALSE);
|
|
|
|
|
|
|
|
$server = new \SoapServer (__DIR__ .'/soap.wsdl');
|
|
|
|
$server->addFunction ('procesaNotificacionSIS');
|
|
|
|
$server->handle ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tryes to confirm a transaction with the given params.
|
|
|
|
**/
|
|
|
|
static function confirm ($params)
|
2015-01-31 01:05:12 +00:00
|
|
|
{
|
2015-01-23 13:09:30 +00:00
|
|
|
if (isset ($params['Ds_Amount'])
|
|
|
|
&& isset ($params['Ds_Order'])
|
|
|
|
&& isset ($params['Ds_MerchantCode'])
|
|
|
|
&& isset ($params['Ds_Currency'])
|
|
|
|
&& isset ($params['Ds_Response'])
|
|
|
|
&& isset ($params['Ds_Signature']))
|
|
|
|
{
|
2015-06-30 12:06:19 +00:00
|
|
|
if (isset ($params['Ds_ErrorCode']))
|
|
|
|
$error = $params['Ds_ErrorCode'];
|
|
|
|
else
|
|
|
|
$error = NULL;
|
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
try {
|
|
|
|
return self::$conn->query (
|
2015-06-30 12:06:19 +00:00
|
|
|
'CALL transaction_confirm_with_check (#, #, #, #, #, #, #)',
|
2015-01-31 01:05:12 +00:00
|
|
|
[
|
|
|
|
$params['Ds_Amount']
|
|
|
|
,$params['Ds_Order']
|
|
|
|
,$params['Ds_MerchantCode']
|
|
|
|
,$params['Ds_Currency']
|
|
|
|
,$params['Ds_Response']
|
|
|
|
,$params['Ds_Signature']
|
2015-06-30 12:06:19 +00:00
|
|
|
,$error
|
2015-01-31 01:05:12 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2015-09-11 09:37:16 +00:00
|
|
|
trigger_error (sprintf ("TPV: DB: %s", $e->getMessage ()), E_USER_WARNING);
|
2015-01-31 01:05:12 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the confirmation process. The corresponding record
|
|
|
|
* must exist in the `tpv_transaction` table.
|
|
|
|
**/
|
|
|
|
static function test ($order)
|
|
|
|
{
|
|
|
|
self::init ();
|
|
|
|
|
|
|
|
if (self::$conn->isOpen () && isset ($order))
|
|
|
|
self::$conn->query ('CALL transaction_confirm_by_id (#)', [$order]);
|
|
|
|
|
|
|
|
self::deinit ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|