Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('omnipay');
$rootNode = $treeBuilder->getRootNode();
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/OmnipayExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yaml');
$loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.php');

$gateways = $config['gateways'];
$gatewayNames = array_keys($gateways);
Expand Down
16 changes: 2 additions & 14 deletions Manager/OmnipayManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@

class OmnipayManager
{
/**
* @var GatewayFactory
*/
protected GatewayFactory $gatewayFactory;

/**
* @var array
*/
protected array $config;

/**
* @var GatewayInterface[]
*/
Expand All @@ -45,10 +35,8 @@ class OmnipayManager
*/
protected bool $initOnBoot = false;

public function __construct(GatewayFactory $gatewayFactory, array $config = [])
public function __construct(protected GatewayFactory $gatewayFactory, protected array $config = [])
{
$this->gatewayFactory = $gatewayFactory;
$this->config = $config;
}

public function get(string $gatewayName): GatewayInterface
Expand Down Expand Up @@ -79,7 +67,7 @@ protected function getGatewayConfig(string $gatewayName): array

public function registerGateway(GatewayInterface $gatewayInstance, ?string $alias = null): void
{
$name = $alias ?? Helper::getGatewayClassName(get_class($gatewayInstance));
$name = $alias ?? Helper::getGatewayClassName($gatewayInstance::class);

if (in_array($name, $this->disabledGateways, true)) {
return;
Expand Down
83 changes: 47 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You can then use these gateways like usual.

## Configuration

Create config file `./config/packages/omnipay.yaml` or copy-paste from [example](config.example.yaml).<br>
Create config file `./config/packages/omnipay.php` or copy-paste from [example](config.example.php).<br>
Gateways can be configured in this file, i.e.:

``` yml
Expand Down Expand Up @@ -76,20 +76,23 @@ The configuration settings vary per gateway - see

Custom gateways can be registered via the container by tagging them with `omnipay.gateway`:

```yml
# services.yaml
services:
my.custom.gateway:
class: Path\To\CustomGateway
tags:
- { name: omnipay.gateway, alias: CustomGateway }

# omnipay.yaml
omnipay:
methods:
# Reference the gateway alias here
CustomGateway:
apiKey: pa$$w0rd
```php
# services.php
$services = $containerConfigurator->services();

$services->set('my.custom.gateway', 'Path\To\CustomGateway')
->tag('omnipay.gateway', [
'alias' => 'CustomGateway',
]);

# omnipay.php
$containerConfigurator->extension('omnipay', [
'methods' => [
'CustomGateway' => [
'apiKey' => 'pa$$w0rd',
],
],
]);
```

You can then obtain the fully-configured gateway by its alias:
Expand All @@ -108,16 +111,19 @@ private function getCustomGateway(OmnipayManager $omnipay): GatewayInteface
### Default gateway

Add default gateway key to your config:
```yml
# omnipay.yaml
omnipay:
gateways:
MyGateway1:
apiKey: pa$$w0rd
MyGateway2:
apiKey: pa$$w0rd

default: MyGateway1
```php
# omnipay.php
$containerConfigurator->extension('omnipay', [
'gateways' => [
'MyGateway1' => [
'apiKey' => 'pa$$w0rd',
],
'MyGateway2' => [
'apiKey' => 'pa$$w0rd',
],
],
'default' => 'MyGateway1',
]);
```

You can now get default gateway instance:
Expand All @@ -128,16 +134,21 @@ $omnipay->getDefaultGateway();
### Disabling gateways

If need to disable a gateway but want to keep all the configuration add `disabled` key to the config:
```yml
# omnipay.yaml
omnipay:
gateways:
MyGateway1:
apiKey: pa$$w0rd
MyGateway2:
apiKey: pa$$w0rd

disabled: [ MyGateway1 ]
```php
# omnipay.php
$containerConfigurator->extension('omnipay', [
'gateways' => [
'MyGateway1' => [
'apiKey' => 'pa$$w0rd',
],
'MyGateway2' => [
'apiKey' => 'pa$$w0rd',
],
],
'disabled' => [
'MyGateway1',
],
]);
```

`MyGateway1` gateway will be skipped during gateway registration now.
Expand Down Expand Up @@ -211,4 +222,4 @@ $ phpunit
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
25 changes: 25 additions & 0 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Janwebdev\OmnipayBundle\Manager\OmnipayManager;
use Omnipay\Common\GatewayFactory;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set('omnipay.class', OmnipayManager::class);

$services = $containerConfigurator->services();

$services->set('omnipay', '%omnipay.class%')
->args([
service('omnipay.factory'),
]);

$services->set('omnipay.factory', GatewayFactory::class)
->private();
};
12 changes: 0 additions & 12 deletions Resources/config/services.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testProcessWithAlias(): void
$methodCalls = $this->getMethodCallsByName($omnipayDefinition, 'registerGateway');
$this->assertCount(1, $methodCalls);

list($reference, $alias) = reset($methodCalls);
[$reference, $alias] = reset($methodCalls);
$this->assertReferenceEquals('test.gateway', $reference);
$this->assertEquals('TestGateway', $alias);
}
Expand All @@ -52,7 +52,7 @@ public function testProcessWithoutAlias(): void
$methodCalls = $this->getMethodCallsByName($omnipayDefinition, 'registerGateway');
$this->assertCount(1, $methodCalls);

list($reference) = reset($methodCalls);
[$reference] = reset($methodCalls);
$this->assertReferenceEquals('test.gateway', $reference);
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/DependencyInjection/Fixtures/php/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', []);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'methods' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
'disabled_gateways' => [
'Stripe',
],
'default_gateway' => 'Stripe',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'gateways' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
'default' => 'Stripe',
'disabled' => [
'PayPal_Express',
],
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'gateways' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
'default' => 'Stripe',
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'gateways' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
'disabled' => [
'Stripe',
],
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'gateways' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
'init_on_boot' => true,
]);
};
23 changes: 23 additions & 0 deletions Tests/DependencyInjection/Fixtures/php/gateways.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('omnipay', [
'gateways' => [
'Stripe' => [
'apiKey' => 'sk_test_BQokikJOvBiI2HlWgH4olfQ2',
],
'PayPal_Express' => [
'username' => 'test-facilitator_api1.example.com',
'password' => '3MPI3VB4NVQ3XSVF',
'signature' => '6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.',
'testMode' => false,
'solutionType' => 'Sole',
'landingPage' => 'Login',
],
],
]);
};
Loading