1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lamoda\Payture\InPayClient\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Lamoda\Payture\InPayClient\PaytureInPayTerminal; |
6
|
|
|
use Lamoda\Payture\InPayClient\PaytureOperation; |
7
|
|
|
use Lamoda\Payture\InPayClient\SessionType; |
8
|
|
|
use Lamoda\Payture\InPayClient\TerminalConfiguration; |
9
|
|
|
use Lamoda\Payture\InPayClient\TransportInterface; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers \Lamoda\Payture\InPayClient\PaytureInPayTerminal |
14
|
|
|
*/ |
15
|
|
|
final class PaytureInPayTerminalTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
private $config; |
18
|
|
|
private $transport; |
19
|
|
|
|
20
|
|
|
/** @var PaytureInPayTerminal */ |
21
|
|
|
private $terminal; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @dataProvider getInitSessionTypes |
25
|
|
|
* |
26
|
|
|
* @param SessionType $type |
27
|
|
|
* @param string $data |
28
|
|
|
* |
29
|
|
|
* @throws \Lamoda\Payture\InPayClient\Exception\TransportException |
30
|
|
|
*/ |
31
|
|
|
public function testPaymentInit(SessionType $type, string $data): void |
32
|
|
|
{ |
33
|
|
|
$this->transport->expects($this->once()) |
34
|
|
|
->method('request') |
35
|
|
|
->with( |
36
|
|
|
PaytureOperation::INIT(), |
37
|
|
|
'apim', |
38
|
|
|
[ |
39
|
|
|
'Key' => 'MerchantKey', |
40
|
|
|
'Data' => $data, |
41
|
|
|
] |
42
|
|
|
)->willReturn('<Init Success="True" SessionId="external-id"/>'); |
43
|
|
|
|
44
|
|
|
$response = $this->terminal->init( |
45
|
|
|
$type, |
46
|
|
|
'Order-123', |
47
|
|
|
'The order', |
48
|
|
|
10000, |
49
|
|
|
'127.0.0.1', |
50
|
|
|
'https://redirect-me.back/', |
51
|
|
|
'template', |
52
|
|
|
[ |
53
|
|
|
'custom_data' => 'value', |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
self::assertTrue($response->isSuccess()); |
58
|
|
|
self::assertEquals('external-id', $response->getSessionId()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getInitSessionTypes() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
[ |
65
|
|
|
SessionType::PAY(), |
66
|
|
|
'SessionType=Pay;OrderId=Order-123;Amount=10000;IP=127.0.0.1;Product=The+order;Url=https%3A%2F%2Fredirect-me.back%2F;TemplateTag=template;custom_data=value', |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
SessionType::BLOCK(), |
70
|
|
|
'SessionType=Block;OrderId=Order-123;Amount=10000;IP=127.0.0.1;Product=The+order;Url=https%3A%2F%2Fredirect-me.back%2F;TemplateTag=template;custom_data=value', |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testPaymentCharge(): void |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->transport->expects($this->once()) |
78
|
|
|
->method('request') |
79
|
|
|
->with( |
80
|
|
|
PaytureOperation::CHARGE(), |
81
|
|
|
'apim', |
82
|
|
|
[ |
83
|
|
|
'Key' => 'MerchantKey', |
84
|
|
|
'OrderId' => 'Order-123', |
85
|
|
|
'Amount' => 10000, |
86
|
|
|
'Password' => 'MerchantPassword', |
87
|
|
|
] |
88
|
|
|
)->willReturn('<Charge Success="True" Amount="10000"/>'); |
89
|
|
|
|
90
|
|
|
$response = $this->terminal->charge('Order-123', 10000); |
91
|
|
|
|
92
|
|
|
self::assertTrue($response->isSuccess()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testPaymentUnblock(): void |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->transport->expects($this->once()) |
98
|
|
|
->method('request') |
99
|
|
|
->with( |
100
|
|
|
PaytureOperation::UNBLOCK(), |
101
|
|
|
'apim', |
102
|
|
|
[ |
103
|
|
|
'Key' => 'MerchantKey', |
104
|
|
|
'OrderId' => 'Order-123', |
105
|
|
|
'Amount' => 10000, |
106
|
|
|
'Password' => 'MerchantPassword', |
107
|
|
|
] |
108
|
|
|
)->willReturn('<Unblock Success="True"/>'); |
109
|
|
|
|
110
|
|
|
$response = $this->terminal->unblock('Order-123', 10000); |
111
|
|
|
|
112
|
|
|
self::assertTrue($response->isSuccess()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testPaymentRefund(): void |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->transport->expects($this->once()) |
118
|
|
|
->method('request') |
119
|
|
|
->with( |
120
|
|
|
PaytureOperation::REFUND(), |
121
|
|
|
'apim', |
122
|
|
|
[ |
123
|
|
|
'Key' => 'MerchantKey', |
124
|
|
|
'OrderId' => 'Order-123', |
125
|
|
|
'Amount' => 6000, |
126
|
|
|
'Password' => 'MerchantPassword', |
127
|
|
|
] |
128
|
|
|
)->willReturn('<Refund Success="True" NewAmount="4000"/>'); |
129
|
|
|
|
130
|
|
|
$response = $this->terminal->refund('Order-123', 6000); |
131
|
|
|
|
132
|
|
|
self::assertTrue($response->isSuccess()); |
133
|
|
|
self::assertEquals(4000, $response->getAmount()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testPaymentStatus(): void |
137
|
|
|
{ |
138
|
|
|
$this->transport->expects($this->once()) |
139
|
|
|
->method('request') |
140
|
|
|
->with( |
141
|
|
|
PaytureOperation::PAY_STATUS(), |
142
|
|
|
'apim', |
143
|
|
|
[ |
144
|
|
|
'Key' => 'MerchantKey', |
145
|
|
|
'OrderId' => 'Order-123', |
146
|
|
|
] |
147
|
|
|
)->willReturn('<PayStatus Success="True" State="Charged" Amount="10000"/>'); |
148
|
|
|
|
149
|
|
|
$response = $this->terminal->payStatus('Order-123'); |
|
|
|
|
150
|
|
|
|
151
|
|
|
self::assertTrue($response->isSuccess()); |
152
|
|
|
self::assertTrue($response->isChargedState()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetState(): void |
156
|
|
|
{ |
157
|
|
|
$rrn = '003770024290'; |
158
|
|
|
$orderId = 'Order-123'; |
159
|
|
|
$this->transport->expects($this->once()) |
160
|
|
|
->method('request') |
161
|
|
|
->with( |
162
|
|
|
PaytureOperation::GET_STATE(), |
163
|
|
|
'apim', |
164
|
|
|
[ |
165
|
|
|
'Key' => 'MerchantKey', |
166
|
|
|
'OrderId' => $orderId, |
167
|
|
|
] |
168
|
|
|
)->willReturn('<GetState Success="True" OrderId="' . $orderId . '" State="Refunded" |
169
|
|
|
Forwarded="False" MerchantContract="Merchant" Amount="12461" RRN="' . $rrn . '"/>'); |
170
|
|
|
|
171
|
|
|
$response = $this->terminal->getState($orderId); |
172
|
|
|
|
173
|
|
|
self::assertTrue($response->isSuccess()); |
174
|
|
|
self::assertTrue($response->isRefundedState()); |
175
|
|
|
self::assertEquals($rrn, $response->getRrn()); |
176
|
|
|
self::assertEquals($orderId, $response->getOrderId()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testCreatingPaymentUrl(): void |
180
|
|
|
{ |
181
|
|
|
self::assertEquals( |
182
|
|
|
'https://nowhere.payture.com/apim/Pay?SessionId=external-id', |
183
|
|
|
$this->terminal->createPaymentUrl('external-id') |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function setUp() |
188
|
|
|
{ |
189
|
|
|
$this->config = new TerminalConfiguration('MerchantKey', 'MerchantPassword', 'https://nowhere.payture.com/'); |
190
|
|
|
$this->transport = $this->createMock(TransportInterface::class); |
191
|
|
|
|
192
|
|
|
$this->terminal = new PaytureInPayTerminal($this->config, $this->transport); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.