Skip to content

Commit e781bc2

Browse files
author
Dennis Garding
authored
Merge pull request #35 from shopware5/pt-13138/fix-orderId-transactionId-mixup
PT-13138 - Fix orderId and transactionId mixup in webhooks
2 parents b96e683 + bd38dea commit e781bc2

File tree

7 files changed

+158
-21
lines changed

7 files changed

+158
-21
lines changed

Tests/Functional/WebhookHandler/AbstractWebhookTest.php

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testGetOrderServiceResultOrderServiceReturnsNullShouldReturnNull
5656
$abstractWebhook = $this->createAbstractWebhook(true);
5757

5858
$webhook = new Webhook();
59-
$webhook->setResource(['id' => 'anyPayPalOrderID']);
59+
$webhook->setResource(TestWebhookResource::create('anyPayPalOrderID'));
6060

6161
$result = $abstractWebhook->getResult($webhook);
6262

@@ -76,7 +76,7 @@ public function testGetOrderServiceResultShouldReturnOrderServiceResult()
7676
$abstractWebhook = $this->createAbstractWebhook();
7777

7878
$webhook = new Webhook();
79-
$webhook->setResource(['id' => 'unitTestTransactionId']);
79+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
8080

8181
$result = $abstractWebhook->getResult($webhook);
8282

@@ -86,6 +86,96 @@ public function testGetOrderServiceResultShouldReturnOrderServiceResult()
8686
static::assertSame(1, $result->getPaymentStatusId());
8787
}
8888

