forked from larabook/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
مشکلات کلی حل شد . درگاه بانک ملت نیز ایجاد گردید
- Loading branch information
Showing
16 changed files
with
754 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Larabookir\Gateway; | ||
|
||
class Enum | ||
{ | ||
const MELLAT = 'MELLAT'; | ||
const SADAD = 'SADAD'; | ||
const ZARINPAL = 'ZARINPAL'; | ||
const PAYLINE = 'PAYLINE'; | ||
const JAHANPAY = 'JAHANPAY'; | ||
const PARSIAN = 'PARSIAN'; | ||
|
||
/** | ||
* 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 = 'عملیات پرداخت با خطا مواجه شد.'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,160 +1,18 @@ | ||
<?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\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; | ||
|
||
class Gateway | ||
{ | ||
const MELLAT = 'MELLAT'; | ||
|
||
const SADAD = 'SADAD'; | ||
|
||
const ZARINPAL = 'ZARINPAL'; | ||
|
||
const PAYLINE = 'PAYLINE'; | ||
|
||
const JAHANPAY = 'JAHANPAY'; | ||
|
||
const PARSIAN = 'PARSIAN'; | ||
|
||
protected $request; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
public $config; | ||
|
||
/** | ||
* Keep current port driver | ||
* | ||
* @var Mellat|Sadad|Zarinpal|Payline|JahanPay | ||
*/ | ||
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 [self::MELLAT, self::SADAD, self::ZARINPAL, self::PAYLINE, self::JAHANPAY]; | ||
} | ||
|
||
/** | ||
* Call methods of current driver | ||
* | ||
* @return mixed | ||
*/ | ||
public function __call($name, $arguments) | ||
{ | ||
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.db_tables.transactions')); | ||
} | ||
|
||
/** | ||
* 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('transaction_id')); | ||
|
||
$transaction = $this->getTable()->whereId($id)->first(); | ||
|
||
if (!$transaction) | ||
throw new NotFoundTransactionException; | ||
|
||
if (in_array($transaction->status, [PortAbstract::TRANSACTION_SUCCEE, PortAbstract::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) | ||
{ | ||
switch ($port) { | ||
case self::MELLAT: | ||
$this->port = new Mellat($this->config, self::MELLAT); | ||
break; | ||
|
||
case self::SADAD: | ||
$this->port = new Sadad($this->config, self::SADAD); | ||
break; | ||
|
||
case self::ZARINPAL: | ||
$this->port = new Zarinpal($this->config, self::ZARINPAL); | ||
break; | ||
|
||
case self::PAYLINE: | ||
$this->port = new Payline($this->config, self::PAYLINE); | ||
break; | ||
|
||
case self::JAHANPAY: | ||
$this->port = new JahanPay($this->config, self::JAHANPAY); | ||
break; | ||
|
||
case self::PARSIAN: | ||
$this->port = new Parsian($this->config, self::PARSIAN); | ||
break; | ||
|
||
default: | ||
throw new PortNotFoundException; | ||
break; | ||
} | ||
|
||
return $this; | ||
} | ||
} | ||
<?php | ||
|
||
namespace Larabookir\Gateway; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Gateway extends Facade | ||
{ | ||
/** | ||
* The name of the binding in the IoC container. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'gateway'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?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\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 | ||
*/ | ||
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]; | ||
} | ||
|
||
/** | ||
* 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.db_tables.transactions')); | ||
} | ||
|
||
/** | ||
* 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 | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.