From c069707eb796a2f92a54f1cfb6744d0dd30a4e72 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Mon, 30 Jun 2014 18:25:28 +0530 Subject: [PATCH 1/7] - Omnipay service modified to uses the new config structure. This allows us to have two instances of same gateway but with different configuration. - Omnipay services uses the 'omnipay.gateway_name.gateway' parameter to identify the Class name for the gateway. - Pass the gateway name to the service instead of gateway class to get an instance of gateway --- DependencyInjection/Configuration.php | 265 ------------- DependencyInjection/OmnipayExtension.php | 30 -- README.md | 38 +- Resources/config/services.yml | 2 +- Service/Omnipay.php | 57 ++- Tests/Service/OmnipayTest.php | 475 ++++++++++++++--------- 6 files changed, 353 insertions(+), 514 deletions(-) delete mode 100644 DependencyInjection/Configuration.php delete mode 100644 DependencyInjection/OmnipayExtension.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php deleted file mode 100644 index a2b88a6..0000000 --- a/DependencyInjection/Configuration.php +++ /dev/null @@ -1,265 +0,0 @@ -root('omnipay'); - - $root - ->children() - - // Authorize.NET AIM - ->arrayNode('authorize_net_aim') - ->children() - ->scalarNode('apiLoginId')->isRequired()->end() - ->scalarNode('transactionKey')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->scalarNode('developerMode')->defaultValue(false)->end() - ->end() - ->end() - - // Authorize.NET SIM - ->arrayNode('authorize_net_sim') - ->children() - ->scalarNode('apiLoginId')->isRequired()->end() - ->scalarNode('transactionKey')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->scalarNode('developerMode')->defaultValue(false)->end() - ->end() - ->end() - - // Buckaroo - ->arrayNode('buckaroo') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('secret')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // CardSave - ->arrayNode('card_save') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->end() - ->end() - - // eWAY Rapid 3.0 - ->arrayNode('eway_rapid') - ->children() - ->scalarNode('apiKey')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // GoCardless - ->arrayNode('go_cardless') - ->children() - ->scalarNode('appId')->isRequired()->end() - ->scalarNode('appSecret')->isRequired()->end() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('accessToken')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // MIGS 2-Party - ->arrayNode('migs_two_party') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('merchantAccessCode')->isRequired()->end() - ->scalarNode('secureHash')->end() - ->end() - ->end() - - // MIGS 3-Party - ->arrayNode('migs_three_party') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('merchantAccessCode')->isRequired()->end() - ->scalarNode('secureHash')->end() - ->end() - ->end() - - // Mollie - ->arrayNode('mollie') - ->children() - ->scalarNode('partnerId')->isRequired()->end() - ->scalarNode('testMode')->end() - ->end() - ->end() - - // MultiSafepay - ->arrayNode('multi_safepay') - ->children() - ->scalarNode('accountId')->isRequired()->end() - ->scalarNode('siteId')->isRequired()->end() - ->scalarNode('siteCode')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // Netaxept - ->arrayNode('netaxept') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // NetBanx - ->arrayNode('net_banx') - ->children() - ->scalarNode('accountNumber')->isRequired()->end() - ->scalarNode('storeId')->isRequired()->end() - ->scalarNode('storePassword')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // PayFast - ->arrayNode('pay_fast') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('merchantKey')->isRequired()->end() - ->scalarNode('pdtKey')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // Payflow Pro - ->arrayNode('payflow_pro') - ->children() - ->scalarNode('username')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->scalarNode('vendor')->end() - ->scalarNode('partner')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // PaymentExpress PxPay - ->arrayNode('payment_express_px_pay') - ->children() - ->scalarNode('username')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->end() - ->end() - - // PaymentExpress PxPost - ->arrayNode('payment_express_px_post') - ->children() - ->scalarNode('username')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->end() - ->end() - - // PayPal Pro - ->arrayNode('pay_pal_pro') - ->children() - ->scalarNode('username')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->scalarNode('signature')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // PayPal Express - ->arrayNode('pay_pal_express') - ->children() - ->scalarNode('username')->isRequired()->end() - ->scalarNode('password')->isRequired()->end() - ->scalarNode('signature')->end() - ->scalarNode('solutionType')->defaultValue(array('Sole', 'Mark'))->end() - ->scalarNode('landingPage')->defaultValue(array('Billing', 'Login'))->end() - ->scalarNode('headerImageUrl')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // Pin - ->arrayNode('pin') - ->children() - ->scalarNode('secretKey')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // Sage Pay Direct - ->arrayNode('sage_pay_direct') - ->children() - ->scalarNode('vendor')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->scalarNode('simulatorMode')->defaultValue(false)->end() - ->end() - ->end() - - // Sage Pay Server - ->arrayNode('sage_pay_server') - ->children() - ->scalarNode('vendor')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->scalarNode('simulatorMode')->defaultValue(false)->end() - ->end() - ->end() - - // SecurePay Direct Post - ->arrayNode('secure_pay_direct_post') - ->children() - ->scalarNode('merchantId')->isRequired()->end() - ->scalarNode('transactionPassword')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // Stripe - ->arrayNode('stripe') - ->children() - ->scalarNode('apiKey')->isRequired()->end() - ->end() - ->end() - - // 2Checkout - ->arrayNode('two_checkout') - ->children() - ->scalarNode('accountNumber')->isRequired()->end() - ->scalarNode('secretWord')->isRequired()->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - // WorldPay - ->arrayNode('world_pay') - ->children() - ->scalarNode('installationId')->isRequired()->end() - ->scalarNode('secretWord')->isRequired()->end() - ->scalarNode('callbackPassword')->end() - ->scalarNode('testMode')->defaultValue(false)->end() - ->end() - ->end() - - ->end(); - - return $builder; - } -} diff --git a/DependencyInjection/OmnipayExtension.php b/DependencyInjection/OmnipayExtension.php deleted file mode 100644 index 6fd7ac4..0000000 --- a/DependencyInjection/OmnipayExtension.php +++ /dev/null @@ -1,30 +0,0 @@ -processConfiguration($configuration, $configs); - - $container->setParameter('omnipay', $config); - - $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.yml'); - } -} diff --git a/README.md b/README.md index e13dfef..06bd373 100644 --- a/README.md +++ b/README.md @@ -24,32 +24,42 @@ it's possible to configure these parameters in your Symfony config files. ```yaml # app/config/password_dev.yml -omnipay: +parameters: # Authorize.NET AIM - authorize_net_aim: - apiLoginId: myLoginId - transactionKey: myTransactionKey + omnipay.authorize_net_aim.apiLoginId: myLoginId + omnipay.authorize_net_aim.transactionKey: myTransactionKey + omnipay.authorize_net_aim.gateway: AuthorizeNet_AIM # Stripe - stripe: - apiKey: myApiKey -``` + omnipay.stripe.apiKey: myApiKey + omnipay.stripe.gateway: Stripe -In the configuration file, you'll need to "underscore" the names of the gateways, but their parameters should be left -intact i.e. the gateway name "AuthorizeNet_AIM" becomes "authorize_net_aim", however all it's parameters remain -unchanged and remain in camel case. + # Custom gateway + omnipay.custom_gateway.apiKey: myCustomGatewayKey + omnipay.custom_gateway.gateway: \Custom\Gateway +``` Usage ----- -Use the new `omnipay` service to create gateway classes: +Use the new `omnipay` service to create gateway object: ```php -// From within a controller -$gateway = $this->get('omnipay')->create('Stripe'); + // From within a controller. This will return an instance `\Omnipay\Stripe` + $gateway = $this->get('omnipay')->create('stripe'); +``` -// The rest is identical to how you would normally use Omnipay +Use `omnipay` service to create custom gateway object +```php + // From within a controller. This will return an instance `\Custom\Gateway` + $gateway = $this->get('omnipay')->create('custom_gateway'); +``` + + +The rest is identical to how you would normally use Omnipay + +```php $formData = ['number' => '4242424242424242', 'expiryMonth' => '11', 'expiryYear' => '2018', 'cvv' => '123']; $response = $gateway->purchase(['amount' => '10.00', 'currency' => 'USD', 'card' => $formData])->send(); diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 9e140cb..1c43c6e 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,4 +4,4 @@ parameters: services: omnipay: class: %omnipay.service.class% - arguments: [ %omnipay% ] + arguments: [ @service_container ] diff --git a/Service/Omnipay.php b/Service/Omnipay.php index bb8cf31..43eca1b 100644 --- a/Service/Omnipay.php +++ b/Service/Omnipay.php @@ -10,26 +10,61 @@ class Omnipay { protected $config; - public function __construct($config) + public function __construct(Helper $container) { - $this->config = $config; + $this->parameters = $container->getParameterBag()->all(); } - public function create($class) + public function create($name) { - /** @var GatewayInterface $gateway */ - $gateway = GatewayFactory::create($class); + $config = $this->getConfig(); - // Normalize the name to be underscored - $name = $gateway->getShortName(); - $name = Helper::underscore($name); - $name = str_replace('.', '_', $name); + /** @var GatewayInterface $gateway */ + $gateway = GatewayFactory::create($config[$name]['gateway']); // Initialize the gateway with config parameters - if(isset($this->config[$name])) { - $gateway->initialize($this->config[$name]); + if (isset($config[$name])) { + $gateway->initialize($config[$name]); } return $gateway; } + + public function getConfig() + { + $key = 'omnipay'; + if (!isset($this->config)) { + // Asked config is not parsed yet. Parse it. + $configs = array($key); + foreach ($this->parameters as $param => $value) { + if (!preg_match("/^$key/", $param)) { + continue; + } + $this->assignArrayByPath($configs, $param, $value); + } + + $this->config = isset($configs[$key]) ? $configs[$key] : null; + } + + return $this->config; + } + + /** + * Helper method to convert a config in dot notation to a multi-dimensional array + * For example: "subscription.default: free" becomes array('subscription' => array('default' => 'free')) + * + * @param array $arr The destination array + * @param string $path The config in dot notation + * @param string $value The value to be assigned to the config + */ + private function assignArrayByPath(&$arr, $path, $value) + { + $keys = explode('.', $path); + + while ($key = array_shift($keys)) { + $arr = & $arr[$key]; + } + + $arr = $value; + } } diff --git a/Tests/Service/OmnipayTest.php b/Tests/Service/OmnipayTest.php index 50140fe..f49a2b6 100644 --- a/Tests/Service/OmnipayTest.php +++ b/Tests/Service/OmnipayTest.php @@ -6,73 +6,116 @@ class OmnipayTest extends \PHPUnit_Framework_TestCase { + private function buildService($params = array()) + { + $defaults = array( + 'container' => $this->getMock('Symfony\Component\DependencyInjection\Container') + ); + + $params = array_merge($defaults, $params); + + return new Omnipay($params['container']); + } + + /** + * @param array $params + * + * @return \Symfony\Component\DependencyInjection\Container + */ + private function getServiceContainer($params) + { + $parameterBag = $this->getMock('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface'); + + $parameterBag + ->expects($this->any()) + ->method('all') + ->will($this->returnValue($params)); + + $serviceContainer = $this->getMock('Symfony\Component\DependencyInjection\Container'); + + $serviceContainer + ->expects($this->once()) + ->method('getParameterBag') + ->will($this->returnValue($parameterBag)); + + return $serviceContainer; + } + public function testCreateAuthorizeNetAIM() { // Do not run test if Gateway has not been included - if(!class_exists('Omnipay\\AuthorizeNet\\AIMGateway')) { + if (!class_exists('Omnipay\\AuthorizeNet\\AIMGateway')) { $this->markTestSkipped('Gateway Omnipay\\AuthorizeNet\\AIMGateway not found'); + return; } $config = array( - 'authorize_net_aim' => array( - 'apiLoginId' => 'abc123', - 'transactionKey' => 'xyz987', - ) + 'omnipay.authorize_net_aim.apiLoginId' => 'abc123', + 'omnipay.authorize_net_aim.transactionKey' => 'xyz987', + 'omnipay.authorize_net_aim.gateway' => 'AuthorizeNet_AIM' ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ - $gateway = $service->create('AuthorizeNet_AIM'); + $gateway = $service->create('authorize_net_aim'); - $this->assertInstanceOf('Omnipay\\AuthorizeNet\\AIMGateway', $gateway, 'Must return an Authorize.NET AIM gateway'); + $this->assertInstanceOf( + 'Omnipay\\AuthorizeNet\\AIMGateway', + $gateway, + 'Must return an Authorize.NET AIM gateway' + ); $this->assertEquals('abc123', $gateway->getApiLoginId(), 'API login ID must match configuration'); $this->assertEquals('xyz987', $gateway->getTransactionKey(), 'Transaction key must match configuration'); } public function testCreateAuthorizeNetSIM() { - if(!class_exists('Omnipay\\AuthorizeNet\\SIMGateway')) { + if (!class_exists('Omnipay\\AuthorizeNet\\SIMGateway')) { $this->markTestSkipped('Gateway Omnipay\\AuthorizeNet\\SIMGateway not found'); + return; } $config = array( - 'authorize_net_sim' => array( - 'apiLoginId' => 'abc123', - 'transactionKey' => 'xyz987', - ) + 'omnipay.authorize_net_sim.apiLoginId' => 'abc123', + 'omnipay.authorize_net_sim.transactionKey' => 'xyz987', + 'omnipay.authorize_net_sim.gateway' => 'AuthorizeNet_SIM', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ - $gateway = $service->create('AuthorizeNet_SIM'); + $gateway = $service->create('authorize_net_sim'); - $this->assertInstanceOf('Omnipay\\AuthorizeNet\\SIMGateway', $gateway, 'Must return an Authorize.NET SIM gateway'); + $this->assertInstanceOf( + 'Omnipay\\AuthorizeNet\\SIMGateway', + $gateway, + 'Must return an Authorize.NET SIM gateway' + ); $this->assertEquals('abc123', $gateway->getApiLoginId(), 'API login ID must match configuration'); $this->assertEquals('xyz987', $gateway->getTransactionKey(), 'Transaction key must match configuration'); } public function testCreateBuckaroo() { - if(!class_exists('Omnipay\\Buckaroo\\Gateway')) { + if (!class_exists('Omnipay\\Buckaroo\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\Buckaroo\\Gateway not found'); + return; } $config = array( - 'buckaroo' => array( - 'merchantId' => 'abc123', - 'secret' => 'xyz987', - ) + 'omnipay.buckaroo.merchantId' => 'abc123', + 'omnipay.buckaroo.secret' => 'xyz987', + 'omnipay.buckaroo.gateway' => 'Buckaroo', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Buckaroo\Gateway $gateway */ - $gateway = $service->create('Buckaroo'); + $gateway = $service->create('buckaroo'); $this->assertInstanceOf('Omnipay\\Buckaroo\\Gateway', $gateway, 'Must return a Buckaroo gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -81,22 +124,22 @@ public function testCreateBuckaroo() public function testCreateCardSave() { - if(!class_exists('Omnipay\\CardSave\\Gateway')) { + if (!class_exists('Omnipay\\CardSave\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\CardSave\\Gateway not found'); + return; } $config = array( - 'card_save' => array( - 'merchantId' => 'abc123', - 'password' => 'xyz987', - ) + 'omnipay.card_save.merchantId' => 'abc123', + 'omnipay.card_save.password' => 'xyz987', + 'omnipay.card_save.gateway' => 'CardSave', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\CardSave\Gateway $gateway */ - $gateway = $service->create('CardSave'); + $gateway = $service->create('card_save'); $this->assertInstanceOf('Omnipay\\CardSave\\Gateway', $gateway, 'Must return a CardSave gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -105,22 +148,22 @@ public function testCreateCardSave() public function testCreateEwayRapid() { - if(!class_exists('Omnipay\\Eway\\RapidGateway')) { + if (!class_exists('Omnipay\\Eway\\RapidGateway')) { $this->markTestSkipped('Gateway Omnipay\\Eway\\RapidGateway not found'); + return; } $config = array( - 'eway_rapid' => array( - 'apiKey' => 'abc123', - 'password' => 'xyz987', - ) + 'omnipay.eway_rapid.apiKey' => 'abc123', + 'omnipay.eway_rapid.password' => 'xyz987', + 'omnipay.eway_rapid.gateway' => 'Eway_Rapid', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Eway\RapidGateway $gateway */ - $gateway = $service->create('Eway_Rapid'); + $gateway = $service->create('eway_rapid'); $this->assertInstanceOf('Omnipay\\Eway\\RapidGateway', $gateway, 'Must return an eWAY Rapid gateway'); $this->assertEquals('abc123', $gateway->getApiKey(), 'API key must match configuration'); @@ -129,24 +172,24 @@ public function testCreateEwayRapid() public function testCreateGoCardless() { - if(!class_exists('Omnipay\\GoCardless\\Gateway')) { + if (!class_exists('Omnipay\\GoCardless\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\GoCardless\\Gateway not found'); + return; } $config = array( - 'go_cardless' => array( - 'appId' => 'abc123', - 'appSecret' => 'xyz987', - 'merchantId' => 'pqr567', - 'accessToken' => 'uvw543', - ) + 'omnipay.go_cardless.appId' => 'abc123', + 'omnipay.go_cardless.appSecret' => 'xyz987', + 'omnipay.go_cardless.merchantId' => 'pqr567', + 'omnipay.go_cardless.accessToken' => 'uvw543', + 'omnipay.go_cardless.gateway' => 'GoCardless', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\GoCardless\Gateway $gateway */ - $gateway = $service->create('GoCardless'); + $gateway = $service->create('go_cardless'); $this->assertInstanceOf('Omnipay\\GoCardless\\Gateway', $gateway, 'Must return a GoCardless gateway'); $this->assertEquals('abc123', $gateway->getAppId(), 'App ID must match configuration'); @@ -157,73 +200,81 @@ public function testCreateGoCardless() public function testCreateMigsTwoParty() { - if(!class_exists('Omnipay\\Migs\\TwoPartyGateway')) { + if (!class_exists('Omnipay\\Migs\\TwoPartyGateway')) { $this->markTestSkipped('Gateway Omnipay\\Migs\\TwoPartyGateway not found'); + return; } $config = array( - 'migs_two_party' => array( - 'merchantId' => 'abc123', - 'merchantAccessCode' => 'xyz987', - 'secureHash' => 'pqr567', - ) + 'omnipay.migs_two_party.merchantId' => 'abc123', + 'omnipay.migs_two_party.merchantAccessCode' => 'xyz987', + 'omnipay.migs_two_party.secureHash' => 'pqr567', + 'omnipay.migs_two_party.gateway' => 'Migs_TwoParty', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Migs\TwoPartyGateway $gateway */ - $gateway = $service->create('Migs_TwoParty'); + $gateway = $service->create('migs_two_party'); $this->assertInstanceOf('Omnipay\\Migs\\TwoPartyGateway', $gateway, 'Must return a MIGS 2-Party gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); - $this->assertEquals('xyz987', $gateway->getMerchantAccessCode(), 'Merchant access code must match configuration'); + $this->assertEquals( + 'xyz987', + $gateway->getMerchantAccessCode(), + 'Merchant access code must match configuration' + ); $this->assertEquals('pqr567', $gateway->getSecureHash(), 'Secure hash must match configuration'); } public function testCreateMigsThreeParty() { - if(!class_exists('Omnipay\\Migs\\ThreePartyGateway')) { + if (!class_exists('Omnipay\\Migs\\ThreePartyGateway')) { $this->markTestSkipped('Gateway Omnipay\\Migs\\ThreePartyGateway not found'); + return; } $config = array( - 'migs_three_party' => array( - 'merchantId' => 'abc123', - 'merchantAccessCode' => 'xyz987', - 'secureHash' => 'pqr567', - ) + 'omnipay.migs_three_party.merchantId' => 'abc123', + 'omnipay.migs_three_party.merchantAccessCode' => 'xyz987', + 'omnipay.migs_three_party.secureHash' => 'pqr567', + 'omnipay.migs_three_party.gateway' => 'Migs_ThreeParty', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Migs\ThreePartyGateway $gateway */ - $gateway = $service->create('Migs_ThreeParty'); + $gateway = $service->create('migs_three_party'); $this->assertInstanceOf('Omnipay\\Migs\\ThreePartyGateway', $gateway, 'Must return a MIGS 3-Party gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); - $this->assertEquals('xyz987', $gateway->getMerchantAccessCode(), 'Merchant access code must match configuration'); + $this->assertEquals( + 'xyz987', + $gateway->getMerchantAccessCode(), + 'Merchant access code must match configuration' + ); $this->assertEquals('pqr567', $gateway->getSecureHash(), 'Secure hash must match configuration'); } public function testCreateMollie() { - if(!class_exists('Omnipay\\Mollie\\Gateway')) { + if (!class_exists('Omnipay\\Mollie\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\Mollie\\Gateway not found'); + return; } $config = array( - 'mollie' => array( - 'partnerId' => 'abc123', - ) + 'omnipay.mollie.partnerId' => 'abc123', + 'omnipay.mollie.gateway' => 'Mollie', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Mollie\Gateway $gateway */ - $gateway = $service->create('Mollie'); + $gateway = $service->create('mollie'); $this->assertInstanceOf('Omnipay\\Mollie\\Gateway', $gateway, 'Must return a Mollie gateway'); $this->assertEquals('abc123', $gateway->getPartnerId(), 'Partner ID must match configuration'); @@ -231,23 +282,23 @@ public function testCreateMollie() public function testCreateMultiSafepay() { - if(!class_exists('Omnipay\\MultiSafepay\\Gateway')) { + if (!class_exists('Omnipay\\MultiSafepay\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\MultiSafepay\\Gateway not found'); + return; } $config = array( - 'multi_safepay' => array( - 'accountId' => 'abc123', - 'siteId' => 'xyz987', - 'siteCode' => 'pqr567', - ) + 'omnipay.multi_safepay.accountId' => 'abc123', + 'omnipay.multi_safepay.siteId' => 'xyz987', + 'omnipay.multi_safepay.siteCode' => 'pqr567', + 'omnipay.multi_safepay.gateway' => 'MultiSafepay', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\MultiSafepay\Gateway $gateway */ - $gateway = $service->create('MultiSafepay'); + $gateway = $service->create('multi_safepay'); $this->assertInstanceOf('Omnipay\\MultiSafepay\\Gateway', $gateway, 'Must return a MultiSafepay gateway'); $this->assertEquals('abc123', $gateway->getAccountId(), 'Account ID must match configuration'); @@ -257,22 +308,22 @@ public function testCreateMultiSafepay() public function testCreateNetaxept() { - if(!class_exists('Omnipay\\Netaxept\\Gateway')) { + if (!class_exists('Omnipay\\Netaxept\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\Netaxept\\Gateway not found'); + return; } $config = array( - 'netaxept' => array( - 'merchantId' => 'abc123', - 'password' => 'xyz987', - ) + 'omnipay.netaxept.merchantId' => 'abc123', + 'omnipay.netaxept.password' => 'xyz987', + 'omnipay.netaxept.gateway' => 'Netaxept', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Netaxept\Gateway $gateway */ - $gateway = $service->create('Netaxept'); + $gateway = $service->create('netaxept'); $this->assertInstanceOf('Omnipay\\Netaxept\\Gateway', $gateway, 'Must return a Netaxept gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -281,23 +332,23 @@ public function testCreateNetaxept() public function testCreateNetBanx() { - if(!class_exists('Omnipay\\NetBanx\\Gateway')) { + if (!class_exists('Omnipay\\NetBanx\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\NetBanx\\Gateway not found'); + return; } $config = array( - 'net_banx' => array( - 'accountNumber' => 'abc123', - 'storeId' => 'xyz987', - 'storePassword' => 'pqr567', - ) + 'omnipay.net_banx.accountNumber' => 'abc123', + 'omnipay.net_banx.storeId' => 'xyz987', + 'omnipay.net_banx.storePassword' => 'pqr567', + 'omnipay.net_banx.gateway' => 'NetBanx', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\NetBanx\Gateway $gateway */ - $gateway = $service->create('NetBanx'); + $gateway = $service->create('net_banx'); $this->assertInstanceOf('Omnipay\\NetBanx\\Gateway', $gateway, 'Must return a NetBanx gateway'); $this->assertEquals('abc123', $gateway->getAccountNumber(), 'Account number must match configuration'); @@ -307,23 +358,23 @@ public function testCreateNetBanx() public function testCreatePayFast() { - if(!class_exists('Omnipay\\PayFast\\Gateway')) { + if (!class_exists('Omnipay\\PayFast\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\PayFast\\Gateway not found'); + return; } $config = array( - 'pay_fast' => array( - 'merchantId' => 'abc123', - 'merchantKey' => 'xyz987', - 'pdtKey' => 'pqr567', - ) + 'omnipay.pay_fast.merchantId' => 'abc123', + 'omnipay.pay_fast.merchantKey' => 'xyz987', + 'omnipay.pay_fast.pdtKey' => 'pqr567', + 'omnipay.pay_fast.gateway' => 'PayFast', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayFast\Gateway $gateway */ - $gateway = $service->create('PayFast'); + $gateway = $service->create('pay_fast'); $this->assertInstanceOf('Omnipay\\PayFast\\Gateway', $gateway, 'Must return a PayFast gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -333,24 +384,24 @@ public function testCreatePayFast() public function testCreatePayflow() { - if(!class_exists('Omnipay\\Payflow\\ProGateway')) { + if (!class_exists('Omnipay\\Payflow\\ProGateway')) { $this->markTestSkipped('Gateway Omnipay\\Payflow\\ProGateway not found'); + return; } $config = array( - 'payflow_pro' => array( - 'username' => 'abc123', - 'password' => 'xyz987', - 'vendor' => 'pqr567', - 'partner' => 'uvw543', - ) + 'omnipay.payflow_pro.username' => 'abc123', + 'omnipay.payflow_pro.password' => 'xyz987', + 'omnipay.payflow_pro.vendor' => 'pqr567', + 'omnipay.payflow_pro.partner' => 'uvw543', + 'omnipay.payflow_pro.gateway' => 'Payflow_Pro', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Payflow\ProGateway $gateway */ - $gateway = $service->create('Payflow_Pro'); + $gateway = $service->create('payflow_pro'); $this->assertInstanceOf('Omnipay\\Payflow\\ProGateway', $gateway, 'Must return a Payflow Pro gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -361,71 +412,79 @@ public function testCreatePayflow() public function testCreatePaymentExpressPxPay() { - if(!class_exists('Omnipay\\PaymentExpress\\PxPayGateway')) { + if (!class_exists('Omnipay\\PaymentExpress\\PxPayGateway')) { $this->markTestSkipped('Gateway Omnipay\\PaymentExpress\\PxPayGateway not found'); + return; } $config = array( - 'payment_express_px_pay' => array( - 'username' => 'abc123', - 'password' => 'xyz987', - ) + 'omnipay.payment_express_px_pay.username' => 'abc123', + 'omnipay.payment_express_px_pay.password' => 'xyz987', + 'omnipay.payment_express_px_pay.gateway' => 'PaymentExpress_PxPay', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PaymentExpress\PxPayGateway $gateway */ - $gateway = $service->create('PaymentExpress_PxPay'); + $gateway = $service->create('payment_express_px_pay'); - $this->assertInstanceOf('Omnipay\\PaymentExpress\\PxPayGateway', $gateway, 'Must return a PaymentExpress PxPay gateway'); + $this->assertInstanceOf( + 'Omnipay\\PaymentExpress\\PxPayGateway', + $gateway, + 'Must return a PaymentExpress PxPay gateway' + ); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); $this->assertEquals('xyz987', $gateway->getPassword(), 'Password must match configuration'); } public function testCreatePaymentExpressPxPost() { - if(!class_exists('Omnipay\\PaymentExpress\\PxPostGateway')) { + if (!class_exists('Omnipay\\PaymentExpress\\PxPostGateway')) { $this->markTestSkipped('Gateway Omnipay\\PaymentExpress\\PxPostGateway not found'); + return; } $config = array( - 'payment_express_px_post' => array( - 'username' => 'abc123', - 'password' => 'xyz987', - ) + 'omnipay.payment_express_px_post.username' => 'abc123', + 'omnipay.payment_express_px_post.password' => 'xyz987', + 'omnipay.payment_express_px_post.gateway' => 'PaymentExpress_PxPost', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PaymentExpress\PxPostGateway $gateway */ - $gateway = $service->create('PaymentExpress_PxPost'); + $gateway = $service->create('payment_express_px_post'); - $this->assertInstanceOf('Omnipay\\PaymentExpress\\PxPostGateway', $gateway, 'Must return a PaymentExpress PxPost gateway'); + $this->assertInstanceOf( + 'Omnipay\\PaymentExpress\\PxPostGateway', + $gateway, + 'Must return a PaymentExpress PxPost gateway' + ); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); $this->assertEquals('xyz987', $gateway->getPassword(), 'Password must match configuration'); } public function testCreatePayPalPro() { - if(!class_exists('Omnipay\\PayPal\\ProGateway')) { + if (!class_exists('Omnipay\\PayPal\\ProGateway')) { $this->markTestSkipped('Gateway Omnipay\\PayPal\\ProGateway not found'); + return; } $config = array( - 'pay_pal_pro' => array( - 'username' => 'abc123', - 'password' => 'xyz987', - 'signature' => 'pqr567', - ) + 'omnipay.pay_pal_pro.username' => 'abc123', + 'omnipay.pay_pal_pro.password' => 'xyz987', + 'omnipay.pay_pal_pro.signature' => 'pqr567', + 'omnipay.pay_pal_pro.gateway' => 'PayPal_Pro', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayPal\ProGateway $gateway */ - $gateway = $service->create('PayPal_Pro'); + $gateway = $service->create('pay_pal_pro'); $this->assertInstanceOf('Omnipay\\PayPal\\ProGateway', $gateway, 'Must return a PayPal Pro gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -435,26 +494,26 @@ public function testCreatePayPalPro() public function testCreatePayPalExpress() { - if(!class_exists('Omnipay\\PayPal\\ExpressGateway')) { + if (!class_exists('Omnipay\\PayPal\\ExpressGateway')) { $this->markTestSkipped('Gateway Omnipay\\PayPal\\ExpressGateway not found'); + return; } $config = array( - 'pay_pal_express' => array( - 'username' => 'abc123', - 'password' => 'xyz987', - 'signature' => 'pqr567', - 'solutionType' => array('foo', 'bar'), - 'landingPage' => array('baz'), - 'headerImageUrl' => 'uvw543', - ) + 'omnipay.pay_pal_express.username' => 'abc123', + 'omnipay.pay_pal_express.password' => 'xyz987', + 'omnipay.pay_pal_express.signature' => 'pqr567', + 'omnipay.pay_pal_express.solutionType' => array('foo', 'bar'), + 'omnipay.pay_pal_express.landingPage' => array('baz'), + 'omnipay.pay_pal_express.headerImageUrl' => 'uvw543', + 'omnipay.pay_pal_express.gateway' => 'PayPal_Express', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayPal\ExpressGateway $gateway */ - $gateway = $service->create('PayPal_Express'); + $gateway = $service->create('pay_pal_express'); $this->assertInstanceOf('Omnipay\\PayPal\\ExpressGateway', $gateway, 'Must return a PayPal Express gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -467,21 +526,21 @@ public function testCreatePayPalExpress() public function testCreatePin() { - if(!class_exists('Omnipay\\Pin\\Gateway')) { + if (!class_exists('Omnipay\\Pin\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\Pin\\Gateway not found'); + return; } $config = array( - 'pin' => array( - 'secretKey' => 'abc123' - ) + 'omnipay.pin.secretKey' => 'abc123', + 'omnipay.pin.gateway' => 'Pin' ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Pin\Gateway $gateway */ - $gateway = $service->create('Pin'); + $gateway = $service->create('pin'); $this->assertInstanceOf('Omnipay\\Pin\\Gateway', $gateway, 'Must return a Pin gateway'); $this->assertEquals('abc123', $gateway->getSecretKey(), 'API key must match configuration'); @@ -489,21 +548,21 @@ public function testCreatePin() public function testCreateSagePayDirect() { - if(!class_exists('Omnipay\\SagePay\\DirectGateway')) { + if (!class_exists('Omnipay\\SagePay\\DirectGateway')) { $this->markTestSkipped('Gateway Omnipay\\SagePay\\DirectGateway not found'); + return; } $config = array( - 'sage_pay_direct' => array( - 'vendor' => 'abc123' - ) + 'omnipay.sage_pay_direct.vendor' => 'abc123', + 'omnipay.sage_pay_direct.gateway' => 'SagePay_Direct' ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SagePay\DirectGateway $gateway */ - $gateway = $service->create('SagePay_Direct'); + $gateway = $service->create('sage_pay_direct'); $this->assertInstanceOf('Omnipay\\SagePay\\DirectGateway', $gateway, 'Must return a SagePay Direct gateway'); $this->assertEquals('abc123', $gateway->getVendor(), 'Vendor must match configuration'); @@ -511,21 +570,21 @@ public function testCreateSagePayDirect() public function testCreateSagePayServer() { - if(!class_exists('Omnipay\\SagePay\\ServerGateway')) { + if (!class_exists('Omnipay\\SagePay\\ServerGateway')) { $this->markTestSkipped('Gateway Omnipay\\SagePay\\ServerGateway not found'); + return; } $config = array( - 'sage_pay_server' => array( - 'vendor' => 'abc123' - ) + 'omnipay.sage_pay_server.vendor' => 'abc123', + 'omnipay.sage_pay_server.gateway' => 'SagePay_Server', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SagePay\ServerGateway $gateway */ - $gateway = $service->create('SagePay_Server'); + $gateway = $service->create('sage_pay_server'); $this->assertInstanceOf('Omnipay\\SagePay\\ServerGateway', $gateway, 'Must return a SagePay Server gateway'); $this->assertEquals('abc123', $gateway->getVendor(), 'Vendor must match configuration'); @@ -533,26 +592,34 @@ public function testCreateSagePayServer() public function testCreateSecurePayDirectPost() { - if(!class_exists('Omnipay\\SecurePay\\DirectPostGateway')) { + if (!class_exists('Omnipay\\SecurePay\\DirectPostGateway')) { $this->markTestSkipped('Gateway Omnipay\\SecurePay\\DirectPostGateway not found'); + return; } $config = array( - 'secure_pay_direct_post' => array( - 'merchantId' => 'abc123', - 'transactionPassword' => 'xyz987', - ) + 'omnipay.secure_pay_direct_post.merchantId' => 'abc123', + 'omnipay.secure_pay_direct_post.transactionPassword' => 'xyz987', + 'omnipay.secure_pay_direct_post.gateway' => 'SecurePay_DirectPost', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SecurePay\DirectPostGateway $gateway */ - $gateway = $service->create('SecurePay_DirectPost'); + $gateway = $service->create('secure_pay_direct_post'); - $this->assertInstanceOf('Omnipay\\SecurePay\\DirectPostGateway', $gateway, 'Must return a SecurePay Direct Post gateway'); + $this->assertInstanceOf( + 'Omnipay\\SecurePay\\DirectPostGateway', + $gateway, + 'Must return a SecurePay Direct Post gateway' + ); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); - $this->assertEquals('xyz987', $gateway->getTransactionPassword(), 'Transaction password must match configuration'); + $this->assertEquals( + 'xyz987', + $gateway->getTransactionPassword(), + 'Transaction password must match configuration' + ); } /** @@ -560,21 +627,21 @@ public function testCreateSecurePayDirectPost() */ public function testCreateStripe() { - if(!class_exists('Omnipay\\Stripe\\Gateway')) { + if (!class_exists('Omnipay\\Stripe\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\Stripe\\Gateway not found'); + return; } $config = array( - 'stripe' => array( - 'apiKey' => 'abc123' - ) + 'omnipay.stripe.apiKey' => 'abc123', + 'omnipay.stripe.gateway' => 'Stripe' ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Stripe\Gateway $gateway */ - $gateway = $service->create('Stripe'); + $gateway = $service->create('stripe'); $this->assertInstanceOf('Omnipay\\Stripe\\Gateway', $gateway, 'Must return a Stripe gateway'); $this->assertEquals('abc123', $gateway->getApiKey(), 'API key must match configuration'); @@ -582,22 +649,22 @@ public function testCreateStripe() public function testCreateTwoCheckout() { - if(!class_exists('Omnipay\\TwoCheckout\\Gateway')) { + if (!class_exists('Omnipay\\TwoCheckout\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\TwoCheckout\\Gateway not found'); + return; } $config = array( - 'two_checkout' => array( - 'accountNumber' => 'abc123', - 'secretWord' => 'xyz987', - ) + 'omnipay.two_checkout.accountNumber' => 'abc123', + 'omnipay.two_checkout.secretWord' => 'xyz987', + 'omnipay.two_checkout.gateway' => 'TwoCheckout', ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\TwoCheckout\Gateway $gateway */ - $gateway = $service->create('TwoCheckout'); + $gateway = $service->create('two_checkout'); $this->assertInstanceOf('Omnipay\\TwoCheckout\\Gateway', $gateway, 'Must return a TwoCheckout gateway'); $this->assertEquals('abc123', $gateway->getAccountNumber(), 'Account number must match configuration'); @@ -606,27 +673,49 @@ public function testCreateTwoCheckout() public function testCreateWorldPay() { - if(!class_exists('Omnipay\\WorldPay\\Gateway')) { + if (!class_exists('Omnipay\\WorldPay\\Gateway')) { $this->markTestSkipped('Gateway Omnipay\\WorldPay\\Gateway not found'); + return; } $config = array( - 'world_pay' => array( - 'installationId' => 'abc123', - 'secretWord' => 'xyz987', - 'callbackPassword' => 'pqr567', - ) + 'omnipay.world_pay.installationId' => 'abc123', + 'omnipay.world_pay.secretWord' => 'xyz987', + 'omnipay.world_pay.callbackPassword' => 'pqr567', + 'omnipay.world_pay.gateway' => 'WorldPay' ); - $service = new Omnipay($config); + $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\WorldPay\Gateway $gateway */ - $gateway = $service->create('WorldPay'); + $gateway = $service->create('world_pay'); $this->assertInstanceOf('Omnipay\\WorldPay\\Gateway', $gateway, 'Must return a WorldPay gateway'); $this->assertEquals('abc123', $gateway->getInstallationId(), 'Installation ID must match configuration'); $this->assertEquals('xyz987', $gateway->getSecretWord(), 'Secret word must match configuration'); $this->assertEquals('pqr567', $gateway->getCallbackPassword(), 'Callback password must match configuration'); } + + public function testGetConfig() + { + $config = array( + 'omnipay.world_pay.installationId' => 'abc123', + 'omnipay.world_pay.secretWord' => 'xyz987', + 'omnipay.world_pay.callbackPassword' => 'pqr567', + ); + $serviceContainer = $this->getServiceContainer($config); + + $service = $this->buildService(array('container' => $serviceContainer)); + + $expected = array( + 'world_pay' => array( + 'installationId' => 'abc123', + 'secretWord' => 'xyz987', + 'callbackPassword' => 'pqr567', + ) + ); + + $this->assertEquals($expected, $service->getConfig(), 'Array structured config should match'); + } } From f452ebff68acb003ef23c940e1a91b92688d83f0 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Mon, 30 Jun 2014 18:27:40 +0530 Subject: [PATCH 2/7] Contributor update. --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index df2a47a..1b5e9bb 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,10 @@ { "name": "Anush Ramani", "email": "anush@xola.com" + }, + { + "name": "Venkata Krishna Kotra", + "email": "kotrakrishna@gmail.com" } ], "require": { From 9ff449931d25c1b12f06553938ab7bd0b9280ceb Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Mon, 30 Jun 2014 19:52:23 +0530 Subject: [PATCH 3/7] Documentation update --- README.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 06bd373..b512781 100644 --- a/README.md +++ b/README.md @@ -25,35 +25,36 @@ it's possible to configure these parameters in your Symfony config files. ```yaml # app/config/password_dev.yml parameters: + # Custom gateway + omnipay.my_custom_name.apiKey: myGatewayKey + omnipay.my_custom_name.gateway: MyGateway + + # Default Stripe gateway + omnipay.stripe_default.apiKey: myApiKey + omnipay.stripe_default.gateway: Stripe + + # Gateway for Stripe Canada account + omnipay.stripe_canada.apiKey: myStripeCanadaApiKey + omnipay.stripe_canada.gateway: Stripe # Authorize.NET AIM - omnipay.authorize_net_aim.apiLoginId: myLoginId omnipay.authorize_net_aim.transactionKey: myTransactionKey omnipay.authorize_net_aim.gateway: AuthorizeNet_AIM - - # Stripe - omnipay.stripe.apiKey: myApiKey - omnipay.stripe.gateway: Stripe - - # Custom gateway - omnipay.custom_gateway.apiKey: myCustomGatewayKey - omnipay.custom_gateway.gateway: \Custom\Gateway ``` +In the sample configuration above, `my_custom_name` is a unique name you define for each of your gateways. `omnipay.my_custom_name.gateway` is the class name for a Omnipay gateway driver (eg `Stripe`). You may choose to define multiple names for the same Omnipay gateway with different credentials. Eg. we have configured two gateway definitions for Stripe, both use the same Omnipay driver, however, they use different sets of credentials. + Usage ----- Use the new `omnipay` service to create gateway object: ```php - // From within a controller. This will return an instance `\Omnipay\Stripe` - $gateway = $this->get('omnipay')->create('stripe'); -``` + // From within a controller. This will return an instance `\Omnipay\Stripe`. `stripe_default` is the key as specified in the config. + $gateway = $this->get('omnipay')->create('stripe_default'); -Use `omnipay` service to create custom gateway object -```php - // From within a controller. This will return an instance `\Custom\Gateway` - $gateway = $this->get('omnipay')->create('custom_gateway'); + // From within a controller. This will return an instance of `\Omnipay\MyGateway` as specified in `omnipay.my_custom_name.gateway` + $gateway = $this->get('omnipay')->create('my_custom_name'); ``` @@ -74,4 +75,4 @@ if ($response->isSuccessful()) { echo $response->getMessage(); } ``` -The gateway classes which are returned are already initialized with the parameters defined in the config files. +The gateway classes which are returned are already initialized with the parameters defined in the config files. \ No newline at end of file From d70c978526175e208b3953b36584131b7c4ad003 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 1 Jul 2014 12:03:15 +0530 Subject: [PATCH 4/7] Add keywords --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1b5e9bb..2222f36 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "xola/omnipay-bundle", "type": "symfony-bundle", "description": "Integrates Omnipay with Symfony2", - "keywords": ["omnipay", "payment", "credit card", "gateway"], + "keywords": ["omnipay", "payment", "credit card", "gateway", "symfony"], "homepage": "https://github.com/xola/OmnipayBundle", "license": "MIT", "authors": [ From 854b982b2c7766e2a23fc55d1886e1126eaf5bf2 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 1 Jul 2014 13:43:21 +0530 Subject: [PATCH 5/7] The GatewayFactory class no longer be called in a static fashion. --- Service/Omnipay.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Service/Omnipay.php b/Service/Omnipay.php index 43eca1b..d9fcb8c 100644 --- a/Service/Omnipay.php +++ b/Service/Omnipay.php @@ -19,8 +19,9 @@ public function create($name) { $config = $this->getConfig(); + $factory = new GatewayFactory(); /** @var GatewayInterface $gateway */ - $gateway = GatewayFactory::create($config[$name]['gateway']); + $gateway = $factory->create($config[$name]['gateway']); // Initialize the gateway with config parameters if (isset($config[$name])) { From af6bcea0ce0ca08f5ca8a8651b6848b716132a03 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 1 Jul 2014 14:32:59 +0530 Subject: [PATCH 6/7] Extension file to load the service configuration so as to make the omnipay service available --- DependencyInjection/OmnipayExtension.php | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 DependencyInjection/OmnipayExtension.php diff --git a/DependencyInjection/OmnipayExtension.php b/DependencyInjection/OmnipayExtension.php new file mode 100644 index 0000000..f0c1de0 --- /dev/null +++ b/DependencyInjection/OmnipayExtension.php @@ -0,0 +1,25 @@ +load('services.yml'); + } +} From d0615d366f1f7acb81ed48ad82ad0059d043f3e8 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 1 Jul 2014 14:54:25 +0530 Subject: [PATCH 7/7] Rename method create to get. --- README.md | 4 +-- Service/Omnipay.php | 20 +++++++++++++- Tests/Service/OmnipayTest.php | 50 +++++++++++++++++------------------ 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b512781..bd5f4f4 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,11 @@ Use the new `omnipay` service to create gateway object: ```php // From within a controller. This will return an instance `\Omnipay\Stripe`. `stripe_default` is the key as specified in the config. - $gateway = $this->get('omnipay')->create('stripe_default'); + $gateway = $this->get('omnipay')->get('stripe_default'); // From within a controller. This will return an instance of `\Omnipay\MyGateway` as specified in `omnipay.my_custom_name.gateway` - $gateway = $this->get('omnipay')->create('my_custom_name'); + $gateway = $this->get('omnipay')->get('my_custom_name'); ``` diff --git a/Service/Omnipay.php b/Service/Omnipay.php index d9fcb8c..246e383 100644 --- a/Service/Omnipay.php +++ b/Service/Omnipay.php @@ -5,21 +5,36 @@ use Symfony\Component\DependencyInjection\Container as Helper; use Omnipay\Common\GatewayInterface; use Omnipay\Common\GatewayFactory; +use Omnipay\Common\AbstractGateway; class Omnipay { protected $config; + protected $cache = array(); public function __construct(Helper $container) { $this->parameters = $container->getParameterBag()->all(); } - public function create($name) + /** + * Returns an Omnipay gateway. + * + * @param string $name Name of the gateway as defined in the config + * + * @return AbstractGateway + */ + public function get($name) { + if (isset($this->cache[$name])) { + // We've already instantiated this gateway, so just return the cached copy + return $this->cache[$name]; + } + $config = $this->getConfig(); $factory = new GatewayFactory(); + /** @var GatewayInterface $gateway */ $gateway = $factory->create($config[$name]['gateway']); @@ -28,6 +43,9 @@ public function create($name) $gateway->initialize($config[$name]); } + // Cache the gateway + $this->cache[$name] = $gateway; + return $gateway; } diff --git a/Tests/Service/OmnipayTest.php b/Tests/Service/OmnipayTest.php index f49a2b6..c889725 100644 --- a/Tests/Service/OmnipayTest.php +++ b/Tests/Service/OmnipayTest.php @@ -59,7 +59,7 @@ public function testCreateAuthorizeNetAIM() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ - $gateway = $service->create('authorize_net_aim'); + $gateway = $service->get('authorize_net_aim'); $this->assertInstanceOf( 'Omnipay\\AuthorizeNet\\AIMGateway', @@ -87,7 +87,7 @@ public function testCreateAuthorizeNetSIM() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ - $gateway = $service->create('authorize_net_sim'); + $gateway = $service->get('authorize_net_sim'); $this->assertInstanceOf( 'Omnipay\\AuthorizeNet\\SIMGateway', @@ -115,7 +115,7 @@ public function testCreateBuckaroo() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Buckaroo\Gateway $gateway */ - $gateway = $service->create('buckaroo'); + $gateway = $service->get('buckaroo'); $this->assertInstanceOf('Omnipay\\Buckaroo\\Gateway', $gateway, 'Must return a Buckaroo gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -139,7 +139,7 @@ public function testCreateCardSave() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\CardSave\Gateway $gateway */ - $gateway = $service->create('card_save'); + $gateway = $service->get('card_save'); $this->assertInstanceOf('Omnipay\\CardSave\\Gateway', $gateway, 'Must return a CardSave gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -163,7 +163,7 @@ public function testCreateEwayRapid() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Eway\RapidGateway $gateway */ - $gateway = $service->create('eway_rapid'); + $gateway = $service->get('eway_rapid'); $this->assertInstanceOf('Omnipay\\Eway\\RapidGateway', $gateway, 'Must return an eWAY Rapid gateway'); $this->assertEquals('abc123', $gateway->getApiKey(), 'API key must match configuration'); @@ -189,7 +189,7 @@ public function testCreateGoCardless() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\GoCardless\Gateway $gateway */ - $gateway = $service->create('go_cardless'); + $gateway = $service->get('go_cardless'); $this->assertInstanceOf('Omnipay\\GoCardless\\Gateway', $gateway, 'Must return a GoCardless gateway'); $this->assertEquals('abc123', $gateway->getAppId(), 'App ID must match configuration'); @@ -216,7 +216,7 @@ public function testCreateMigsTwoParty() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Migs\TwoPartyGateway $gateway */ - $gateway = $service->create('migs_two_party'); + $gateway = $service->get('migs_two_party'); $this->assertInstanceOf('Omnipay\\Migs\\TwoPartyGateway', $gateway, 'Must return a MIGS 2-Party gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -246,7 +246,7 @@ public function testCreateMigsThreeParty() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Migs\ThreePartyGateway $gateway */ - $gateway = $service->create('migs_three_party'); + $gateway = $service->get('migs_three_party'); $this->assertInstanceOf('Omnipay\\Migs\\ThreePartyGateway', $gateway, 'Must return a MIGS 3-Party gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -274,7 +274,7 @@ public function testCreateMollie() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Mollie\Gateway $gateway */ - $gateway = $service->create('mollie'); + $gateway = $service->get('mollie'); $this->assertInstanceOf('Omnipay\\Mollie\\Gateway', $gateway, 'Must return a Mollie gateway'); $this->assertEquals('abc123', $gateway->getPartnerId(), 'Partner ID must match configuration'); @@ -298,7 +298,7 @@ public function testCreateMultiSafepay() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\MultiSafepay\Gateway $gateway */ - $gateway = $service->create('multi_safepay'); + $gateway = $service->get('multi_safepay'); $this->assertInstanceOf('Omnipay\\MultiSafepay\\Gateway', $gateway, 'Must return a MultiSafepay gateway'); $this->assertEquals('abc123', $gateway->getAccountId(), 'Account ID must match configuration'); @@ -323,7 +323,7 @@ public function testCreateNetaxept() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Netaxept\Gateway $gateway */ - $gateway = $service->create('netaxept'); + $gateway = $service->get('netaxept'); $this->assertInstanceOf('Omnipay\\Netaxept\\Gateway', $gateway, 'Must return a Netaxept gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -348,7 +348,7 @@ public function testCreateNetBanx() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\NetBanx\Gateway $gateway */ - $gateway = $service->create('net_banx'); + $gateway = $service->get('net_banx'); $this->assertInstanceOf('Omnipay\\NetBanx\\Gateway', $gateway, 'Must return a NetBanx gateway'); $this->assertEquals('abc123', $gateway->getAccountNumber(), 'Account number must match configuration'); @@ -374,7 +374,7 @@ public function testCreatePayFast() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayFast\Gateway $gateway */ - $gateway = $service->create('pay_fast'); + $gateway = $service->get('pay_fast'); $this->assertInstanceOf('Omnipay\\PayFast\\Gateway', $gateway, 'Must return a PayFast gateway'); $this->assertEquals('abc123', $gateway->getMerchantId(), 'Merchant ID must match configuration'); @@ -401,7 +401,7 @@ public function testCreatePayflow() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Payflow\ProGateway $gateway */ - $gateway = $service->create('payflow_pro'); + $gateway = $service->get('payflow_pro'); $this->assertInstanceOf('Omnipay\\Payflow\\ProGateway', $gateway, 'Must return a Payflow Pro gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -427,7 +427,7 @@ public function testCreatePaymentExpressPxPay() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PaymentExpress\PxPayGateway $gateway */ - $gateway = $service->create('payment_express_px_pay'); + $gateway = $service->get('payment_express_px_pay'); $this->assertInstanceOf( 'Omnipay\\PaymentExpress\\PxPayGateway', @@ -455,7 +455,7 @@ public function testCreatePaymentExpressPxPost() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PaymentExpress\PxPostGateway $gateway */ - $gateway = $service->create('payment_express_px_post'); + $gateway = $service->get('payment_express_px_post'); $this->assertInstanceOf( 'Omnipay\\PaymentExpress\\PxPostGateway', @@ -484,7 +484,7 @@ public function testCreatePayPalPro() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayPal\ProGateway $gateway */ - $gateway = $service->create('pay_pal_pro'); + $gateway = $service->get('pay_pal_pro'); $this->assertInstanceOf('Omnipay\\PayPal\\ProGateway', $gateway, 'Must return a PayPal Pro gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -513,7 +513,7 @@ public function testCreatePayPalExpress() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\PayPal\ExpressGateway $gateway */ - $gateway = $service->create('pay_pal_express'); + $gateway = $service->get('pay_pal_express'); $this->assertInstanceOf('Omnipay\\PayPal\\ExpressGateway', $gateway, 'Must return a PayPal Express gateway'); $this->assertEquals('abc123', $gateway->getUsername(), 'Username must match configuration'); @@ -540,7 +540,7 @@ public function testCreatePin() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Pin\Gateway $gateway */ - $gateway = $service->create('pin'); + $gateway = $service->get('pin'); $this->assertInstanceOf('Omnipay\\Pin\\Gateway', $gateway, 'Must return a Pin gateway'); $this->assertEquals('abc123', $gateway->getSecretKey(), 'API key must match configuration'); @@ -562,7 +562,7 @@ public function testCreateSagePayDirect() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SagePay\DirectGateway $gateway */ - $gateway = $service->create('sage_pay_direct'); + $gateway = $service->get('sage_pay_direct'); $this->assertInstanceOf('Omnipay\\SagePay\\DirectGateway', $gateway, 'Must return a SagePay Direct gateway'); $this->assertEquals('abc123', $gateway->getVendor(), 'Vendor must match configuration'); @@ -584,7 +584,7 @@ public function testCreateSagePayServer() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SagePay\ServerGateway $gateway */ - $gateway = $service->create('sage_pay_server'); + $gateway = $service->get('sage_pay_server'); $this->assertInstanceOf('Omnipay\\SagePay\\ServerGateway', $gateway, 'Must return a SagePay Server gateway'); $this->assertEquals('abc123', $gateway->getVendor(), 'Vendor must match configuration'); @@ -607,7 +607,7 @@ public function testCreateSecurePayDirectPost() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\SecurePay\DirectPostGateway $gateway */ - $gateway = $service->create('secure_pay_direct_post'); + $gateway = $service->get('secure_pay_direct_post'); $this->assertInstanceOf( 'Omnipay\\SecurePay\\DirectPostGateway', @@ -641,7 +641,7 @@ public function testCreateStripe() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\Stripe\Gateway $gateway */ - $gateway = $service->create('stripe'); + $gateway = $service->get('stripe'); $this->assertInstanceOf('Omnipay\\Stripe\\Gateway', $gateway, 'Must return a Stripe gateway'); $this->assertEquals('abc123', $gateway->getApiKey(), 'API key must match configuration'); @@ -664,7 +664,7 @@ public function testCreateTwoCheckout() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\TwoCheckout\Gateway $gateway */ - $gateway = $service->create('two_checkout'); + $gateway = $service->get('two_checkout'); $this->assertInstanceOf('Omnipay\\TwoCheckout\\Gateway', $gateway, 'Must return a TwoCheckout gateway'); $this->assertEquals('abc123', $gateway->getAccountNumber(), 'Account number must match configuration'); @@ -689,7 +689,7 @@ public function testCreateWorldPay() $service = new Omnipay($this->getServiceContainer($config)); /** @var \Omnipay\WorldPay\Gateway $gateway */ - $gateway = $service->create('world_pay'); + $gateway = $service->get('world_pay'); $this->assertInstanceOf('Omnipay\\WorldPay\\Gateway', $gateway, 'Must return a WorldPay gateway'); $this->assertEquals('abc123', $gateway->getInstallationId(), 'Installation ID must match configuration');