Skip to content

Commit

Permalink
PT-13155 - Add instance id
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisGarding committed Jun 14, 2024
1 parent 73e5dbb commit 4fc8087
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Components/TransactionReport/TransactionReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)],
];
Expand Down
7 changes: 7 additions & 0 deletions Setup/Assets/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 8 additions & 1 deletion Setup/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function __construct(
}

/**
* @return bool
* @throws InstallationException
*
* @return bool
*/
public function install()
{
Expand All @@ -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;
}

Expand Down
77 changes: 77 additions & 0 deletions Setup/InstanceIdService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* (c) shopware AG <[email protected]>
*
* 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;
}
}
7 changes: 7 additions & 0 deletions Setup/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -256,6 +257,12 @@ public function update($oldVersion)
$this->connection
))->update();
}

if (\version_compare($oldVersion, '6.1.8', '<')) {
(new UpdateTo618(
$this->connection
))->update();
}
}

/**
Expand Down
46 changes: 46 additions & 0 deletions Setup/Versions/UpdateTo618.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* (c) shopware AG <[email protected]>
*
* 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;'
);
}
}
2 changes: 2 additions & 0 deletions Subscriber/TransactionReportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
);
}
Expand Down
3 changes: 2 additions & 1 deletion SwagPaymentPayPalUnified.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions Tests/Functional/Setup/InstanceIdServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* (c) shopware AG <[email protected]>
*
* 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);
}
}
46 changes: 46 additions & 0 deletions Tests/Functional/Setup/Versions/UpdateTo618Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* (c) shopware AG <[email protected]>
*
* 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);
}
}
20 changes: 10 additions & 10 deletions Tests/Functional/Subscriber/PayUponInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
24 changes: 12 additions & 12 deletions Tests/Unit/Controllers/Frontend/PaypalUnifiedApmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4fc8087

Please sign in to comment.