Skip to content

Commit

Permalink
Merge pull request #2 from kotrakrishna/master
Browse files Browse the repository at this point in the history
Log all requests/response made through guzzle
  • Loading branch information
anush committed Sep 16, 2014
2 parents 926d7d7 + e6760f6 commit d9482ac
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ php:
- 5.4
- 5.5

env:
- SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.1"

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
4 changes: 3 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ parameters:
services:
omnipay:
class: %omnipay.service.class%
arguments: [ @service_container ]
arguments: [ @service_container, @logger ]
tags:
- { name: monolog.logger, channel: omnipay }
27 changes: 20 additions & 7 deletions Service/Omnipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

namespace Xola\OmnipayBundle\Service;

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)
public function __construct(Container $container, LoggerInterface $logger)
{
$this->initConfig($container->getParameterBag()->all());
$this->logger = $logger;
}

private function initConfig($parameters)
Expand All @@ -34,8 +41,8 @@ private function initConfig($parameters)
/**
* Returns an Omnipay gateway.
*
* @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.
* @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
Expand All @@ -60,9 +67,15 @@ public function get($key = null, $parameters = null)
throw new \RuntimeException('Gateway key "' . $key . '" is not configured');
}

$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($gatewayName);
$gateway = $factory->create($gatewayName, $client);

if ($parameters) {
// Custom parameters have been specified, so use them
Expand Down Expand Up @@ -118,8 +131,8 @@ public function setConfig($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)
Expand All @@ -132,4 +145,4 @@ private function assignArrayByPath(&$arr, $path, $value)

$arr = $value;
}
}
}
61 changes: 33 additions & 28 deletions Tests/Service/OmnipayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
}

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "~2.1",
"omnipay/common": "~2.0"
"omnipay/common": "~2.0",
"guzzle/http": "~3.7",
"guzzle/plugin": "~3.7",
"guzzle/log": "~3.7",
"psr/log": "1.0"
},
"require-dev": {
"omnipay/stripe": "~2.0"
"omnipay/omnipay": "~2.0"
},
"autoload": {
"psr-0": {
Expand Down

0 comments on commit d9482ac

Please sign in to comment.