├── .gitignore ├── src ├── Facade.php ├── ServiceProvider.php └── GatewayManager.php ├── composer.json ├── LICENSE ├── config └── omnipay.php └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | composer.phar 4 | composer.lock 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | publishes([$configPath => config_path('omnipay.php')]); 24 | 25 | $this->app->singleton('omnipay', function ($app) { 26 | $defaults = $app['config']->get('omnipay.defaults', array()); 27 | return new GatewayManager($app, new GatewayFactory, $defaults); 28 | }); 29 | } 30 | 31 | /** 32 | * Get the services provided by the provider. 33 | * 34 | * @return array 35 | */ 36 | public function provides() 37 | { 38 | return array('omnipay'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2014 Barry vd. Heuvel 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /config/omnipay.php: -------------------------------------------------------------------------------- 1 | env('OMNIPAY_GATEWAY', 'PayPal_Express'), 14 | 15 | /* 16 | |-------------------------------------------------------------------------- 17 | | Default settings 18 | |-------------------------------------------------------------------------- 19 | | 20 | | Here you can specify default settings for gateways. 21 | | 22 | */ 23 | 'defaults' => [ 24 | 'testMode' => env('OMNIPAY_TESTMODE', false), 25 | ], 26 | 27 | /* 28 | |-------------------------------------------------------------------------- 29 | | Gateway specific settings 30 | |-------------------------------------------------------------------------- 31 | | 32 | | Here you can specify gateway specific settings. 33 | | 34 | */ 35 | 'gateways' => [ 36 | 'PayPal_Express' => [ 37 | 'username' => env('OMNIPAY_PAYPAL_USERNAME'), 38 | 'landingPage' => ['billing', 'login'], 39 | ], 40 | ], 41 | 42 | ]; 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Omnipay for Laravel 2 | 3 | This is a package to integrate [Omnipay](https://github.com/omnipay/omnipay) with Laravel. 4 | You can use it to easily manage your configuration, and use the Facade to provide shortcuts to your gateway. 5 | 6 | ## Installation 7 | 8 | Require this package with composer. 9 | 10 | ``` 11 | $ composer require barryvdh/laravel-omnipay 12 | ``` 13 | 14 | Pre Laravel 5.5: After updating composer, add the ServiceProvider to the providers array in config/app.php 15 | 16 | ```php 17 | 'Barryvdh\Omnipay\ServiceProvider', 18 | ``` 19 | 20 | You need to publish the config for this package. A sample configuration is provided. The defaults will be merged with gateway specific configuration. 21 | 22 | ``` 23 | $ php artisan vendor:publish --provider=Barryvdh\Omnipay\ServiceProvider 24 | ``` 25 | 26 | To use the Facade (`Omnipay::purchase()` instead of `App::make(`omnipay`)->purchase()`), add that to the facades array. 27 | 28 | ```php 29 | 'Omnipay' => 'Barryvdh\Omnipay\Facade', 30 | ``` 31 | 32 | When calling the Omnipay facade/instance, it will create the default gateway, based on the configuration. 33 | You can change the default gateway by calling `Omnipay::setDefaultGateway('My\Gateway')`. 34 | You can get a different gateway by calling `Omnipay::gateway('My\Cass')` 35 | 36 | ## Examples 37 | 38 | ```php 39 | $params = [ 40 | 'amount' => $order->amount, 41 | 'issuer' => $issuerId, 42 | 'description' => $order->description, 43 | 'returnUrl' => URL::action('PurchaseController@return', [$order->id]), 44 | ]; 45 | 46 | $response = Omnipay::purchase($params)->send(); 47 | 48 | if ($response->isSuccessful()) { 49 | // payment was successful: update database 50 | print_r($response); 51 | } elseif ($response->isRedirect()) { 52 | // redirect to offsite payment gateway 53 | return $response->getRedirectResponse(); 54 | } else { 55 | // payment failed: display message to customer 56 | echo $response->getMessage(); 57 | } 58 | ``` 59 | 60 | Besides the gateway calls, there is also a shortcut for the creditcard: 61 | 62 | ```php 63 | $formInputData = [ 64 | 'firstName' => 'Bobby', 65 | 'lastName' => 'Tables', 66 | 'number' => '4111111111111111', 67 | ]; 68 | 69 | $card = Omnipay::CreditCard($formInputData); 70 | ``` 71 | -------------------------------------------------------------------------------- /src/GatewayManager.php: -------------------------------------------------------------------------------- 1 | app = $app; 37 | $this->factory = $factory; 38 | $this->defaults = $defaults; 39 | } 40 | 41 | /** 42 | * Get a gateway 43 | * 44 | * @param string The gateway to retrieve (null=default) 45 | * @return \Omnipay\Common\GatewayInterface 46 | */ 47 | public function gateway($class = null) 48 | { 49 | $class = $class ?: $this->getDefaultGateway(); 50 | 51 | if (!isset($this->gateways[$class])) { 52 | $gateway = $this->factory->create($class, null, $this->app['request']); 53 | $gateway->initialize($this->getConfig($class)); 54 | $this->gateways[$class] = $gateway; 55 | } 56 | 57 | return $this->gateways[$class]; 58 | } 59 | 60 | /** 61 | * Get the configuration, based on the config and the defaults. 62 | */ 63 | protected function getConfig($name) 64 | { 65 | return array_merge( 66 | $this->defaults, 67 | $this->app['config']->get('omnipay.gateways.'.$name, []) 68 | ); 69 | } 70 | 71 | /** 72 | * Get the default gateway name. 73 | * 74 | * @return string 75 | */ 76 | public function getDefaultGateway() 77 | { 78 | return $this->app['config']['omnipay.gateway']; 79 | } 80 | 81 | /** 82 | * Set the default gateway name. 83 | * 84 | * @param string $name 85 | * @return void 86 | */ 87 | public function setDefaultGateway($name) 88 | { 89 | $this->app['config']['omnipay.gateway'] = $name; 90 | } 91 | 92 | /** 93 | * Dynamically call the default driver instance. 94 | * 95 | * @param string $method 96 | * @param array $parameters 97 | * @return mixed 98 | */ 99 | public function __call($method, $parameters) 100 | { 101 | return call_user_func_array([$this->gateway(), $method], $parameters); 102 | } 103 | } 104 | --------------------------------------------------------------------------------