Skip to content

Commit

Permalink
Merge 5b2cfaa into 3e453ab
Browse files Browse the repository at this point in the history
  • Loading branch information
haltuf committed Mar 11, 2017
2 parents 3e453ab + 5b2cfaa commit 458967c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Service/PaymentsService.php
Expand Up @@ -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);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions tests/cases/unit/Service/PaymentService.phpt
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
});

0 comments on commit 458967c

Please sign in to comment.