diff --git a/.gitignore b/.gitignore index 5826402..72726d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.idea /vendor composer.phar composer.lock diff --git a/composer.json b/composer.json index 99611b0..0f1385a 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,10 @@ { "name": "barryvdh/laravel-omnipay", "description": "Omnipay Service Provider for Laravel", - "keywords": ["laravel", "omnipay"], + "keywords": [ + "laravel", + "omnipay" + ], "license": "MIT", "authors": [ { @@ -10,7 +13,8 @@ } ], "require": { - "illuminate/support": "^5", + "php": "^8.1", + "illuminate/support": "^9|^10|^11|^12", "league/omnipay": "~3.0" }, "autoload": { diff --git a/config/omnipay.php b/config/omnipay.php index aafb7cf..5856e56 100644 --- a/config/omnipay.php +++ b/config/omnipay.php @@ -1,21 +1,42 @@ 'PayPal_Express', + /* + |-------------------------------------------------------------------------- + | Default Gateway + |-------------------------------------------------------------------------- + | + | Here you can specify the gateway that the facade should use by default. + | + */ + 'gateway' => env('OMNIPAY_GATEWAY', 'PayPal_Express'), - /** The default settings, applied to all gateways */ - 'defaults' => array( - 'testMode' => false, - ), + /* + |-------------------------------------------------------------------------- + | Default settings + |-------------------------------------------------------------------------- + | + | Here you can specify default settings for gateways. + | + */ + 'defaults' => [ + 'testMode' => env('OMNIPAY_TESTMODE', false), + ], - /** Gateway specific parameters */ - 'gateways' => array( - 'PayPal_Express' => array( - 'username' => '', - 'landingPage' => array('billing', 'login'), - ), - ), + /* + |-------------------------------------------------------------------------- + | Gateway specific settings + |-------------------------------------------------------------------------- + | + | Here you can specify gateway specific settings. + | + */ + 'gateways' => [ + 'PayPal_Express' => [ + 'username' => env('OMNIPAY_PAYPAL_USERNAME'), + 'landingPage' => ['billing', 'login'], + ], + ], -); +]; diff --git a/readme.md b/readme.md index a892071..352bbf1 100644 --- a/readme.md +++ b/readme.md @@ -1,25 +1,33 @@ -## Omnipay for Laravel 5 +## Omnipay for Laravel This is a package to integrate [Omnipay](https://github.com/omnipay/omnipay) with Laravel. You can use it to easily manage your configuration, and use the Facade to provide shortcuts to your gateway. ## Installation -Require this package in your composer.json and run composer update (or run `composer require barryvdh/laravel-omnipay:0.3.x` directly): - - "barryvdh/laravel-omnipay": "0.3.*@dev" +Require this package with composer. +``` +$ composer require barryvdh/laravel-omnipay +``` + Pre Laravel 5.5: After updating composer, add the ServiceProvider to the providers array in config/app.php - 'Barryvdh\Omnipay\ServiceProvider', +```php +'Barryvdh\Omnipay\ServiceProvider', +``` You need to publish the config for this package. A sample configuration is provided. The defaults will be merged with gateway specific configuration. - $ php artisan vendor:publish +``` +$ php artisan vendor:publish --provider=Barryvdh\Omnipay\ServiceProvider +``` To use the Facade (`Omnipay::purchase()` instead of `App::make(`omnipay`)->purchase()`), add that to the facades array. - 'Omnipay' => 'Barryvdh\Omnipay\Facade', +```php +'Omnipay' => 'Barryvdh\Omnipay\Facade', +``` When calling the Omnipay facade/instance, it will create the default gateway, based on the configuration. You can change the default gateway by calling `Omnipay::setDefaultGateway('My\Gateway')`. @@ -27,30 +35,36 @@ You can get a different gateway by calling `Omnipay::gateway('My\Cass')` ## Examples - $params = [ - 'amount' => $order->amount, - 'issuer' => $issuerId, - 'description' => $order->description, - 'returnUrl' => URL::action('PurchaseController@return', [$order->id]), - ]; - $response = Omnipay::purchase($params)->send(); - - if ($response->isSuccessful()) { - // payment was successful: update database - print_r($response); - } elseif ($response->isRedirect()) { - // redirect to offsite payment gateway - return $response->getRedirectResponse(); - } else { - // payment failed: display message to customer - echo $response->getMessage(); - } +```php +$params = [ + 'amount' => $order->amount, + 'issuer' => $issuerId, + 'description' => $order->description, + 'returnUrl' => URL::action('PurchaseController@return', [$order->id]), +]; + +$response = Omnipay::purchase($params)->send(); + +if ($response->isSuccessful()) { + // payment was successful: update database + print_r($response); +} elseif ($response->isRedirect()) { + // redirect to offsite payment gateway + return $response->getRedirectResponse(); +} else { + // payment failed: display message to customer + echo $response->getMessage(); +} +``` Besides the gateway calls, there is also a shortcut for the creditcard: - $formInputData = array( - 'firstName' => 'Bobby', - 'lastName' => 'Tables', - 'number' => '4111111111111111', - ); - $card = Omnipay::CreditCard($formInputData); +```php +$formInputData = [ + 'firstName' => 'Bobby', + 'lastName' => 'Tables', + 'number' => '4111111111111111', +]; + +$card = Omnipay::CreditCard($formInputData); +``` diff --git a/src/Facade.php b/src/Facade.php index 7c29890..7a64d06 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -2,8 +2,8 @@ use Omnipay\Common\CreditCard; -class Facade extends \Illuminate\Support\Facades\Facade { - +class Facade extends \Illuminate\Support\Facades\Facade +{ /** * @param array $parameters * @return CreditCard @@ -16,6 +16,8 @@ public static function creditCard($parameters = null) /** * {@inheritDoc} */ - protected static function getFacadeAccessor() { return 'omnipay'; } - + protected static function getFacadeAccessor() + { + return 'omnipay'; + } } diff --git a/src/GatewayManager.php b/src/GatewayManager.php index 90ef29a..bc419d5 100644 --- a/src/GatewayManager.php +++ b/src/GatewayManager.php @@ -2,8 +2,8 @@ use Omnipay\Common\GatewayFactory; -class GatewayManager{ - +class GatewayManager +{ /** * The application instance. * @@ -11,6 +11,9 @@ class GatewayManager{ */ protected $app; + /** @var GatewayFactory */ + protected $factory; + /** * The registered gateways */ @@ -45,7 +48,7 @@ public function gateway($class = null) { $class = $class ?: $this->getDefaultGateway(); - if(!isset($this->gateways[$class])){ + if (!isset($this->gateways[$class])) { $gateway = $this->factory->create($class, null, $this->app['request']); $gateway->initialize($this->getConfig($class)); $this->gateways[$class] = $gateway; @@ -61,7 +64,7 @@ protected function getConfig($name) { return array_merge( $this->defaults, - $this->app['config']->get('omnipay.gateways.'.$name, array()) + $this->app['config']->get('omnipay.gateways.'.$name, []) ); } @@ -95,7 +98,6 @@ public function setDefaultGateway($name) */ public function __call($method, $parameters) { - return call_user_func_array(array($this->gateway(), $method), $parameters); + return call_user_func_array([$this->gateway(), $method], $parameters); } - } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index dfdac1c..b06d2d9 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,7 +2,8 @@ use Omnipay\Common\GatewayFactory; -class ServiceProvider extends \Illuminate\Support\ServiceProvider { +class ServiceProvider extends \Illuminate\Support\ServiceProvider +{ /** * Indicates if loading of the provider is deferred. @@ -21,7 +22,7 @@ public function register() $configPath = __DIR__ . '/../config/omnipay.php'; $this->publishes([$configPath => config_path('omnipay.php')]); - $this->app->singleton('omnipay',function ($app){ + $this->app->singleton('omnipay', function ($app) { $defaults = $app['config']->get('omnipay.defaults', array()); return new GatewayManager($app, new GatewayFactory, $defaults); });