From 4fc80875524e591f273c4e63b6433d9d9ba29e45 Mon Sep 17 00:00:00 2001 From: Dennis Garding Date: Thu, 13 Jun 2024 13:06:07 +0200 Subject: [PATCH] PT-13155 - Add instance id --- .../TransactionReport/TransactionReport.php | 3 +- Setup/Assets/tables.sql | 7 ++ Setup/Installer.php | 9 ++- Setup/InstanceIdService.php | 77 +++++++++++++++++++ Setup/Updater.php | 7 ++ Setup/Versions/UpdateTo618.php | 46 +++++++++++ Subscriber/TransactionReportSubscriber.php | 2 + SwagPaymentPayPalUnified.php | 3 +- .../TransactionReportTest.php | 2 +- .../Setup/InstanceIdServiceTest.php | 35 +++++++++ .../Setup/Versions/UpdateTo618Test.php | 46 +++++++++++ .../Subscriber/PayUponInvoiceTest.php | 20 ++--- .../Frontend/PaypalUnifiedApmTest.php | 24 +++--- 13 files changed, 255 insertions(+), 26 deletions(-) create mode 100644 Setup/InstanceIdService.php create mode 100644 Setup/Versions/UpdateTo618.php create mode 100644 Tests/Functional/Setup/InstanceIdServiceTest.php create mode 100644 Tests/Functional/Setup/Versions/UpdateTo618Test.php diff --git a/Components/TransactionReport/TransactionReport.php b/Components/TransactionReport/TransactionReport.php index 517de950..6b2dcca2 100644 --- a/Components/TransactionReport/TransactionReport.php +++ b/Components/TransactionReport/TransactionReport.php @@ -50,7 +50,7 @@ public function reportOrder($orderId) * * @return void */ - public function report($shopwareVersion, Client $client) + public function report($shopwareVersion, $instanceId, Client $client) { $reportResult = $this->getReportResult($this->getReportedOrderIds()); $currencies = $reportResult->getCurrencies(); @@ -60,6 +60,7 @@ public function report($shopwareVersion, Client $client) 'identifier' => self::API_IDENTIFIER, 'reportDate' => (new DateTime())->format('Y-m-d\\TH:i:sP'), 'shopwareVersion' => $shopwareVersion, + 'instanceId' => $instanceId, 'currency' => $currency, 'reportDataKeys' => ['turnover' => $reportResult->getTurnover($currency)], ]; diff --git a/Setup/Assets/tables.sql b/Setup/Assets/tables.sql index d2b2b196..53c3ffda 100644 --- a/Setup/Assets/tables.sql +++ b/Setup/Assets/tables.sql @@ -150,3 +150,10 @@ CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_transaction_report` ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci; + +CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance` +( + `instance_id` VARCHAR(255) NOT NULL +) ENGINE = InnoDB + DEFAULT CHARSET = utf8 + COLLATE = utf8_unicode_ci; diff --git a/Setup/Installer.php b/Setup/Installer.php index 5ceedaed..5c37916e 100644 --- a/Setup/Installer.php +++ b/Setup/Installer.php @@ -87,9 +87,9 @@ public function __construct( } /** + * @return bool * @throws InstallationException * - * @return bool */ public function install() { @@ -108,6 +108,13 @@ public function install() true ); + try { + // call the instance id service to create the instance id + (new InstanceIdService($this->connection))->getInstanceId(); + } catch (\Exception $e) { + throw new InstallationException($e->getMessage()); + } + return true; } diff --git a/Setup/InstanceIdService.php b/Setup/InstanceIdService.php new file mode 100644 index 00000000..d78d34eb --- /dev/null +++ b/Setup/InstanceIdService.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagPaymentPayPalUnified\Setup; + +use Doctrine\DBAL\Connection; +use SwagPaymentPayPalUnified\Components\Uuid; + +final class InstanceIdService +{ + /** + * @var Connection + */ + private $connection; + + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * @return string + */ + public function getInstanceId() + { + $instanceId = $this->get(); + + if ($instanceId === null) { + $instanceId = $this->create(); + } + + return $instanceId; + } + + /** + * @return string|null + */ + private function get() + { + $result = $this->connection->createQueryBuilder() + ->select('instance_id') + ->from('swag_payment_paypal_unified_instance') + ->execute() + ->fetchColumn(); + + if (!$result) { + return null; + } + + return $result; + } + + /** + * @return string + */ + private function create() + { + $instanceId = Uuid::generateUuid(); + + $this->connection->createQueryBuilder() + ->insert('swag_payment_paypal_unified_instance') + ->values(['instance_id' => ':instanceId']) + ->setParameter('instanceId', $instanceId) + ->execute(); + + if ($instanceId !== $this->get()) { + throw new \RuntimeException('Could not create instance id'); + } + + return $instanceId; + } +} diff --git a/Setup/Updater.php b/Setup/Updater.php index b2224692..c2a81676 100644 --- a/Setup/Updater.php +++ b/Setup/Updater.php @@ -32,6 +32,7 @@ use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo610; use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo616; use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo617; +use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo618; class Updater { @@ -256,6 +257,12 @@ public function update($oldVersion) $this->connection ))->update(); } + + if (\version_compare($oldVersion, '6.1.8', '<')) { + (new UpdateTo618( + $this->connection + ))->update(); + } } /** diff --git a/Setup/Versions/UpdateTo618.php b/Setup/Versions/UpdateTo618.php new file mode 100644 index 00000000..d4f324b0 --- /dev/null +++ b/Setup/Versions/UpdateTo618.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagPaymentPayPalUnified\Setup\Versions; + +use Doctrine\DBAL\Connection; +use SwagPaymentPayPalUnified\Setup\InstanceIdService; + +class UpdateTo618 +{ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * @return void + */ + public function update() + { + $this->createInstanceTable(); + + (new InstanceIdService($this->connection))->getInstanceId(); + } + + /** + * @return void + */ + private function createInstanceTable() + { + $this->connection->executeQuery( + ' + CREATE TABLE IF NOT EXISTS `swag_payment_paypal_unified_instance` + ( + `instance_id` VARCHAR(255) NOT NULL + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8 + COLLATE = utf8_unicode_ci;' + ); + } +} diff --git a/Subscriber/TransactionReportSubscriber.php b/Subscriber/TransactionReportSubscriber.php index 35b94375..d9c024fe 100644 --- a/Subscriber/TransactionReportSubscriber.php +++ b/Subscriber/TransactionReportSubscriber.php @@ -13,6 +13,7 @@ use GuzzleHttp\Client; use Shopware; use SwagPaymentPayPalUnified\Components\TransactionReport\TransactionReport; +use SwagPaymentPayPalUnified\Setup\InstanceIdService; use Symfony\Component\DependencyInjection\ContainerInterface; class TransactionReportSubscriber implements SubscriberInterface @@ -54,6 +55,7 @@ public function onTransactionReport() (new TransactionReport($this->connection))->report( $shopwareVersion, + (new InstanceIdService($this->connection))->getInstanceId(), new Client(['base_uri' => TransactionReport::POST_URL]) ); } diff --git a/SwagPaymentPayPalUnified.php b/SwagPaymentPayPalUnified.php index dfc3f669..aa7d687b 100644 --- a/SwagPaymentPayPalUnified.php +++ b/SwagPaymentPayPalUnified.php @@ -102,7 +102,8 @@ public function update(UpdateContext $context) $this->getPaymentMethodProvider(), new PaymentModelFactory($context->getPlugin()), $this->getTranslation(), - new TranslationTransformer($this->container->get('models')) + new TranslationTransformer($this->container->get('models')), + $this->getPath() ); $updater->update($context->getCurrentVersion()); diff --git a/Tests/Functional/Components/TransactionReport/TransactionReportTest.php b/Tests/Functional/Components/TransactionReport/TransactionReportTest.php index e8098e1d..57bf4883 100644 --- a/Tests/Functional/Components/TransactionReport/TransactionReportTest.php +++ b/Tests/Functional/Components/TransactionReport/TransactionReportTest.php @@ -73,7 +73,7 @@ public function testReport() } $clientMock->expects(static::exactly(4))->method('post'); - $transactionReport->report('5.7.18', $clientMock); + $transactionReport->report('5.7.18', 'instanceId', $clientMock); } /** diff --git a/Tests/Functional/Setup/InstanceIdServiceTest.php b/Tests/Functional/Setup/InstanceIdServiceTest.php new file mode 100644 index 00000000..2b484aa2 --- /dev/null +++ b/Tests/Functional/Setup/InstanceIdServiceTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagPaymentPayPalUnified\Tests\Functional\Setup; + +use PHPUnit\Framework\TestCase; +use SwagPaymentPayPalUnified\Setup\InstanceIdService; +use SwagPaymentPayPalUnified\Tests\Functional\ContainerTrait; + +class InstanceIdServiceTest extends TestCase +{ + use ContainerTrait; + + /** + * @return void + */ + public function testGetInstanceId() + { + $connection = $this->getContainer()->get('dbal_connection'); + $instanceIdService = new InstanceIdService($connection); + + $instanceId = $instanceIdService->getInstanceId(); + static::assertNotEmpty($instanceId); + + $connection->executeQuery('DELETE FROM swag_payment_paypal_unified_instance WHERE true'); + + $instanceId = $instanceIdService->getInstanceId(); + static::assertNotEmpty($instanceId); + } +} diff --git a/Tests/Functional/Setup/Versions/UpdateTo618Test.php b/Tests/Functional/Setup/Versions/UpdateTo618Test.php new file mode 100644 index 00000000..735dc594 --- /dev/null +++ b/Tests/Functional/Setup/Versions/UpdateTo618Test.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagPaymentPayPalUnified\Tests\Functional\Setup; + +use PHPUnit\Framework\TestCase; +use SwagPaymentPayPalUnified\Setup\InstanceIdService; +use SwagPaymentPayPalUnified\Setup\Versions\UpdateTo618; +use SwagPaymentPayPalUnified\Tests\Functional\ContainerTrait; +use SwagPaymentPayPalUnified\Tests\Functional\DatabaseHelperTrait; + +class UpdateTo618Test extends TestCase +{ + use ContainerTrait; + use DatabaseHelperTrait; + + /** + * @return void + */ + public function testUpdate() + { + $sql = 'DROP TABLE `swag_payment_paypal_unified_instance`'; + $connection = $this->getContainer()->get('dbal_connection'); + $connection->exec($sql); + static::assertFalse($this->checkTableExists($connection, 'swag_payment_paypal_unified_instance')); + + $updater = new UpdateTo618($connection); + $updater->update(); + $updater->update(); + + static::assertTrue($this->checkTableExists($connection, 'swag_payment_paypal_unified_instance')); + + $result = $connection->createQueryBuilder() + ->select('instance_id') + ->from('swag_payment_paypal_unified_instance') + ->execute() + ->fetchColumn(); + + static::assertNotEmpty($result); + } +} diff --git a/Tests/Functional/Subscriber/PayUponInvoiceTest.php b/Tests/Functional/Subscriber/PayUponInvoiceTest.php index f9dc1691..40cd2bce 100644 --- a/Tests/Functional/Subscriber/PayUponInvoiceTest.php +++ b/Tests/Functional/Subscriber/PayUponInvoiceTest.php @@ -506,16 +506,16 @@ public function testAssignPaypalPaymentInstructions() $subscriber->assignPaypalPaymentInstructions($args); static::assertEquals([ - 'id' => null, - 'orderNumber' => '000000', - 'order' => null, - 'bankName' => 'FOO Bank', - 'accountHolder' => 'FOO BAR', - 'iban' => 'IBAN', - 'bic' => 'BIC', - 'amount' => '99', - 'dueDate' => null, - 'reference' => null, + 'id' => null, + 'orderNumber' => '000000', + 'order' => null, + 'bankName' => 'FOO Bank', + 'accountHolder' => 'FOO BAR', + 'iban' => 'IBAN', + 'bic' => 'BIC', + 'amount' => '99', + 'dueDate' => null, + 'reference' => null, ], $args->getSubject()->View()->getAssign('paypalUnifiedPaymentInstructions')); } diff --git a/Tests/Unit/Controllers/Frontend/PaypalUnifiedApmTest.php b/Tests/Unit/Controllers/Frontend/PaypalUnifiedApmTest.php index dee434ce..94f22ac9 100644 --- a/Tests/Unit/Controllers/Frontend/PaypalUnifiedApmTest.php +++ b/Tests/Unit/Controllers/Frontend/PaypalUnifiedApmTest.php @@ -83,19 +83,19 @@ public function testPaymentStateIsUpdatedCorrectly($intent, $paypalOrderState, $ $this->givenThePayPalOrder( self::PAYPAL_ORDER_ID, (new Order())->assign([ - 'id' => self::PAYPAL_ORDER_ID, - 'intent' => $intent, - 'status' => $paypalOrderState, - 'purchaseUnits' => [ - $this->createConfiguredMock(PurchaseUnit::class, [ - 'getAmount' => $this->createMock(Amount::class), - 'getPayments' => $this->createConfiguredMock(Payments::class, [ - 'getCaptures' => [$capture], - 'getAuthorizations' => [$authorization], + 'id' => self::PAYPAL_ORDER_ID, + 'intent' => $intent, + 'status' => $paypalOrderState, + 'purchaseUnits' => [ + $this->createConfiguredMock(PurchaseUnit::class, [ + 'getAmount' => $this->createMock(Amount::class), + 'getPayments' => $this->createConfiguredMock(Payments::class, [ + 'getCaptures' => [$capture], + 'getAuthorizations' => [$authorization], + ]), ]), - ]), - ], - ]), + ], + ]), $orderWillReturnOrder ); $this->givenTheCustomer(self::DEFAULT_CUSTOMER_DATA);