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

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