From 5938c10d248a3a7433e9676b24f9bb4d224b1fd9 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Mon, 8 Sep 2014 19:21:59 +0530 Subject: [PATCH 1/5] Log all requests/responses made through guzzle --- Resources/config/services.yml | 4 +- Service/Omnipay.php | 123 +++++++++++++++++++++++++--------- composer.json | 5 +- 3 files changed, 98 insertions(+), 34 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1c43c6e..788727b 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,4 +4,6 @@ parameters: services: omnipay: class: %omnipay.service.class% - arguments: [ @service_container ] + arguments: [ @service_container, @logger ] + tags: + - { name: monolog.logger, channel: omnipay } diff --git a/Service/Omnipay.php b/Service/Omnipay.php index 246e383..5d45bb9 100644 --- a/Service/Omnipay.php +++ b/Service/Omnipay.php @@ -2,88 +2,147 @@ namespace Xola\OmnipayBundle\Service; -use Symfony\Component\DependencyInjection\Container as Helper; -use Omnipay\Common\GatewayInterface; -use Omnipay\Common\GatewayFactory; +use Guzzle\Http\Client; +use Guzzle\Log\MessageFormatter; +use Guzzle\Log\PsrLogAdapter; +use Guzzle\Plugin\Log\LogPlugin; use Omnipay\Common\AbstractGateway; +use Omnipay\Common\GatewayFactory; +use Omnipay\Common\GatewayInterface; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Container; class Omnipay { protected $config; protected $cache = array(); + protected $logger; + + public function __construct(Container $container, LoggerInterface $logger) + { + $this->initConfig($container->getParameterBag()->all()); + $this->logger = $logger; + } - public function __construct(Helper $container) + private function initConfig($parameters) { - $this->parameters = $container->getParameterBag()->all(); + $key = 'omnipay'; + $configs = array($key); + foreach($parameters as $param => $value) { + if(!preg_match("/^$key/", $param)) { + continue; + } + $this->assignArrayByPath($configs, $param, $value); + } + + $this->config = isset($configs[$key]) ? $configs[$key] : null; } /** * Returns an Omnipay gateway. * - * @param string $name Name of the gateway as defined in the config + * @param string $key Gateway key as defined in the config + * @param array $parameters Custom gateway parameters. If set, any default parameters configured will be ignored. * + * @throws \RuntimeException If no gateway is configured for the key * @return AbstractGateway */ - public function get($name) + public function get($key = null, $parameters = null) { - if (isset($this->cache[$name])) { + $config = $this->getConfig(); + + if(is_null($key) && isset($config['default'])) { + // No key was specified, so use the default gateway + $key = $config['default']; + } + + if(isset($this->cache[$key])) { // We've already instantiated this gateway, so just return the cached copy - return $this->cache[$name]; + return $this->cache[$key]; } - $config = $this->getConfig(); + $gatewayName = $this->getGatewayName($key); + if(!$gatewayName) { + // Invalid gateway key + throw new \RuntimeException('Gateway key "' . $key . '" is not configured'); + } - $factory = new GatewayFactory(); + $adapter = new PsrLogAdapter($this->logger); + $logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT); + $client = new Client(); + $client->addSubscriber($logPlugin); + + $factory = new GatewayFactory(); /** @var GatewayInterface $gateway */ - $gateway = $factory->create($config[$name]['gateway']); + $gateway = $factory->create($gatewayName, $client); - // Initialize the gateway with config parameters - if (isset($config[$name])) { - $gateway->initialize($config[$name]); + if($parameters) { + // Custom parameters have been specified, so use them + $gateway->initialize($parameters); + + } elseif(isset($config[$key])) { + // Default parameters have been configured, so use them + $gateway->initialize($config[$key]); } // Cache the gateway - $this->cache[$name] = $gateway; + $this->cache[$key] = $gateway; return $gateway; } - public function getConfig() + /** + * Returns the gateway name (or type) for a given gateway key. + * + * @param string $key The configured gateway key + * + * @return null|string The gateway name, or NULL if not found + */ + public function getGatewayName($key) { - $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); - } + $gateway = null; + $config = $this->getConfig(); - $this->config = isset($configs[$key]) ? $configs[$key] : null; + if(isset($config[$key])) { + $gateway = $config[$key]['gateway']; } + return $gateway; + } + + public function getConfig() + { return $this->config; } + /** + * Sets the default parameters for a gateway key. + * + * @param string $key Gateway key + * @param mixed $value Parameters for the gateway + */ + public function setConfig($key, $value) + { + $this->config[$key] = $value; + } + /** * 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 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)) { + while($key = array_shift($keys)) { $arr = & $arr[$key]; } $arr = $value; } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 2222f36..1d18da9 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,10 @@ "require": { "php": ">=5.3.2", "symfony/framework-bundle": "~2.1", - "omnipay/omnipay": "~2.0" + "omnipay/omnipay": "~2.0", + "guzzle/http": "~3.7", + "guzzle/plugin": "~3.7", + "guzzle/log": "~3.7" }, "autoload": { "psr-0": { "Xola\\OmnipayBundle": "" } From 20ef1298f77d5afb8bba254b310198268c35181a Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 9 Sep 2014 09:43:56 +0530 Subject: [PATCH 2/5] Guzzle version required is 3.9 for log plugin to work. Updated travis.yml --- .travis.yml | 2 +- Service/Omnipay.php | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 51526a9..9ce690a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ php: - 5.5 env: - - SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.1" + - SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.9" before_script: - composer self-update diff --git a/Service/Omnipay.php b/Service/Omnipay.php index 5d45bb9..c42156c 100644 --- a/Service/Omnipay.php +++ b/Service/Omnipay.php @@ -28,8 +28,8 @@ private function initConfig($parameters) { $key = 'omnipay'; $configs = array($key); - foreach($parameters as $param => $value) { - if(!preg_match("/^$key/", $param)) { + foreach ($parameters as $param => $value) { + if (!preg_match("/^$key/", $param)) { continue; } $this->assignArrayByPath($configs, $param, $value); @@ -51,18 +51,18 @@ public function get($key = null, $parameters = null) { $config = $this->getConfig(); - if(is_null($key) && isset($config['default'])) { + if (is_null($key) && isset($config['default'])) { // No key was specified, so use the default gateway $key = $config['default']; } - if(isset($this->cache[$key])) { + if (isset($this->cache[$key])) { // We've already instantiated this gateway, so just return the cached copy return $this->cache[$key]; } $gatewayName = $this->getGatewayName($key); - if(!$gatewayName) { + if (!$gatewayName) { // Invalid gateway key throw new \RuntimeException('Gateway key "' . $key . '" is not configured'); } @@ -77,11 +77,11 @@ public function get($key = null, $parameters = null) /** @var GatewayInterface $gateway */ $gateway = $factory->create($gatewayName, $client); - if($parameters) { + if ($parameters) { // Custom parameters have been specified, so use them $gateway->initialize($parameters); - } elseif(isset($config[$key])) { + } elseif (isset($config[$key])) { // Default parameters have been configured, so use them $gateway->initialize($config[$key]); } @@ -104,7 +104,7 @@ public function getGatewayName($key) $gateway = null; $config = $this->getConfig(); - if(isset($config[$key])) { + if (isset($config[$key])) { $gateway = $config[$key]['gateway']; } @@ -139,7 +139,7 @@ private function assignArrayByPath(&$arr, $path, $value) { $keys = explode('.', $path); - while($key = array_shift($keys)) { + while ($key = array_shift($keys)) { $arr = & $arr[$key]; } From d2f5227df8c77ac1e21ac03752207286a9ffbe45 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 9 Sep 2014 10:03:11 +0530 Subject: [PATCH 3/5] Mock logger interface to test omnipay service --- Tests/Service/OmnipayTest.php | 61 +++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/Tests/Service/OmnipayTest.php b/Tests/Service/OmnipayTest.php index c85463a..14efc43 100644 --- a/Tests/Service/OmnipayTest.php +++ b/Tests/Service/OmnipayTest.php @@ -10,12 +10,13 @@ class OmnipayTest extends \PHPUnit_Framework_TestCase private function buildService($params = array()) { $defaults = array( - 'container' => $this->getMock('Symfony\Component\DependencyInjection\Container') + 'container' => $this->getMock('Symfony\Component\DependencyInjection\Container'), + 'logger' => $this->getMock('Psr\Log\LoggerInterface') ); $params = array_merge($defaults, $params); - return new Omnipay($params['container']); + return new Omnipay($params['container'], $params['logger']); } /** @@ -57,7 +58,7 @@ public function testCreateAuthorizeNetAIM() 'omnipay.authorize_net_aim.gateway' => 'AuthorizeNet_AIM' ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ $gateway = $service->get('authorize_net_aim'); @@ -85,7 +86,7 @@ public function testCreateAuthorizeNetSIM() 'omnipay.authorize_net_sim.gateway' => 'AuthorizeNet_SIM', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\AuthorizeNet\AIMGateway $gateway */ $gateway = $service->get('authorize_net_sim'); @@ -113,7 +114,7 @@ public function testCreateBuckaroo() 'omnipay.buckaroo.gateway' => 'Buckaroo', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Buckaroo\Gateway $gateway */ $gateway = $service->get('buckaroo'); @@ -137,7 +138,7 @@ public function testCreateCardSave() 'omnipay.card_save.gateway' => 'CardSave', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\CardSave\Gateway $gateway */ $gateway = $service->get('card_save'); @@ -161,7 +162,7 @@ public function testCreateEwayRapid() 'omnipay.eway_rapid.gateway' => 'Eway_Rapid', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Eway\RapidGateway $gateway */ $gateway = $service->get('eway_rapid'); @@ -187,7 +188,7 @@ public function testCreateGoCardless() 'omnipay.go_cardless.gateway' => 'GoCardless', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\GoCardless\Gateway $gateway */ $gateway = $service->get('go_cardless'); @@ -214,7 +215,7 @@ public function testCreateMigsTwoParty() 'omnipay.migs_two_party.gateway' => 'Migs_TwoParty', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Migs\TwoPartyGateway $gateway */ $gateway = $service->get('migs_two_party'); @@ -244,7 +245,7 @@ public function testCreateMigsThreeParty() 'omnipay.migs_three_party.gateway' => 'Migs_ThreeParty', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Migs\ThreePartyGateway $gateway */ $gateway = $service->get('migs_three_party'); @@ -272,7 +273,7 @@ public function testCreateMollie() 'omnipay.mollie.gateway' => 'Mollie', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Mollie\Gateway $gateway */ $gateway = $service->get('mollie'); @@ -296,7 +297,7 @@ public function testCreateMultiSafepay() 'omnipay.multi_safepay.gateway' => 'MultiSafepay', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\MultiSafepay\Gateway $gateway */ $gateway = $service->get('multi_safepay'); @@ -321,7 +322,7 @@ public function testCreateNetaxept() 'omnipay.netaxept.gateway' => 'Netaxept', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Netaxept\Gateway $gateway */ $gateway = $service->get('netaxept'); @@ -346,7 +347,7 @@ public function testCreateNetBanx() 'omnipay.net_banx.gateway' => 'NetBanx', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\NetBanx\Gateway $gateway */ $gateway = $service->get('net_banx'); @@ -372,7 +373,7 @@ public function testCreatePayFast() 'omnipay.pay_fast.gateway' => 'PayFast', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\PayFast\Gateway $gateway */ $gateway = $service->get('pay_fast'); @@ -399,7 +400,7 @@ public function testCreatePayflow() 'omnipay.payflow_pro.gateway' => 'Payflow_Pro', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Payflow\ProGateway $gateway */ $gateway = $service->get('payflow_pro'); @@ -425,7 +426,7 @@ public function testCreatePaymentExpressPxPay() 'omnipay.payment_express_px_pay.gateway' => 'PaymentExpress_PxPay', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\PaymentExpress\PxPayGateway $gateway */ $gateway = $service->get('payment_express_px_pay'); @@ -453,7 +454,7 @@ public function testCreatePaymentExpressPxPost() 'omnipay.payment_express_px_post.gateway' => 'PaymentExpress_PxPost', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\PaymentExpress\PxPostGateway $gateway */ $gateway = $service->get('payment_express_px_post'); @@ -482,7 +483,7 @@ public function testCreatePayPalPro() 'omnipay.pay_pal_pro.gateway' => 'PayPal_Pro', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\PayPal\ProGateway $gateway */ $gateway = $service->get('pay_pal_pro'); @@ -511,7 +512,7 @@ public function testCreatePayPalExpress() 'omnipay.pay_pal_express.gateway' => 'PayPal_Express', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\PayPal\ExpressGateway $gateway */ $gateway = $service->get('pay_pal_express'); @@ -538,7 +539,7 @@ public function testCreatePin() 'omnipay.pin.gateway' => 'Pin' ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Pin\Gateway $gateway */ $gateway = $service->get('pin'); @@ -560,7 +561,7 @@ public function testCreateSagePayDirect() 'omnipay.sage_pay_direct.gateway' => 'SagePay_Direct' ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\SagePay\DirectGateway $gateway */ $gateway = $service->get('sage_pay_direct'); @@ -582,7 +583,7 @@ public function testCreateSagePayServer() 'omnipay.sage_pay_server.gateway' => 'SagePay_Server', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\SagePay\ServerGateway $gateway */ $gateway = $service->get('sage_pay_server'); @@ -605,7 +606,7 @@ public function testCreateSecurePayDirectPost() 'omnipay.secure_pay_direct_post.gateway' => 'SecurePay_DirectPost', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\SecurePay\DirectPostGateway $gateway */ $gateway = $service->get('secure_pay_direct_post'); @@ -639,7 +640,7 @@ public function testCreateStripe() 'omnipay.stripe.gateway' => 'Stripe' ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\Stripe\Gateway $gateway */ $gateway = $service->get('stripe'); @@ -662,7 +663,7 @@ public function testCreateTwoCheckout() 'omnipay.two_checkout.gateway' => 'TwoCheckout', ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\TwoCheckout\Gateway $gateway */ $gateway = $service->get('two_checkout'); @@ -687,7 +688,7 @@ public function testCreateWorldPay() 'omnipay.world_pay.gateway' => 'WorldPay' ); - $service = new Omnipay($this->getServiceContainer($config)); + $service = $this->buildService(array('container' => $this->getServiceContainer($config))); /** @var \Omnipay\WorldPay\Gateway $gateway */ $gateway = $service->get('world_pay'); @@ -727,7 +728,11 @@ public function testGetGatewayName() ); $serviceContainer = $this->getServiceContainer($config); $service = $this->buildService(array('container' => $serviceContainer)); - $this->assertEquals('Stripe', $service->getGatewayName('stripe_canada'), 'The configured gateway name should return'); + $this->assertEquals( + 'Stripe', + $service->getGatewayName('stripe_canada'), + 'The configured gateway name should return' + ); $this->assertNull($service->getGatewayName('stripe_uk'), 'Invalid gateway key should return null'); } From aad4fee939f4f6859227a54cf5237b710aa9d7f1 Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 9 Sep 2014 11:59:12 +0530 Subject: [PATCH 4/5] omnipay/common is required for the bundle. omnipay/omnipay is required for test. --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c8337e0..e9b2235 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,14 @@ "require": { "php": ">=5.3.2", "symfony/framework-bundle": "~2.1", - "omnipay/omnipay": "~2.0", + "omnipay/common": "~2.0", "guzzle/http": "~3.7", "guzzle/plugin": "~3.7", "guzzle/log": "~3.7" }, + "require-dev": { + "omnipay/omnipay": "~2.0" + }, "autoload": { "psr-0": { "Xola\\OmnipayBundle": "" From e6760f697860ba6b952d0da87cda1180cd430fde Mon Sep 17 00:00:00 2001 From: Venkat Krishna Kotra Date: Tue, 9 Sep 2014 12:35:29 +0530 Subject: [PATCH 5/5] Add Psr/log as dependency. Clean up travis configuration --- .travis.yml | 7 ------- composer.json | 3 ++- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9ce690a..13b709f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,14 +5,7 @@ php: - 5.4 - 5.5 -env: - - SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.9" - before_script: - - composer self-update - - composer --version - - composer require symfony/http-foundation:${SYMFONY_VERSION} --no-update - - composer require guzzle/http:${GUZZLE_VERSION} --no-update - composer install -n --dev --prefer-source script: phpunit --coverage-text --configuration phpunit.xml.dist diff --git a/composer.json b/composer.json index e9b2235..38574f2 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "omnipay/common": "~2.0", "guzzle/http": "~3.7", "guzzle/plugin": "~3.7", - "guzzle/log": "~3.7" + "guzzle/log": "~3.7", + "psr/log": "1.0" }, "require-dev": { "omnipay/omnipay": "~2.0"