Skip to content

Commit

Permalink
Merge pull request #6 from Adyen/develop
Browse files Browse the repository at this point in the history
Release 1.4.1
  • Loading branch information
acampos1916 authored Jun 16, 2020
2 parents 9d71f12 + 61bf786 commit 648357f
Show file tree
Hide file tree
Showing 122 changed files with 9,153 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @cyattilakiss @acampos1916 @msilvagarcia
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#development
.idea/
*.iml
.DS_Store

#composer
vendor/
238 changes: 238 additions & 0 deletions AdyenPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php

namespace AdyenPayment;

use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\Tools\SchemaTool;
use Exception;
use AdyenPayment\Components\CompilerPass\NotificationProcessorCompilerPass;
use AdyenPayment\Models\Notification;
use AdyenPayment\Models\PaymentInfo;
use AdyenPayment\Models\Refund;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
use Shopware\Components\Logger;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\PaymentInstaller;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

/**
* Class AdyenPayment
* @package AdyenPayment
*/
class AdyenPayment extends Plugin
{
const NAME = 'AdyenPayment';
const ADYEN_GENERAL_PAYMENT_METHOD = 'adyen_general_payment_method';
const ADYEN_PAYMENT_PAYMENT_METHOD = 'adyen_payment_payment_method';

const SESSION_ADYEN_PAYMENT = 'adyenPayment';
const SESSION_ADYEN_PAYMENT_VALID = 'adyenPaymentValid';
const SESSION_ADYEN_PAYMENT_DATA = 'adyenPaymentData';

/**
* @return bool
*/
public static function isPackage(): bool
{
return file_exists(self::getPackageVendorAutoload());
}

/**
* @return string
*/
public static function getPackageVendorAutoload(): string
{
return __DIR__ . '/vendor/autoload.php';
}

/**
* @param ContainerBuilder $container
* @throws Exception
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new NotificationProcessorCompilerPass());

parent::build($container);

//set default logger level for 5.4
if (!$container->hasParameter('kernel.default_error_level')) {
$container->setParameter('kernel.default_error_level', Logger::ERROR);
}

$loader = new XmlFileLoader(
$container,
new FileLocator()
);

$loader->load(__DIR__ . '/Resources/services.xml');

$versionCheck = $container->get('adyen_payment.components.shopware_version_check');

if ($versionCheck->isHigherThanShopwareVersion('v5.6.2')) {
$loader->load(__DIR__ . '/Resources/services/version/563.xml');
}
}

/**
* @param InstallContext $context
* @throws Exception
*/
public function install(InstallContext $context)
{
$this->installAttributes();

$tool = new SchemaTool($this->container->get('models'));
$classes = $this->getModelMetaData();
$tool->updateSchema($classes, true);
}

/**
* @param UninstallContext $context
* @throws Exception
*/
public function uninstall(UninstallContext $context)
{
$this->deactivatePaymentMethods();

if (!$context->keepUserData()) {
$this->uninstallAttributes($context);

$tool = new SchemaTool($this->container->get('models'));
$classes = $this->getModelMetaData();
$tool->dropSchema($classes);
}

if ($context->getPlugin()->getActive()) {
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}
}

/**
* @param DeactivateContext $context
*/
public function deactivate(DeactivateContext $context)
{
$this->deactivatePaymentMethods();

$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}

/**
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
/** @var PaymentInstaller $installer */
$installer = $this->container->get('shopware.plugin_payment_installer');

$paymentOptions[] = $this->getPaymentOptions();

foreach ($paymentOptions as $key => $options) {
$installer->createOrUpdate($context->getPlugin(), $options);
}

$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}

/**
* Deactivate all Adyen payment methods
*/
private function deactivatePaymentMethods()
{
$em = $this->container->get('models');
$qb = $em->createQueryBuilder();

$query = $qb->update('Shopware\Models\Payment\Payment', 'p')
->set('p.active', '?1')
->where($qb->expr()->like('p.name', '?2'))
->setParameter(1, false)
->setParameter(2, self::ADYEN_GENERAL_PAYMENT_METHOD)
->getQuery();

$query->execute();
}

