<?php
$data = array(
	'name' => 'OneCard',
	'label' => 'onecard',
	'settings' => array(
		array(
			'name' => 'Merchant ID',
			'label' => 'onecard_id',
			'type' => 'text',
			'items' => array(),
			'value' => '',
			'help' => 'Type in your OneCard merchant ID.',
		),
		array(
			'name' => 'Password',
			'label' => 'onecard_secret',
			'type' => 'text',
			'items' => array(),
			'value' => '',
			'help' => 'Type in your Password (must match the one you have set in your OneCard account).',
		),
		array(
			'name' => 'Transkey',
			'label' => 'onecard_transkey',
			'type' => 'text',
			'items' => array(),
			'value' => '',
			'help' => 'Type in your OneCard Transkey',
		),
		array(
			'name' => 'Keyword',
			'label' => 'onecard_keyword',
			'type' => 'text',
			'items' => array(),
			'value' => '',
			'help' => 'Type in your OneCard Keyword',
		),
		array(
			'name' => 'Currency',
			'label' => 'onecard_currency',
			'type' => 'select',
			'items' => array(
				'USD' => '(USD) U.S. Dollar',
				'CSH' => '(CSH) OneCard Points',
				'AED' => '(UAE) UAE Dirham',
				'EUR' => '(EUR) Euro',
				'JOD' => '(JOD) Jordanian Dollar',
				'EGP' => '(EGP) Egyptian Pound',
				'SAR' => '(SAR) Saudi Riyal',
				'DZD' => '(DZD) Algerian Dinar',
				'LBP' => '(LBP) Lebanese Pound',
				'MAD' => '(MAD) Moroccan Dirham',
				'QAR' => '(QAR) Qatar Riyal',
				'TRY' => '(TRY) Turkish Lira',
			),
			'value' => '',
			'help' => 'Select preferred currency.',
		),
		array(
			'name' => 'Test mode',
			'label' => 'onecard_test',
			'type' => 'boolean',
			'items' => array(),
			'value' => '0',
			'help' => 'Enable if you want to run test transactions.',
		),
		array(
			'name' => 'Return URL',
			'label' => 'onecard_return_url',
			'type' => 'text',
			'items' => array(),
			'value' => VIR_PATH.($PREFS->conf['fancy_urls'] ? 'account/upgrade/ipn/onecard/' : 'index.php?m=account_upgrade&p=ipn&id=onecard'),
			'help' => 'Type in this URL in your OneCard account.',
		),
	),
);
//------------------------------------------------
// Payment form
//------------------------------------------------
function onecard_payment_form($package_id, $package_name, $package_amount, $package_group_id, $package_credits, $package_term_length, $package_term_type, $package_recurring, $member_gift_id = 0)
{
	global $DB, $PREFS, $SESSION, $LANG, $TEMPLATE;

	onecard_fetch_settings();

	$post_url = 'http://onecard.n2vsb.com/customer/integratedPayment.html';

	$html = '
    <form action="'.$post_url.'" method="post">
		<input type="submit" name="submit" value="Pay Now" class="submit" />
		<input type="hidden" name="merchant_id" value="'.$PREFS->conf['gateways']['onecard']['onecard_id'].'">
		<input type="hidden" name="onecard_secret" value="'.$PREFS->conf['gateways']['onecard']['onecard_transkey'].'">
		<input type="hidden" name="amount" value="'.$package_amount.'">
		<input type="hidden" name="currency" value="'.$PREFS->conf['gateways']['onecard']['onecard_currency'].'">
		<input type="hidden" name="language" value="'.($SESSION->conf['language'] == 'english' ? 'en' : 'ar').'">
		<input type="hidden" name="display_text" value="'.htmlentities2utf8($package_name).'">
		<input type="hidden" name="txt1" value="'.htmlentities2utf8($package_name).'">
		<input type="hidden" name="txt2" value="'.$SESSION->conf['member_id'].'x'.$package_id.($member_gift_id ? 'x'.$member_gift_id : '').'">
		<input type="hidden" name="onecard_keyword" value="'.$PREFS->conf['gateways']['onecard']['onecard_keyword'].'">
		<input type="hidden" name="token" value="'.md5(strtolower($PREFS->conf['gateways']['onecard']['onecard_id'].':'.$PREFS->conf['gateways']['onecard']['onecard_transkey'].':'.$package_amount.':'.$PREFS->conf['gateways']['onecard']['onecard_currency']).':'.$PREFS->conf['gateways']['onecard']['onecard_keyword']).'">
		<input type="hidden" name="test_mode" value="'.($PREFS->conf['gateways']['onecard']['onecard_test'] ? 1 : 0).'">
	</form> ';

	return $html;
}
//------------------------------------------------
// Process payment
//------------------------------------------------
function onecard_ipn()
{
	global $DB, $PREFS, $SESSION, $LANG, $TEMPLATE;

	if ( !count($_POST) ) return;
	onecard_fetch_settings();

	if ( isset($_POST['sRequest']) && $_POST['sRequest'] ) {

		$xml = $_POST['sRequest'];
		$p = xml_parser_create();
		xml_parse_into_struct($p, $xml, $vals, $index);
		xml_parser_free($p);

		$xml = array();
		foreach ( $vals as $val ) {
			$xml[strtolower($val['tag'])] = $val['value'];
		}

		$txn_id = isset($xml['onecard_trnid']) ? $xml['onecard_trnid'] : '';
		$payment_amount = isset($xml['amount']) ? $xml['amount'] : '';
		$payment_currency = isset($xml['currency']) ? $xml['currency'] : '';
		$hash = isset($xml['onecardtoken']) ? $xml['onecardtoken'] : '';
		$custom = isset($xml['txt2']) ? explode('x', $xml['txt2']) : '';

		if ( strcmp($hash, md5(strtolower($PREFS->conf['gateways']['onecard']['onecard_id'].':'.trim($txn_id).':'.$PREFS->conf['gateways']['onecard']['onecard_secret']))) !== 0 ) return;
	}
	else {
		$txn_id = isset($_POST['trn_id']) ? $_POST['trn_id'] : '';
		$payment_amount = isset($_POST['amount']) ? $_POST['amount'] : '';
		$payment_currency = isset($_POST['currency']) ? $_POST['currency'] : '';
		$token = isset($_POST['token']) ? $_POST['token'] : '';
		$hash = isset($_POST['verificationString']) ? $_POST['verificationString'] : '';
		$custom = isset($_POST['txt2']) ? explode('x', $_POST['txt2']) : '';

		if ( strcmp($token, md5(strtolower($PREFS->conf['gateways']['onecard']['onecard_id'].':'.$payment_amount.':'.$payment_currency).':'.$PREFS->conf['gateways']['onecard']['onecard_secret'])) !== 0 ) return;
		if ( strcmp($hash, sha1(strtolower($PREFS->conf['gateways']['onecard']['onecard_id']).':'.trim($txn_id).':'.$PREFS->conf['gateways']['onecard']['onecard_secret'])) !== 0 ) return;
	}

	$member_id = isset($custom[0]) ? mysql_real_escape_string($custom[0]) : 0;
	$package_id = isset($custom[1]) ? intval($custom[1]) : 0;
	$member_gift_id = isset($custom[2]) ? mysql_real_escape_string($custom[2]) : 0;

	$result = $DB->query("SELECT * FROM " . DB_PREFIX . "packages WHERE package_id='$package_id' LIMIT 1");
	if ( !$DB->num_rows($result) ) return;
	$obj = $DB->fetch_object($result);

	$package_price = $obj->price;
	$package_term = $obj->term;
	$package_type = $obj->termtype;
	$package_credits = $obj->credits;
	$new_group_id = $obj->group_id;


	$result = $DB->query("SELECT order_id FROM " . DB_PREFIX . "orders WHERE txn_id='cu".$txn_id."' LIMIT 1");
	if ( $DB->num_rows($result) ) return;

	if ( $payment_amount != $package_price ) return;

	$DB->query("INSERT INTO " . DB_PREFIX . "orders (member_id, member_gift_id, package_id, txn_id, paymentdate, amount, credits, term, termtype)
				VALUES('$member_id', '$member_gift_id', '$package_id', 'cu".$txn_id."', '" . time() . "', '$package_price', '$package_credits', '$package_term', '$package_type')");

	set_membership_term($member_id, $member_gift_id, $new_group_id, $package_type, $package_term, $package_credits);

	if ( isset($_POST['sRequest']) && $_POST['sRequest'] ) {
		$post = array(
			'sRequest' => '<onecardTransaction><merchant_id>'.$xml['merchant_id'].'</merchant_id>'.
			'<cashU_trnID>'.$xml['onecard_trnid'].'</onecard_trnID>'.
			'<cashUToken>'.$xml['onecardtoken'].'</onecardToken>'.
			'<responseCode>OK</responseCode>'.
			'<responseDate>'.$xml['trndate'].'</responseDate></onecardTransaction>'
		);
		$response = send_remote_request('http://onecard.n2vsb.com/remote/paymentIntegrationQuery.html', $post);
	}

	redirect(VIR_PATH.($PREFS->conf['fancy_urls'] ? 'account/upgrade/success/' : 'index.php?m=account_upgrade&p=success'));
}


//------------------------------------------------
// Fetch settings
//------------------------------------------------
function onecard_fetch_settings()
{
	global $DB, $PREFS;

	$result = $DB->query("SELECT * FROM " . DB_PREFIX . "payment_gateways WHERE label='onecard' LIMIT 1");

	if ($DB->num_rows($result))
	{
		$obj = $DB->fetch_object($result);
		$PREFS->conf['gateways']['onecard'] = @unserialize($obj->settings);
	}
}
?>