1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
<?php
namespace JMS\Payment\CoreBundle\Form;
use JMS\Payment\CoreBundle\Form\Transformer\ChoosePaymentMethodTransformer;
use JMS\Payment\CoreBundle\PluginController\PluginControllerInterface;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Util\Legacy;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Form Type for Choosing a Payment Method.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ChoosePaymentMethodType extends AbstractType
{
private $pluginController;
private $paymentMethods;
private $transformer;
public function __construct(PluginControllerInterface $pluginController, array $paymentMethods)
{
if (!$paymentMethods) {
throw new \InvalidArgumentException('There is no payment method available. Did you forget to register concrete payment provider bundles such as JMSPaymentPaypalBundle?');
}
$this->pluginController = $pluginController;
$this->paymentMethods = $paymentMethods;
}
public function setDataTransformer(DataTransformerInterface $transformer)
{
$this->transformer = $transformer;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$options['available_methods'] = $this->getPaymentMethods($options['allowed_methods']);
$this->buildChoiceList($builder, $options);
foreach ($options['available_methods'] as $method => $class) {
$methodOptions = isset($options['method_options'][$method]) ? $options['method_options'][$method] : array();
$builder->add('data_'.$method, $class, $methodOptions);
}
$self = $this;
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($form) use ($self, $options) {
$self->validate($form, $options);
});
// To maintain BC, we instantiate a new ChoosePaymentMethodTransformer in
// case it hasn't been supplied.
$transformer = $this->transformer
? $this->transformer
: new ChoosePaymentMethodTransformer()
;
$transformer->setOptions($options);
$builder->addModelTransformer($transformer);
}
protected function buildChoiceList(FormBuilderInterface $builder, array $options)
{
$methods = $options['available_methods'];
$choiceOptions = $options['choice_options'];
$options = array_merge(array(
'expanded' => true,
'data' => $options['default_method'],
), $options);
// Remove unwanted options
$options = array_intersect_key($options, array_flip(array(
'expanded',
'data',
)));
$options = array_merge($options, $choiceOptions);
$options['choices'] = array();
foreach (array_keys($methods) as $method) {
$label = 'form.label.'.$method;
if (Legacy::formChoicesAsValues()) {
$options['choices'][$method] = $label;
} else {
$options['choices'][$label] = $method;
}
}
$type = Legacy::supportsFormTypeName()
? 'choice'
: 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
;
$builder->add('method', $type, $options);
}
public function validate(FormEvent $event, array $options)
{
$form = $event->getForm();
$instruction = $form->getData();
if (null === $instruction->getPaymentSystemName()) {
$form->addError(new FormError('form.error.payment_method_required'));
return;
}
if (!array_key_exists($instruction->getPaymentSystemName(), $options['available_methods'])) {
$form->addError(new FormError('form.error.invalid_payment_method'));
return;
}
$result = $this->pluginController->checkPaymentInstruction($instruction);
if (Result::STATUS_SUCCESS !== $result->getStatus()) {
$this->applyErrorsToForm($form, $result);
return;
}
$result = $this->pluginController->validatePaymentInstruction($instruction);
if (Result::STATUS_SUCCESS !== $result->getStatus()) {
$this->applyErrorsToForm($form, $result);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array(
'amount',
'currency',
));
$resolver->setDefaults(array(
'predefined_data' => array(),
'allowed_methods' => array(),
'default_method' => null,
'method_options' => array(),
'choice_options' => array(),
));
$allowedTypes = array(
'amount' => array('numeric', 'closure'),
'currency' => 'string',
'predefined_data' => 'array',
'allowed_methods' => 'array',
'default_method' => array('null', 'string'),
'method_options' => 'array',
'choice_options' => 'array',
);
if (Legacy::supportsFormTypeConfigureOptions()) {
$resolver->setAllowedTypes($allowedTypes);
} else {
foreach ($allowedTypes as $key => $value) {
$resolver->addAllowedTypes($key, $value);
}
}
}
public function getBlockPrefix()
{
return 'jms_choose_payment_method';
}
/**
* Legacy support for Symfony < 3.0.
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}
/**
* Legacy support for Symfony < 3.0.
*/
public function getName()
{
return $this->getBlockPrefix();
}
private function applyErrorsToForm(FormInterface $form, Result $result)
{
$ex = $result->getPluginException();
$globalErrors = $ex->getGlobalErrors();
$dataErrors = $ex->getDataErrors();
// add a generic error message
if (!$dataErrors && !$globalErrors) {
$form->addError(new FormError('form.error.invalid_payment_instruction'));
return;
}
foreach ($globalErrors as $error) {
$form->addError(new FormError($error));
}
foreach ($dataErrors as $path => $error) {
$path = explode('.', $path);
$field = $form;
do {
$field = $field->get(array_shift($path));
} while ($path);
$field->addError(new FormError($error));
}
}
private function getPaymentMethods($allowedMethods)
{
$allowAllMethods = empty($allowedMethods);
$availableMethods = array();
foreach ($this->paymentMethods as $methodKey => $methodClass) {
if (!$allowAllMethods && !in_array($methodKey, $allowedMethods, true)) {
continue;
}
$availableMethods[$methodKey] = $methodClass;
}
if (empty($availableMethods)) {
throw new \RuntimeException(sprintf(
'You have not selected any payment methods. Available methods: "%s"',
implode(', ', $this->paymentMethods)
));
}
return $availableMethods;
}
} |
Partager