Skip to content

Commit

Permalink
رفع خطای ناشی از آپلود فایل های پکیجح در مسیر اشتباه
Browse files Browse the repository at this point in the history
  • Loading branch information
hpakdaman authored Oct 3, 2016
1 parent 5edc3c1 commit f012cf5
Show file tree
Hide file tree
Showing 36 changed files with 2,971 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Larabookir\Gateway;

class Enum
{
const MELLAT = 'MELLAT';
const SADAD = 'SADAD';
const ZARINPAL = 'ZARINPAL';
const PAYLINE = 'PAYLINE';
const JAHANPAY = 'JAHANPAY';
const PARSIAN = 'PARSIAN';
const PASARGAD = 'PASARGAD';

/**
* Status code for status field in poolport_transactions table
*/
const TRANSACTION_INIT = 'INIT';
const TRANSACTION_INIT_TEXT = 'تراکنش ایجاد شد.';

/**
* Status code for status field in poolport_transactions table
*/
const TRANSACTION_SUCCEED = 'SUCCEED';
const TRANSACTION_SUCCEED_TEXT = 'پرداخت با موفقیت انجام شد.';

/**
* Status code for status field in poolport_transactions table
*/
const TRANSACTION_FAILED = 'FAILED';
const TRANSACTION_FAILED_TEXT = 'عملیات پرداخت با خطا مواجه شد.';

}
11 changes: 11 additions & 0 deletions Exceptions/BankException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Larabookir\Gateway\Exceptions;
/**
* This exception when throws, user try to submit a payment request who submitted before
*/
class BankException extends \Exception
{
protected $code=-100;
protected $message = 'خطای بانک.';
}
8 changes: 8 additions & 0 deletions Exceptions/ConfigFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Larabookir\Gateway\Exceptions;

class ConfigFileNotFoundException extends GatewayException {
protected $code=-105;
protected $message='فایل تنظیمات یافت نشد.';
}
11 changes: 11 additions & 0 deletions Exceptions/GatewayException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Larabookir\Gateway\Exceptions;
/**
* This exception when throws, user try to submit a payment request who submitted before
*/
class GatewayException extends \Exception
{
protected $code=-200;
protected $message = 'خطای سرویس.';
}
9 changes: 9 additions & 0 deletions Exceptions/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Larabookir\Gateway\Exceptions;

class InvalidRequestException extends GatewayException {

protected $code=-104;
protected $message='اطلاعات بازگشتی از بانک صحیح نمی باشد. ';
}
9 changes: 9 additions & 0 deletions Exceptions/NotFoundTransactionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Larabookir\Gateway\Exceptions;

class NotFoundTransactionException extends GatewayException
{
protected $code=-103;
protected $message = 'چنین رکورد پرداختی موجود نمی باشد.';
}
9 changes: 9 additions & 0 deletions Exceptions/PortNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Larabookir\Gateway\Exceptions;

class PortNotFoundException extends GatewayException {

protected $code=-102;
protected $message='درگاهی برای تراکنش مورد نظر در سایت یافت نشد.';
}
11 changes: 11 additions & 0 deletions Exceptions/RetryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Larabookir\Gateway\Exceptions;
/**
* This exception when throws, user try to submit a payment request who submitted before
*/
class RetryException extends GatewayException
{
protected $code=-101;
protected $message = 'نتیجه تراکنش قبلا از طرف بانک اعلام گردیده.';
}
21 changes: 21 additions & 0 deletions Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Larabookir\Gateway;

use Illuminate\Support\Facades\Facade;

