Skip to content

Commit

Permalink
Merge pull request #41 from Payum/add-apis-before-actions
Browse files Browse the repository at this point in the history
Add apis before actions
  • Loading branch information
makasim committed Aug 18, 2013
2 parents 28631b7 + 948f3af commit 58d2bf1
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 185 deletions.
116 changes: 96 additions & 20 deletions DependencyInjection/Factory/Payment/AbstractPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,17 @@ public function create(ContainerBuilder $container, $contextName, array $config)
$paymentId = 'payum.context.'.$contextName.'.payment';
$container->setDefinition($paymentId, $paymentDefinition);

foreach (array_reverse($config['apis']) as $apiId) {
$paymentDefinition->addMethodCall(
'addApi',
array(new Reference($apiId), $forcePrepend = true)
);
}

foreach (array_reverse($config['actions']) as $actionId) {
$paymentDefinition->addMethodCall(
'addAction',
array(new Reference($actionId), $forcePrepend = true)
);
}
$this->addCommonApis($paymentDefinition, $container, $contextName, $config);
$this->addApis($paymentDefinition, $container, $contextName, $config);
$this->addCustomApis($paymentDefinition, $container, $contextName, $config);

foreach (array_reverse($config['extensions']) as $extensionId) {
$paymentDefinition->addMethodCall(
'addExtension',
array(new Reference($extensionId), $forcePrepend = true)
);
}

$this->addCommonActions($paymentDefinition);
$this->addActions($paymentDefinition, $container, $contextName, $config);
$this->addCustomActions($paymentDefinition, $container, $contextName, $config);

$this->addCommonExtensions($paymentDefinition);
$this->addExtensions($paymentDefinition, $container, $contextName, $config);
$this->addCustomExtensions($paymentDefinition, $container, $contextName, $config);

return $paymentId;
}
Expand Down Expand Up @@ -104,6 +92,94 @@ protected function createContextParameter(ContainerBuilder $container, $contextN
return "%{$contextParameter}%";
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addCustomApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
foreach (array_reverse($config['apis']) as $apiId) {
$paymentDefinition->addMethodCall(
'addApi',
array(new Reference($apiId), $forcePrepend = true)
);
}
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addCustomActions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
foreach (array_reverse($config['actions']) as $actionId) {
$paymentDefinition->addMethodCall(
'addAction',
array(new Reference($actionId), $forcePrepend = true)
);
}
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addCustomExtensions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
foreach (array_reverse($config['extensions']) as $extensionId) {
$paymentDefinition->addMethodCall(
'addExtension',
array(new Reference($extensionId), $forcePrepend = true)
);
}
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addActions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
}

/**
* @param Definition $paymentDefinition
* @param ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addExtensions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
}

/**
* @param Definition $paymentDefinition
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param $contextName
* @param array $config
*/
protected function addCommonApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
}

/**
* @param Definition $paymentDefinition
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,11 @@ public function create(ContainerBuilder $container, $contextName, array $config)
if (false == class_exists('Payum\AuthorizeNet\Aim\PaymentFactory')) {
throw new RuntimeException('Cannot find Authorize.net payment factory class. Have you installed payum/authorize-net-aim package?');
}

$paymentId = parent::create($container, $contextName, $config);
$paymentDefinition = $container->getDefinition($paymentId);


$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/payment'));
$loader->load('authorize_net_aim.xml');

$apiDefinition = new DefinitionDecorator('payum.authorize_net_aim.api');
$apiDefinition->replaceArgument(0, $config['api']['options']['login_id']);
$apiDefinition->replaceArgument(1, $config['api']['options']['transaction_key']);
$apiDefinition->addMethodCall('setSandbox', array($config['api']['options']['sandbox']));
$apiId = 'payum.context.'.$contextName.'.api';
$container->setDefinition($apiId, $apiDefinition);
$paymentDefinition->addMethodCall('addApi', array(new Reference($apiId)));

$captureActionDefinition = new DefinitionDecorator('payum.authorize_net_aim.action.capture');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new DefinitionDecorator('payum.authorize_net_aim.action.status');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));

return $paymentId;
return parent::create($container, $contextName, $config);
}

/**
Expand Down Expand Up @@ -75,4 +54,34 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->end()
->end();
}

/**
* {@inheritDoc}
*/
protected function addApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$apiDefinition = new DefinitionDecorator('payum.authorize_net_aim.api');
$apiDefinition->replaceArgument(0, $config['api']['options']['login_id']);
$apiDefinition->replaceArgument(1, $config['api']['options']['transaction_key']);
$apiDefinition->addMethodCall('setSandbox', array($config['api']['options']['sandbox']));
$apiId = 'payum.context.'.$contextName.'.api';
$container->setDefinition($apiId, $apiDefinition);
$paymentDefinition->addMethodCall('addApi', array(new Reference($apiId)));
}

/**
* {@inheritDoc}
*/
protected function addActions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$captureActionDefinition = new DefinitionDecorator('payum.authorize_net_aim.action.capture');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new DefinitionDecorator('payum.authorize_net_aim.action.status');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));
}
}
53 changes: 31 additions & 22 deletions DependencyInjection/Factory/Payment/Be2BillPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,10 @@ public function create(ContainerBuilder $container, $contextName, array $config)
throw new RuntimeException('Cannot find be2bill payment factory class. Have you installed payum/be2bill package?');
}

