Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve refundRequest Validation #77

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Request/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function validate()
{
$props = get_object_vars($this);

if (! empty($this->items)) {
// Count the total amount of the payment.
if (!empty($this->items)) {
// Count the total amount of the refund.
$items_total = array_reduce($this->items, function ($carry = 0, ?RefundItem $item = null) {
if ($item === null) {
return $carry;
Expand All @@ -49,12 +49,8 @@ public function validate()
$items_total = $this->amount;
}

if (empty($props['amount'])) {
throw new ValidationException('Amount can not be empty');
}

if (filter_var($props['amount'], FILTER_VALIDATE_INT) === false) {
throw new ValidationException('Amount is not an integer');
if (empty($props['amount']) && empty($this->items)) {
throw new ValidationException('Amount can not be empty when making refund without items');
}

if ($items_total !== $props['amount']) {
Expand All @@ -65,6 +61,7 @@ public function validate()
throw new ValidationException('CallbackUrls are not set');
}

// Validate callbackUrls
$this->callbackUrls->validate();

return true;
Expand Down
127 changes: 26 additions & 101 deletions tests/Request/EmailRefundRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

class EmailRefundRequestTest extends TestCase
{
public function testEmailRefundRequest()
public function testEmailRefundRequestIsValid()
{
$er = new EmailRefundRequest();
$er->setAmount(20);
$er->setEmail('some@email.com');
$emailRequest = new EmailRefundRequest();
$emailRequest->setAmount(20);
$emailRequest->setEmail('some@email.com');

$item = new RefundItem();
$item->setAmount(10)
Expand All @@ -26,115 +26,40 @@ public function testEmailRefundRequest()
$item2->setAmount(10)
->setStamp('anotherStamp');

$er->setItems([$item, $item2]);
$emailRequest->setItems([$item, $item2]);

$cb = new CallbackUrl();
$cb->setCancel('https://some.url.com/cancel')
->setSuccess('https://some.url.com/success');

$er->setCallbackUrls($cb);
$emailRequest->setCallbackUrls($cb);

$er->setRefundReference('ref-1234')
$emailRequest->setRefundReference('ref-1234')
->setRefundStamp('c7557cd5d5f548daa5332ccc4abb264f');

$this->assertEquals(true, $er->validate());
$this->assertTrue($emailRequest->validate());
}

public function testExceptions()
public function testRefundRequestWithoutItemsAndAmountThrowsException()
{
$er = new EmailRefundRequest();

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals("Amount can not be empty", $e->getMessage());
}

$er->setAmount(99999);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('CallbackUrls are not set', $e->getMessage());
}

$cb = new CallbackUrl();
$er->setCallbackUrls($cb);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('Success is empty', $e->getMessage());
}

$cb->setSuccess('someurl.somewhere.com/success');
$er->setCallbackUrls($cb);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('Cancel is empty', $e->getMessage());
}

$cb->setCancel('someurl.somewhere.com/cancel');
$er->setCallbackUrls($cb);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('Success is not a valid URL', $e->getMessage());
}

$cb->setSuccess('https://someurl.somewhere.com/success');
$er->setCallbackUrls($cb);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('Cancel is not a valid URL', $e->getMessage());
}

$cb->setCancel('https://someurl.somewhere.com/cancel');
$er->setCallbackUrls($cb);

try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('email can not be empty', $e->getMessage());
}

$er->setEmail('some@email.com');

// Items are not mandatory, so should pass from here
try {
$er->validate();
} catch (ValidationException $e) {
var_dump($e->getMessage());
}

$item = new RefundItem();
$item->setAmount(110)
->setStamp('someStamp');

$item2 = new RefundItem();
$item2->setAmount(10)
->setStamp('anotherStamp');

$er->setItems([$item, $item2]);

// Fails, as refund->total was set to 9999
try {
$er->validate();
} catch (ValidationException $e) {
$this->assertEquals('ItemsTotal does not match Amount', $e->getMessage());
}
$this->expectException(ValidationException::class);
(new EmailRefundRequest())->validate();
}

// Set correct amount
$er->setAmount(120);
public function testRefundWithItemsAndAmountMismatchThrowsException()
{
$this->expectException(ValidationException::class);
(new EmailRefundRequest())->setAmount(100)
->setItems([
(new RefundItem())->setAmount(200)
->setStamp('foobar')
])->validate();
}

try {
$this->assertEquals(true, $er->validate());
} catch (ValidationException $e) {
}
public function testRefundRequestWithoutCallbackUrlsThrowsError()
{
$this->expectException(ValidationException::class);
(new EmailRefundRequest())->setAmount(100)
->validate();
}
}