<?php
/*
Virtual Freer
http://freer.ir/virtual

Copyright (c) 2011 Sayaweb.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 (http://www.gnu.org/licenses/gpl-3.0.html)
as published by the Free Software Foundation.
*/

$pluginData[paypax][type] = 'payment';
$pluginData[paypax][name] = 'پی پکس';
$pluginData[paypax][uniq] = 'paypax';
$pluginData[paypax][description] = 'مخصوص پرداخت با دروازه پرداخت <a href="http://paypax.info">پی پکس</a>';
$pluginData[paypax][author][name] = 'paypax';
$pluginData[paypax][author][url] = 'http://paypax.info';
$pluginData[paypax][author][email] = 'info@paypax.info';


$pluginData[paypax][field][config][1][title] = 'API';
$pluginData[paypax][field][config][1][name] = 'merchant';
$pluginData[paypax][field][config][2][title] = 'عنوان خرید';
$pluginData[paypax][field][config][2][name] = 'title';

function gateway__paypax($data)
{
global $config,$db,$smarty;
$merchantID 	= $data[merchant];
$amount 		= round($data[amount]);
$invoice_id		= $data[invoice_id];
$callBackUrl 	= $data[callback];
$wsdl_url = "http://paypax.info/WebService/WebService.asmx?wsdl";
$client = new SoapClient($wsdl_url);
$params = array('API' => $merchantID , 'Amount' => $amount, 'CallBack' => $callBackUrl, 'OrderId' => $invoice_id, 'Text' => urlencode($data[title]));
$res = $client->requestpayment($params);
$resResult = $res->requestpaymentResult;
if (intval($resResult) > 0)
{
$update[payment_rand]		= $resResult;
$sql = $db->queryUpdate('payment', $update, 'WHERE `payment_rand` = "'.$invoice_id.'" LIMIT 1;');
$db->execute($sql);
header('location: http://paypax.info/payment/pay_invoice/'.$res->requestpaymentResult);
exit;
}
else
{
$data[title] = 'خطای سیستمي';
$data[message] = '<font color="red">خطا در اتصال به کارت خوان کد خطا</font>'.$resResult.'<br /><a href="index.php" class="button">بازگشت</a>';
$query	= 'SELECT * FROM `config` WHERE `config_id` = "1" LIMIT 1';
$conf	= $db->fetch($query);
$smarty->assign('config', $conf);
$smarty->assign('data', $data);
$smarty->display('message.tpl');
}
}


function callback__paypax($data)
{
global $db,$get;
$au = $_POST['au'];
$ref_id = $_POST['order_id'];
if (strlen($au)>4)
{
$merchantID = $data[merchant];
$sql 		= 'SELECT * FROM `payment` WHERE `payment_rand` = "'.$au.'" LIMIT 1;';
$payment 	= $db->fetch($sql);

$amount		= round($payment[payment_amount]);
$wsdl_url = "http://paypax.info/WebService/WebService.asmx?wsdl";
$client = new SoapClient($wsdl_url);
$params = array('API' => $merchantID , 'Amount' => $amount, 'InvoiceId' => $au);
$res = $client->verifypayment($params);
$resResult = $res->verifypaymentResult;
if ($payment[payment_status] == 1)
{
if ( ! empty($resResult) and intval($resResult) == 1)
{
$output[status]		= 1;
$output[res_num]	= $au;
$output[ref_num]	= $ref_id;
$output[payment_id]	= $payment[payment_id];
}
else
{

$output[status]	= 0;
$output[message]= 'پرداخت توسط کارت خوان انجام نشده است .' . $resResult;
}
}
else
{

$output[status]	= 0;
$output[message]= 'سفارش قبلا پرداخت شده است.';
}
}
else
{

$output[status]	= 0;
$output[message]= 'شماره یکتا اشتباه است.';
}
return $output;
}