Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DependencyInjection/PayumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected function loadDynamicGateways(array $dynamicGatewaysConfig, ContainerBu
$container->setDefinition('payum.dynamic_registry', $registry);

if ($dynamicGatewaysConfig['sonata_admin']) {
throw new \LogicException('Not supported. Has to wait till Sonata Admin 4.x will be released.');
//throw new \LogicException('Not supported. Has to wait till Sonata Admin 4.x will be released.');

if (false === class_exists(AbstractAdmin::class)) {
throw new LogicException('Admin class does not exists. Did you install SonataAdmin bundle?');
Expand All @@ -239,7 +239,7 @@ protected function loadDynamicGateways(array $dynamicGatewaysConfig, ContainerBu
$configClass,
null
]);
$gatewayConfigAdmin->addMethodCall('setFormFactory', [new Reference('form.factory')]);
$gatewayConfigAdmin->addMethodCall('setGatewayFactoryRegistry', [new Reference('payum')]);

if ($container->hasDefinition('payum.dynamic_gateways.cypher')) {
$gatewayConfigAdmin->addMethodCall('setCypher', [new Reference('payum.dynamic_gateways.cypher')]);
Expand Down
12 changes: 10 additions & 2 deletions Form/Type/GatewayConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ public function buildCredentials(FormEvent $event): void
}

$form = $event->getForm();

$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
$config = $gatewayFactory->createConfig();

if (isset($config['payum.gateway_config_type'])) {
$form->add('config', $config['payum.gateway_config_type']);
$event->setData($data);

return;
}

$form->add('config', FormType::class);
$configForm = $form->get('config');

$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
$config = $gatewayFactory->createConfig();
$propertyPath = is_array($data) ? '[config]' : 'config';
$firstTime = ! PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
foreach ($config['payum.default_options'] as $name => $value) {
Expand Down
98 changes: 77 additions & 21 deletions Sonata/GatewayConfigAdmin.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
<?php
namespace Payum\Bundle\PayumBundle\Sonata;

use Payum\Bundle\PayumBundle\Form\Type\GatewayFactoriesChoiceType;
use Payum\Core\Registry\GatewayFactoryRegistryInterface;
use Payum\Core\Security\CryptedInterface;
use Payum\Core\Security\CypherInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Payum\Bundle\PayumBundle\Form\Type\GatewayConfigType;
use Symfony\Component\PropertyAccess\PropertyAccess;

class GatewayConfigAdmin extends AbstractAdmin
{
protected FormFactoryInterface $formFactory;
protected ?CypherInterface $cypher;

public function setFormFactory(FormFactoryInterface $formFactory): void
{
$this->formFactory = $formFactory;
}

protected GatewayFactoryRegistryInterface $registry;

public function setCypher(CypherInterface $cypher): void
{
$this->cypher = $cypher;
}

public function setGatewayFactoryRegistry(GatewayFactoryRegistryInterface $registry)
{
$this->registry = $registry;
}

/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $form): void
{
$form->reorder(array()); //hack!

$form
->with('General', ['class' => 'col-md-4'])->end()
->with('Configuration', ['class' => 'col-md-8'])->end()
;

$form
->with('General')
->add('gatewayName')
->add('factoryName', GatewayFactoriesChoiceType::class, [
'disabled' => (bool) $this->getSubject() && null !== $this->getSubject()->getId(),
])
->end()
;

if ($this->getSubject() && $this->getSubject()->getId()) {
$this->buildCredentials($form, $this->getSubject());
}
}

/**
Expand Down Expand Up @@ -78,28 +104,58 @@ public function prePersist($object): void
/**
* {@inheritdoc}
*/
public function getObject($id)
protected function alterObject(object $object): void
{
$object = parent::getObject($id);

if ($this->cypher && $object instanceof CryptedInterface) {
$object->decrypt($this->cypher);
}

return $object;
}

/**
* {@inheritDoc}
*/
public function getFormBuilder(): FormBuilderInterface
public function buildCredentials(FormMapper $form, object $object): void
{
$formBuilder = $this->formFactory->createBuilder(GatewayConfigType::class, $this->getSubject(), array(
'data_class' => get_class($this->getSubject()),
));
/** @var array $data */
$data = $object;


$propertyPath = is_array($data) ? '[factoryName]' : 'factoryName';
$factoryName = PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);
if (empty($factoryName)) {
return;
}

$gatewayFactory = $this->registry->getGatewayFactory($factoryName);
$config = $gatewayFactory->createConfig();

if (isset($config['payum.gateway_config_type'])) {
$form
->with('Configuration')
->add('config', $config['payum.gateway_config_type'], [
'label' => false,
])
->end()
;

$this->defineFormBuilder($formBuilder);
return;
}

$form->add('config', FormType::class);
$configForm = $form->get('config');

$propertyPath = is_array($data) ? '[config]' : 'config';
$firstTime = ! PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath);

foreach ($config['payum.default_options'] as $name => $value) {
$propertyPath = is_array($data) ? "[config][{$name}]" : "config[{$name}]";
if ($firstTime) {
PropertyAccess::createPropertyAccessor()->setValue($data, $propertyPath, $value);
}

return $formBuilder;
$type = is_bool($value) ? CheckboxType::class : TextType::class;

$options = [];
$options['required'] = in_array($name, $config['payum.required_options']);

$configForm->add($name, $type, $options);
}
}
}