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((string) $xml->Response == 'Approved');
$response->setResponseCode((string) $xml->ProcReturnCode);
if (!$response->isSuccess()) {
$errorMessages = array();
if (property_exists($xml, 'Error')) {
$errorMessages[] = sprintf('Error: %s', (string) $xml->Error);
}
if (property_exists($xml, 'ErrMsg')) {
$errorMessages[] = sprintf('Error Message: %s ', (string) $xml->ErrMsg);
}
if (property_exists($xml, 'Extra') && property_exists($xml->Extra, 'HOSTMSG')) {
$errorMessages[] = sprintf('Host Message: %s', (string) $xml->Extra->HOSTMSG);
}
$errorMessage = implode(' ', $errorMessages);
$response->setResponseMessage($errorMessage);
} else {
$response->setResponseMessage('Success');
$response->setOrderId((string) $xml->OrderId);
$response->setTransactionId((string) $xml->TransId);
}
$event = $response->isSuccess() ? self::EVENT_ON_TRANSACTION_SUCCESSFUL : self::EVENT_ON_TRANSACTION_FAILED;
$this->getDispatcher()->dispatch($event, new PaymentEventArg(null, $response, $transactionType));
return $response;
}