From f7e28f2bd4662dd8dd4af6e3c784634f867ca10c Mon Sep 17 00:00:00 2001 From: Haniyeh Date: Sun, 22 Aug 2021 10:13:06 +0430 Subject: [PATCH] asan pardakht --- src/Asanpardakht/Asanpardakht.php | 430 +++++++++++++++++------------- 1 file changed, 242 insertions(+), 188 deletions(-) diff --git a/src/Asanpardakht/Asanpardakht.php b/src/Asanpardakht/Asanpardakht.php index 9244f3d..a6dd2fb 100644 --- a/src/Asanpardakht/Asanpardakht.php +++ b/src/Asanpardakht/Asanpardakht.php @@ -2,6 +2,8 @@ namespace Hpez\Gateway\Asanpardakht; +use Hpez\Gateway\Enum; +use GuzzleHttp\Client; use Illuminate\Support\Facades\Input; use SoapClient; use Hpez\Gateway\PortAbstract; @@ -15,10 +17,24 @@ class Asanpardakht extends PortAbstract implements PortInterface * @var string */ protected $serverUrl = 'https://services.asanpardakht.net/paygate/merchantservices.asmx?wsdl'; + protected $verifyUrl = 'Api/v1/Verify'; + protected $getTokenUrl = 'Api/v1/Token'; + protected $baseUrl = 'https://asan.shaparak.ir/'; + protected $tranResultUrl = 'Api/v1/Payment/'; - /** + protected $token; + protected $payGateTranId; + + /** * {@inheritdoc} */ + public function __construct() + { + parent::__construct(); + + $this->client = new Client(['base_uri' => $this->baseUrl]); + } + public function set($amount) { $this->amount = $amount; @@ -54,8 +70,10 @@ public function verify($transaction) { parent::verify($transaction); - $this->userPayment(); - $this->verifyAndSettlePayment(); + $this->userPayment(); + $this->verifyPayment(); + $this->settlePayment(); + return $this; } @@ -94,197 +112,233 @@ protected function sendPayRequest() { $this->newTransaction(); - $username = $this->config->get('gateway.asanpardakht.username'); - $password = $this->config->get('gateway.asanpardakht.password'); - $orderId = $this->transactionId(); - $price = $this->amount; - $localDate = date("Ymd His"); - $additionalData = ""; - $callBackUrl = $this->getCallback(); - $req = "1,{$username},{$password},{$orderId},{$price},{$localDate},{$additionalData},{$callBackUrl},0"; - - $encryptedRequest = $this->encrypt($req); - $params = array( - 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), - 'encryptedRequest' => $encryptedRequest - ); - - try { - $soap = new SoapClient($this->serverUrl); - $response = $soap->RequestOperation($params); - - } catch (\SoapFault $e) { - $this->transactionFailed(); - $this->newLog('SoapFault', $e->getMessage()); - throw $e; - } - - - $response = $response->RequestOperationResult; - $responseCode = explode(",", $response)[0]; - if ($responseCode != '0') { - $this->transactionFailed(); - $this->newLog($response, AsanpardakhtException::getMessageByCode($response)); - throw new AsanpardakhtException($response); - } - $this->refId = substr($response, 2); - $this->transactionSetRefId(); - } - - - /** - * Check user payment - * - * @return bool - * - * @throws AsanpardakhtException - */ - protected function userPayment() - { - $ReturningParams = Input::get('ReturningParams'); - $ReturningParams = $this->decrypt($ReturningParams); - - $paramsArray = explode(",", $ReturningParams); - $Amount = $paramsArray[0]; - $SaleOrderId = $paramsArray[1]; - $RefId = $paramsArray[2]; - $ResCode = $paramsArray[3]; - $ResMessage = $paramsArray[4]; - $PayGateTranID = $paramsArray[5]; - $RRN = $paramsArray[6]; - $LastFourDigitOfPAN = $paramsArray[7]; - - - $this->trackingCode = $PayGateTranID; - $this->cardNumber = $LastFourDigitOfPAN; - $this->refId = $RefId; - - - if ($ResCode == '0' || $ResCode == '00') { - return true; - } - - $this->transactionFailed(); - $this->newLog($ResCode, $ResMessage . " - " . AsanpardakhtException::getMessageByCode($ResCode)); - throw new AsanpardakhtException($ResCode); - } - - - /** - * Verify and settle user payment from bank server + $params = [ + "merchantConfigurationId" => $this->config->get('gateway.asanpardakht.merchantConfigId'), + "serviceTypeId" => 1, + "localInvoiceId" => $this->transactionId(), + "amountInRials"=> $this->amount, + "localDate"=> date("Ymd His"), + "additionalData" => "", + "callbackURL" => $this->getCallback(), + "paymentId" => "", + ]; + + $response = $this->client->post($this->getTokenUrl, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $this->config->get('gateway.asanpardakht.username'), + 'pwd' => $this->config->get('gateway.asanpardakht.password') + ], + 'json' => $params + ]); + + $response = json_decode($response->getBody()->getContents()); + + if ($response->status && $response->status != 200) { + $this->newLog(-1, Enum::TRANSACTION_FAILED_TEXT); + throw new AsanpardakhtException($response->status); + } + + $this->token = $response->Token; + } + + /** + * Verify payment from bank server * * @return bool * * @throws AsanpardakhtException - * @throws SoapFault - */ - protected function verifyAndSettlePayment() - { - - $username = $this->config->get('gateway.asanpardakht.username'); - $password = $this->config->get('gateway.asanpardakht.password'); - - $encryptedCredintials = $this->encrypt("{$username},{$password}"); - $params = array( - 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), - 'encryptedCredentials' => $encryptedCredintials, - 'payGateTranID' => $this->trackingCode - ); - - - try { - $soap = new SoapClient($this->serverUrl); - $response = $soap->RequestVerification($params); - $response = $response->RequestVerificationResult; - - } catch (\SoapFault $e) { - $this->transactionFailed(); - $this->newLog('SoapFault', $e->getMessage()); - throw $e; - } - - if ($response != '500') { - $this->transactionFailed(); - $this->newLog($response, AsanpardakhtException::getMessageByCode($response)); - throw new AsanpardakhtException($response); - } - - - try { - - $response = $soap->RequestReconciliation($params); - $response = $response->RequestReconciliationResult; - - if ($response != '600') - $this->newLog($response, AsanpardakhtException::getMessageByCode($response)); - - } catch (\SoapFault $e) { - //If fail, shaparak automatically do it in next 12 houres. - } - - - $this->transactionSucceed(); - - return true; - } - - - /** - * Encrypt string by key and iv from config - * - * @param string $string - * @return string - */ - private function encrypt($string = "") - { - - $key = $this->config->get('gateway.asanpardakht.key'); - $iv = $this->config->get('gateway.asanpardakht.iv'); - - try { - - $soap = new SoapClient("https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL"); - $params = array( - 'aesKey' => $key, - 'aesVector' => $iv, - 'toBeEncrypted' => $string - ); - - $response = $soap->EncryptInAES($params); - return $response->EncryptInAESResult; - - } catch (\SoapFault $e) { - return ""; - } - } - - - /** - * Decrypt string by key and iv from config - * - * @param string $string - * @return string */ - private function decrypt($string = "") + protected function verifyPayment() { - $key = $this->config->get('gateway.asanpardakht.key'); - $iv = $this->config->get('gateway.asanpardakht.iv'); - - try { - - $soap = new SoapClient("https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL"); - $params = array( - 'aesKey' => $key, - 'aesVector' => $iv, - 'toBeDecrypted' => $string - ); - - $response = $soap->DecryptInAES($params); - return $response->DecryptInAESResult; - - } catch (\SoapFault $e) { - return ""; - } + $username = $this->config->get('gateway.asanpardakht.username'); + $password = $this->config->get('gateway.asanpardakht.password'); + + $response = $this->client->get($this->verifyUrl, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $username, + 'pwd' => $password + ], + ]); + + $response = json_decode($response->getBody()->getContents()); + + if ($response->status != '200') { + $this->transactionFailed(); + $this->newLog($response->status, AsanpardakhtException::getMessageByCode($response->status)); + throw new AsanpardakhtException($response->status); + } + + return true; } + /** + * Settle user payment from bank server + * + * @return bool + * + * @throws AsanpardakhtException + */ + protected function settlePayment() + { + $username = $this->config->get('gateway.asanpardakht.username'); + $password = $this->config->get('gateway.asanpardakht.password'); + + $params = [ + 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), + 'LocalInvoiceId' => $this->transactionId(), + ]; + + $response = $this->client->get($this->tranResultUrl, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $username, + 'pwd' => $password + ], + 'json' => $params + ]); + + $response = json_decode($response->getBody()->getContents()); + + if ($response->status != '200') { + //If fail, shaparak automatically do it in next 12 houres. + } + + $this->transactionSucceed(); + + return true; + } + + /** + * Check user payment + * + * @return bool + * + * @throws AsanpardakhtException + */ + protected function userPayment() + { + $ReturningParams = $this->tranResult(); + + if ($ReturningParams->status && $ReturningParams->status != '200') { + $this->transactionFailed(); + $this->newLog($ReturningParams->status, AsanpardakhtException::getMessageByCode($ReturningParams->status)); + throw new AsanpardakhtException($ReturningParams->status); + } + + return true; + } + + /** + * Get transaction result + * + * @return bool + * + * @throws AsanpardakhtException + */ + protected function tranResult() + { + $username = $this->config->get('gateway.asanpardakht.username'); + $password = $this->config->get('gateway.asanpardakht.password'); + + $params = [ + 'merchantConfigurationID' => $this->config->get('gateway.asanpardakht.merchantConfigId'), + 'LocalInvoiceId' => $this->transactionId(), + ]; + + $response = $this->client->get($this->tranResultUrl, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $username, + 'pwd' => $password + ], + 'json' => $params + ]); + + $response = json_decode($response->getBody()->getContents()); + + if (($response->status && $response->status != '200') || $response->amount != $this->amount) { + $this->transactionFailed(); + $this->newLog($response->status, AsanpardakhtException::getMessageByCode($response->status)); + throw new AsanpardakhtException($response->status); + } + + $this->refId = $response->refId; + $this->payGateTranId = $response->payGateTranID; + $this->trackingCode = $response->payGateTranID; + $this->cardNumber = $response->cardNumber; + + return $response; + } + + /** + * Reverse transaction + * + * @return bool + * + * @throws AsanpardakhtException + */ + protected function revers() + { + $returningParams = $this->tranResult(); + + $params = [ + "merchantConfigurationId" => $this->config->get('gateway.asanpardakht.merchantConfigId'), + "payGateTranId" => $returningParams['payGateTranId'], + ]; + + $response = $this->client->post('v1/Reverse', [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $this->config->get('gateway.asanpardakht.username'), + 'pwd' => $this->config->get('gateway.asanpardakht.password') + ], + 'json' => $params + ]); + + $response = json_decode($response->getBody()->getContents()); + + if ($response->status && $response->status != 200) { + $this->newLog($response->status, AsanpardakhtException::getMessageByCode($response->status)); + throw new AsanpardakhtException($response->status); + } + + return true; + } + + /** + * Cancel transaction + * + * @return bool + * + * @throws AsanpardakhtException + */ + protected function cancel() + { + $returningParams = $this->tranResult(); + + $params = [ + "merchantConfigurationId" => $this->config->get('gateway.asanpardakht.merchantConfigId'), + "payGateTranId" => $returningParams['payGateTranId'], + ]; + + $response = $this->client->post('v1/Cancel', [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'usr' => $this->config->get('gateway.asanpardakht.username'), + 'pwd' => $this->config->get('gateway.asanpardakht.password') + ], + 'json' => $params + ]); + + $response = json_decode($response->getBody()->getContents()); + + if ($response->status && $response->status != 200) { + $this->newLog($response->status, AsanpardakhtException::getMessageByCode($response->status)); + throw new AsanpardakhtException($response->status); + } + + return true; + } } \ No newline at end of file