diff --git a/src/Service/PaymentsService.php b/src/Service/PaymentsService.php index 4aec05d..c5a5d64 100644 --- a/src/Service/PaymentsService.php +++ b/src/Service/PaymentsService.php @@ -72,12 +72,21 @@ public function createPreauthorizedPayment(PreauthorizedPayment $payment) /** * @param int|float $id * @param float $amount + * @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) + public function refundPayment($id, $amount, $items = NULL, $eet = NULL) { - // Make request - return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', ['amount' => round($amount * 100)], Http::CONTENT_FORM); + // without EET + if (is_null($items) || is_null($eet)) { + return $this->makeRequest('POST', 'payments/payment/' . $id . '/refund', ['amount' => round($amount * 100)], Http::CONTENT_FORM); + } + + // with EET + $data = array_merge(['amount' => round($amount * 100)], ['items' => $items], ['eet' => $eet]); + + 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 bab3a9f..386f261 100644 --- a/tests/cases/unit/Service/PaymentService.phpt +++ b/tests/cases/unit/Service/PaymentService.phpt @@ -8,10 +8,14 @@ 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; +use Markette\GopayInline\Http\Http; use Markette\GopayInline\Service\PaymentsService; use Tester\Assert; @@ -112,3 +116,68 @@ 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)); +}); + +// 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); + $items = [$item->toArray()]; + + $eet = new Eet(); + $eet->setSum(-1199.90); + $eet->setTaxBase(-991.65); + $eet->setTax(-208.25); + $eet->setCurrency(Currency::CZK); + $eet = $eet->toArray(); + + $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' => [[ + 'type' => 'ITEM', + 'name' => 'lodicky', + //'product_url' => 'https://www.eshop.cz/boty/damske/lodicky-cervene', + //'ean' => 1234567890123, + 'amount' => (float) - 119990, + 'count' => 1, + 'vat_rate' => 21, + ]], + 'eet' => [ + 'celk_trzba' => (float) - 119990, + 'zakl_dan1' => (float) - 99165, + 'dan1' => (float) - 20825, + 'mena' => Currency::CZK, + ], + ], + Http::CONTENT_JSON + ) + ->andReturn(TRUE); + + Assert::true($service->refundPayment(99, 1199.90, $items, $eet)); +});