Skip to content

Code cleanup and PSR-2 compatibility #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2020
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/vendor
composer.phar
composer.lock
Expand Down
51 changes: 36 additions & 15 deletions config/omnipay.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
<?php

return array(
return [

/** The default gateway name */
'gateway' => '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'],
],
],

);
];
76 changes: 45 additions & 31 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
## 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 in your composer.json and run composer update (or run `composer require barryvdh/laravel-omnipay` directly):

```php
"barryvdh/laravel-omnipay": "0.3.*"
```

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 --provider='Barryvdh\Omnipay\ServiceProvider'
```
$ 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')`.
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);
```
10 changes: 6 additions & 4 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +16,8 @@ public static function creditCard($parameters = null)
/**
* {@inheritDoc}
*/
protected static function getFacadeAccessor() { return 'omnipay'; }

protected static function getFacadeAccessor()
{
return 'omnipay';
}
}
11 changes: 5 additions & 6 deletions src/GatewayManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use Omnipay\Common\GatewayFactory;

class GatewayManager{

class GatewayManager
{
/**
* The application instance.
*
Expand Down Expand Up @@ -45,7 +45,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;
Expand All @@ -61,7 +61,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, [])
);
}

Expand Down Expand Up @@ -95,7 +95,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);
}

}
5 changes: 3 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
});
Expand Down