diff --git a/DependencyInjection/PayumExtension.php b/DependencyInjection/PayumExtension.php index ab21c17..d6f3add 100644 --- a/DependencyInjection/PayumExtension.php +++ b/DependencyInjection/PayumExtension.php @@ -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?'); @@ -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')]); diff --git a/Form/Type/GatewayConfigType.php b/Form/Type/GatewayConfigType.php index cce9859..99eea52 100644 --- a/Form/Type/GatewayConfigType.php +++ b/Form/Type/GatewayConfigType.php @@ -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) { diff --git a/Sonata/GatewayConfigAdmin.php b/Sonata/GatewayConfigAdmin.php index a5a6fec..15fb35d 100644 --- a/Sonata/GatewayConfigAdmin.php +++ b/Sonata/GatewayConfigAdmin.php @@ -1,36 +1,62 @@ 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()); + } } /** @@ -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); + } } }