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