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
19 changes: 18 additions & 1 deletion src/Resources/PaymentTypes/SepaDirectDebit.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class SepaDirectDebit extends BasePaymentType
{
use CanDirectCharge;
use CanPayout;
use CanRecur;
use CanRecur {
setRecurring as public;
}

/** @var string $iban */
protected $iban;
Expand Down Expand Up @@ -91,4 +93,19 @@ public function setHolder(?string $holder): self
$this->holder = $holder;
return $this;
}

/**
* Always exposes the recurring flag (default false) so the SEPA direct debit type registration
* carries the mandate intent.
*
* {@inheritDoc}
*/
public function expose()
{
$exposed = (array) parent::expose();
$exposed['recurring'] = $this->recurring;
ksort($exposed);

return $exposed;
}
}
40 changes: 40 additions & 0 deletions test/integration/PaymentTypes/SepaDirectDebitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,46 @@ public function sepaDirectDebitShouldBeCreatable(): void
$this->assertEquals($sdd->expose(), $fetchedDirectDebit->expose());
}

/**
* Verify sepa direct debit can be registered for recurring collection.
*
* @test
*/
public function sepaDirectDebitShouldBeCreatableWithRecurringFlag(): void
{
$sdd = (new SepaDirectDebit('DE89370400440532013000'))->setRecurring(true);
/** @var SepaDirectDebit $sdd */
$sdd = $this->unzer->createPaymentType($sdd);
$this->assertInstanceOf(SepaDirectDebit::class, $sdd);
$this->assertNotNull($sdd->getId());

/** @var SepaDirectDebit $fetchedDirectDebit */
$fetchedDirectDebit = $this->unzer->fetchPaymentType($sdd->getId());
$this->assertTrue($fetchedDirectDebit->isRecurring());
}

/**
* Verify the (deprecated) recurring activation still routes a SepaDirectDebit through to PAPI.
*
* Keeping the `CanRecur` trait on the type means `activateRecurring()` passes the ResourceService
* capability gate and reaches the API instead of failing client-side. For SEPA the mandate is
* registered during a charge/authorize, so PAPI rejects a direct recurring call — asserting that
* specific API error confirms the activation flow is still wired correctly for the type.
*
* @test
*/
public function sepaDirectDebitShouldActivateRecurringViaType(): void
{
/** @var SepaDirectDebit $sdd */
$sdd = $this->unzer->createPaymentType(new SepaDirectDebit('DE89370400440532013000'));
$this->assertFalse($sdd->isRecurring());

// SEPA activates the mandate via charge/authorize, so a direct recurring call is rejected by PAPI.
$this->expectException(UnzerApiException::class);
$this->expectExceptionCode('API.640.550.005');
$sdd->activateRecurring(self::RETURN_URL);
}

/**
* Verify authorization is not allowed for sepa direct debit.
*
Expand Down
94 changes: 94 additions & 0 deletions test/unit/Resources/PaymentTypes/SepaDirectDebitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use UnzerSDK\Resources\PaymentTypes\SepaDirectDebit;
use UnzerSDK\test\BasePaymentTest;
use stdClass;

class SepaDirectDebitTest extends BasePaymentTest
{
Expand Down Expand Up @@ -42,4 +43,97 @@ public function getterAndSetterWorkAsExpected(): void
$sdd->setHolder('Max Mustermann');
$this->assertEquals('Max Mustermann', $sdd->getHolder());
}

/**
* Verify the recurring flag defaults to false and round-trips through the fluent setter.
*
* @test
*
* @dataProvider recurringValueProvider
*/
public function recurringSetterAndGetterWorkAsExpected(bool $value): void
{
$sdd = new SepaDirectDebit('DE89370400440532013000');
$this->assertFalse($sdd->isRecurring());

$this->assertSame($sdd, $sdd->setRecurring($value));
$this->assertSame($value, $sdd->isRecurring());
}

/**
* Verify the recurring flag is always exposed, defaulting to false when never set.
*
* @test
*
* @dataProvider exposeRecurringProvider
*/
public function recurringIsAlwaysExposed(?bool $value, bool $expected): void
{
$sdd = new SepaDirectDebit('DE89370400440532013000');
if ($value !== null) {
$sdd->setRecurring($value);
}

$exposed = $sdd->expose();

$this->assertArrayHasKey('recurring', $exposed);
$this->assertSame($expected, $exposed['recurring']);
}

/**
* Verify the recurring flag is exposed even when the type is otherwise empty, i.e. the expose()
* override handles parent::expose() returning an empty stdClass rather than an array.
*
* @test
*/
public function recurringIsExposedWhenTypeIsOtherwiseEmpty(): void
{
$sdd = (new SepaDirectDebit(null))->setRecurring(true);

$exposed = $sdd->expose();

$this->assertIsArray($exposed);
$this->assertSame(['recurring' => true], $exposed);
}

/**
* Verify a response containing the recurring flag is mapped onto the resource.
*
* @test
*
* @dataProvider recurringValueProvider
*/
public function recurringIsMappedFromResponse(bool $value): void
{
$sdd = new SepaDirectDebit('DE89370400440532013000');

$response = new stdClass();
$response->recurring = $value;
$sdd->handleResponse($response);

$this->assertSame($value, $sdd->isRecurring());
}

/**
* @return array
*/
public function recurringValueProvider(): array
{
return [
'recurring true' => [true],
'recurring false' => [false],
];
}

/**
* @return array
*/
public function exposeRecurringProvider(): array
{
return [
'set to true' => [true, true],
'set to false' => [false, false],
'not set defaults false' => [null, false],
];
}
}
22 changes: 22 additions & 0 deletions test/unit/Services/ResourceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,28 @@ public function createRecurringShouldNotFetchThePaymentTypeByObject(): void
$resourceSrvMock->activateRecurringPayment($paymentType, 'returnUrl', null);
}

/**
* Verify activateRecurringPayment accepts a SepaDirectDebit, i.e. the type keeps the CanRecur trait the
* capability gate checks via class_uses(). Guards against removing the trait and silently breaking both
* $sdd->activateRecurring(...) and $unzer->activateRecurringPayment($sdd, ...).
*
* @test
*/
public function activateRecurringPaymentAcceptsSepaDirectDebit(): void
{
$paymentType = (new SepaDirectDebit('DE89370400440532013000'))->setId('myId');
/** @var ResourceServiceInterface|MockObject $resourceSrvMock */
$resourceSrvMock = $this->getMockBuilder(ResourceService::class)->disableOriginalConstructor()->setMethods(['fetchPaymentType', 'createResource'])->getMock();
$resourceSrvMock->expects(self::never())->method('fetchPaymentType');
/** @noinspection PhpParamsInspection */
$resourceSrvMock->expects(self::once())->method('createResource')
->with($this::callback(static function ($data) {
return $data instanceof Recurring && $data->getReturnUrl() === 'returnUrl' && $data->getPaymentTypeId() === 'myId';
}));

$resourceSrvMock->activateRecurringPayment($paymentType, 'returnUrl', null);
}

/**
* Verify createRecurring throws exception if it is called with a payment type which does not support recurring payment.
*
Expand Down
Loading