PluginProbe
PayPlug for WooCommerce (Official) / 1.0.3
PayPlug for WooCommerce (Official) v1.0.3
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
payplug / vendor / payplug / payplug-php / tests / unit_tests / Resource / PaymentTest.php

PaymentTest.php in PayPlug for WooCommerce (Official) 1.0.3, at vendor/payplug/payplug-php/tests/unit_tests/Resource/PaymentTest.php

445 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Payplug\Resource;
3 use Payplug;
4
5 /**
6 * @group unit
7 * @group ci
8 * @group recommended
9 */
10 class PaymentTest extends \PHPUnit_Framework_TestCase
11 {
12 private $_requestMock;
13 private $_configuration;
14
15 protected function setUp()
16 {
17 $this->_configuration = new Payplug\Payplug('abc');
18 Payplug\Payplug::setDefaultConfiguration($this->_configuration);
19
20 $this->_requestMock = $this->getMock('\Payplug\Core\IHttpRequest');
21 Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock;
22 }
23
24 public function testCreatePaymentFromAttributes()
25 {
26 $payment = Payment::fromAttributes(array(
27 'id' => 'pay_490329',
28 'object' => 'payment',
29 'is_live' => true,
30 'amount' => 3300,
31 'amount_refunded' => 0,
32 'currency' => 'EUR',
33 'created_at' => 1410437760,
34 'is_paid' => true,
35 'is_refunded' => false,
36 'is_3ds' => false,
37 'save_card' => false,
38 'card' => array(
39 'last4' => '1800',
40 'country' => 'FR',
41 'exp_year' => 2017,
42 'exp_month' => 9,
43 'brand' => 'Mastercard'
44 ),
45 'customer' => array(
46 'email' => '[email protected]',
47 'first_name' => 'John',
48 'last_name' => 'Doe'
49 ),
50 'hosted_payment' => array(
51 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
52 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
53 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
54 'paid_at' => 1410437806
55 ),
56 'notification' => array(
57 'url' => 'http://yourwebsite.com/payplug_ipn',
58 'response_code' => 200
59 ),
60 'failure' => array(
61 'code' => null,
62 'message' => null
63 ),
64 'metadata' => array(
65 'a_custom_field' => 'a custom value',
66 'another_key' => 'another value'
67 )
68 ));
69
70 $this->assertEquals('pay_490329', $payment->id);
71 $this->assertEquals('payment', $payment->object);
72 $this->assertEquals(true, $payment->is_live);
73 $this->assertEquals(3300, $payment->amount);
74 $this->assertEquals(0, $payment->amount_refunded);
75 $this->assertEquals('EUR', $payment->currency);
76 $this->assertEquals(1410437760, $payment->created_at);
77 $this->assertEquals(true, $payment->is_paid);
78 $this->assertEquals(false, $payment->is_refunded);
79 $this->assertEquals(false, $payment->is_3ds);
80 $this->assertEquals(false, $payment->save_card);
81
82 $this->assertEquals('1800', $payment->card->last4);
83 $this->assertEquals('FR', $payment->card->country);
84 $this->assertEquals(2017, $payment->card->exp_year);
85 $this->assertEquals(9, $payment->card->exp_month);
86 $this->assertEquals('Mastercard', $payment->card->brand);
87
88 $this->assertEquals('[email protected]', $payment->customer->email);
89 $this->assertEquals('John', $payment->customer->first_name);
90 $this->assertEquals('Doe', $payment->customer->last_name);
91
92 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
93
94 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
95 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
96 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
97 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
98 $this->assertEquals(200, $payment->notification->response_code);
99
100 $this->assertNull($payment->failure->code);
101 $this->assertNull($payment->failure->message);
102
103 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
104 $this->assertEquals('another value', $payment->metadata['another_key']);
105 }
106
107 public function testPaymentCreate()
108 {
109 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
110
111 $this->_requestMock
112 ->expects($this->once())
113 ->method('exec')
114 ->will($this->returnValue('{"status":"ok"}'));
115
116 $this->_requestMock
117 ->expects($this->atLeastOnce())
118 ->method('setopt')
119 ->will($this->returnCallback(function($option, $value = null) {
120 switch($option) {
121 case CURLOPT_POSTFIELDS:
122 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
123 return true;
124 }
125 return true;
126 }));
127 $this->_requestMock
128 ->expects($this->any())
129 ->method('getinfo')
130 ->will($this->returnCallback(function($option) {
131 switch($option) {
132 case CURLINFO_HTTP_CODE:
133 return 200;
134 }
135 return null;
136 }));
137
138 $payment = Payment::create(array(
139 'amount' => 999,
140 'currency' => 'EUR',
141 'customer' => array(
142 'email' => '[email protected]',
143 'first_name' => 'John',
144 'last_name' => 'Doe'
145 ),
146 'hosted_payment' => array(
147 'return_url' => 'https://www.example.com/thank_you_for_your_payment.html',
148 'cancel_url' => 'https://www.example.com/so_bad_it_didnt_make_it.html'
149 ),
150 'notification_url' => 'http://www.example.org/callbackURL',
151 'force_3ds' => false
152 ));
153
154 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
155 $this->assertEquals('ok', $payment->status);
156
157 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
158 }
159
160 public function testPaymentAbort()
161 {
162 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
163
164 $this->_requestMock
165 ->expects($this->once())
166 ->method('exec')
167 ->will($this->returnValue('{"status":"ok"}'));
168
169 $this->_requestMock
170 ->expects($this->atLeastOnce())
171 ->method('setopt')
172 ->will($this->returnCallback(function($option, $value = null) {
173 switch($option) {
174 case CURLOPT_POSTFIELDS:
175 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
176 return true;
177 }
178 return true;
179 }));
180 $this->_requestMock
181 ->expects($this->any())
182 ->method('getinfo')
183 ->will($this->returnCallback(function($option) {
184 switch($option) {
185 case CURLINFO_HTTP_CODE:
186 return 200;
187 }
188 return null;
189 }));
190
191 $payment = Payplug\Payment::abort('a_payment_id');
192
193 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
194 $this->assertTrue($GLOBALS['CURLOPT_POSTFIELDS_DATA'] === array('abort' => true));
195 $this->assertEquals('ok', $payment->status);
196
197 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
198 }
199
200 public function testPaymentRetrieve()
201 {
202 $GLOBALS['CURLOPT_URL_DATA'] = null;
203
204 $this->_requestMock
205 ->expects($this->once())
206 ->method('exec')
207 ->will($this->returnValue('{"status":"ok"}'));
208
209 $this->_requestMock
210 ->expects($this->any())
211 ->method('getinfo')
212 ->will($this->returnCallback(function($option) {
213 switch($option) {
214 case CURLINFO_HTTP_CODE:
215 return 200;
216 }
217 return null;
218 }));
219 $this->_requestMock
220 ->expects($this->any())
221 ->method('setopt')
222 ->will($this->returnCallback(function($option, $value = null) {
223 switch($option) {
224 case CURLOPT_URL:
225 $GLOBALS['CURLOPT_URL_DATA'] = $value;
226 return true;
227 }
228 return true;
229 }));
230
231 $payment = Payment::retrieve('a_payment_id');
232
233 $this->assertStringEndsWith('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
234 $this->assertEquals('ok', $payment->status);
235
236 unset($GLOBALS['CURLOPT_URL_DATA']);
237 }
238
239 public function testPaymentList()
240 {
241 $GLOBALS['CURLOPT_URL_DATA'] = null;
242
243 $this->_requestMock
244 ->expects($this->once())
245 ->method('exec')
246 ->will($this->returnValue('{"data":[{"id": "payment1"}, {"id": "payment2"}]}'));
247
248 $this->_requestMock
249 ->expects($this->any())
250 ->method('getinfo')
251 ->will($this->returnCallback(function($option) {
252 switch($option) {
253 case CURLINFO_HTTP_CODE:
254 return 200;
255 }
256 return null;
257 }));
258
259 $payments = Payment::listPayments();
260 $this->assertEquals(2, count($payments));
261 $this->assertTrue($payments[0]->id === 'payment1' || $payments[0]->id === 'payment2');
262 $this->assertTrue($payments[1]->id === 'payment1' || $payments[1]->id === 'payment2');
263 $this->assertTrue($payments[0]->id !== $payments[1]->id);
264
265
266 unset($GLOBALS['CURLOPT_URL_DATA']);
267 }
268
269
270 public function testPaymentPaginationList()
271 {
272 $GLOBALS['CURLOPT_URL_DATA'] = null;
273
274 $this->_requestMock
275 ->expects($this->once())
276 ->method('exec')
277 ->will($this->returnValue('{"per_page": 1, "page": 0, "data":[{"id": "payment1"}]}'));
278
279 $this->_requestMock
280 ->expects($this->any())
281 ->method('getinfo')
282 ->will($this->returnCallback(function($option) {
283 switch($option) {
284 case CURLINFO_HTTP_CODE:
285 return 200;
286 }
287 return null;
288 }));
289
290 $perPage = 1;
291 $page = 0;
292 $payments = Payment::listPayments($perPage, $page);
293
294 $this->assertEquals(1, count($payments));
295 $this->assertTrue($payments[0]->id == 'payment1');
296
297
298 unset($GLOBALS['CURLOPT_URL_DATA']);
299 }
300
301 public function testPaymentRefundWhenPaymentIsInvalid()
302 {
303 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
304
305 $payment = Payment::fromAttributes(array('fake' => 'payment'));
306 $payment->refund(array('amount' => 3300));
307 }
308
309 public function testPaymentRefund()
310 {
311 $GLOBALS['CURLOPT_URL_DATA'] = null;
312
313 $this->_requestMock
314 ->expects($this->once())
315 ->method('exec')
316 ->will($this->returnValue('{"status":"ok"}'));
317
318 $this->_requestMock
319 ->expects($this->any())
320 ->method('getinfo')
321 ->will($this->returnCallback(function($option) {
322 switch($option) {
323 case CURLINFO_HTTP_CODE:
324 return 200;
325 }
326 return null;
327 }));
328 $this->_requestMock
329 ->expects($this->any())
330 ->method('setopt')
331 ->will($this->returnCallback(function($option, $value = null) {
332 switch($option) {
333 case CURLOPT_URL:
334 $GLOBALS['CURLOPT_URL_DATA'] = $value;
335 return true;
336 }
337 return true;
338 }));
339
340 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
341 $payment->refund(array('amount' => 3300));
342
343 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
344
345 unset($GLOBALS['CURLOPT_URL_DATA']);
346 }
347
348 public function testPaymentListRefundsWhenPaymentIsInvalid()
349 {
350 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
351
352 $payment = Payment::fromAttributes(array('fake' => 'payment'));
353 $payment->listRefunds();
354 }
355
356 public function testPaymentListRefunds()
357 {
358 $GLOBALS['CURLOPT_URL_DATA'] = null;
359
360 $this->_requestMock
361 ->expects($this->once())
362 ->method('exec')
363 ->will($this->returnValue('{"data":[{"id": "refund1"}, {"id": "refund2"}]}'));
364
365 $this->_requestMock
366 ->expects($this->any())
367 ->method('getinfo')
368 ->will($this->returnCallback(function($option) {
369 switch($option) {
370 case CURLINFO_HTTP_CODE:
371 return 200;
372 }
373 return null;
374 }));
375 $this->_requestMock
376 ->expects($this->any())
377 ->method('setopt')
378 ->will($this->returnCallback(function($option, $value = null) {
379 switch($option) {
380 case CURLOPT_URL:
381 $GLOBALS['CURLOPT_URL_DATA'] = $value;
382 return true;
383 }
384 return true;
385 }));
386
387 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
388 $refunds = $payment->listRefunds();
389
390 $this->assertEquals(2, count($refunds));
391 $this->assertTrue($refunds[0]->id === 'refund1' || $refunds[0]->id === 'refund2');
392 $this->assertTrue($refunds[1]->id === 'refund1' || $refunds[1]->id === 'refund2');
393 $this->assertTrue($refunds[0]->id !== $refunds[1]->id);
394
395 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
396
397 unset($GLOBALS['CURLOPT_URL_DATA']);
398 }
399
400 public function testRetrieveConsistentPaymentWhenIdIsUndefined()
401 {
402 $this->setExpectedException('\PayPlug\Exception\UndefinedAttributeException');
403
404 $payment = Payment::fromAttributes(array('this_payment' => 'has_no_id'));
405 $payment->getConsistentResource();
406 }
407
408 public function testRetrieveConsistentPayment()
409 {
410 function testRetrieveConsistentPayment_getinfo($option) {
411 switch($option) {
412 case CURLINFO_HTTP_CODE:
413 return 200;
414 }
415 return null;
416 }
417
418 $this->_requestMock
419 ->expects($this->once())
420 ->method('exec')
421 ->will($this->returnValue('{"id": "pay_345"}'));
422
423 $this->_requestMock
424 ->expects($this->any())
425 ->method('setopt')
426 ->will($this->returnValue(true));
427 $this->_requestMock
428 ->expects($this->any())
429 ->method('getinfo')
430 ->will($this->returnCallback(function($option) {
431 switch($option) {
432 case CURLINFO_HTTP_CODE:
433 return 200;
434 }
435 return null;
436 }));
437
438 $payment1 = Payment::fromAttributes(array('id' => 'pay_123'));
439 $payment2 = $payment1->getConsistentResource($this->_configuration);
440
441 $this->assertEquals('pay_123', $payment1->id);
442 $this->assertEquals('pay_345', $payment2->id);
443 }
444 }
445