Skip to content

Commit 69f3332

Browse files
committed
fix: payment issues with PaymentFinalizeAction in ContextResolver
1 parent 50d8e0d commit 69f3332

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

src/Context/ContextResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public function assemblePaymentFinalize(RequestInterface $request, ShopInterface
159159
$this->parseSource($body['source'], $shop),
160160
new OrderTransaction($body['orderTransaction']),
161161
isset($body['recurring']) ? new RecurringData($body['recurring']) : null,
162-
$body['queryParameters'] ?? []
162+
// Support both Shopware 6.7 (requestData) and 6.6 (queryParameters) for backward compatibility
163+
$body['requestData'] ?? $body['queryParameters'] ?? []
163164
);
164165
}
165166

src/Context/Payment/PaymentFinalizeAction.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,21 @@ public function __construct(
2424
public readonly array $queryParameters = [],
2525
) {
2626
}
27+
28+
/**
29+
* @deprecated Will be removed in a future major. Use getRequestData() instead.
30+
* @return array<mixed>
31+
*/
32+
public function getQueryParameters(): array
33+
{
34+
return $this->queryParameters;
35+
}
36+
37+
/**
38+
* @return array<mixed> Contains payload passed to Shopware at the redirect of the payment provider (Shopware 6.7+)
39+
*/
40+
public function getRequestData(): array
41+
{
42+
return $this->queryParameters;
43+
}
2744
}

tests/Context/ContextResolverTest.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public function testAssemblePayFinalize(): void
635635
'orderTransaction' => [
636636
'id' => 'bar',
637637
],
638-
'queryParameters' => [
638+
'requestData' => [
639639
'returnId' => '123',
640640
],
641641
'recurring' => [
@@ -658,11 +658,106 @@ public function testAssemblePayFinalize(): void
658658
static::assertFalse($paymentPayResponse->source->inAppPurchases->has('baz'));
659659
static::assertSame('bar', $paymentPayResponse->orderTransaction->getId());
660660
static::assertSame(['returnId' => '123'], $paymentPayResponse->queryParameters);
661+
static::assertSame(['returnId' => '123'], $paymentPayResponse->getRequestData());
662+
static::assertSame(['returnId' => '123'], $paymentPayResponse->getQueryParameters());
663+
static::assertNotNull($paymentPayResponse->recurring);
664+
static::assertSame('baz', $paymentPayResponse->recurring->getSubscriptionId());
665+
static::assertEquals(new \DateTime('2023-07-18T17:00:00.000+00:00'), $paymentPayResponse->recurring->getNextSchedule());
666+
}
667+
668+
public function testAssemblePayFinalizeWithQueryParameters(): void
669+
{
670+
$collection = new Collection([
671+
'foo' => new InAppPurchase('foo', 1),
672+
'bar' => new InAppPurchase('bar', 2),
673+
]);
674+
675+
$provider = $this->createMock(InAppPurchaseProvider::class);
676+
$provider
677+
->method('decodePurchases')
678+
->willReturn($collection);
679+
680+
$contextResolver = new ContextResolver($provider);
681+
682+
$body = [
683+
'source' => [
684+
'url' => 'https://example.com',
685+
'appVersion' => 'foo',
686+
'inAppPurchases' => 'ey',
687+
],
688+
'orderTransaction' => [
689+
'id' => 'bar',
690+
],
691+
'queryParameters' => [
692+
'returnId' => '123',
693+
],
694+
'recurring' => [
695+
'subscriptionId' => 'baz',
696+
'nextSchedule' => '2023-07-18T17:00:00.000+00:00',
697+
],
698+
];
699+
700+
$paymentPayResponse = $contextResolver->assemblePaymentFinalize(
701+
new Request('POST', '/', [], \json_encode($body, JSON_THROW_ON_ERROR)),
702+
$this->getShop()
703+
);
704+
705+
static::assertInstanceOf(PaymentFinalizeAction::class, $paymentPayResponse);
706+
static::assertSame('https://example.com', $paymentPayResponse->source->url);
707+
static::assertSame('foo', $paymentPayResponse->source->appVersion);
708+
static::assertSame('bar', $paymentPayResponse->orderTransaction->getId());
709+
static::assertSame(['returnId' => '123'], $paymentPayResponse->getRequestData());
710+
static::assertSame(['returnId' => '123'], $paymentPayResponse->getQueryParameters());
661711
static::assertNotNull($paymentPayResponse->recurring);
662712
static::assertSame('baz', $paymentPayResponse->recurring->getSubscriptionId());
663713
static::assertEquals(new \DateTime('2023-07-18T17:00:00.000+00:00'), $paymentPayResponse->recurring->getNextSchedule());
664714
}
665715

716+
public function testAssemblePayFinalizeWithBothParameters(): void
717+
{
718+
$collection = new Collection([
719+
'foo' => new InAppPurchase('foo', 1),
720+
'bar' => new InAppPurchase('bar', 2),
721+
]);
722+
723+
$provider = $this->createMock(InAppPurchaseProvider::class);
724+
$provider
725+
->method('decodePurchases')
726+
->willReturn($collection);
727+
728+
$contextResolver = new ContextResolver($provider);
729+
730+
$body = [
731+
'source' => [
732+
'url' => 'https://example.com',
733+
'appVersion' => 'foo',
734+
'inAppPurchases' => 'ey',
735+
],
736+
'orderTransaction' => [
737+
'id' => 'bar',
738+
],
739+
'requestData' => [
740+
'newParam' => 'newValue',
741+
],
742+
'queryParameters' => [
743+
'oldParam' => 'oldValue',
744+
],
745+
'recurring' => [
746+
'subscriptionId' => 'baz',
747+
'nextSchedule' => '2023-07-18T17:00:00.000+00:00',
748+
],
749+
];
750+
751+
$paymentPayResponse = $contextResolver->assemblePaymentFinalize(
752+
new Request('POST', '/', [], \json_encode($body, JSON_THROW_ON_ERROR)),
753+
$this->getShop()
754+
);
755+
756+
static::assertInstanceOf(PaymentFinalizeAction::class, $paymentPayResponse);
757+
static::assertSame('bar', $paymentPayResponse->orderTransaction->getId());
758+
static::assertSame(['newParam' => 'newValue'], $paymentPayResponse->getRequestData());
759+
}
760+
666761
public function testPaymentPayCapture(): void
667762
{
668763
$collection = new Collection([

tests/Context/Payment/PaymentFinalizeActionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function testConstructDefault(): void
3131
static::assertSame($orderTransaction, $action->orderTransaction);
3232
static::assertNull($action->recurring);
3333
static::assertSame([], $action->queryParameters);
34+
static::assertSame([], $action->getQueryParameters());
35+
static::assertSame([], $action->getRequestData());
3436
static::assertSame($IAPs, $action->source->inAppPurchases);
3537
}
3638

@@ -50,6 +52,8 @@ public function testConstruct(): void
5052
static::assertSame($orderTransaction, $action->orderTransaction);
5153
static::assertSame($recurring, $action->recurring);
5254
static::assertSame($queryParameters, $action->queryParameters);
55+
static::assertSame($queryParameters, $action->getRequestData());
56+
static::assertSame($queryParameters, $action->getQueryParameters());
5357
static::assertSame($IAPs, $action->source->inAppPurchases);
5458
}
5559
}

0 commit comments

Comments
 (0)