bank_xml_file_path = $main_path.self::BANK_XML_FILE;
$this->log_file_path = $main_path.self::LOG_FILE;
$this->images_path = $main_path.self::IMAGE_DIRECTORY;
if ($config['sandbox'] == 1) {
$this->sandbox = true;
$this->bank_xml_file_path = $main_path.self::SANDBOX_BANK_XML_FILE;
$this->log_file_path = $main_path.self::SANDBOX_LOG_FILE;
$this->images_path = $main_path.self::SANDBOX_IMAGE_DIRECTORY;
}
$this->log_operations = ($config['log'])?true:false;
$this->createDirectories();
}
public function getBankFormLink()
{
if ($this->sandbox) {
return self::PBN_TEST_BANK_PAYMENT_LINK;
}
return self::PBN_BANK_PAYMENT_LINK;
}
/**
* Create directories for storage bank images
*/
private function createDirectories()
{
if (!is_dir(dirname(__FILE__).'/../cache/')) {
mkdir(dirname(__FILE__).'/../cache/', 0777);
}
if (!is_dir($this->images_path)) {
mkdir($this->images_path, 0777);
}
}
/**
* Generates cache if needed
* check if cached file exists and if it's new
* @return string XML
*/
public function getBanksXml()
{
$cache_file = $this->bank_xml_file_path;
$banks_list_xml_cache = array();
if (file_exists($cache_file)) {
$banks_list_xml_cache['xml'] = file_get_contents($cache_file);
$banks_list_xml_cache['last_mod'] = date ("Y-m-d", filemtime($cache_file));
}
if ($banks_list_xml_cache['last_mod'] == date("Y-m-d")) {
return $banks_list_xml_cache['xml'];
}
return $this->setBanksXmlCache();
}
public function removeBankFile()
{
$cache_file = $this->bank_xml_file_path;
if (file_exists($cache_file)) {
unlink($cache_file);
}
}
public function getBnaksFileTime()
{
$file = array(
'name' => 'no data',
'date' => 'no data'
);
$cache_file = $this->bank_xml_file_path;
if (file_exists($cache_file)) {
$file['date'] = date ("Y-m-d H:i:s", filemtime($cache_file));
$file['name'] = basename($cache_file);
}
return $file;
}
public function getBanksArray($with_cc = 1)
{
$banks_xml_string = $this->getBanksXml();
$banks_array = array();
try {
$banks_xml = new SimpleXMLElement( $banks_xml_string );
foreach ($banks_xml->bank as $bank) {
if ($with_cc == 0 && $bank->card == 1) {
continue;
}
if (is_file($this->images_path.$bank->image)) {
$banks_array[(string)$bank->id] = array(
'id' => (string)$bank->id,
'name' => (string)$bank->name,
'image' => (string)$bank->image,
'card' => (string)$bank->card
);
}
}
} catch ( Exception $e ) {
$msg = $e->getMessage();
$this->logOperation($msg, 'ERROR');
}
return $banks_array;
}
/**
* Saves cache with xml data
* @return string XML code
*/
private function setBanksXmlCache()
{
$link_banks_xml = self::PBN_BANK_LINK;
if ($this->sandbox == true) {
$link_banks_xml = self::PBN_TEST_BANK_LINK;
}
$cache_file = $this->bank_xml_file_path;
$banks_list_xml_for_caceh = file_get_contents( $link_banks_xml );
if ($banks_list_xml_for_caceh) {
try{
file_put_contents($cache_file, $banks_list_xml_for_caceh );
} catch ( Exception $e) {
$msg = $e->getMessage();
$this->logOperation($msg, 'ERROR');
}
$this->setBanksImagesCache();
}
return $banks_list_xml_for_caceh;
}
/**
* Generates cache
* with bank images porived form XML
*/
private function setBanksImagesCache()
{
$link_img_banks = self::PBN_BANK_IMG_LINK;
if ($this->sandbox == true) {
$link_img_banks = self::PBN_TEST_BANK_IMG_LINK;
}
$cache_file = $this->bank_xml_file_path;
$cache_img_dir = $this->images_path;
if (file_exists($cache_file))
{
$banks_xml_string = file_get_contents($cache_file);
try {
$banks_xml = new SimpleXMLElement( $banks_xml_string );
foreach ($banks_xml->bank as $bank) {
$img = file_get_contents( $link_img_banks . $bank->image);
if (!$img) {
$msg = 'download Image ' . $bank->image . ' failed';
$this->logOperation($msg, 'ERROR');
continue;
}
$write_img = file_put_contents($cache_img_dir . $bank->image, $img ) ;
if (!$write_img ){
$msg = 'write Image ' . $bank->image . ' failed';
$this->logOperation($msg, 'ERROR');
continue;
}
}
} catch ( Exception $e ) {
$msg = $e->getMessage();
$this->logOperation($msg, 'ERROR');
}
}
}
public function getFormElements($params)
{
if (is_numeric($params['paybynet_date_valid'])) {
$pay_time = (int)$params['paybynet_date_valid'] * 60;
} else {
$pay_time = 50000 * 60;
}
$date = new DateTime();
$date->add(new DateInterval('PT'.$pay_time.'S'));
$accname = trim($params['paybynet_name']) . '^NM^' . trim($params['paybynet_zip_code']) . '^ZP^' . trim($params['paybynet_city']) . '^CI^' . trim($params['paybynet_street']) . '^ST^' . trim($params['paybynet_country']) . '^CT^';
$parameters = array();
$parameters['id_client'] = ''.$params['paybynet_id_client'].'';
$parameters['id_trans'] = ''.sprintf("%010s", $params['id_trans']).'';
$parameters['date_valid'] = '' . $date->format('d-m-Y H:i:s') . '';
$parameters['amount'] = '' . number_format($params['amount'], 2,',','') . '';
$parameters['currency'] = '' . $params['paybynet_currency'] . '';
$parameters['email'] = '' . $params['email'] . '';
$parameters['account'] = '' . $params['paybynet_account'] . '';
$parameters['accname'] = '' . $accname . '';
$parameters['backpage'] = '' . $params['backpage'] . '';
$parameters['backpagereject'] = '' . $params['backpage_reject'] . '';
$parameters['password'] = '' . $params['paybynet_password'] . '';
$parameters['hash'] = '' . sha1($parameters['id_client'].$parameters['id_trans'].$parameters['date_valid'].$parameters['amount'].$parameters['currency'].$parameters['email'].$parameters['account'].$parameters['accname'].$parameters['backpage'].$parameters['backpagereject'].$parameters['password']).'';
$form = '';
foreach ($parameters as $key => $val) {
if ($key == 'password') {
continue;
}
$form .= $val;
}
return base64_encode($form);
}
public function transactionStatus($status_id)
{
$transaction_status = '';
switch ($status_id) {
case '2203':
case '2303':
$transaction_status = 'approved';
break;
case '2202':
case '2302':
$transaction_status = 'rejected';
break;
case '2201':
case '2301':
$transaction_status = 'expired';
break;
}
return $transaction_status;
}
/**
* Log operations to file
* LOG const defines if the log is gathered to file
* @param string $message
* @param string $type
*/
public function logOperation($message, $type = 'INFO')
{
if ($this->log_operations) {
// Bad place for log but it's stays here for now
$content = date("Y-m-d H:i:s") . " " . strtoupper($type) . ": " . $message;
$content_coded = base64_encode($content). PHP_EOL;
file_put_contents($this->log_file_path, $content_coded, FILE_APPEND ) ;
}
}
}