<?php

require_once ROOT_PATH.'/zipmarket/apps/payments/models/payments_abstract.class.php';

class payline extends payments_abstract {
	
	public function generateForm($order_data = array()) {
		global $langArray, $currency, $config, $languageURL;
		
		$api = $this->getMeta('payline_api'); //'adxcv-zzadq-polkjsad-opp13opoz-1sdf455aadzmck1244567';
		$url = 'http://' . $config['domain'] . '/' . $languageURL . 'payments/payline/?oID='.(int)$order_data['id'];
		$result = $this->send('http://payline.ir/payment-test/gateway-send', $api, intval($order_data['price']) * 10, urlencode($url), $this->getUserData('firstname'), $this->getUserData('lastname'), $this->getUserData('email'));
		
		$form  = '<form class="form-for-pay" action="http://payline.ir/payment-test/gateway-'.$result.'" method="post" id="payline_form_submit">'."\n";
		// htmlspecialchars($order_data['item_name'], ENT_QUOTES, 'utf-8')
		// http://' . $config['domain'] . '/' . $languageURL . 'payments/payline/pdt/
		// http://' . $config['domain'] . '/' . $languageURL . 'items/' . $order_data['item_id'] . '/' . url($order_data['item_name']) . '.html
		$form .= '<button id="payline-purchase-button" class="btn-icon purchase" type="submit">خرید کنید</button>'."\n";
		$form .= '</form>'."\n";
		
		return $form;
	}
	
	private function send($url,$api,$amount,$redirect,$name='',$family='',$email='')
	{
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_POSTFIELDS,"api=$api&amount=$amount&redirect=$redirect&name=$name $family&email=$email");
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$res = curl_exec($ch);
		curl_close($ch);
		return $res;
	}

	public function generateDepositForm($order_data = array()) {
		global $langArray, $currency, $config, $languageURL;
		
		$api = $this->getMeta('payline_api'); //'adxcv-zzadq-polkjsad-opp13opoz-1sdf455aadzmck1244567';
		$url = 'http://' . $config['domain'] . '/' . $languageURL . 'payments/deposit_payline/?oID='.(int)$order_data['id'];
		$result = $this->send('http://payline.ir/payment-test/gateway-send', $api, intval($order_data['deposit']) * 10, urlencode($url), $this->getUserData('firstname'), $this->getUserData('lastname'), $this->getUserData('email'));
		
		$form  = '<form class="form-for-pay" action="http://payline.ir/payment-test/gateway-'.$result.'" method="post" id="payline_form_submit">'."\n";
		// (int)$order_data['id']
		// http://' . $config['domain'] . '/' . $languageURL . 'payments/deposit_payline/pdt/
		// http://' . $config['domain'] . '/' . $languageURL . 'users/deposit
		$form .= '<button id="payline-purchase-button" class="btn-icon purchase" type="submit">خرید کنید</button>'."\n";
		$form .= '</form>'."\n";
		
		return $form;
	}
	
}

?>