Created
February 15, 2016 19:07
-
-
Save koemeet/841d90482a5c8bcbb753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* (c) Steffen Brem <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Mango\Bundle\CoreBundle\Form\Type; | |
use Mango\Component\Core\Model\PaymentProviderInterface; | |
use Mango\Component\Core\Payum\Config\GatewayConfigResolver; | |
use Payum\Core\Registry\GatewayFactoryRegistryInterface; | |
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; | |
use Sylius\Component\Resource\Repository\RepositoryInterface; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
/** | |
* PaymentGatewayType | |
* | |
* @author Steffen Brem <[email protected]> | |
*/ | |
class PaymentGatewayType extends AbstractResourceType | |
{ | |
protected $registry; | |
protected $paymentProviderRepository; | |
public function __construct( | |
$dataClass, | |
$validationGroups, | |
GatewayFactoryRegistryInterface $registry, | |
RepositoryInterface $paymentProviderRepository | |
) { | |
parent::__construct($dataClass, $validationGroups); | |
$this->registry = $registry; | |
$this->paymentProviderRepository = $paymentProviderRepository; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('workspace', 'mango_workspace_choice') | |
->add('provider', 'mango_payment_provider_choice') | |
->add('config', 'form') | |
; | |
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'buildCredentials')); | |
} | |
/** | |
* @param FormEvent $event | |
*/ | |
public function buildCredentials(FormEvent $event) | |
{ | |
$data = $event->getData(); | |
/** @var PaymentProviderInterface $provider */ | |
$provider = $this->paymentProviderRepository->find($data['provider']); | |
if (!$provider) { | |
return; | |
} | |
$resolver = new GatewayConfigResolver($provider->getFactoryName(), $this->registry); | |
$config = $resolver->resolve(); | |
$configForm = $event->getForm()->get('config'); | |
foreach ($config['payum.default_options'] as $name => $value) { | |
$type = is_bool($value) ? 'checkbox' : 'text'; | |
$options = array(); | |
$options['required'] = in_array($name, $config['payum.required_options']); | |
$configForm->add($name, $type, $options); | |
} | |
} | |
public function getName() | |
{ | |
return 'mango_payment_gateway'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment