1
|
|
|
<?php |
2
|
|
|
namespace Paranoia\Processor\Gvp; |
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->Transaction->Response, 'ErrorMsg')) { |
18
|
|
|
$errorMessages[] = sprintf( |
19
|
|
|
'Error Message: %s', |
20
|
|
|
(string)$xml->Transaction->Response->ErrorMsg |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
if (property_exists($xml->Transaction->Response, 'SysErrMsg')) { |
24
|
|
|
$errorMessages[] = sprintf( |
25
|
|
|
'System Error Message: %s', |
26
|
|
|
(string)$xml->Transaction->Response->SysErrMsg |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
$errorMessage = implode(' ', $errorMessages); |
30
|
|
|
$response->setResponseMessage($errorMessage); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \SimpleXMLElement $xml |
35
|
|
|
* @param Response $response |
36
|
|
|
*/ |
37
|
|
|
private function prepareTransactionDetails(\SimpleXMLElement $xml, Response $response) |
38
|
|
|
{ |
39
|
|
|
if (property_exists($xml, 'Order') && property_exists($xml->Order, 'OrderID')) { |
40
|
|
|
$response->setOrderId((string)$xml->Order->OrderID); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (property_exists($xml, 'Transaction')) { |
44
|
|
|
if (property_exists($xml->Transaction, 'RetrefNum')) { |
45
|
|
|
$response->setTransactionId((string)$xml->Transaction->RetrefNum); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (property_exists($xml->Transaction, 'AuthCode')) { |
49
|
|
|
$response->setAuthCode((string)$xml->Transaction->AuthCode); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $rawResponse |
56
|
|
|
* @return Response |
57
|
|
|
* @throws BadResponseException |
58
|
|
|
*/ |
59
|
|
|
protected function processCommonResponse($rawResponse) |
60
|
|
|
{ |
61
|
|
|
$response = new Response(); |
62
|
|
|
try { |
63
|
|
|
/** @var \SimpleXMLElement $xml */ |
64
|
|
|
$xml = new \SimpleXmlElement($rawResponse); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
$exception = new BadResponseException('Provider returned unexpected response: ' . $rawResponse); |
67
|
|
|
throw $exception; |
68
|
|
|
} |
69
|
|
|
$this->validateResponse($xml); |
70
|
|
|
$response->setIsSuccess('00' == (string)$xml->Transaction->Response->Code); |
71
|
|
|
$response->setResponseCode((string)$xml->Transaction->Response->ReasonCode); |
|
|
|
|
72
|
|
|
if (!$response->isSuccess()) { |
73
|
|
|
$this->prepareErrorDetails($xml, $response); |
74
|
|
|
} else { |
75
|
|
|
$this->prepareTransactionDetails($xml, $response); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function validateResponse($transformedResponse) |
82
|
|
|
{ |
83
|
|
|
//TODO: response validation should implemented. |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|