89+
/**
90+
* @return void
91+
*/
92+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithoutSupplementaryData()
93+
{
94+
$abstractWebhook = $this->createAbstractWebhook(true);
95+
96+
$webhook = new Webhook();
97+
$webhook->setResource([]);
98+
99+
$result = $abstractWebhook->getResult($webhook);
100+
101+
static::assertNull($result);
102+
}
103+
104+
/**
105+
* @return void
106+
*/
107+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithSupplementaryDataIsNotAnArray()
108+
{
109+
$abstractWebhook = $this->createAbstractWebhook(true);
110+
111+
$webhook = new Webhook();
112+
$webhook->setResource(['supplementary_data' => '']);
113+
114+
$result = $abstractWebhook->getResult($webhook);
115+
116+
static::assertNull($result);
117+
}
118+
119+
/**
120+
* @return void
121+
*/
122+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithRelatedIdsIsNotAnArray()
123+
{
124+
$abstractWebhook = $this->createAbstractWebhook(true);
125+
126+
$webhook = new Webhook();
127+
$webhook->setResource(['supplementary_data' => ['related_ids' => '']]);
128+
129+
$result = $abstractWebhook->getResult($webhook);
130+
131+
static::assertNull($result);
132+
}
133+
134+
/**
135+
* @return void
136+
*/
137+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithoutRelatedIds()
138+
{
139+
$abstractWebhook = $this->createAbstractWebhook(true);
140+
141+
$webhook = new Webhook();
142+
$webhook->setResource(['supplementary_data' => []]);
143+
144+
$result = $abstractWebhook->getResult($webhook);
145+
146+
static::assertNull($result);
147+
}
148+
149+
/**
150+
* @return void
151+
*/
152+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithoutOrderId()
153+
{
154+
$abstractWebhook = $this->createAbstractWebhook(true);
155+
156+
$webhook = new Webhook();
157+
$webhook->setResource(['supplementary_data' => ['related_ids' => []]]);
158+
159+
$result = $abstractWebhook->getResult($webhook);
160+
161+
static::assertNull($result);
162+
}
163+
164+
/**
165+
* @return void
166+
*/
167+
public function testGetOrderServiceResultLogErrorWithResourceArrayWithOrderIdIsNotAString()
168+
{
169+
$abstractWebhook = $this->createAbstractWebhook(true);
170+
171+
$webhook = new Webhook();
172+
$webhook->setResource(['supplementary_data' => ['related_ids' => ['order_id' => false]]]);
173+
174+
$result = $abstractWebhook->getResult($webhook);
175+
176+
static::assertNull($result);
177+
}
178+
89179
/**
90180
* @param bool $loggerExpectCallErrorMethod
91181
*

Tests/Functional/WebhookHandler/CheckoutPaymentApprovalReversedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testGetEventType()
4242
public function testInvokeShouldThrowExceptionBecauseThereIsNoOrderServiceResult()
4343
{
4444
$webhook = new Webhook();
45-
$webhook->setResource(['id' => 'unitTestTransactionId']);
45+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
4646

4747
$this->expectException(WebhookException::class);
4848
$this->expectExceptionMessage('SwagPaymentPayPalUnified\WebhookHandlers\CheckoutPaymentApprovalReversed::invoke expect OrderAndPaymentStatusResult, got NULL');
@@ -61,7 +61,7 @@ public function testInvoke()
6161
$this->getContainer()->get('dbal_connection')->exec($sql);
6262

6363
$webhook = new Webhook();
64-
$webhook->setResource(['id' => 'unitTestTransactionId']);
64+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
6565

6666
$result = $this->createCheckoutPaymentApprovalReversed()->invoke($webhook);
6767
$databaseResult = $this->getContainer()->get('dbal_connection')->createQueryBuilder()

Tests/Functional/WebhookHandler/PaymentCaptureCompletedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testGetEventType()
4242
public function testInvokeShouldReturnFalseBecauseThereIsNoOrderServiceResult()
4343
{
4444
$webhook = new Webhook();
45-
$webhook->setResource(['id' => 'unitTestTransactionId']);
45+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
4646

4747
$this->expectException(WebhookException::class);
4848
$this->expectExceptionMessage('SwagPaymentPayPalUnified\WebhookHandlers\PaymentCaptureCompleted::invoke expect OrderAndPaymentStatusResult, got NULL');
@@ -62,7 +62,7 @@ public function testInvokeShouldReturnTrueAndUpdateOrderAndPaymentStatus()
6262
$this->updatePaymentStatus();
6363

6464
$webhook = new Webhook();
65-
$webhook->setResource(['id' => 'unitTestTransactionId']);
65+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
6666

6767
$result = $this->createPaymentCaptureCompleted()->invoke($webhook);
6868

Tests/Functional/WebhookHandler/PaymentCaptureDeniedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testGetEventType()
4242
public function testInvokeShouldThrowExceptionBecauseThereIsNoOrderServiceResult()
4343
{
4444
$webhook = new Webhook();
45-
$webhook->setResource(['id' => 'unitTestTransactionId']);
45+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
4646

4747
$this->expectException(WebhookException::class);
4848
$this->expectExceptionMessage('SwagPaymentPayPalUnified\WebhookHandlers\PaymentCaptureDenied::invoke expect OrderAndPaymentStatusResult, got NULL');
@@ -61,7 +61,7 @@ public function testInvokeShouldReturnTrueAndUpdateOrderAndPaymentStatus()
6161
$this->getContainer()->get('dbal_connection')->exec($sql);
6262

6363
$webhook = new Webhook();
64-
$webhook->setResource(['id' => 'unitTestTransactionId']);
64+
$webhook->setResource(TestWebhookResource::create('unitTestTransactionId'));
6565

6666
$result = $this->createPaymentCaptureDenied()->invoke($webhook);
6767
$databaseResult = $this->getContainer()->get('dbal_connection')->createQueryBuilder()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* (c) shopware AG <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace SwagPaymentPayPalUnified\Tests\Functional\WebhookHandler;
10+
11+
class TestWebhookResource
12+
{
13+
/**
14+
* @param string $orderId
15+
*
16+
* @return array<string,mixed>
17+
*/
18+
public static function create($orderId)
19+
{
20+
return [
21+
'supplementary_data' => [
22+
'related_ids' => [
23+
'order_id' => $orderId,
24+
],
25+
],
26+
];
27+
}
28+
}

