<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL4HLD2A8696 - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Real Estate Agency
 *	DOMAIN: gevimmo.com
 *	FILE: STRIPE.INC.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive,
 *	non-transferable license for one domain or IP address. Therefore distribution,
 *	sale or transfer of the file in whole or in part without permission of Flynax
 *	respective owners is considered to be illegal and breach of Flynax License End
 *	User Agreement.
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2017 |  All copyrights reserved.
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/
$reefless->loadClass('Categories');
$reefless->loadClass('Stripe', null, 'stripe');
$rlStripe->getCardTypes(true);
$rlSmarty->assign('countries', $rlCategories->getDF('countries'));

/* if Shopping Cart plugin */
$service = version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0 ? $rlPayment->getOption('serive') : $_SESSION['complete_payment']['service'];

if (($service == 'shopping' || $service == 'auction') && $GLOBALS['config']['shc_method'] == 'multi') {
	$rlStripe->setDealerCredentials((int)$_SESSION['complete_payment']['item_id']);
}

if ($_POST['form']) {
	$errors = false;
	$error_fields = array();
	$data = $_POST['f'];

	// validate fields
	if($_POST['use_account_info'] && empty($account_info['Mail'])) {
		$errors[] = $lang['account_email_not_exists'];
	} 
	if(!$data['email'] && !$_POST['use_account_info'] && version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0) {
		$errors[] = str_replace('{field}', "<b>\"{$lang['mail']}\"</b>", $lang['notice_field_empty']);
		array_push($error_fields, 'email');
	}
	if (!$data['card_number']) {
		$errors[] = str_replace('{field}', "<b>\"{$lang['stripe_card_number']}\"</b>", $lang['notice_field_empty']);
		array_push($error_fields, 'card_number');
	}
	if (!$data['exp_month']) {
		$errors[] = str_replace('{field}', "<b>\"{$lang['stripe_card_expiry_date']}\"</b>", $lang['notice_field_empty']);
		array_push($error_fields, 'exp_month');
	}
	if (!$data['exp_year']) {
		$errors[] = str_replace('{field}', "<b>\"{$lang['stripe_card_expiry_year']}\"</b>", $lang['notice_field_empty']);
		array_push($error_fields, 'exp_year');
	}
	if (!$errors) {
		$rlStripe->init();
		if (version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0) {
			$rlPayment->setOption('token', $_POST['stripe-token']);
			$rlPayment->setOption('email', $data['email'] ? $data['email'] : $account_info['Mail']);
		}
		$rlStripe->callBack();

		if (!$rlStripe->hasError()) {
			$redirect_url = version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0 ? $rlPayment->getOption('success_url') : $_SESSION['complete_payment']['callback']['success_url'];
			$reefless->redirect(false, str_replace("&amp;", "&", $redirect_url));
		} else {
			$errors = $rlStripe->getErrors();
		}
	}
}