protected function parseResponse($rawResponse, $transactionType)
{
$response = new PaymentResponse();
try {
/**
* @var object $xml
*/
$xml = new \SimpleXmlElement($rawResponse);
} catch (\Exception $e) {
$exception = new UnexpectedResponse('Provider returned unexpected response: ' . $rawResponse);
$eventArg = new PaymentEventArg(null, null, $transactionType, $exception);
$this->getDispatcher()->dispatch(self::EVENT_ON_EXCEPTION, $eventArg);
throw $exception;
}
$response->setIsSuccess('00' == (string) $xml->Transaction->Response->Code);
$response->setResponseCode((string) $xml->Transaction->ReasonCode);
if (!$response->isSuccess()) {
$errorMessages = array();
if (property_exists($xml->Transaction->Response, 'ErrorMsg')) {
$errorMessages[] = sprintf('Error Message: %s', (string) $xml->Transaction->Response->ErrorMsg);
}
if (property_exists($xml->Transaction->Response, 'SysErrMsg')) {
$errorMessages[] = sprintf('System Error Message: %s', (string) $xml->Transaction->Response->SysErrMsg);
}
$errorMessage = implode(' ', $errorMessages);
$response->setResponseMessage($errorMessage);
} else {
$response->setResponseMessage('Success');
$response->setOrderId((string) $xml->Order->OrderID);
$response->setTransactionId((string) $xml->Transaction->RetrefNum);
}
$event = $response->isSuccess() ? self::EVENT_ON_TRANSACTION_SUCCESSFUL : self::EVENT_ON_TRANSACTION_FAILED;
$this->getDispatcher()->dispatch($event, new PaymentEventArg(null, $response, $transactionType));
return $response;
}