/**
* @see \Larabookir\Gateway\GatewayResolver
*/
class Gateway extends Facade
{
/**
* The name of the binding in the IoC container.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'gateway';
}
}
152 changes: 152 additions & 0 deletions GatewayResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Larabookir\Gateway;

use Larabookir\Gateway\Parsian\Parsian;
use Larabookir\Gateway\Sadad\Sadad;
use Larabookir\Gateway\Mellat\Mellat;
use Larabookir\Gateway\Payline\Payline;
use Larabookir\Gateway\Pasargad\Pasargad;
use Larabookir\Gateway\Zarinpal\Zarinpal;
use Larabookir\Gateway\JahanPay\JahanPay;
use Larabookir\Gateway\Exceptions\RetryException;
use Larabookir\Gateway\Exceptions\PortNotFoundException;
use Larabookir\Gateway\Exceptions\InvalidRequestException;
use Larabookir\Gateway\Exceptions\NotFoundTransactionException;
use Illuminate\Support\Facades\DB;

class GatewayResolver
{

protected $request;

/**
* @var Config
*/
public $config;

/**
* Keep current port driver
*
* @var Mellat|Sadad|Zarinpal|Payline|JahanPay|Parsian
*/
protected $port;

/**
* Gateway constructor.
* @param null $config
* @param null $port
*/
public function __construct($config = null, $port = null)
{
$this->config = app('config');
$this->request = app('request');

if ($this->config->has('gateway.timezone'))
date_default_timezone_set($this->config->get('gateway.timezone'));

if (!is_null($port)) $this->make($port);
}

/**
* Get supported ports
*
* @return array
*/
public function getSupportedPorts()
{
return [Enum::MELLAT, Enum::SADAD, Enum::ZARINPAL, Enum::PAYLINE, Enum::JAHANPAY, Enum::PARSIAN, Enum::PASARGAD];
}

/**
* Call methods of current driver
*
* @return mixed
*/
public function __call($name, $arguments)
{

// calling by this way ( Gateway::mellat()->.. , Gateway::parsian()->.. )
if(in_array(strtoupper($name),$this->getSupportedPorts())){
return $this->make($name);
}

return call_user_func_array([$this->port, $name], $arguments);
}

/**
* Gets query builder from you transactions table
* @return mixed
*/
function getTable()
{
return DB::table($this->config->get('gateway.table'));
}

/**
* Callback
*
* @return $this->port
*
* @throws InvalidRequestException
* @throws NotFoundTransactionException
* @throws PortNotFoundException
* @throws RetryException
*/
public function verify()
{
if (!$this->request->has('transaction_id'))
throw new InvalidRequestException;

$id = intval($this->request->get('transaction_id'));

$transaction = $this->getTable()->whereId($id)->first();

if (!$transaction)
throw new NotFoundTransactionException;

if (in_array($transaction->status, [Enum::TRANSACTION_SUCCEED, Enum::TRANSACTION_FAILED]))
throw new RetryException;

$this->make($transaction->port);

return $this->port->verify($transaction);
}


/**
* Create new object from port class
*
* @param int $port
* @throws PortNotFoundException
*/
function make($port)
{
if ($port InstanceOf Mellat) {
$name = Enum::MELLAT;
} elseif ($port InstanceOf Parsian) {
$name = Enum::PARSIAN;
} elseif ($port InstanceOf Payline) {
$name = Enum::PAYLINE;
} elseif ($port InstanceOf Zarinpal) {
$name = Enum::ZARINPAL;
} elseif ($port InstanceOf JahanPay) {
$name = Enum::JAHANPAY;
} elseif ($port InstanceOf SADAD) {
$name = Enum::SADAD;
} elseif(in_array(strtoupper($port),$this->getSupportedPorts())){
$port=ucfirst(strtolower($port));
$name=strtoupper($port);
$class=__NAMESPACE__.'\\'.$port.'\\'.$port;
$port=new $class;
} else
throw new PortNotFoundException;

$this->port = $port;
$this->port->setConfig($this->config); // injects config
$this->port->setPortName($name); // injects config
$this->port->boot();

return $this;
}
}
60 changes: 60 additions & 0 deletions GatewayServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Larabookir\Gateway;

use Illuminate\Support\ServiceProvider;

class GatewayServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$config = __DIR__ . '/../config/gateway.php';
$migrations = __DIR__ . '/../migrations/';
$views = __DIR__ . '/../views/';

//php artisan vendor:publish --provider=Larabookir\Gateway\GatewayServiceProvider --tag=config
$this->publishes([
$config => config_path('gateway.php'),
], 'config');

// php artisan vendor:publish --provider=Larabookir\Gateway\GatewayServiceProvider --tag=migrations
$this->publishes([
$migrations => base_path('database/migrations')
], 'migrations');


$this->loadViewsFrom($views, 'gateway');

// php artisan vendor:publish --provider=Larabookir\Gateway\GatewayServiceProvider --tag=views
$this->publishes([
$views => base_path('resources/views/vendor/gateway'),
], 'views');

//$this->mergeConfigFrom( $config,'gateway')
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('gateway', function () {
return new GatewayResolver();
});

}
}
Loading

0 comments on commit f012cf5

Please sign in to comment.