Skip to content

Commit

Permalink
Double request bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Derrick Egersdorfer committed Oct 29, 2015
1 parent 0a370f3 commit a19fb97
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Aeon/Aeon.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public function execute()

// Check for an error
if( ! isset($response['data']['ErrorCode']) ){
$this->response[] = $object->execute($this->config);
$this->response[] = $response;
}

// Show error
else{
throw new \Exception('ERROR (Code '.$response['data']['ErrorCode'].') ' . $response['data']['ErrorText']);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Aeon/Request/Electricity.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ public function execute($config)
$xml_post_string_one = '<request><EventType>ConfirmMeter</EventType><event><DeviceId>'.$config['DeviceId'].'</DeviceId><DeviceSer>'.$config['DeviceSer'].'</DeviceSer><UserPin>'.$config['UserPin'].'</UserPin><MeterNum>'.$this->meterNumber.'</MeterNum><Amount>'.$this->credit.'</Amount><Reference>'.$this->reference.'</Reference></event></request>'.PHP_EOL;
$xml_post_string_two = '<request><SessionId></SessionId><EventType>GetVoucher</EventType><event><Type></Type><TransRef></TransRef><Reference>'.$this->reference.'</Reference></event></request>'.PHP_EOL;

// Create steps three's confirmation settings
// Create step three's confirmation settings
$xml_post_string_thr = '<request><EventType>SoldVoucher</EventType><event><DeviceId>'.$config['DeviceId'].'</DeviceId><DeviceSer>'.$config['DeviceSer'].'</DeviceSer><UserPin>'.$config['UserPin'].'</UserPin><TransRef></TransRef><Reference>'.$this->reference.'</Reference></event></request>'.PHP_EOL;

// Create a TCP/IP socket
$socket = new \CodeChap\Aeon\Socket($config);

// STEP 1. Authenticate //
// STEP 1. AUTHENTICATE //

// Send confirmation request
$socket->write($xml_post_string_one);

// Get result of send
$result_one = $socket->get();

// STEP 2. Buy //
// STEP 2. BUY //

// Find session id field
preg_match('/<SessionId>(.*)<\/SessionId>/', $result_one, $SessionId);
// Find transfer reference field
preg_match('/<TransRef>(.*)<\/TransRef>/', $result_one, $TransRef);
// Swop in Session ID
// Swop in Session ID and transfer ref with first result
$xml_post_string_two = preg_replace('/<SessionId><\/SessionId>/', $SessionId[0], $xml_post_string_two);
// Swop in transfer reference
$xml_post_string_two = preg_replace('/<TransRef><\/TransRef>/', $TransRef[0], $xml_post_string_two);

// Send voucher request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@ public function execute($config)
// Create a TCP/IP socket
$socket = new \CodeChap\Aeon\Socket($config);

// STEP 1. Authenticate //

// Send confirmation request
$socket->write($xml_post_string_one);

// Get result of send
$result_one = $socket->get();
$result = $socket->get();

//Close the socket
$socket->close();

// Done
$finalResult = \LSS\XML2Array::createArray($result_one);
$finalResult = \LSS\XML2Array::createArray($result);

// Return usefull data
return $finalResult['response'];
Expand Down
29 changes: 21 additions & 8 deletions src/Aeon/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,40 @@
/**
* Holds the open socket conenction
*/
public $socket = false;
public static $socket = false;

/**
* Opens a persistant socket connection
*/
public function __construct($config)
{
if( ! $this->socket = pfsockopen($config['ip'], $config['port'], $errno, $errstr, 30)){
// Connect
if( ! static::$socket = pfsockopen($config['ip'], $config['port'], $errno, $errstr, 30)){
throw new Exception("Unable to connect to " . $config['ip'] . ' on port ' . $config['port']);
}

// FuelPHP logger
@\Log::debug('SOCKET OPEN');
}

/**
* Writes to the socket
*/
public function write($xmlPostString)
{
fwrite($this->socket, $xmlPostString);
// FuelPHP logger
@\Log::debug('SOCKET: ' . trim($xmlPostString));

fwrite(static::$socket, $xmlPostString);
}

/**
* Gets the results of the socket
*/
public function get()
{
while( $buffer = fgets($this->socket, 1024) ){
while( $buffer = fgets(static::$socket, 1024) ){

$response = isset($response) ? $response.$buffer : $buffer;

if(preg_match('/<\/response>/', $buffer)){
Expand All @@ -53,6 +60,9 @@ public function get()
// Check for complete response
if(isset($response)){

// FuelPHP logger
@\Log::debug('SOCKET: ' . trim($response));

// Check for error code
if(preg_match('/<EventCode>(.*)<\/EventCode>/', $response, $error)){
if($error[1] !== '0'){
Expand All @@ -61,7 +71,7 @@ public function get()
preg_match('/<ErrorCode>(.*)<\/ErrorCode>/', $response, $errorCode);
preg_match('/<ErrorText>(.*)<\/ErrorText>/', $response, $errorText);

// Thrw exception
// Throw exception
throw new \Exception('Error code ' . $errorCode[1] . ': ' . $errorText[1]);
}

Expand All @@ -80,8 +90,11 @@ public function get()
*/
public function close()
{
fclose($this->socket);
fclose(static::$socket);

static::$socket = false;

$this->socket = false;
// FuelPHP logger
@\Log::debug('SOCKET CLOSED');
}
}

0 comments on commit a19fb97

Please sign in to comment.