From 0b17a80b23bf0e9f9a0d78d7fbe396421e34196b Mon Sep 17 00:00:00 2001 From: ako Date: Sat, 9 Nov 2019 16:59:38 +0330 Subject: [PATCH] Make config file dynamic --- src/Asanpardakht/Asanpardakht.php | 22 +++++++++++----------- src/GatewayResolver.php | 17 +++++++++++------ src/JahanPay/JahanPay.php | 6 +++--- src/Mellat/Mellat.php | 20 ++++++++++---------- src/Parsian/Parsian.php | 6 +++--- src/Pasargad/Pasargad.php | 14 +++++++------- src/Payir/Payir.php | 6 +++--- src/Payline/Payline.php | 6 +++--- src/Paypal/Paypal.php | 6 +++--- src/PortAbstract.php | 18 +++++++++--------- src/Sadad/Sadad.php | 14 +++++++------- src/Saman/Saman.php | 10 +++++----- src/Zarinpal/Zarinpal.php | 16 ++++++++-------- 13 files changed, 83 insertions(+), 78 deletions(-) diff --git a/src/Asanpardakht/Asanpardakht.php b/src/Asanpardakht/Asanpardakht.php index 7332586..5d12250 100644 --- a/src/Asanpardakht/Asanpardakht.php +++ b/src/Asanpardakht/Asanpardakht.php @@ -97,7 +97,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.asanpardakht.callback-url'); + $this->callbackUrl = $this->config['asanpardakht']['callback-url']; $url = $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); @@ -115,8 +115,8 @@ protected function sendPayRequest() { $this->newTransaction(); - $username = $this->config->get('gateway.asanpardakht.username'); - $password = $this->config->get('gateway.asanpardakht.password'); + $username = $this->config['asanpardakht']['username']; + $password = $this->config['asanpardakht']['password']; $orderId = $this->transactionId(); $price = $this->amount; $localDate = date("Ymd His"); @@ -126,7 +126,7 @@ protected function sendPayRequest() $encryptedRequest = $this->encrypt($req); $params = array( - 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), + 'merchantConfigurationID' => $this->config['asanpardakht']['merchantConfigId'], 'encryptedRequest' => $encryptedRequest ); @@ -202,12 +202,12 @@ protected function userPayment() protected function verifyAndSettlePayment() { - $username = $this->config->get('gateway.asanpardakht.username'); - $password = $this->config->get('gateway.asanpardakht.password'); + $username = $this->config['asanpardakht']['username']; + $password = $this->config['asanpardakht']['password']; $encryptedCredintials = $this->encrypt("{$username},{$password}"); $params = array( - 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), + 'merchantConfigurationID' => $this->config['asanpardakht']['merchantConfigId'], 'encryptedCredentials' => $encryptedCredintials, 'payGateTranID' => $this->trackingCode ); @@ -260,8 +260,8 @@ protected function verifyAndSettlePayment() private function encrypt($string = "") { - $key = $this->config->get('gateway.asanpardakht.key'); - $iv = $this->config->get('gateway.asanpardakht.iv'); + $key = $this->config['asanpardakht']['key']; + $iv = $this->config['asanpardakht']['iv']; try { @@ -289,8 +289,8 @@ private function encrypt($string = "") */ private function decrypt($string = "") { - $key = $this->config->get('gateway.asanpardakht.key'); - $iv = $this->config->get('gateway.asanpardakht.iv'); + $key = $this->config['asanpardakht']['key']; + $iv = $this->config['asanpardakht']['iv']; try { diff --git a/src/GatewayResolver.php b/src/GatewayResolver.php index 3d342b8..e8abd1c 100644 --- a/src/GatewayResolver.php +++ b/src/GatewayResolver.php @@ -23,7 +23,7 @@ class GatewayResolver protected $request; /** - * @var Config + * @var array Config */ public $config; @@ -36,16 +36,16 @@ class GatewayResolver /** * Gateway constructor. - * @param null $config + * @param array $config * @param null $port */ - public function __construct($config = null, $port = null) + public function __construct($config = [], $port = null) { - $this->config = app('config'); + $this->config = !$config ?: app('config')['gateway']; $this->request = app('request'); - if ($this->config->has('gateway.timezone')) - date_default_timezone_set($this->config->get('gateway.timezone')); + if ($tz = $this->getTimeZone()) + date_default_timezone_set($tz); if (!is_null($port)) $this->make($port); } @@ -168,4 +168,9 @@ function make($port) return $this; } + + private function getTimeZone() + { + return $this->config['gateway']['timezone'] ?? null; + } } diff --git a/src/JahanPay/JahanPay.php b/src/JahanPay/JahanPay.php index beedf46..f0f596f 100644 --- a/src/JahanPay/JahanPay.php +++ b/src/JahanPay/JahanPay.php @@ -82,7 +82,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.jahanpay.callback-url'); + $this->callbackUrl = $this->config['jahanpay']['callback-url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -101,7 +101,7 @@ protected function sendPayRequest() try { $soap = new SoapClient($this->serverUrl); $response = $soap->requestpayment( - $this->config->get('gateway.jahanpay.api'), + $this->config['jahanpay']['api'], $this->amount, $this->getCallback(), $this->transactionId(), @@ -157,7 +157,7 @@ protected function verifyPayment() try { $soap = new SoapClient($this->serverUrl); $response = $soap->verification( - $this->config->get('gateway.jahanpay.api'), + $this->config['jahanpay']['api'], $this->amount, $this->refId ); diff --git a/src/Mellat/Mellat.php b/src/Mellat/Mellat.php index 2a799f0..3aef3ce 100644 --- a/src/Mellat/Mellat.php +++ b/src/Mellat/Mellat.php @@ -105,7 +105,7 @@ function getAdditionalData () function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.mellat.callback-url'); + $this->callbackUrl = $this->config['mellat']['callback-url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -124,9 +124,9 @@ protected function sendPayRequest() $this->newTransaction(); $fields = array( - 'terminalId' => $this->config->get('gateway.mellat.terminalId'), - 'userName' => $this->config->get('gateway.mellat.username'), - 'userPassword' => $this->config->get('gateway.mellat.password'), + 'terminalId' => $this->config['mellat']['terminalId'], + 'userName' => $this->config['mellat']['username'], + 'userPassword' => $this->config['mellat']['password'], 'orderId' => $this->transactionId(), 'amount' => $this->amount, 'localDate' => $dateTime->format('Ymd'), @@ -191,9 +191,9 @@ protected function userPayment() protected function verifyPayment() { $fields = array( - 'terminalId' => $this->config->get('gateway.mellat.terminalId'), - 'userName' => $this->config->get('gateway.mellat.username'), - 'userPassword' => $this->config->get('gateway.mellat.password'), + 'terminalId' => $this->config['mellat']['terminalId'], + 'userName' => $this->config['mellat']['username'], + 'userPassword' => $this->config['mellat']['password'], 'orderId' => $this->transactionId(), 'saleOrderId' => $this->transactionId(), 'saleReferenceId' => $this->trackingCode() @@ -229,9 +229,9 @@ protected function verifyPayment() protected function settleRequest() { $fields = array( - 'terminalId' => $this->config->get('gateway.mellat.terminalId'), - 'userName' => $this->config->get('gateway.mellat.username'), - 'userPassword' => $this->config->get('gateway.mellat.password'), + 'terminalId' => $this->config['mellat']['terminalId'], + 'userName' => $this->config['mellat']['username'], + 'userPassword' => $this->config['mellat']['password'], 'orderId' => $this->transactionId(), 'saleOrderId' => $this->transactionId(), 'saleReferenceId' => $this->trackingCode diff --git a/src/Parsian/Parsian.php b/src/Parsian/Parsian.php index 52cc4b8..e9c9953 100644 --- a/src/Parsian/Parsian.php +++ b/src/Parsian/Parsian.php @@ -85,7 +85,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.parsian.callback-url'); + $this->callbackUrl = $this->config['parsian']['callback-url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -124,7 +124,7 @@ protected function sendPayRequest() $this->newTransaction(); $params = array( - 'LoginAccount' => $this->config->get('gateway.parsian.pin'), + 'LoginAccount' => $this->config['parsian']['pin'], 'Amount' => $this->amount, 'OrderId' => $this->transactionId(), 'CallBackUrl' => $this->getCallback(), @@ -186,7 +186,7 @@ protected function verifyPayment() throw new ParsianErrorException('تراکنشی یافت نشد', -1); $params = array( - 'LoginAccount' => $this->config->get('gateway.parsian.pin'), + 'LoginAccount' => $this->config['parsian']['pin'], 'Token' => $token ); diff --git a/src/Pasargad/Pasargad.php b/src/Pasargad/Pasargad.php index 7ca0066..77c1436 100644 --- a/src/Pasargad/Pasargad.php +++ b/src/Pasargad/Pasargad.php @@ -53,14 +53,14 @@ public function ready() public function redirect() { - $processor = new RSAProcessor($this->config->get('gateway.pasargad.certificate-path'),RSAKeyType::XMLFile); + $processor = new RSAProcessor($this->config['pasargad']['certificate-path'],RSAKeyType::XMLFile); $url = $this->gateUrl; $redirectUrl = $this->getCallback(); $invoiceNumber = $this->transactionId(); $amount = $this->amount; - $terminalCode = $this->config->get('gateway.pasargad.terminalId'); - $merchantCode = $this->config->get('gateway.pasargad.merchantId'); + $terminalCode = $this->config['pasargad']['terminalId']; + $merchantCode = $this->config['pasargad']['merchantId']; $timeStamp = date("Y/m/d H:i:s"); $invoiceDate = date("Y/m/d H:i:s"); $action = 1003; @@ -101,7 +101,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.pasargad.callback-url'); + $this->callbackUrl = $this->config['pasargad']['callback-url']; return $this->callbackUrl; } @@ -125,7 +125,7 @@ protected function sendPayRequest() */ protected function verifyPayment() { - $processor = new RSAProcessor($this->config->get('gateway.pasargad.certificate-path'),RSAKeyType::XMLFile); + $processor = new RSAProcessor($this->config['pasargad']['certificate-path'],RSAKeyType::XMLFile); if (!Input::has('tref')) throw new PasargadErrorException('درخواست غیر معتبر', -1); @@ -141,8 +141,8 @@ protected function verifyPayment() } $fields = array( - 'MerchantCode' => $this->config->get('gateway.pasargad.merchantId'), - 'TerminalCode' => $this->config->get('gateway.pasargad.terminalId'), + 'MerchantCode' => $this->config['pasargad']['merchantId'], + 'TerminalCode' => $this->config['pasargad']['terminalId'], 'InvoiceNumber' => $check_array['resultObj']['invoiceNumber'], 'InvoiceDate' => Input::get('iD'), 'amount' => $check_array['resultObj']['amount'], diff --git a/src/Payir/Payir.php b/src/Payir/Payir.php index a4de5b6..8049708 100644 --- a/src/Payir/Payir.php +++ b/src/Payir/Payir.php @@ -99,7 +99,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.payir.callback-url'); + $this->callbackUrl = $this->config['payir']['callback-url']; return urlencode($this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()])); } @@ -114,7 +114,7 @@ protected function sendPayRequest() { $this->newTransaction(); $fields = [ - 'api' => $this->config->get('gateway.payir.api'), + 'api' => $this->config['payir']['api'], 'amount' => $this->amount, 'redirect' => $this->getCallback(), ]; @@ -172,7 +172,7 @@ protected function userPayment() protected function verifyPayment() { $fields = [ - 'api' => $this->config->get('gateway.payir.api'), + 'api' => $this->config['payir']['api'], 'transId' => $this->refId(), ]; $ch = curl_init(); diff --git a/src/Payline/Payline.php b/src/Payline/Payline.php index 1953515..cc1f69a 100644 --- a/src/Payline/Payline.php +++ b/src/Payline/Payline.php @@ -88,7 +88,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.payline.callback-url'); + $this->callbackUrl = $this->config['payline']['callback-url']; return urlencode($this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()])); } @@ -105,7 +105,7 @@ protected function sendPayRequest() $this->newTransaction(); $fields = array( - 'api' => $this->config->get('gateway.payline.api'), + 'api' => $this->config['payline']['api'], 'amount' => $this->amount, 'redirect' => $this->getCallback(), ); @@ -164,7 +164,7 @@ protected function userPayment() protected function verifyPayment() { $fields = array( - 'api' => $this->config->get('gateway.payline.api'), + 'api' => $this->config['payline']['api'], 'id_get' => $this->refId(), 'trans_id' => $this->trackingCode() ); diff --git a/src/Paypal/Paypal.php b/src/Paypal/Paypal.php index b9e81ca..2933069 100644 --- a/src/Paypal/Paypal.php +++ b/src/Paypal/Paypal.php @@ -62,7 +62,7 @@ public function redirect() function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.paypal.settings.call_back_url'); + $this->callbackUrl = $this->config['paypal.settings']['call_back_url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -236,7 +236,7 @@ protected function verifyPayment() public function getProductName(){ if(!$this->productName){ - return $this->config->get('gateway.paypal.default_product_name'); + return $this->config['paypal']['default_product_name']; } return $this->productName; @@ -244,7 +244,7 @@ public function getProductName(){ public function getShipmentPrice(){ if(!$this->shipmentPrice){ - return $this->config->get('gateway.paypal.default_shipment_price'); + return $this->config['paypal']['default_shipment_price']; } return $this->shipmentPrice; diff --git a/src/PortAbstract.php b/src/PortAbstract.php index e2101d0..1e95445 100644 --- a/src/PortAbstract.php +++ b/src/PortAbstract.php @@ -27,7 +27,7 @@ abstract class PortAbstract protected $cardNumber = ''; /** - * @var Config + * @var array $config */ protected $config; @@ -73,13 +73,10 @@ abstract class PortAbstract */ protected $trackingCode; - /** - * Initialize of class - * - * @param Config $config - * @param DataBaseManager $db - * @param int $port - */ + /** + * Initialize of class + * + */ function __construct() { $this->db = app('db'); @@ -90,6 +87,9 @@ function boot(){ } + /** + * @param array $config + */ function setConfig($config) { $this->config = $config; @@ -108,7 +108,7 @@ function getTable() */ function getLogTable() { - return $this->db->table($this->config->get('gateway.table') . '_logs'); + return $this->db->table($this->config['table') '][' '_logs']; } /** diff --git a/src/Sadad/Sadad.php b/src/Sadad/Sadad.php index 5592a65..bf5e33f 100644 --- a/src/Sadad/Sadad.php +++ b/src/Sadad/Sadad.php @@ -82,7 +82,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.sadad.callback-url'); + $this->callbackUrl = $this->config['sadad']['callback-url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -104,11 +104,11 @@ protected function sendPayRequest() $soap = new SoapClient($this->serverUrl); $response = $soap->PaymentUtility( - $this->config->get('gateway.sadad.merchant'), + $this->config['sadad']['merchant'], $this->amount, $this->transactionId(), - $this->config->get('gateway.sadad.transactionKey'), - $this->config->get('gateway.sadad.terminalId'), + $this->config['sadad']['transactionKey'], + $this->config['sadad']['terminalId'], $this->getCallback() ); @@ -142,9 +142,9 @@ protected function verifyPayment() $result = $soap->CheckRequestStatusResult( $this->transactionId(), - $this->config->get('gateway.sadad.merchant'), - $this->config->get('gateway.sadad.terminalId'), - $this->config->get('gateway.sadad.transactionKey'), + $this->config['sadad']['merchant'], + $this->config['sadad']['terminalId'], + $this->config['sadad']['transactionKey'], $this->refId(), $this->amount ); diff --git a/src/Saman/Saman.php b/src/Saman/Saman.php index e2f372c..bf5cc3e 100644 --- a/src/Saman/Saman.php +++ b/src/Saman/Saman.php @@ -64,7 +64,7 @@ public function redirect() { $main_data = [ 'amount' => $this->amount, - 'merchant' => $this->config->get('gateway.saman.merchant'), + 'merchant' => $this->config['saman']['merchant'], 'resNum' => $this->transactionId(), 'callBackUrl' => $this->getCallback() ]; @@ -104,7 +104,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.saman.callback-url'); + $this->callbackUrl = $this->config['saman']['callback-url']; $url = $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); @@ -142,14 +142,14 @@ protected function userPayment() * @return bool * * @throws SamanException - * @throws SoapFault + * @throws \SoapFault */ protected function verifyPayment() { $fields = array( - "merchantID" => $this->config->get('gateway.saman.merchant'), + "merchantID" => $this->config['saman']['merchant'], "RefNum" => $this->refId, - "password" => $this->config->get('gateway.saman.password'), + "password" => $this->config['saman']['password'], ); diff --git a/src/Zarinpal/Zarinpal.php b/src/Zarinpal/Zarinpal.php index a3c3d6e..b0d5263 100644 --- a/src/Zarinpal/Zarinpal.php +++ b/src/Zarinpal/Zarinpal.php @@ -111,7 +111,7 @@ public function ready() */ public function redirect() { - switch ($this->config->get('gateway.zarinpal.type')) { + switch ($this->config['zarinpal']['type']) { case 'zarin-gate': return \Redirect::to(str_replace('$Authority', $this->refId, $this->zarinGateUrl)); break; @@ -153,7 +153,7 @@ function setCallback($url) function getCallback() { if (!$this->callbackUrl) - $this->callbackUrl = $this->config->get('gateway.zarinpal.callback-url'); + $this->callbackUrl = $this->config['zarinpal']['callback-url']; return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); } @@ -170,12 +170,12 @@ protected function sendPayRequest() $this->newTransaction(); $fields = array( - 'MerchantID' => $this->config->get('gateway.zarinpal.merchant-id'), + 'MerchantID' => $this->config['zarinpal']['merchant-id'], 'Amount' => $this->amount, 'CallbackURL' => $this->getCallback(), - 'Description' => $this->description ? $this->description : $this->config->get('gateway.zarinpal.description', ''), - 'Email' => $this->email ? $this->email :$this->config->get('gateway.zarinpal.email', ''), - 'Mobile' => $this->mobileNumber ? $this->mobileNumber : $this->config->get('gateway.zarinpal.mobile', ''), + 'Description' => $this->description ? $this->description : $this->config['zarinpal']['description', ''], + 'Email' => $this->email ? $this->email :$this->config['zarinpal']['email', ''], + 'Mobile' => $this->mobileNumber ? $this->mobileNumber : $this->config['zarinpal']['mobile', ''], ); try { @@ -230,7 +230,7 @@ protected function verifyPayment() { $fields = array( - 'MerchantID' => $this->config->get('gateway.zarinpal.merchant-id'), + 'MerchantID' => $this->config['zarinpal']['merchant-id'], 'Authority' => $this->refId, 'Amount' => $this->amount, ); @@ -264,7 +264,7 @@ protected function verifyPayment() */ protected function setServer() { - $server = $this->config->get('gateway.zarinpal.server', 'germany'); + $server = $this->config['zarinpal']['server', 'germany']; switch ($server) { case 'iran': $this->serverUrl = $this->iranServer;