$paymentId = parent::create($container, $contextName, $config);
$paymentDefinition = $container->getDefinition($paymentId);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/payment'));
$loader->load('be2bill.xml');

$apiDefinition = new DefinitionDecorator('payum.be2bill.api');
$apiDefinition->replaceArgument(0, new Reference($config['api']['client']));
$apiDefinition->replaceArgument(1, $config['api']['options']);
$apiDefinition->setPublic(true);
$apiId = 'payum.context.'.$contextName.'.api';
$container->setDefinition($apiId, $apiDefinition);
$paymentDefinition->addMethodCall('addApi', array(new Reference($apiId)));

$captureActionDefinition = new DefinitionDecorator('payum.be2bill.action.capture');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new DefinitionDecorator('payum.be2bill.action.status');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));

return $paymentId;
return parent::create($container, $contextName, $config);
}

/**
Expand Down Expand Up @@ -76,4 +55,34 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->end()
->end();
}

/**
* {@inheritDoc}
*/
protected function addApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$apiDefinition = new DefinitionDecorator('payum.be2bill.api');
$apiDefinition->replaceArgument(0, new Reference($config['api']['client']));
$apiDefinition->replaceArgument(1, $config['api']['options']);
$apiDefinition->setPublic(true);
$apiId = 'payum.context.'.$contextName.'.api';
$container->setDefinition($apiId, $apiDefinition);
$paymentDefinition->addMethodCall('addApi', array(new Reference($apiId)));
}

/**
* {@inheritDoc}
*/
protected function addActions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$captureActionDefinition = new DefinitionDecorator('payum.be2bill.action.capture');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new DefinitionDecorator('payum.be2bill.action.status');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));
}
}
70 changes: 38 additions & 32 deletions DependencyInjection/Factory/Payment/OmnipayPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,7 @@ public function create(ContainerBuilder $container, $contextName, array $config)
throw new RuntimeException('Cannot find GatewayInterface interface. Have you installed omnipay/omnipay package?');
}

$paymentId = parent::create($container, $contextName, $config);
$paymentDefinition = $container->getDefinition($paymentId);

$gatewayDefinition = new Definition();
$gatewayDefinition->setClass('Omnipay\Common\GatewayInterface');
$gatewayDefinition->setFactoryClass('Omnipay\Common\GatewayFactory');
$gatewayDefinition->setFactoryMethod('create');
$gatewayDefinition->addArgument($config['type']);
$gatewayDefinition->setPublic(true);
foreach ($config['options'] as $name => $value) {
$gatewayDefinition->addMethodCall('set'.strtoupper($name), array($value));
}

$gatewayId = 'payum.context.'.$contextName.'.gateway';
$container->setDefinition($gatewayId, $gatewayDefinition);

//TODO: work around for current version. Do better fix in 0.6.x
$methodCalls = $paymentDefinition->getMethodCalls();
array_unshift($methodCalls, array('addApi', array(new Reference($gatewayId))));
$paymentDefinition->setMethodCalls($methodCalls);

$captureActionDefinition = new Definition('Payum\Bridge\Omnipay\Action\CaptureAction');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new Definition('Payum\Bridge\Omnipay\Action\StatusAction');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));

return $paymentId;
return parent::create($container, $contextName, $config);
}

/**
Expand Down Expand Up @@ -102,4 +71,41 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->thenInvalid('A message')
;
}

/**
* {@inheritDoc}
*/
protected function addApis(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$gatewayDefinition = new Definition();
$gatewayDefinition->setClass('Omnipay\Common\GatewayInterface');
$gatewayDefinition->setFactoryClass('Omnipay\Common\GatewayFactory');
$gatewayDefinition->setFactoryMethod('create');
$gatewayDefinition->addArgument($config['type']);
$gatewayDefinition->setPublic(true);
foreach ($config['options'] as $name => $value) {
$gatewayDefinition->addMethodCall('set'.strtoupper($name), array($value));
}

$gatewayId = 'payum.context.'.$contextName.'.gateway';
$container->setDefinition($gatewayId, $gatewayDefinition);

$paymentDefinition->addMethodCall('addApi', array(new Reference($gatewayId)));
}

/**
* {@inheritDoc}
*/
protected function addActions(Definition $paymentDefinition, ContainerBuilder $container, $contextName, array $config)
{
$captureActionDefinition = new Definition('Payum\Bridge\Omnipay\Action\CaptureAction');
$captureActionId = 'payum.context.'.$contextName.'.action.capture';
$container->setDefinition($captureActionId, $captureActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($captureActionId)));

$statusActionDefinition = new Definition('Payum\Bridge\Omnipay\Action\StatusAction');
$statusActionId = 'payum.context.'.$contextName.'.action.status';
$container->setDefinition($statusActionId, $statusActionDefinition);
$paymentDefinition->addMethodCall('addAction', array(new Reference($statusActionId)));
}
}
Loading

0 comments on commit 58d2bf1

Please sign in to comment.