<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Viettel Class
 * 
 * @author dzungnv02
 *
 */

class Sendmessage extends MX_Controller 
{
	private $_xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
		
	public function __construct() 
	{
		parent::__construct();
	}
	
	public function testmo() {
		$url = 'http://10.58.38.162/violetservice/soanbai/mo';
		//$url = 'http://viettel.violet.vn/violetservice/soanbai/mo';
		$aryParams = array(
				'username' => 'sbg',
				'password' => 'sbg571285',
				'requestID' => '4',
				'userID' => '841662860000',
				'receiverID' => '84988568786',
				'serviceID' => '8062',
				'commandCode' => 'SBG',
				'contentType' => '0',
				'info' => 'SBG DK',
				'receiveTime' => date('Ymd H:i:s')
		);
		
		$client = new nusoap_client($url, false);
		$client->useHTTPPersistentConnection();
		$xml = $this->load->view('mo', null, true);
		
		$operator = 'MOReceiver';
		$operation = 'http://tempuri.org/MOReceiver';

		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;
		$result = $client->send($this->_xmlHeader.$xml, $operation);		
		
		//$result = $client->call('MOReceiver', $aryParams ,$operator ,$operation);
		
		$err = $client->getError();
			
		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}
		
		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";		
	}
	
	public function send($msg)
	{
		$url = $this->config->item('mt_service_url');
		$username = 'sbg';
		$password = 'sbg56ab77cdsgb';
		$operator = 'InsertMT';
		$operation = 'http://tempuri.org/InsertMT';
			
		$aryParams = array(
				'User' => $username,
				'Pass' => $password,
				'CPCode' => '601',
				'UserID' => '841662860000',
				'RequestID' => '4',
				'ReceiverID' => '84988568786',
				'ServiceID' => '8062',
				'CommandCode' => 'SBG',
				'ContentType' => '0',
				'Info' => urldecode($msg).' - Host: '.$_SERVER['HTTP_HOST'].' - '. date('Y-m-d H:i:s')//'Test MT '. date('Y-m-d H:i:s'),
		);
	
		$xml = $this->load->view('mt', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;
	
		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();
			
		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}
	
		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
	
	}
	
	public function topup () 
	{
		$url = $this->config->item('topup_service_url');
		$operator = 'TopupService';
		$operation = 'http://tempuri.org/ITopupService/TopUp';
		
		$username = 'sbg';
		$password = 'sbg56ab77cdsgb';
		
		$aryParams = array(
			'EWalletID' => 0,
			'CardSerial' => '11111',
			'PinCard' => 'eeeeeeeee'
		);
		
		$xml = $this->load->view('topup', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;
		
		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();
			
		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}
		
		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";		
	}
	
	public function charging ()
	{
		$url = $this->config->item('charging_service_url');
		
		$username = $this->config->item('charging_username');
		$password = $this->config->item('charging_password');
		
		$operator = 'processCharging';
		$operation = 'http://tempuri.org/processCharging';
		
		$aryParams = array(
				'user' => $username,
				'pass' => $password,
				'MSISDN' => '1689925260',
				'Charging' => '100',
				'Contents' => 'Namta-Active'
		);
		
		$xml = $this->load->view('charging', $aryParams, true);
		$client = new nusoap_client($url);
		$client->useHTTPPersistentConnection();
		$client->soap_defencoding = 'UTF-8';
		$client->operation = $operation;
		
		$result = $client->send($this->_xmlHeader.$xml, $operation);
		$err = $client->getError();
			
		if ($err) {
			echo '<p><b>Error: ' . $err . '</b></p>';
		}
		
		echo "<br/>";
		echo "<h2>Request</h2>";
		echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
		echo "<h2>Response</h2>";
		echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";		
	}
}