diff --git a/src/Resources/PaymentTypes/SepaDirectDebit.php b/src/Resources/PaymentTypes/SepaDirectDebit.php index ba7e51e1..8686525a 100755 --- a/src/Resources/PaymentTypes/SepaDirectDebit.php +++ b/src/Resources/PaymentTypes/SepaDirectDebit.php @@ -16,7 +16,9 @@ class SepaDirectDebit extends BasePaymentType { use CanDirectCharge; use CanPayout; - use CanRecur; + use CanRecur { + setRecurring as public; + } /** @var string $iban */ protected $iban; @@ -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; + } } diff --git a/test/integration/PaymentTypes/SepaDirectDebitTest.php b/test/integration/PaymentTypes/SepaDirectDebitTest.php index b97268e0..6da35a57 100755 --- a/test/integration/PaymentTypes/SepaDirectDebitTest.php +++ b/test/integration/PaymentTypes/SepaDirectDebitTest.php @@ -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. * diff --git a/test/unit/Resources/PaymentTypes/SepaDirectDebitTest.php b/test/unit/Resources/PaymentTypes/SepaDirectDebitTest.php index 50c31677..6bd5bf82 100755 --- a/test/unit/Resources/PaymentTypes/SepaDirectDebitTest.php +++ b/test/unit/Resources/PaymentTypes/SepaDirectDebitTest.php @@ -7,6 +7,7 @@ use UnzerSDK\Resources\PaymentTypes\SepaDirectDebit; use UnzerSDK\test\BasePaymentTest; +use stdClass; class SepaDirectDebitTest extends BasePaymentTest { @@ -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], + ]; + } } diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index 09525cf9..01d6dc3b 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -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. *