WebhookHandlers/AbstractWebhook.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SwagPaymentPayPalUnified\PayPalBundle\Components\LoggerServiceInterface;
1414
use SwagPaymentPayPalUnified\PayPalBundle\Components\Webhook\WebhookEventTypes;
1515
use SwagPaymentPayPalUnified\PayPalBundle\Structs\Webhook;
16+
use UnexpectedValueException;
1617

1718
abstract class AbstractWebhook
1819
{
@@ -49,9 +50,10 @@ protected function getOrderServiceResult(Webhook $webhook)
4950
return null;
5051
}
5152

52-
$transactionId = $resource['id'];
53-
if (!\is_string($transactionId)) {
54-
$this->logger->error(sprintf('[Webhook]Event: %s. ResourceID is not an string got: %s', $this->getEventType(), \gettype($transactionId)), $webhook->toArray());
53+
try {
54+
$transactionId = $this->getOrderIdFromResource($resource);
55+
} catch (UnexpectedValueException $exception) {
56+
$this->logger->error(sprintf('[Webhook]Event: %s. Resource structure is not valid. Message: %s', $this->getEventType(), $exception->getMessage()));
5557

5658
return null;
5759
}
@@ -66,4 +68,28 @@ protected function getOrderServiceResult(Webhook $webhook)
6668

6769
return $shopwareOrderServiceResult;
6870
}
71+
72+
/**
73+
* @param array<string,mixed> $resource
74+
*
75+
* @return string
76+
*/
77+
private function getOrderIdFromResource(array $resource)
78+
{
79+
if (!\array_key_exists('supplementary_data', $resource) || !\is_array($resource['supplementary_data'])) {
80+
throw new UnexpectedValueException('Expect resource has array key "supplementary_data" with array value');
81+
}
82+
83+
$supplementaryData = $resource['supplementary_data'];
84+
if (!\array_key_exists('related_ids', $supplementaryData) || !\is_array($supplementaryData['related_ids'])) {
85+
throw new UnexpectedValueException('Expect supplementary_data has array key "related_ids" with array value');
86+
}
87+
88+
$relatedIds = $supplementaryData['related_ids'];
89+
if (!\array_key_exists('order_id', $relatedIds) || !\is_string($relatedIds['order_id'])) {
90+
throw new UnexpectedValueException('Expect related_ids has array key "order_id" with string value');
91+
}
92+
93+
return $relatedIds['order_id'];
94+
}
6995
}

plugin.xml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,21 @@
1212
<author>shopware AG</author>
1313
<compatibility minVersion="5.2.27" maxVersion="5.99.99"/>
1414

15-
<changelog version="6.1.2">
15+
<changelog version="6.1.1">
1616
<changes lang="de">
17+
PT-13127 - Verbessert die Benutzerfreundlichkeit für den Rechnungskauf im Checkout-Formular;
1718
PT-13134 - Verhindert das Erstellen einer PayPal Zahlung mit einem leeren Warenkorb;
1819
PT-13138 - Verbessert die Bearbeitung von Webhooks;
1920
PT-13140 - Die Zahlungsart "MyBank" ist nun standardmäßig deaktiviert;
2021
</changes>
2122
<changes lang="en">
23+
PT-13127 - Improves the usability for pay upon invoice in the checkout form;
2224
PT-13134 - Prevents the creation of a PayPal payment with an empty shopping cart;
2325
PT-13138 - Improves the processing of webhooks;
2426
PT-13140 - The payment method "MyBank" is now deactivated by default;
2527
</changes>
2628
</changelog>
2729

28-
<changelog version="6.1.1">
29-
<changes lang="de">
30-
PT-13127 - Verbessert die Benutzerfreundlichkeit für den Rechnungskauf im Checkout-Formular;
31-
</changes>
32-
<changes lang="en">
33-
PT-13127 - Improves the usability for pay upon invoice in the checkout form;
34-
</changes>
35-
</changelog>
36-
3730
<changelog version="6.1.0">
3831
<changes lang="de">
3932
PT-13108 - Nutzt Standard-Landingpage, wenn die Einstellung nicht korrekt gesetzt wurde;

0 commit comments

Comments
 (0)