Skip to content

Commit 63d54d8

Browse files
authored
Merge pull request #12 from lamoda/feature/get-state-additional-info
add additionalFields in GetState response
2 parents db95109 + 32e1a74 commit 63d54d8

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
vendor/
22
composer.lock
33

4+
.idea
5+
46
.php_cs.cache

src/TerminalResponse.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ final class TerminalResponse
8181
*/
8282
private $errorCode = '';
8383

84+
/**
85+
* @var array|null
86+
*/
87+
private $additionalInfo;
88+
8489
/**
8590
* @param string $success Operation success flag
8691
* @param mixed $orderId Payment ID in Merchant system
@@ -116,6 +121,11 @@ public function getRrn(): ?string
116121
return $this->rrn;
117122
}
118123

124+
public function getAdditionalInfo(): ?array
125+
{
126+
return $this->additionalInfo;
127+
}
128+
119129
public function getSessionId(): string
120130
{
121131
return $this->sessionId;
@@ -159,6 +169,11 @@ public function setRrn(string $rrn): void
159169
$this->rrn = $rrn;
160170
}
161171

172+
public function setAdditionalInfo(array $additionalInfo): void
173+
{
174+
$this->additionalInfo = $additionalInfo;
175+
}
176+
162177
public function isNewState(): bool
163178
{
164179
return $this->isStateEqual(static::STATE_NEW);

src/TerminalResponseBuilder.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public static function parseTransportResponse(
1616
string $transportResponse,
1717
PaytureOperation $operation
1818
): TerminalResponse {
19-
$attributes = self::parseAttributesFromXmlResponse($transportResponse, self::mapOperationToRootNode($operation));
19+
$response = self::parseXmlResponse($transportResponse, self::mapOperationToRootNode($operation));
20+
21+
$attributes = $response['@attributes'];
2022

2123
if (!isset($attributes['Success'])) {
2224
throw InvalidResponseException::becauseUndefinedSuccessAttribute();
@@ -48,13 +50,17 @@ public static function parseTransportResponse(
4850
$result->setRrn($attributes['RRN']);
4951
}
5052

53+
if (isset($response['AddInfo'])) {
54+
$result->setAdditionalInfo($response['AddInfo']);
55+
}
56+
5157
return $result;
5258
}
5359

5460
/**
5561
* @throws InvalidResponseException
5662
*/
57-
private static function parseAttributesFromXmlResponse(string $xml, string $operation): array
63+
private static function parseXmlResponse(string $xml, string $operation): array
5864
{
5965
$oldUseInternalXmlErrors = libxml_use_internal_errors(true);
6066
$rootNode = simplexml_load_string($xml);
@@ -74,7 +80,26 @@ private static function parseAttributesFromXmlResponse(string $xml, string $oper
7480
throw InvalidResponseException::becauseEmptyAttributes();
7581
}
7682

77-
return $data['@attributes'];
83+
$result = [];
84+
$result['@attributes'] = $data['@attributes'];
85+
86+
if (isset($data['AddInfo'])) {
87+
$result['AddInfo'] = self::parseAddInfo($data['AddInfo']);
88+
}
89+
90+
return $result;
91+
}
92+
93+
private static function parseAddInfo(array $rawAddInfo): array
94+
{
95+
$result = [];
96+
97+
foreach ($rawAddInfo as $info) {
98+
$infoArray = (array) $info;
99+
$result[$infoArray['@attributes']['Key']] = $infoArray['@attributes']['Value'];
100+
}
101+
102+
return $result;
78103
}
79104

80105
private static function mapOperationToRootNode(PaytureOperation $operation): string

tests/Unit/PaytureInPayTerminalTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,42 @@ public function testGetState(): void
173173
self::assertEquals($orderId, $response->getOrderId());
174174
}
175175

176+
public function testGetStateWithAdditionalInfo(): void
177+
{
178+
$rrn = '003770024290';
179+
$orderId = 'Order-123';
180+
$this->transport->expects($this->once())
181+
->method('request')
182+
->with(
183+
PaytureOperation::GET_STATE(),
184+
'apim',
185+
[
186+
'Key' => 'MerchantKey',
187+
'OrderId' => $orderId,
188+
]
189+
)->willReturn('<GetState Success="True" OrderId="'.$orderId.'" State="Refunded"
190+
Forwarded="False" MerchantContract="Merchant" Amount="12464" RRN="'.$rrn.'" VWUserLgn="123@ya.ru"
191+
CardId="bd712147-48da-2ffc-ef31-8341806c65cf" PANMask="521885xxxxxx5484">
192+
<AddInfo Key="PaymentSystem" Value="MasterCard" />
193+
<AddInfo Key="BankHumanName" Value="SBERBANK" />
194+
<AddInfo Key="BankCountryCode" Value="RU" />
195+
<AddInfo Key="BankCity" Value="" />
196+
</GetState>');
197+
198+
$response = $this->terminal->getState($orderId);
199+
200+
self::assertTrue($response->isSuccess());
201+
self::assertTrue($response->isRefundedState());
202+
self::assertEquals($rrn, $response->getRrn());
203+
self::assertEquals($orderId, $response->getOrderId());
204+
self::assertEquals([
205+
'BankCity' => '',
206+
'BankHumanName' => 'SBERBANK',
207+
'PaymentSystem' => 'MasterCard',
208+
'BankCountryCode' => 'RU'
209+
], $response->getAdditionalInfo());
210+
}
211+
176212
public function testCreatingPaymentUrl(): void
177213
{
178214
self::assertEquals(

0 commit comments

Comments
 (0)