PluginProbe
PayPlug for WooCommerce (Official) / 1.2.4
PayPlug for WooCommerce (Official) v1.2.4
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.2.4, at vendor/payplug/payplug-php/tests/unit_tests/Resource/PaymentTest.php

759 lines 29.3 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 protected function setUpTwice()
25 {
26 $this->_configuration = new Payplug\Payplug('abc','1970-01-01');
27 Payplug\Payplug::setDefaultConfiguration($this->_configuration);
28
29 $this->_requestMock = $this->getMock('\Payplug\Core\IHttpRequest');
30 Payplug\Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock;
31 }
32
33 public function testRetroCreatePaymentFromAttributes()
34 {
35 $payment = Payment::fromAttributes(array(
36 'id' => 'pay_490329',
37 'object' => 'payment',
38 'is_live' => true,
39 'amount' => 3300,
40 'amount_refunded' => 0,
41 'currency' => 'EUR',
42 'created_at' => 1410437760,
43 'is_paid' => true,
44 'is_refunded' => false,
45 'is_3ds' => false,
46 'save_card' => false,
47 'card' => array(
48 'last4' => '1800',
49 'country' => 'FR',
50 'exp_year' => 2017,
51 'exp_month' => 9,
52 'brand' => 'Mastercard'
53 ),
54 'customer' => array(
55 'email' => '[email protected]',
56 'first_name' => 'John',
57 'last_name' => 'Doe'
58 ),
59 'hosted_payment' => array(
60 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
61 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
62 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
63 'paid_at' => 1410437806
64 ),
65 'notification' => array(
66 'url' => 'http://yourwebsite.com/payplug_ipn',
67 'response_code' => 200
68 ),
69 'failure' => null,
70 'metadata' => array(
71 'a_custom_field' => 'a custom value',
72 'another_key' => 'another value'
73 )
74 ));
75
76 $this->assertEquals('pay_490329', $payment->id);
77 $this->assertEquals('payment', $payment->object);
78 $this->assertEquals(true, $payment->is_live);
79 $this->assertEquals(3300, $payment->amount);
80 $this->assertEquals(0, $payment->amount_refunded);
81 $this->assertEquals('EUR', $payment->currency);
82 $this->assertEquals(1410437760, $payment->created_at);
83 $this->assertEquals(true, $payment->is_paid);
84 $this->assertEquals(false, $payment->is_refunded);
85 $this->assertEquals(false, $payment->is_3ds);
86 $this->assertEquals(false, $payment->save_card);
87
88 $this->assertEquals('1800', $payment->card->last4);
89 $this->assertEquals('FR', $payment->card->country);
90 $this->assertEquals(2017, $payment->card->exp_year);
91 $this->assertEquals(9, $payment->card->exp_month);
92 $this->assertEquals('Mastercard', $payment->card->brand);
93
94 $this->assertEquals('[email protected]', $payment->customer->email);
95 $this->assertEquals('John', $payment->customer->first_name);
96 $this->assertEquals('Doe', $payment->customer->last_name);
97
98 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
99
100 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
101 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
102 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
103 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
104 $this->assertEquals(200, $payment->notification->response_code);
105
106 $this->assertNull($payment->failure);
107
108 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
109 $this->assertEquals('another value', $payment->metadata['another_key']);
110 }
111
112 public function testCompleteRetroCreatePaymentFromAttributes()
113 {
114 $payment = Payment::fromAttributes(array(
115 'id' => 'pay_490329',
116 'object' => 'payment',
117 'is_live' => true,
118 'amount' => 3300,
119 'amount_refunded' => 0,
120 'currency' => 'EUR',
121 'created_at' => 1410437760,
122 'is_paid' => true,
123 'is_refunded' => false,
124 'is_3ds' => false,
125 'save_card' => false,
126 'card' => array(
127 'last4' => '1800',
128 'country' => 'FR',
129 'exp_year' => 2017,
130 'exp_month' => 9,
131 'brand' => 'Mastercard'
132 ),
133 'customer' => array(
134 'email' => '[email protected]',
135 'first_name' => 'John',
136 'last_name' => 'Doe'
137 ),
138 'billing' => array(
139 "title" => "Mr",
140 "first_name" => "John",
141 "last_name" => "Doe",
142 "email" => "[email protected]",
143 "phone_number" => "0123456789",
144 "address1" => "77 rue la Boétie",
145 "address2" => null,
146 "company_name" => "PayPlug",
147 "postcode" => "75008",
148 "city" => "Paris",
149 "state" => null,
150 "country" => "FR",
151 "language" => "fr"
152 ),
153 'shipping' => array(
154 "title" => "Mr",
155 "first_name" => "John",
156 "last_name" => "Doe",
157 "email" => "[email protected]",
158 "phone_number" => "0123456789",
159 "address1" => "77 rue la Boétie",
160 "address2" => null,
161 "company_name" => "PayPlug",
162 "postcode" => "75008",
163 "city" => "Paris",
164 "state" => null,
165 "country" => "FR",
166 "language" => "fr"
167 ),
168 'hosted_payment' => array(
169 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
170 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
171 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
172 'paid_at' => 1410437806
173 ),
174 'notification' => array(
175 'url' => 'http://yourwebsite.com/payplug_ipn',
176 'response_code' => 200
177 ),
178 'failure' => null,
179 'metadata' => array(
180 'a_custom_field' => 'a custom value',
181 'another_key' => 'another value'
182 )
183 ));
184
185 $this->assertEquals('pay_490329', $payment->id);
186 $this->assertEquals('payment', $payment->object);
187 $this->assertEquals(true, $payment->is_live);
188 $this->assertEquals(3300, $payment->amount);
189 $this->assertEquals(0, $payment->amount_refunded);
190 $this->assertEquals('EUR', $payment->currency);
191 $this->assertEquals(1410437760, $payment->created_at);
192 $this->assertEquals(true, $payment->is_paid);
193 $this->assertEquals(false, $payment->is_refunded);
194 $this->assertEquals(false, $payment->is_3ds);
195 $this->assertEquals(false, $payment->save_card);
196
197 $this->assertEquals('1800', $payment->card->last4);
198 $this->assertEquals('FR', $payment->card->country);
199 $this->assertEquals(2017, $payment->card->exp_year);
200 $this->assertEquals(9, $payment->card->exp_month);
201 $this->assertEquals('Mastercard', $payment->card->brand);
202
203 $this->assertEquals('[email protected]', $payment->customer->email);
204 $this->assertEquals('John', $payment->customer->first_name);
205 $this->assertEquals('Doe', $payment->customer->last_name);
206
207 // Billing / Shiping
208 $this->assertEquals('Mr', $payment->shipping->title);
209 $this->assertEquals('John', $payment->billing->first_name);
210 $this->assertEquals('Doe', $payment->shipping->last_name);
211 $this->assertEquals('[email protected]', $payment->billing->email);
212 $this->assertEquals('0123456789', $payment->shipping->phone_number);
213 $this->assertEquals('77 rue la Boétie', $payment->billing->address1);
214 $this->assertEquals(null, $payment->shipping->address2);
215 $this->assertEquals('PayPlug', $payment->billing->company_name);
216 $this->assertEquals('75008', $payment->shipping->postcode);
217 $this->assertEquals('Paris', $payment->billing->city);
218 $this->assertEquals(null, $payment->billing->state);
219 $this->assertEquals('FR', $payment->shipping->country);
220 $this->assertEquals('fr', $payment->billing->language);
221
222 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
223
224 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
225 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
226 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
227 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
228 $this->assertEquals(200, $payment->notification->response_code);
229
230 $this->assertNull($payment->failure);
231
232 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
233 $this->assertEquals('another value', $payment->metadata['another_key']);
234 }
235
236 public function testCreatePaymentFromAttributes()
237 {
238 $payment = Payment::fromAttributes(array(
239 'id' => 'pay_490329',
240 'object' => 'payment',
241 'is_live' => true,
242 'amount' => 3300,
243 'amount_refunded' => 0,
244 'currency' => 'EUR',
245 'created_at' => 1410437760,
246 'is_paid' => true,
247 'is_refunded' => false,
248 'is_3ds' => false,
249 'save_card' => false,
250 'card' => array(
251 'last4' => '1800',
252 'country' => 'FR',
253 'exp_year' => 2017,
254 'exp_month' => 9,
255 'brand' => 'Mastercard'
256 ),
257 'billing' => array(
258 "title" => "Mr",
259 "first_name" => "John",
260 "last_name" => "Doe",
261 "email" => "[email protected]",
262 "phone_number" => "0123456789",
263 "address1" => "77 rue la Boétie",
264 "address2" => null,
265 "company_name" => "PayPlug",
266 "postcode" => "75008",
267 "city" => "Paris",
268 "state" => null,
269 "country" => "FR",
270 "language" => "fr"
271 ),
272 'shipping' => array(
273 "title" => "Mr",
274 "first_name" => "John",
275 "last_name" => "Doe",
276 "email" => "[email protected]",
277 "phone_number" => "0123456789",
278 "address1" => "77 rue la Boétie",
279 "address2" => null,
280 "company_name" => "PayPlug",
281 "postcode" => "75008",
282 "city" => "Paris",
283 "state" => null,
284 "country" => "FR",
285 "language" => "fr"
286 ),
287 'hosted_payment' => array(
288 'payment_url' => 'https://www.payplug.com/p/b9868d18546711e490c612314307c934',
289 'return_url' => 'http://yourwebsite.com/payplug_return?someid=11235',
290 'cancel_url' => 'http://yourwebsite.com/payplug_cancel?someid=81321',
291 'paid_at' => 1410437806
292 ),
293 'notification' => array(
294 'url' => 'http://yourwebsite.com/payplug_ipn',
295 'response_code' => 200
296 ),
297 'failure' => null,
298 'metadata' => array(
299 'a_custom_field' => 'a custom value',
300 'another_key' => 'another value'
301 )
302 ));
303
304 $this->assertEquals('pay_490329', $payment->id);
305 $this->assertEquals('payment', $payment->object);
306 $this->assertEquals(true, $payment->is_live);
307 $this->assertEquals(3300, $payment->amount);
308 $this->assertEquals(0, $payment->amount_refunded);
309 $this->assertEquals('EUR', $payment->currency);
310 $this->assertEquals(1410437760, $payment->created_at);
311 $this->assertEquals(true, $payment->is_paid);
312 $this->assertEquals(false, $payment->is_refunded);
313 $this->assertEquals(false, $payment->is_3ds);
314 $this->assertEquals(false, $payment->save_card);
315
316 $this->assertEquals('1800', $payment->card->last4);
317 $this->assertEquals('FR', $payment->card->country);
318 $this->assertEquals(2017, $payment->card->exp_year);
319 $this->assertEquals(9, $payment->card->exp_month);
320 $this->assertEquals('Mastercard', $payment->card->brand);
321
322
323 // Billing / Shiping
324 $this->assertEquals('Mr', $payment->shipping->title);
325 $this->assertEquals('John', $payment->billing->first_name);
326 $this->assertEquals('Doe', $payment->shipping->last_name);
327 $this->assertEquals('[email protected]', $payment->billing->email);
328 $this->assertEquals('0123456789', $payment->shipping->phone_number);
329 $this->assertEquals('77 rue la Boétie', $payment->billing->address1);
330 $this->assertEquals(null, $payment->shipping->address2);
331 $this->assertEquals('PayPlug', $payment->billing->company_name);
332 $this->assertEquals('75008', $payment->shipping->postcode);
333 $this->assertEquals('Paris', $payment->billing->city);
334 $this->assertEquals(null, $payment->billing->state);
335 $this->assertEquals('FR', $payment->shipping->country);
336 $this->assertEquals('fr', $payment->billing->language);
337
338 $this->assertEquals('https://www.payplug.com/p/b9868d18546711e490c612314307c934', $payment->hosted_payment->payment_url);
339
340 $this->assertEquals('http://yourwebsite.com/payplug_return?someid=11235', $payment->hosted_payment->return_url);
341 $this->assertEquals('http://yourwebsite.com/payplug_cancel?someid=81321', $payment->hosted_payment->cancel_url);
342 $this->assertEquals(1410437806, $payment->hosted_payment->paid_at);
343 $this->assertEquals('http://yourwebsite.com/payplug_ipn', $payment->notification->url);
344 $this->assertEquals(200, $payment->notification->response_code);
345
346 $this->assertNull($payment->failure);
347
348 $this->assertEquals('a custom value', $payment->metadata['a_custom_field']);
349 $this->assertEquals('another value', $payment->metadata['another_key']);
350 }
351
352 public function testPaymentCreate()
353 {
354 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
355
356 $this->_requestMock
357 ->expects($this->once())
358 ->method('exec')
359 ->will($this->returnValue('{"status":"ok"}'));
360
361 $this->_requestMock
362 ->expects($this->atLeastOnce())
363 ->method('setopt')
364 ->will($this->returnCallback(function($option, $value = null) {
365 switch($option) {
366 case CURLOPT_POSTFIELDS:
367 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
368 return true;
369 }
370 return true;
371 }));
372 $this->_requestMock
373 ->expects($this->any())
374 ->method('getinfo')
375 ->will($this->returnCallback(function($option) {
376 switch($option) {
377 case CURLINFO_HTTP_CODE:
378 return 200;
379 }
380 return null;
381 }));
382
383 $payment = Payment::create(array(
384 'amount' => 999,
385 'currency' => 'EUR',
386 'customer' => array(
387 'email' => '[email protected]',
388 'first_name' => 'John',
389 'last_name' => 'Doe'
390 ),
391 'billing' => array(
392 'title' => 'Mr',
393 'first_name' => 'John',
394 'last_name' => 'Doe',
395 'email' => '[email protected]',
396 'phone_number' => '0123456789',
397 'address1' => '77 rue la Boétie',
398 'address2' => 'ul',
399 'company_name' => 'PayPlug',
400 'postcode' => '75008',
401 'city' => 'Paris',
402 'state' => 'ul',
403 'country' => 'FR',
404 'language' => 'fr'
405 ),
406 'shipping' => array(
407 'title' => 'Mr',
408 'first_name' => 'John',
409 'last_name' => 'Doe',
410 'email' => '[email protected]',
411 'phone_number' => '0123456789',
412 'address1' => '77 rue la Boétie',
413 'address2' => 'ul',
414 'company_name' => 'PayPlug',
415 'postcode' => '75008',
416 'city' => 'Paris',
417 'state' => 'ul',
418 'country' => 'FR',
419 'language' => 'fr'
420 ),
421 'hosted_payment' => array(
422 'return_url' => 'https://www.example.com/thank_you_for_your_payment.html',
423 'cancel_url' => 'https://www.example.com/so_bad_it_didnt_make_it.html'
424 ),
425 'notification_url' => 'http://www.example.org/callbackURL',
426 'force_3ds' => false
427 ));
428
429 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
430 $this->assertEquals('ok', $payment->status);
431
432 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
433 }
434
435 public function testPaymentAbort()
436 {
437 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
438
439 $this->_requestMock
440 ->expects($this->once())
441 ->method('exec')
442 ->will($this->returnValue('{"status":"ok"}'));
443
444 $this->_requestMock
445 ->expects($this->atLeastOnce())
446 ->method('setopt')
447 ->will($this->returnCallback(function($option, $value = null) {
448 switch($option) {
449 case CURLOPT_POSTFIELDS:
450 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
451 return true;
452 }
453 return true;
454 }));
455 $this->_requestMock
456 ->expects($this->any())
457 ->method('getinfo')
458 ->will($this->returnCallback(function($option) {
459 switch($option) {
460 case CURLINFO_HTTP_CODE:
461 return 200;
462 }
463 return null;
464 }));
465
466 $payment = Payplug\Payment::abort('a_payment_id');
467
468 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
469 $this->assertTrue($GLOBALS['CURLOPT_POSTFIELDS_DATA'] === array('aborted' => true));
470 $this->assertEquals('ok', $payment->status);
471
472 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
473 }
474
475 public function testPaymentCapture()
476 {
477 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = null;
478
479 $this->_requestMock
480 ->expects($this->once())
481 ->method('exec')
482 ->will($this->returnValue('{"status":"ok"}'));
483
484 $this->_requestMock
485 ->expects($this->atLeastOnce())
486 ->method('setopt')
487 ->will($this->returnCallback(function($option, $value = null) {
488 switch($option) {
489 case CURLOPT_POSTFIELDS:
490 $GLOBALS['CURLOPT_POSTFIELDS_DATA'] = json_decode($value, true);
491 return true;
492 }
493 return true;
494 }));
495 $this->_requestMock
496 ->expects($this->any())
497 ->method('getinfo')
498 ->will($this->returnCallback(function($option) {
499 switch($option) {
500 case CURLINFO_HTTP_CODE:
501 return 200;
502 }
503 return null;
504 }));
505
506 $payment = Payplug\Payment::capture('a_payment_id');
507
508 $this->assertTrue(is_array($GLOBALS['CURLOPT_POSTFIELDS_DATA']));
509 $this->assertTrue($GLOBALS['CURLOPT_POSTFIELDS_DATA'] === array('captured' => true));
510 $this->assertEquals('ok', $payment->status);
511
512 unset($GLOBALS['CURLOPT_POSTFIELDS_DATA']);
513 }
514
515 public function testPaymentRetrieve()
516 {
517 $GLOBALS['CURLOPT_URL_DATA'] = null;
518
519 $this->_requestMock
520 ->expects($this->once())
521 ->method('exec')
522 ->will($this->returnValue('{"status":"ok"}'));
523
524 $this->_requestMock
525 ->expects($this->any())
526 ->method('getinfo')
527 ->will($this->returnCallback(function($option) {
528 switch($option) {
529 case CURLINFO_HTTP_CODE:
530 return 200;
531 }
532 return null;
533 }));
534 $this->_requestMock
535 ->expects($this->any())
536 ->method('setopt')
537 ->will($this->returnCallback(function($option, $value = null) {
538 switch($option) {
539 case CURLOPT_URL:
540 $GLOBALS['CURLOPT_URL_DATA'] = $value;
541 return true;
542 }
543 return true;
544 }));
545
546 $payment = Payment::retrieve('a_payment_id');
547
548 $this->assertStringEndsWith('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
549 $this->assertEquals('ok', $payment->status);
550
551 unset($GLOBALS['CURLOPT_URL_DATA']);
552 }
553
554 public function testPaymentList()
555 {
556 $GLOBALS['CURLOPT_URL_DATA'] = null;
557
558 $this->_requestMock
559 ->expects($this->once())
560 ->method('exec')
561 ->will($this->returnValue('{"data":[{"id": "payment1"}, {"id": "payment2"}]}'));
562
563 $this->_requestMock
564 ->expects($this->any())
565 ->method('getinfo')
566 ->will($this->returnCallback(function($option) {
567 switch($option) {
568 case CURLINFO_HTTP_CODE:
569 return 200;
570 }
571 return null;
572 }));
573
574 $payments = Payment::listPayments();
575 $this->assertEquals(2, count($payments));
576 $this->assertTrue($payments[0]->id === 'payment1' || $payments[0]->id === 'payment2');
577 $this->assertTrue($payments[1]->id === 'payment1' || $payments[1]->id === 'payment2');
578 $this->assertTrue($payments[0]->id !== $payments[1]->id);
579
580
581 unset($GLOBALS['CURLOPT_URL_DATA']);
582 }
583
584 public function testPaymentPaginationList()
585 {
586 $GLOBALS['CURLOPT_URL_DATA'] = null;
587
588 $this->_requestMock
589 ->expects($this->once())
590 ->method('exec')
591 ->will($this->returnValue('{"per_page": 1, "page": 0, "data":[{"id": "payment1"}]}'));
592
593 $this->_requestMock
594 ->expects($this->any())
595 ->method('getinfo')
596 ->will($this->returnCallback(function($option) {
597 switch($option) {
598 case CURLINFO_HTTP_CODE:
599 return 200;
600 }
601 return null;
602 }));
603
604 $perPage = 1;
605 $page = 0;
606 $payments = Payment::listPayments($perPage, $page);
607
608 $this->assertEquals(1, count($payments));
609 $this->assertTrue($payments[0]->id == 'payment1');
610
611
612 unset($GLOBALS['CURLOPT_URL_DATA']);
613 }
614
615 public function testPaymentRefundWhenPaymentIsInvalid()
616 {
617 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
618
619 $payment = Payment::fromAttributes(array('fake' => 'payment'));
620 $payment->refund(array('amount' => 3300));
621 }
622
623 public function testPaymentRefund()
624 {
625 $GLOBALS['CURLOPT_URL_DATA'] = null;
626
627 $this->_requestMock
628 ->expects($this->once())
629 ->method('exec')
630 ->will($this->returnValue('{"status":"ok"}'));
631
632 $this->_requestMock
633 ->expects($this->any())
634 ->method('getinfo')
635 ->will($this->returnCallback(function($option) {
636 switch($option) {
637 case CURLINFO_HTTP_CODE:
638 return 200;
639 }
640 return null;
641 }));
642 $this->_requestMock
643 ->expects($this->any())
644 ->method('setopt')
645 ->will($this->returnCallback(function($option, $value = null) {
646 switch($option) {
647 case CURLOPT_URL:
648 $GLOBALS['CURLOPT_URL_DATA'] = $value;
649 return true;
650 }
651 return true;
652 }));
653
654 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
655 $payment->refund(array('amount' => 3300));
656
657 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
658
659 unset($GLOBALS['CURLOPT_URL_DATA']);
660 }
661
662 public function testPaymentListRefundsWhenPaymentIsInvalid()
663 {
664 $this->setExpectedException('\PayPlug\Exception\InvalidPaymentException');
665
666 $payment = Payment::fromAttributes(array('fake' => 'payment'));
667 $payment->listRefunds();
668 }
669
670 public function testPaymentListRefunds()
671 {
672 $GLOBALS['CURLOPT_URL_DATA'] = null;
673
674 $this->_requestMock
675 ->expects($this->once())
676 ->method('exec')
677 ->will($this->returnValue('{"data":[{"id": "refund1"}, {"id": "refund2"}]}'));
678
679 $this->_requestMock
680 ->expects($this->any())
681 ->method('getinfo')
682 ->will($this->returnCallback(function($option) {
683 switch($option) {
684 case CURLINFO_HTTP_CODE:
685 return 200;
686 }
687 return null;
688 }));
689 $this->_requestMock
690 ->expects($this->any())
691 ->method('setopt')
692 ->will($this->returnCallback(function($option, $value = null) {
693 switch($option) {
694 case CURLOPT_URL:
695 $GLOBALS['CURLOPT_URL_DATA'] = $value;
696 return true;
697 }
698 return true;
699 }));
700
701 $payment = Payment::fromAttributes(array('id' => 'a_payment_id'));
702 $refunds = $payment->listRefunds();
703
704 $this->assertEquals(2, count($refunds));
705 $this->assertTrue($refunds[0]->id === 'refund1' || $refunds[0]->id === 'refund2');
706 $this->assertTrue($refunds[1]->id === 'refund1' || $refunds[1]->id === 'refund2');
707 $this->assertTrue($refunds[0]->id !== $refunds[1]->id);
708
709 $this->assertContains('a_payment_id', $GLOBALS['CURLOPT_URL_DATA']);
710
711 unset($GLOBALS['CURLOPT_URL_DATA']);
712 }
713
714 public function testRetrieveConsistentPaymentWhenIdIsUndefined()
715 {
716 $this->setExpectedException('\PayPlug\Exception\UndefinedAttributeException');
717
718 $payment = Payment::fromAttributes(array('this_payment' => 'has_no_id'));
719 $payment->getConsistentResource();
720 }
721
722 public function testRetrieveConsistentPayment()
723 {
724 function testRetrieveConsistentPayment_getinfo($option) {
725 switch($option) {
726 case CURLINFO_HTTP_CODE:
727 return 200;
728 }
729 return null;
730 }
731
732 $this->_requestMock
733 ->expects($this->once())
734 ->method('exec')
735 ->will($this->returnValue('{"id": "pay_345"}'));
736
737 $this->_requestMock
738 ->expects($this->any())
739 ->method('setopt')
740 ->will($this->returnValue(true));
741 $this->_requestMock
742 ->expects($this->any())
743 ->method('getinfo')
744 ->will($this->returnCallback(function($option) {
745 switch($option) {
746 case CURLINFO_HTTP_CODE:
747 return 200;
748 }
749 return null;
750 }));
751
752 $payment1 = Payment::fromAttributes(array('id' => 'pay_123'));
753 $payment2 = $payment1->getConsistentResource($this->_configuration);
754
755 $this->assertEquals('pay_123', $payment1->id);
756 $this->assertEquals('pay_345', $payment2->id);
757 }
758 }
759