From 7f3b8c717834666c9800ea01c279328b8dadd0bb Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 12:44:19 +0100 Subject: [PATCH 1/7] Test cover PaymentsService::refundPayment() for current functionality --- tests/cases/unit/Service/PaymentService.phpt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/cases/unit/Service/PaymentService.phpt b/tests/cases/unit/Service/PaymentService.phpt index bab3a9f..d1c845f 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -12,6 +12,7 @@ use Markette\GopayInline\Api\Lists\TargetType; use Markette\GopayInline\Api\Objects\Target; use Markette\GopayInline\Client; use Markette\GopayInline\Config; +use Markette\GopayInline\Http\Http; use Markette\GopayInline\Service\PaymentsService; use Tester\Assert; @@ -112,3 +113,16 @@ test(function () { Assert::equal(100, $payment->getAmount()); Assert::equal(99, $payment->getTarget()->goid); }); + +// Refund payment +test(function() { + $client = new Client(new Config(1, 2, 3)); + $service = Mockery::mock(PaymentsService::class, [$client]) + ->makePartial() + ->shouldAllowMockingProtectedMethods(); + $service->shouldReceive('makeRequest') + ->with('POST', 'payments/payment/99/refund', ['amount' => (float)12345], Http::CONTENT_FORM) + ->andReturn(TRUE); + + Assert::true($service->refundPayment(99, 123.45)); +}); \ No newline at end of file From ada476d550de35d9014c3622c18ea104661d643a Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 13:50:48 +0100 Subject: [PATCH 2/7] coding standard --- tests/cases/unit/Service/PaymentService.phpt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/cases/unit/Service/PaymentService.phpt b/tests/cases/unit/Service/PaymentService.phpt index d1c845f..b152da5 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -115,14 +115,13 @@ test(function () { }); // Refund payment -test(function() { +test(function () { $client = new Client(new Config(1, 2, 3)); $service = Mockery::mock(PaymentsService::class, [$client]) ->makePartial() ->shouldAllowMockingProtectedMethods(); $service->shouldReceive('makeRequest') - ->with('POST', 'payments/payment/99/refund', ['amount' => (float)12345], Http::CONTENT_FORM) + ->with('POST', 'payments/payment/99/refund', ['amount' => (float) 12345], Http::CONTENT_FORM) ->andReturn(TRUE); Assert::true($service->refundPayment(99, 123.45)); -}); \ No newline at end of file From 49cab538d09cd6099fd7c8dc94a5aa2d4f3496bd Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 13:51:12 +0100 Subject: [PATCH 3/7] Test for new functionality - Refund with EET --- tests/cases/unit/Service/PaymentService.phpt | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/cases/unit/Service/PaymentService.phpt b/tests/cases/unit/Service/PaymentService.phpt index b152da5..abdf498 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -8,7 +8,10 @@ use Markette\GopayInline\Api\Entity\Payment; use Markette\GopayInline\Api\Entity\PreauthorizedPayment; use Markette\GopayInline\Api\Entity\RecurrentPayment; use Markette\GopayInline\Api\Lists\Currency; +use Markette\GopayInline\Api\Lists\PaymentType; use Markette\GopayInline\Api\Lists\TargetType; +use Markette\GopayInline\Api\Objects\Eet; +use Markette\GopayInline\Api\Objects\Item; use Markette\GopayInline\Api\Objects\Target; use Markette\GopayInline\Client; use Markette\GopayInline\Config; @@ -125,3 +128,58 @@ test(function () { ->andReturn(TRUE); Assert::true($service->refundPayment(99, 123.45)); +}); + +// Refund payment when EET is defined, see https://doc.gopay.com/cs/#refundace-platby-(storno)24 +test(function () { + $item = new Item(); + $item->setType(PaymentType::ITEM); + $item->setName('lodicky'); + // $item->setProductUrl not implemented yet + // $item->setEan not implemented yet + $item->setAmount(-1199.90); + $item->setCount(1); + $item->setVatRate(21); + + $eet = new Eet(); + $eet->setSum(-1199.90); + $eet->setTaxBase(-991.65); + $eet->setTax(-208.25); + $eet->setCurrency(Currency::CZK); + + $payment = new Payment(); + $payment->addItem($item); + $payment->setEet($eet); + + $client = new Client(new Config(1, 2, 3)); + $service = Mockery::mock(PaymentsService::class, [$client]) + ->makePartial() + ->shouldAllowMockingProtectedMethods(); + $service->shouldReceive('makeRequest') + ->with( + 'POST', + 'payments/payment/99/refund', + [ + 'amount' => (float) 119990, + 'items' => [ 0 => [ + 'name' => 'lodicky', + //'product_url' => 'https://www.eshop.cz/boty/damske/lodicky-cervene', + //'ean' => 1234567890123, + 'amount' => (float) - 119990, + 'count' => 1, + 'type' => 'ITEM', + 'vat_rate' => 21, + ]], + 'eet' => [ + 'celk_trzba' => (float) - 119990, + 'mena' => Currency::CZK, + 'zakl_dan1' => (float) - 99165, + 'dan1' => (float) - 20825, + ], + ], + Http::CONTENT_JSON + ) + ->andReturn(TRUE); + + Assert::true($service->refundPayment(99, 1199.90, $payment)); +}); From 03e87b2fa1bf03835925e71498a80eea7fa68483 Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 13:51:27 +0100 Subject: [PATCH 4/7] Refund with EET --- src/Service/PaymentsService.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Service/PaymentsService.php b/src/Service/PaymentsService.php index 4aec05d..73e8aa9 100644 --- a/src/Service/PaymentsService.php +++ b/src/Service/PaymentsService.php @@ -72,12 +72,23 @@ public function createPreauthorizedPayment(PreauthorizedPayment $payment) /** * @param int|float $id * @param float $amount + * @param Payment $payment Use in case you need to refund payment with EET * @return Response */ - public function refundPayment($id, $amount) + public function refundPayment($id, $amount, Payment $payment = NULL) { - // Make request - return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', ['amount' => round($amount * 100)], Http::CONTENT_FORM); + // without EET + if (is_null($payment)) { + return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', ['amount' => round($amount * 100)], Http::CONTENT_FORM); + } + + $this->preConfigure($payment); + + // with EET + $data = $payment->toArray(); + $usedata = array_merge(['amount' => round($amount * 100)], ['items' => $data['items']], ['eet' => $data['eet']]); + + return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', $usedata, Http::CONTENT_JSON); } /** From 89ed858c346b509ef9a86b321c11ea8b92fae1e2 Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 14:05:38 +0100 Subject: [PATCH 5/7] code checker --- src/Service/PaymentsService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/PaymentsService.php b/src/Service/PaymentsService.php index 73e8aa9..f31deb9 100644 --- a/src/Service/PaymentsService.php +++ b/src/Service/PaymentsService.php @@ -72,7 +72,7 @@ public function createPreauthorizedPayment(PreauthorizedPayment $payment) /** * @param int|float $id * @param float $amount - * @param Payment $payment Use in case you need to refund payment with EET + * @param Payment $payment Use in case you need to refund payment with EET * @return Response */ public function refundPayment($id, $amount, Payment $payment = NULL) From 4e3dd0b275e845d7639d841f1a08e90a84ce0fd5 Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 14:22:51 +0100 Subject: [PATCH 6/7] 2nd trial: refundPayment now accepts $items and $eet as arrays, instead of Payment object --- src/Service/PaymentsService.php | 14 ++++++-------- tests/cases/unit/Service/PaymentService.phpt | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Service/PaymentsService.php b/src/Service/PaymentsService.php index f31deb9..c5a5d64 100644 --- a/src/Service/PaymentsService.php +++ b/src/Service/PaymentsService.php @@ -72,23 +72,21 @@ public function createPreauthorizedPayment(PreauthorizedPayment $payment) /** * @param int|float $id * @param float $amount - * @param Payment $payment Use in case you need to refund payment with EET + * @param array $items Use in case you need to refund payment with EET + * @param array $eet Use in case you need to refund payment with EET * @return Response */ - public function refundPayment($id, $amount, Payment $payment = NULL) + public function refundPayment($id, $amount, $items = NULL, $eet = NULL) { // without EET - if (is_null($payment)) { + if (is_null($items) || is_null($eet)) { return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', ['amount' => round($amount * 100)], Http::CONTENT_FORM); } - $this->preConfigure($payment); - // with EET - $data = $payment->toArray(); - $usedata = array_merge(['amount' => round($amount * 100)], ['items' => $data['items']], ['eet' => $data['eet']]); + $data = array_merge(['amount' => round($amount * 100)], ['items' => $items], ['eet' => $eet]); - return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', $usedata, Http::CONTENT_JSON); + return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', $data, Http::CONTENT_JSON); } /** diff --git a/tests/cases/unit/Service/PaymentService.phpt b/tests/cases/unit/Service/PaymentService.phpt index abdf498..ddaabd9 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -140,16 +140,14 @@ test(function () { $item->setAmount(-1199.90); $item->setCount(1); $item->setVatRate(21); + $items = array($item->toArray()); $eet = new Eet(); $eet->setSum(-1199.90); $eet->setTaxBase(-991.65); $eet->setTax(-208.25); $eet->setCurrency(Currency::CZK); - - $payment = new Payment(); - $payment->addItem($item); - $payment->setEet($eet); + $eet = $eet->toArray(); $client = new Client(new Config(1, 2, 3)); $service = Mockery::mock(PaymentsService::class, [$client]) @@ -161,25 +159,25 @@ test(function () { 'payments/payment/99/refund', [ 'amount' => (float) 119990, - 'items' => [ 0 => [ + 'items' => [[ + 'type' => 'ITEM', 'name' => 'lodicky', //'product_url' => 'https://www.eshop.cz/boty/damske/lodicky-cervene', //'ean' => 1234567890123, 'amount' => (float) - 119990, 'count' => 1, - 'type' => 'ITEM', 'vat_rate' => 21, ]], 'eet' => [ 'celk_trzba' => (float) - 119990, - 'mena' => Currency::CZK, 'zakl_dan1' => (float) - 99165, 'dan1' => (float) - 20825, + 'mena' => Currency::CZK, ], ], Http::CONTENT_JSON ) ->andReturn(TRUE); - Assert::true($service->refundPayment(99, 1199.90, $payment)); + Assert::true($service->refundPayment(99, 1199.90, $items, $eet)); }); From 5b2cfaab30be8df00a89592a19c9c052211e08f9 Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Sat, 11 Mar 2017 14:23:49 +0100 Subject: [PATCH 7/7] coding standard --- tests/cases/unit/Service/PaymentService.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/unit/Service/PaymentService.phpt b/tests/cases/unit/Service/PaymentService.phpt index ddaabd9..386f261 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -140,7 +140,7 @@ test(function () { $item->setAmount(-1199.90); $item->setCount(1); $item->setVatRate(21); - $items = array($item->toArray()); + $items = [$item->toArray()]; $eet = new Eet(); $eet->setSum(-1199.90);