Skip to content
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
10 changes: 10 additions & 0 deletions src/Services/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ public function fetchResourceByUrl($url)
}
$resource = $unzer->fetchReversal($paymentId, $resourceId);
break;
case $resourceType === IdStrings::CHARGEBACK:
$paymentId = IdService::getResourceIdFromUrl($url, IdStrings::PAYMENT);
$chargeId = IdService::getResourceIdOrNullFromUrl($url, IdStrings::CHARGE);

$resource = $this->fetchChargebackById(
$paymentId,
$resourceId,
$chargeId
);
break;
case $resourceType === IdStrings::PAYOUT:
$resource = $unzer->fetchPayout(IdService::getResourceIdFromUrl($url, IdStrings::PAYMENT));
break;
Expand Down
23 changes: 23 additions & 0 deletions test/integration/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use UnzerSDK\Constants\ApiResponseCodes;
use UnzerSDK\Constants\WebhookEvents;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\TransactionTypes\Chargeback;
use UnzerSDK\Resources\Webhook;
use UnzerSDK\test\BaseIntegrationTest;
use function count;
Expand All @@ -22,6 +23,7 @@
class WebhookTest extends BaseIntegrationTest
{
//<editor-fold desc="Webhook tests">
const CHARGEDBACK_PAYMENT_ID = 's-pay-341196';

/**
* Verify Webhook resource can be registered and fetched.
Expand Down Expand Up @@ -221,6 +223,27 @@ public function bulkSettingOnlyOneWebhookShouldBePossible(): void
$this->assertEquals($url, $webhook->getUrl());
}

/**
* @test
*/
public function testFetchResourceFromEvent(): void
{
$webhookNotification = [
'event' => 'chargebacks',
'publicKey' => 's-pub-xyz',
'retrieveUrl' => 'https://sbx-api.unzer.com/v1/payments/' . self::CHARGEDBACK_PAYMENT_ID . '/charges/s-chg-1/chargebacks/s-cbk-1',
'paymentId' => self::CHARGEDBACK_PAYMENT_ID
];

// when
$chargeback = $this->unzer->fetchResourceFromEvent(json_encode($webhookNotification));

// then
$this->assertInstanceOf(Chargeback::class, $chargeback);
$this->assertNotNull($chargeback);
$this->assertEquals('s-cbk-1', $chargeback->getId());
}

//</editor-fold>

//<editor-fold desc="Helpers">
Expand Down
56 changes: 53 additions & 3 deletions test/unit/Services/WebhooksServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@

namespace UnzerSDK\test\unit\Services;

use UnzerSDK\Unzer;
use RuntimeException;
use stdClass;
use UnzerSDK\Interfaces\ResourceServiceInterface;
use UnzerSDK\Resources\TransactionTypes\Charge;
use UnzerSDK\Resources\TransactionTypes\Chargeback;
use UnzerSDK\Resources\Webhook;
use UnzerSDK\Resources\Webhooks;
use UnzerSDK\Services\ResourceService;
use UnzerSDK\Services\WebhookService;
use UnzerSDK\test\BasePaymentTest;
use UnzerSDK\test\unit\DummyResource;
use RuntimeException;
use stdClass;
use UnzerSDK\Unzer;

class WebhooksServiceTest extends BasePaymentTest
{
Expand Down Expand Up @@ -328,6 +330,54 @@ public function fetchResourceByEventWithEmptyRetrieveUrlShouldThrowException():
$webhookService->fetchResourceFromEvent();
}

/**
* Verify that a chargeback is returned from a webhook event.
*
* @test
*/
public function fetchChargebackByEventShouldReturnChargeback(): void
{
// given
$unzer = new Unzer('s-priv-1234');
$webhookService = new WebhookService($unzer);

$paymentId = 's-pay-42';
$chargeId = 's-chg-1';
$chargebackId = 's-cbk-1';
$retrieveUrl = "https://api.unzer.com/v1/payments/{$paymentId}/charges/{$chargeId}/chargebacks/{$chargebackId}";

// Partial mock: keep original behavior except fetchChargebackById which we want to intercept
$resourceServiceMock = $this->getMockBuilder(ResourceService::class)
->setConstructorArgs([$unzer])
->setMethods(['fetchChargebackById'])
->getMock();

$chargeback = (new Chargeback(10.0))
->setId($chargebackId)
->setParentResource((new Charge())->setId($chargeId));

$resourceServiceMock->expects($this->once())
->method('fetchChargebackById')
->with($paymentId, $chargebackId, $chargeId)
->willReturn($chargeback);

$webhookService->setResourceService($resourceServiceMock);

$eventJson = json_encode([
'event' => 'chargeback',
'publicKey' => 's-pub-xyz',
'retrieveUrl' => $retrieveUrl,
'paymentId' => $paymentId
]);

// when
$fetchedChargeback = $webhookService->fetchResourceFromEvent($eventJson);

// then
$this->assertSame($chargeback, $fetchedChargeback);
$this->assertNotNull($fetchedChargeback);
}

/**
* Verify exception is thrown if the retrieveURL is empty.
*
Expand Down