/**
* @param UninstallContext $uninstallContext
* @throws Exception
*/
private function uninstallAttributes(UninstallContext $uninstallContext)
{
$crudService = $this->container->get('shopware_attribute.crud_service');
$crudService->delete('s_user_attributes', self::ADYEN_PAYMENT_PAYMENT_METHOD);

$this->rebuildAttributeModels();
}

/**
* @throws Exception
*/
private function installAttributes()
{
$crudService = $this->container->get('shopware_attribute.crud_service');
$crudService->update(
's_user_attributes',
self::ADYEN_PAYMENT_PAYMENT_METHOD,
TypeMapping::TYPE_STRING,
[
'displayInBackend' => true,
'label' => 'Adyen Payment Method'
]
);

$this->rebuildAttributeModels();
}

/**
* @return array
*/
private function getModelMetaData(): array
{
$entityManager = $this->container->get('models');

return [
$entityManager->getClassMetadata(Notification::class),
$entityManager->getClassMetadata(PaymentInfo::class),
$entityManager->getClassMetadata(Refund::class),
];
}

/**
* @return array
*/
private function getPaymentOptions()
{
$options = [
'name' => self::ADYEN_GENERAL_PAYMENT_METHOD,
'description' => 'Adyen payment methods',
'action' => null,
'active' => 1,
'position' => 0,
'additionalDescription' => ''
];

return $options;
}

private function rebuildAttributeModels()
{
/** @var Cache $metaDataCache */
$metaDataCache = $this->container->get('models')->getConfiguration()->getMetadataCacheImpl();
if ($metaDataCache) {
$metaDataCache->deleteAll();
}

$this->container->get('models')->generateAttributeModels(['s_user_attributes']);
}
}

if (AdyenPayment::isPackage()) {
require_once AdyenPayment::getPackageVendorAutoload();
}
94 changes: 94 additions & 0 deletions Commands/ProcessNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace AdyenPayment\Commands;

use AdyenPayment\Components\FifoNotificationLoader;
use AdyenPayment\Components\NotificationProcessor;
use AdyenPayment\Models\Feedback\NotificationProcessorFeedback;
use AdyenPayment\Subscriber\Cronjob\ProcessNotifications as ProcessNotificationsCronjob;
use Shopware\Commands\ShopwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ProcessNotifications
* @package AdyenPayment\Commands
*/
class ProcessNotifications extends ShopwareCommand
{
/**
* @var FifoNotificationLoader
*/
private $loader;
/**
* @var NotificationProcessor
*/
private $notificationProcessor;

/**
* ProcessNotifications constructor.
* @param FifoNotificationLoader $fifoNotificationLoader
* @param NotificationProcessor $notificationProcessor
*/
public function __construct(
FifoNotificationLoader $fifoNotificationLoader,
NotificationProcessor $notificationProcessor
) {
$this->loader = $fifoNotificationLoader;
$this->notificationProcessor = $notificationProcessor;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('adyen:payment:process:notifications')
->setDescription('Process notifications in queue')
->addOption(
'number',
'no',
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
'Number of notifications to process. Defaults to ' .
ProcessNotificationsCronjob::NUMBER_OF_NOTIFICATIONS_TO_HANDLE . '.',
ProcessNotificationsCronjob::NUMBER_OF_NOTIFICATIONS_TO_HANDLE
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void|null
* @throws \Doctrine\ORM\ORMException
* @throws \Enlight_Event_Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$number = $input->getOption('number');

/** @var \Generator<NotificationProcessorFeedback> $feedback */
$feedback = $this->notificationProcessor->processMany(
$this->loader->load($number)
);

$totalCount = 0;
$successCount = 0;

/** @var NotificationProcessorFeedback $item */
foreach ($feedback as $item) {
$totalCount++;
$successCount += (int)$item->isSuccess();
$output->writeln($item->getNotification()->getId() . ": " . $item->getMessage());
}

$output->writeln(sprintf(
'Imported %d items. %s OK, %s FAILED',
$totalCount,
$successCount,
$totalCount - $successCount
));
}
}
Loading

0 comments on commit 648357f

Please sign in to comment.