1
|
|
|
<?php |
2
|
|
|
namespace Paranoia\Processor\Posnet; |
3
|
|
|
|
4
|
|
|
use Paranoia\Exception\BadResponseException; |
5
|
|
|
use Paranoia\Processor\AbstractResponseProcessor; |
6
|
|
|
use Paranoia\Response; |
7
|
|
|
|
8
|
|
|
abstract class BaseResponseProcessor extends AbstractResponseProcessor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \SimpleXMLElement $xml |
12
|
|
|
* @param Response $response |
13
|
|
|
*/ |
14
|
|
|
private function prepareErrorDetails(\SimpleXMLElement $xml, Response $response) |
15
|
|
|
{ |
16
|
|
|
$errorMessages = array(); |
17
|
|
|
if (property_exists($xml, 'respCode')) { |
18
|
|
|
$response->setResponseCode((string)$xml->respCode); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
if (property_exists($xml, 'respText')) { |
21
|
|
|
$errorMessages[] = sprintf('%s ', (string)$xml->respText); |
22
|
|
|
} |
23
|
|
|
$errorMessage = implode(' ', $errorMessages); |
24
|
|
|
$response->setResponseMessage($errorMessage); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \SimpleXMLElement $xml |
29
|
|
|
* @param Response $response |
30
|
|
|
*/ |
31
|
|
|
private function prepareTransactionDetails(\SimpleXMLElement $xml, Response $response) |
32
|
|
|
{ |
33
|
|
|
if (property_exists($xml, 'orderId')) { |
34
|
|
|
$response->setOrderId((string)$xml->orderId); |
35
|
|
|
} |
36
|
|
|
$response->setTransactionId((string)$xml->hostlogkey); |
37
|
|
|
if (property_exists($xml, 'authCode')) { |
38
|
|
|
$response->setAuthCode((string)$xml->authCode); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $rawResponse |
44
|
|
|
* @return Response |
45
|
|
|
* @throws BadResponseException |
46
|
|
|
*/ |
47
|
|
|
protected function processCommonResponse($rawResponse) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
/** @var \SimpleXMLElement $xml */ |
51
|
|
|
$xml = new \SimpleXmlElement($rawResponse); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
$exception = new BadResponseException('Provider returned unexpected response: ' . $rawResponse); |
54
|
|
|
throw $exception; |
55
|
|
|
} |
56
|
|
|
$this->validateResponse($xml); |
57
|
|
|
$response = new Response(); |
58
|
|
|
$response->setIsSuccess((int)$xml->approved > 0); |
59
|
|
|
if (!$response->isSuccess()) { |
60
|
|
|
$this->prepareErrorDetails($xml, $response); |
61
|
|
|
} else { |
62
|
|
|
$this->prepareTransactionDetails($xml, $response); |
63
|
|
|
} |
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function validateResponse($transformedResponse) |
68
|
|
|
{ |
69
|
|
|
//TODO: response validation should implemented. |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|