Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created October 2, 2010 16:46
Show Gist options
  • Save avalanche123/607787 to your computer and use it in GitHub Desktop.
Save avalanche123/607787 to your computer and use it in GitHub Desktop.
<?php
namespace MyCompany\Bundle\PaymentBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class PaymentExtension extends Extension
{
/**
* Loads the services based on your application configuration.
* The full configuration is as follows:
*
* payment.config:
* paypal:
* username: [email protected]
* token: XXXXX-XXXXX-XXX-X
* authorize_net:
* config:
* username: [email protected]
* token: XXXXXX-XXXXX-XXX-X
* version: V2
*
* @param mixed $config
*/
public function configLoad($config, ContainerBuilder $container)
{
if (!$container->hasDefinition('payment_gateway')) {
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
$loader->load('services.xml');
}
if (isset($config['paypal'])) {
foreach (array('username', 'token') as $key) {
if (isset($config['paypal'][$key]) {
$container->setParameter('payment_gateway.adapter.paypal.'.$key, $config['paypal'][$key]);
}
}
}
if (isset($config['authorize_net']['config'])) {
$parameters = $container->getParameter('payment_gateway.adapter.authorize_net.config');
foreach (array('username', 'token', 'version') as $key) {
if (isset($config['authorize_net']['config'][$key])) {
$parameters[$key] = $config['authorize_net']['config'][$key];
}
}
$container->setParameter('payment_gateway.adapter.authorize_net.config', $parameters);
}
}
/**
* @inheritDoc
*/
public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}
/**
* @inheritDoc
*/
public function getNamespace()
{
return 'http://avalanche123.com/schema/dic/payment';
}
/**
* @inheritDoc
*/
public function getAlias()
{
return 'payment';
}
}
<?php
use Symfony\Component\DependencyInjection\Container;
$container = new Container();
$container->setParameter('payment_gateway.adapter.paypal.username', 'API_USERNAME');
$container->setParameter('payment_gateway.adapter.paypal.token', 'API_TOKEN');
$container->setParameter('payment_gateway.adapter.authorize_net.config', array(
'username' => 'API_USERNAME',
'token' => 'API_TOKEN',
'version' => 'V2',
));
$paypal = new \MyCompany\Component\Payment\Gateway\Adapter\Paypal(
$container->getParameter('payment_gateway.adapter.paypal.username'),
$container->getParameter('payment_gateway.adapter.paypal.token')
);
$container->setService('payment_gateway.adapter.paypal', $paypal);
$authorizeNet = new \MyCompany\Component\Payment\Gateway\Adapter\AuthorizeNet(
$container->getParameter('payment_gateway.adapter.authorize_net.config')
);
$container->setService('payment_gateway.adapter.authorize_net', $authorizeNet);
$gateway = new \MyCompany\Component\Payment\Gateway();
$gateway->setAdapter('paypal', $container->getService('payment_gateway.adapter.paypal'));
$gateway->setAdapter('authorize_net', $container->getService('payment_gateway.adapter.authorize_net'));
$container->setService('payment_gateway', $gateway);
<?xml version="1.0" ?>
<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="payment_gateway.adapter.paypal.username">API_USERNAME</parameter>
<parameter key="payment_gateway.adapter.paypal.token">API_TOKEN</parameter>
<parameter key="payment_gateway.adapter.authorize_net.config" type="collection">
<parameter key="username">API_USERNAME</parameter>
<parameter key="token">API_TOKEN</parameter>
<parameter key="version">V2</parameter>
</parameter>
</parameters>
<services>
<service id="payment_gateway.adapter.paypal" class="MyCompany\Component\Payment\Gateway\Adapter\Paypal">
<argument>%payment_gateway.adapter.paypal.username%</argument>
<argument>%payment_gateway.adapter.paypal.token%</argument>
</service>
<service id="payment_gateway.adapter.authorize_net" class="MyCompany\Component\Payment\Gateway\Adapter\AuthorizeNet">
<argument>%payment_gateway.adapter.authorize_net.config%</argument>
</service>
<service id="payment_gateway" class="MyCompany\Component\Payment\Gateway">
<call method="setAdapter">
<argument>paypal</argument>
<argument type="service" id="payment_gateway.adapter.paypal" />
</call>
<call method="setAdapter">
<argument>authorize_net</argument>
<argument type="service" id="payment_gateway.adapter.authorize_net" />
</call>
</service>
</services>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment