From a9a407d251d612095f050e9e437fd64321fb9352 Mon Sep 17 00:00:00 2001 From: Aboozar Ghaffari Date: Thu, 29 Jun 2023 20:10:43 +0330 Subject: [PATCH] changed htran generation method --- config/wallet.php | 12 +- src/Contracts/Factory.php | 11 +- src/Contracts/Provider.php | 50 +--- src/Contracts/Transaction.php | 75 +---- src/Enums/AsanpardakhtStatusEnum.php | 4 - src/Exception.php | 3 - src/Facades/LaraWallet.php | 5 - src/LaraWalletServiceProvider.php | 12 +- src/Provider/AbstractProvider.php | 103 +------ src/Provider/AsanPardakhtProvider.php | 386 ++++++++++++-------------- src/WalletManager.php | 41 +-- 11 files changed, 206 insertions(+), 496 deletions(-) diff --git a/config/wallet.php b/config/wallet.php index 6c56568..8d55337 100644 --- a/config/wallet.php +++ b/config/wallet.php @@ -14,12 +14,12 @@ */ 'mode' => env('WALLET_MODE', 'production'), - 'asanpardakht' => [ 'debug' => true, - 'host_id' => '', - 'url' => '', - 'private_key' => '', - ] - + 'host_id' => env('ASANPARDAKHT_WALLET_HOST_ID'), + 'url' => env( + 'ASANPARDAKHT_WALLET_URL', + 'https://thirdparty.dev.tasn.ir/exts/v1/' + ).env('ASANPARDAKHT_WALLET_HOST_ID').'/1', + ], ]; diff --git a/src/Contracts/Factory.php b/src/Contracts/Factory.php index 05767d0..3fbb156 100644 --- a/src/Contracts/Factory.php +++ b/src/Contracts/Factory.php @@ -4,10 +4,9 @@ interface Factory { - -// /** -// * @param $driver -// * @return mixed -// */ -// public function driver($driver = null): mixed; + // /** + // * @param $driver + // * @return mixed + // */ + // public function driver($driver = null): mixed; } diff --git a/src/Contracts/Provider.php b/src/Contracts/Provider.php index c1e2082..916e760 100644 --- a/src/Contracts/Provider.php +++ b/src/Contracts/Provider.php @@ -6,88 +6,42 @@ interface Provider { /** * Determines whether the provider supports reverse transaction - * - * @return bool */ public function refundSupport(): bool; /** - * @param array $parameters operation parameters - * - * @return Provider + * @param array $parameters operation parameters */ public function setParameters(array $parameters = []): Provider; /** - * @param string|null $key - * - * @param null $default - * - * @return mixed + * @param null $default */ public function getParameters(string $key = null, $default = null): mixed; /** * return rendered goto gate form - * - * @return string */ public function getForm(): string; - - /** - * @return array - */ public function getFormParameters(): array; - - /** - * @return Transaction - */ public function getTransaction(): Transaction; /** * verify transaction - * - * @return bool */ public function verifyTransaction(): bool; - - /** - * @return bool - */ public function settleTransaction(): bool; - - /** - * @return bool - */ public function refundTransaction(): bool; - - /** - * @return string - */ public function getGatewayReferenceId(): string; - - /** - * @param string $action - * @return string - */ public function getUrlFor(string $action): string; - - /** - * @return bool - */ public function canContinueWithCallbackParameters(): bool; - - /** - * @param array $parameters - * @return void - */ public function checkRequiredActionParameters(array $parameters): void; } diff --git a/src/Contracts/Transaction.php b/src/Contracts/Transaction.php index 40523fd..814a64d 100644 --- a/src/Contracts/Transaction.php +++ b/src/Contracts/Transaction.php @@ -6,16 +6,12 @@ interface Transaction { /** * return the callback url of the transaction process - * - * @return string */ public function getCallbackUrl(): string; /** * set gateway token of transaction * - * @param string $token - * @param bool $save * * @return mixed */ @@ -24,105 +20,38 @@ public function setGatewayToken(string $token, bool $save = true): bool; /** * set reference ID of transaction * - * @param string $referenceId - * @param bool $save * * @return mixed */ public function setReferenceId(string $referenceId, bool $save = true): bool; - - /** - * @return int - */ public function getGatewayOrderId(): int; - - /** - * @return bool - */ public function isReadyForTokenRequest(): bool; - - /** - * @return bool - */ public function isReadyForVerify(): bool; - - /** - * @return bool - */ public function isReadyForInquiry(): bool; - - /** - * @return bool - */ public function isReadyForSettle(): bool; - - /** - * @return bool - */ public function isReadyForRefund(): bool; - - /** - * @param bool $save - * @return bool - */ public function setVerified(bool $save = true): bool; - - /** - * @param bool $save - * @return bool - */ public function setSettled(bool $save = true): bool; - - /** - * @param bool $save - * @return bool - */ public function setAccomplished(bool $save = true): bool; - - /** - * @param bool $save - * @return bool - */ public function setRefunded(bool $save = true): bool; - - /** - * @return int - */ public function getPayableAmount(): int; - - /** - * @param string $cardNumber - * @param bool $save - * @return bool - */ public function setCardNumber(string $cardNumber, bool $save = false): bool; - - /** - * @param array $parameters - * @param bool $save - * @return bool - */ public function setCallBackParameters(array $parameters, bool $save = true): bool; - - /** - * @param string $key - * @param $value - * @param bool $save - * @return bool - */ public function addExtra(string $key, $value, bool $save = true): bool; + + public function getWalletTransactionId(): int; } diff --git a/src/Enums/AsanpardakhtStatusEnum.php b/src/Enums/AsanpardakhtStatusEnum.php index 5b01128..f0bb489 100644 --- a/src/Enums/AsanpardakhtStatusEnum.php +++ b/src/Enums/AsanpardakhtStatusEnum.php @@ -2,9 +2,6 @@ namespace PhpMonsters\LaraWallet\Enums; -/** - * - */ enum AsanpardakhtStatusEnum: int { //----------------------generate by Asanpardakht------------------- @@ -31,7 +28,6 @@ enum AsanpardakhtStatusEnum: int case SettleRequestHop = 2002; case RefundRequestHop = 2003; - //----------------------generate by code------------------- case SuccessResponse = 10001; case AccessDeniedResponse = 10000; diff --git a/src/Exception.php b/src/Exception.php index 81eabb7..ee357ef 100755 --- a/src/Exception.php +++ b/src/Exception.php @@ -2,9 +2,6 @@ namespace PhpMonsters\LaraWallet; -/** - * - */ class Exception extends \Exception { } diff --git a/src/Facades/LaraWallet.php b/src/Facades/LaraWallet.php index b6c04bc..9f473eb 100644 --- a/src/Facades/LaraWallet.php +++ b/src/Facades/LaraWallet.php @@ -6,17 +6,12 @@ use PhpMonsters\LaraWallet\Contracts\Factory; /** - * * @method static log(string $message, array $params, string $level) */ class LaraWallet extends Facade { - /** - * @return string - */ public static function getFacadeAccessor(): string { return Factory::class; } - } diff --git a/src/LaraWalletServiceProvider.php b/src/LaraWalletServiceProvider.php index da4131b..00b6767 100644 --- a/src/LaraWalletServiceProvider.php +++ b/src/LaraWalletServiceProvider.php @@ -5,15 +5,10 @@ use Illuminate\Support\ServiceProvider; use PhpMonsters\LaraWallet\Contracts\Factory; -/** - * - */ class LaraWalletServiceProvider extends ServiceProvider { /** * Register services. - * - * @return void */ public function register(): void { @@ -24,21 +19,16 @@ public function register(): void /** * Bootstrap services. - * - * @return void */ public function boot(): void { $this->registerPublishing(); } - /** - * @return void - */ protected function registerPublishing(): void { $this->publishes([ - __DIR__ . '/../config/wallet.php' => config_path('wallet.php') + __DIR__.'/../config/wallet.php' => config_path('wallet.php'), ], 'config'); } } diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 4ddc9bf..f8ff970 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -17,18 +17,13 @@ * Class AbstractProvider * * @author Maryam Nabiyan - * @package Wallet - * @package Wallet\Wallet + * * @version v1.0 */ abstract class AbstractProvider extends WalletProvider { const CONNECTION_TIME_OUT = 5; - - /** - * @var string - */ protected string $cellNumber; /** @@ -36,9 +31,6 @@ abstract class AbstractProvider extends WalletProvider */ protected string $environment; - /** - * @var string - */ protected string $url; /** @@ -46,18 +38,11 @@ abstract class AbstractProvider extends WalletProvider */ protected $transaction; - - /** - * @param array $configs - * @param string $environment - * @param $transaction - * @param $mobileNumber - */ public function __construct( array $configs, string $environment, //todo adds type of transaction - $transaction = null, + $transaction, $mobileNumber ) { $this->environment = $environment; @@ -67,50 +52,26 @@ public function __construct( $this->setUrl(config('wallet.asanpardakht.url')); } - - /** - * @param $transaction - * @return void - */ public function setTransaction($transaction = null): void { $this->transaction = $transaction; } - /** - * @param $cellNumber - * @return void - */ public function setCellNumber($cellNumber): void { $this->cellNumber = $cellNumber; } - /** - * @return string - */ public function getCellNumber(): string { return $this->cellNumber; } - /** - * @param string $data - * @return bool|string - */ public function hashParam(string $data): bool|string { return hash('md5', $data, false); } - - /** - * @param string $hostRequest - * @param string $hostRequestSign - * @param $method - * @param $url - * @return mixed - */ public function sendInfoToAp(string $hostRequest, string $hostRequestSign, $method, $url): mixed { $rawResponse = Http::withHeaders([ @@ -119,7 +80,7 @@ public function sendInfoToAp(string $hostRequest, string $hostRequestSign, $meth ])->withBody( '{"hreq":"'.str_replace( ['"{', '}"'], - ["{", "}"], + ['{', '}'], json_encode($hostRequest, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK), ).'","hsign":"'.$hostRequestSign.'","ver":"1.9.2"}', 'application/json' @@ -130,9 +91,7 @@ public function sendInfoToAp(string $hostRequest, string $hostRequestSign, $meth return json_decode($rawResponse->body(), true); } - /** - * @param array $parameters * @return $this */ public function setParameters(array $parameters = []): static @@ -145,10 +104,7 @@ public function setParameters(array $parameters = []): static return $this; } - /** - * @param string|null $key - * @param $default * @return mixed|null */ public function getParameters(string $key = null, $default = null): mixed @@ -158,29 +114,25 @@ public function getParameters(string $key = null, $default = null): mixed } $key = strtolower($key); + return $this->parameters[$key] ?? $default; } /** * @param mixed|string $environment - * @return AbstractProvider */ public function setEnvironment(mixed $environment): AbstractProvider { $this->environment = $environment; + return $this; } - public function getTransaction() { return $this->transaction; } - /** - * @param string $url - * @return AbstractProvider - */ public function setUrl(string $url): AbstractProvider { $this->url = $url; @@ -188,56 +140,33 @@ public function setUrl(string $url): AbstractProvider return $this; } - /** - * @return string - */ public function getUrl(): string { return $this->url; } - /** - * @param string $message - * @param array $params - * @param string $level - * @return void - */ protected function log(string $message, array $params = [], string $level = 'debug'): void { $reflect = new ReflectionClass($this); $provider = strtolower(str_replace('Provider', '', $reflect->getShortName())); - $message = $provider.": ".$message; + $message = $provider.': '.$message; LaraWallet::log($message, $params, $level); } - /** - * @param $message - * @param int $statusCode - * @param int|null $errorCode - * @return JsonResponse - */ public static function generalExceptionResponse( int $errorCode = null, $message = null, int $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR, ): JsonResponse { return response()->json([ - "code" => $errorCode, - "message" => $message, - "x_track_id" => resolve(env('XLOG_TRACK_ID_KEY', 'xTrackId')), + 'code' => $errorCode, + 'message' => $message, + 'x_track_id' => resolve(env('XLOG_TRACK_ID_KEY', 'xTrackId')), ], $statusCode); } - - /** - * @param int $code - * @param string|null $value - * @param array|null $result - * @param int $statusCode - * @return JsonResponse - */ public static function generalResponse( int $code = 0, string $value = null, @@ -245,21 +174,15 @@ public static function generalResponse( int $statusCode = Response::HTTP_OK, ): JsonResponse { return response()->json([ - "code" => $code, - "value" => $value, - "result" => $result, - "x_track_id" => resolve(env('XLOG_TRACK_ID_KEY', 'xTrackId')), + 'code' => $code, + 'value' => $value, + 'result' => $result, + 'x_track_id' => resolve(env('XLOG_TRACK_ID_KEY', 'xTrackId')), ], $statusCode); } - /** - * @param $view - * @param $withErrors - * @return Factory|View|Application - */ public static function generalViewErrorResponse($view, $withErrors): Factory|View|Application { return view($view)->withErrors([$withErrors]); } - } diff --git a/src/Provider/AsanPardakhtProvider.php b/src/Provider/AsanPardakhtProvider.php index ab123ae..e0d02d8 100644 --- a/src/Provider/AsanPardakhtProvider.php +++ b/src/Provider/AsanPardakhtProvider.php @@ -2,15 +2,16 @@ namespace PhpMonsters\LaraWallet\Provider; -use App\Ship\Enums\ErrorType; use App\Ship\Enums\LogType; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ServerException; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Storage; +use JsonException; use PhpMonsters\LaraWallet\Enums\AsanpardakhtStatusEnum; use PhpMonsters\LaraWallet\Exception; use PhpMonsters\Log\Facades\XLog; +use RuntimeException; /** * AsanPardakhtProvider class @@ -20,32 +21,29 @@ class AsanPardakhtProvider extends AbstractProvider public const POST_METHOD = 'POST'; protected bool $refundSupport = true; - protected array $parameters = []; + protected array $parameters = []; /** - * @return JsonResponse|array - * @throws Exception + * @throws Exception|JsonException */ public function checkWalletBalance(): JsonResponse|array { try { - $arrayData = [ - "caurl" => $this->getParameters('callback_url'), - "mo" => $this->getCellNumber(), - "hi" => $this->getParameters('host_id'), - "walet" => 5, - "htran" => random_int(5000, 50000) . time(), - "hop" => AsanpardakhtStatusEnum::WalletBalanceHop->value, - "htime" => time(), - "hkey" => $this->getParameters('api_key') - ]; - - $hostRequest = $this->prepareJsonString($arrayData); + $hostRequest = $this->prepareJsonString([ + 'caurl' => $this->getParameters('callback_url'), + 'mo' => $this->getCellNumber(), + 'hi' => $this->getParameters('host_id'), + 'walet' => 5, + 'htran' => random_int(5000, 50000).time(), + 'hop' => AsanpardakhtStatusEnum::WalletBalanceHop->value, + 'htime' => time(), + 'hkey' => $this->getParameters('api_key'), + ]); $hostRequestSign = $this->signRequest($hostRequest); $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = json_decode($rawResponse["hresp"], false, 512, JSON_THROW_ON_ERROR); + $responseJson = json_decode($rawResponse['hresp'], false, 512, JSON_THROW_ON_ERROR); if ($responseJson->st !== 1100) { $credit = 0; @@ -62,22 +60,26 @@ public function checkWalletBalance(): JsonResponse|array XLog::emergency('asan pardakht wallet service check balance failure', $rawResponse); - throw new \Exception(); + throw new RuntimeException(); } catch (ClientException|\Exception $exception) { $exceptionMessage = $this->getBalanceWalletError($exception); - throw new Exception((json_decode($exceptionMessage->content(), false))->message, - $exceptionMessage->statusCode); + throw new Exception( + (json_decode($exceptionMessage->content(), false, 512, JSON_THROW_ON_ERROR))->message, + $exceptionMessage->getStatusCode()); } } - /** - * @param string $input - * @return string + * @throws JsonException */ + public function prepareJsonString(array $data): string|array|false + { + return json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); + } + public function signRequest(string $input): string { - $binary_signature = ""; + $binary_signature = ''; openssl_sign( $input, @@ -86,38 +88,54 @@ public function signRequest(string $input): string OPENSSL_ALGO_SHA256 ); - return '1#1#' . base64_encode($binary_signature); + return '1#1#'.base64_encode($binary_signature); } + public function getBalanceWalletError(\Exception $exception): JsonResponse + { + if (method_exists($exception, 'getResponse') && ! empty($exception->getResponse())) { + $errorJson = json_decode($exception->getResponse()->getBody()->getContents()); + $errorMsg = $errorJson != null && property_exists( + $errorJson, + 'description' + ) ? $errorJson->description : $exception->getMessage(); + } else { + $errorMsg = $exception->getMessage(); + } + + XLog::emergency('wallet service check balance failure'.' '.' message: '.$errorMsg); + + return self::generalExceptionResponse( + AsanpardakhtStatusEnum::FailedResponse->value, + 'error in wallet service' + ); + } /** - * @return JsonResponse|array - * @throws \JsonException + * @throws JsonException */ public function payByWallet(): JsonResponse|array { $responseJson = ''; try { - $arrayData = [ - "caurl" => $this->getParameters('callback_url'), - "pid" => $this->hashParam($this->transaction->id), - "ao" => $this->getTransaction()->getPayableAmount(), - "mo" => $this->getCellNumber(), - "hi" => $this->getParameters('host_id'), - "walet" => 5, - "htran" => random_int(5000, 50000) . time(), - "hop" => AsanpardakhtStatusEnum::PayByWalletHop->value, - "htime" => time(), - "stime" => time(), - "hkey" => $this->getParameters('api_key') - ]; - - $hostRequest = $this->prepareJsonString($arrayData); + $hostRequest = $this->prepareJsonString([ + 'caurl' => $this->getParameters('callback_url'), + 'pid' => $this->hashParam($this->transaction->id), + 'ao' => $this->getTransaction()->getPayableAmount(), + 'mo' => $this->getCellNumber(), + 'hi' => $this->getParameters('host_id'), + 'walet' => 5, + 'htran' => $this->transaction->getWalletTransactionId(), + 'hop' => AsanpardakhtStatusEnum::PayByWalletHop->value, + 'htime' => time(), + 'stime' => time(), + 'hkey' => $this->getParameters('api_key'), + ]); $hRequestSign = $this->signRequest($hostRequest); $rawResponse = $this->sendInfoToAp($hostRequest, $hRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = $this->getHresponseData($rawResponse["hresp"]); + $responseJson = $this->getHresponseData($rawResponse['hresp']); if ($responseJson['st'] == AsanpardakhtStatusEnum::SuccessRequest->value) { $this->getTransaction()->setCallBackParameters($responseJson); @@ -151,32 +169,79 @@ public function payByWallet(): JsonResponse|array } /** - * @return JsonResponse|array - * @throws \JsonException + * @throws JsonException */ - public function walletCharge(): JsonResponse|array + public function getHresponseData($hresp): mixed { - $responseJson = ''; + return json_decode($hresp, true, 512, JSON_THROW_ON_ERROR); + } + + /** + * @throws JsonException + */ + public function reverseWalletPaymentResult(): mixed + { + $time = time(); + + $hostRequest = $this->prepareJsonString([ + 'caurl' => $this->getParameters('callback_url'), + 'ao' => $this->getTransaction()->getPayableAmount(), + 'mo' => $this->getCellNumber(), + 'hi' => $this->getParameters('host_id'), + 'walet' => 5, + 'htran' => $this->transaction->getWalletTransactionId(), + 'hop' => AsanpardakhtStatusEnum::ReverseRequestHop->value, + 'htime' => $time, + 'stime' => $time, + 'hkey' => $this->getParameters('api_key'), + ]); + + $hostRequestSign = $this->signRequest($hostRequest); try { - $arrayData = [ - "caurl" => $this->getTransaction()->callback_url, - "ao" => $this->getTransaction()->amount, - "mo" => $this->getCellNumber(), - "hi" => $this->getParameters('host_id'), - "walet" => 5, - "htran" => random_int(5000, 50000) . time(), - "hop" => AsanpardakhtStatusEnum::ChargeWallet->value, - "htime" => time(), - "stime" => time(), - "hkey" => $this->getParameters('api_key') - ]; + $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); + $responseJson = json_decode($rawResponse['hresp'], true, 512, JSON_THROW_ON_ERROR); - $hostRequest = $this->prepareJsonString($arrayData); + $result = $responseJson['st']; + + //----------------------------------successfully reversed------------------------------------- + + if ($responseJson['st'] === AsanpardakhtStatusEnum::SuccessRequest->value) { + $this->log('successfully reversed', [], LogType::INFO->value); + $this->getTransaction()->setCallBackParameters($responseJson); + + $result = self::generalResponse( + code: AsanpardakhtStatusEnum::SuccessResponse->value, + ); + } + } catch (ServerException $ex) { + $this->log($ex->getMessage(), [], 'error'); + $errorJson = json_decode($ex->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); + $result = [AsanpardakhtStatusEnum::FailedResponse->value, $errorJson]; + } + + return $result; + } + + public function walletCharge(): JsonResponse|array + { + try { + $hostRequest = $this->prepareJsonString([ + 'caurl' => $this->getTransaction()->callback_url, + 'ao' => $this->getTransaction()->amount, + 'mo' => $this->getCellNumber(), + 'hi' => $this->getParameters('host_id'), + 'walet' => 5, + 'htran' => $this->transaction->getWalletTransactionId(), + 'hop' => AsanpardakhtStatusEnum::ChargeWallet->value, + 'htime' => time(), + 'stime' => time(), + 'hkey' => $this->getParameters('api_key'), + ]); $hRequestSign = $this->signRequest($hostRequest); $rawResponse = $this->sendInfoToAp($hostRequest, $hRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = $this->getHresponseData($rawResponse["hresp"]); + $responseJson = $this->getHresponseData($rawResponse['hresp']); if ($responseJson['st'] == AsanpardakhtStatusEnum::SuccessRequest->value) { return self::generalResponse( @@ -184,6 +249,11 @@ public function walletCharge(): JsonResponse|array value: $responseJson['addData']['ipgURL'], ); } + + return [ + AsanpardakhtStatusEnum::FailedResponse->value, + '', + ]; } catch (\Exception $exception) { return self::generalExceptionResponse( AsanpardakhtStatusEnum::FailedResponse->value, @@ -192,44 +262,40 @@ public function walletCharge(): JsonResponse|array } } - /** - * @return mixed - * @throws \JsonException + * @throws JsonException */ public function verifyWalletPaymentResult(): mixed { $getCallbackParams = $this->getTransaction()->getCallbackParams(); - $arrayData = [ - "ao" => $getCallbackParams['ao'], - "hi" => $this->getParameters('host_id'), - "htran" => $getCallbackParams['htran'], - "hop" => AsanpardakhtStatusEnum::VerifyRequestHop->value, - "htime" => $getCallbackParams['htime'], - "stime" => time(), - "stkn" => $getCallbackParams['stkn'], - "hkey" => $this->getParameters('api_key') - ]; - - $hostRequest = $this->prepareJsonString($arrayData); + $hostRequest = $this->prepareJsonString([ + 'ao' => $getCallbackParams['ao'], + 'hi' => $this->getParameters('host_id'), + 'htran' => $getCallbackParams['htran'], + 'hop' => AsanpardakhtStatusEnum::VerifyRequestHop->value, + 'htime' => $getCallbackParams['htime'], + 'stime' => time(), + 'stkn' => $getCallbackParams['stkn'], + 'hkey' => $this->getParameters('api_key'), + ]); $hostRequestSign = $this->signRequest($hostRequest); try { $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = json_decode($rawResponse["hresp"]); + $responseJson = json_decode($rawResponse['hresp'], false, 512, JSON_THROW_ON_ERROR); $result = $responseJson->st; -//----------------------------------successfully verified------------------------------------- - if ($responseJson->st == AsanpardakhtStatusEnum::SuccessRequest->value or $responseJson->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenVerified->value) { + //----------------------------------successfully verified------------------------------------- + if ($responseJson->st == AsanpardakhtStatusEnum::SuccessRequest->value || $responseJson->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenVerified->value) { $result = self::generalResponse( code: AsanpardakhtStatusEnum::SuccessResponse->value, ); } } catch (ServerException $ex) { - $errorJson = json_decode($ex->getResponse()->getBody()->getContents()); + $errorJson = json_decode($ex->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); $result = self::generalExceptionResponse( AsanpardakhtStatusEnum::FailedResponse->value, @@ -240,24 +306,22 @@ public function verifyWalletPaymentResult(): mixed return $result; } - /** - * @return mixed - * @throws \JsonException + * @throws JsonException */ public function settleWalletPaymentResult(): mixed { $getCallbackParams = $this->getTransaction()->getCallbackParams(); $arrayData = [ - "ao" => $getCallbackParams['ao'], - "hi" => $this->getParameters('host_id'), - "htran" => $getCallbackParams['htran'], - "hop" => AsanpardakhtStatusEnum::SettleRequestHop->value, - "htime" => $getCallbackParams['htime'], - "stime" => time(), - "stkn" => $getCallbackParams['stkn'], - "hkey" => $this->getParameters('api_key') + 'ao' => $getCallbackParams['ao'], + 'hi' => $this->getParameters('host_id'), + 'htran' => $getCallbackParams['htran'], + 'hop' => AsanpardakhtStatusEnum::SettleRequestHop->value, + 'htime' => $getCallbackParams['htime'], + 'stime' => time(), + 'stkn' => $getCallbackParams['stkn'], + 'hkey' => $this->getParameters('api_key'), ]; $hostRequest = $this->prepareJsonString($arrayData); @@ -265,17 +329,17 @@ public function settleWalletPaymentResult(): mixed try { $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = json_decode($rawResponse["hresp"]); + $responseJson = json_decode($rawResponse['hresp']); $result = $responseJson->st; //---------------------Successfully verified-------------------------- - if ($responseJson->st == AsanpardakhtStatusEnum::SuccessResponse->value or $responseJson->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenSettled->value) { + if ($responseJson->st == AsanpardakhtStatusEnum::SuccessResponse->value || $responseJson->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenSettled->value) { $result = self::generalResponse( code: AsanpardakhtStatusEnum::SuccessResponse->value, ); } } catch (ServerException $ex) { - $errorJson = json_decode($ex->getResponse()->getBody()->getContents()); + $errorJson = json_decode($ex->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); $result = self::generalExceptionResponse( AsanpardakhtStatusEnum::FailedResponse->value, @@ -286,131 +350,29 @@ public function settleWalletPaymentResult(): mixed return $result; } - - /** - * @param \Exception $exception - * @return JsonResponse - */ - public function getBalanceWalletError(\Exception $exception): JsonResponse - { - if (method_exists($exception, 'getResponse') && !empty($exception->getResponse())) { - $errorJson = json_decode($exception->getResponse()->getBody()->getContents()); - $errorMsg = $errorJson != null && property_exists( - $errorJson, - 'description' - ) ? $errorJson->description : $exception->getMessage(); - } else { - $errorMsg = $exception->getMessage(); - } - - XLog::emergency('wallet service check balance failure' . ' ' . ' message: ' . $errorMsg); - - return self::generalExceptionResponse( - AsanpardakhtStatusEnum::FailedResponse->value, - 'error in wallet service' - ); - } - - - /** - * @param array $data - * @return string|array|false - * @throws \JsonException - */ - public function prepareJsonString(array $data): string|array|false - { - return json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); - } - - /** - * @param $hresp - * @return mixed - * @throws \JsonException - */ - public function getHresponseData($hresp): mixed - { - return json_decode($hresp, true, 512, JSON_THROW_ON_ERROR); - } - - - /** - * @return mixed - * @throws \JsonException - */ - public function reverseWalletPaymentResult(): mixed - { - $time = time(); - - $arrayData = [ - "caurl" => $this->getParameters('callback_url'), - "ao" => $this->getTransaction()->getPayableAmount(), - "mo" => $this->getCellNumber(), - "hi" => $this->getParameters('host_id'), - "walet" => 5, - "htran" => random_int(5000, 50000) . time(), - "hop" => AsanpardakhtStatusEnum::ReverseRequestHop->value, - "htime" => $time, - "stime" => $time, - "hkey" => $this->getParameters('api_key') - ]; - - $hostRequest = $this->prepareJsonString($arrayData); - - $hostRequestSign = $this->signRequest($hostRequest); - - try { - $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = json_decode($rawResponse["hresp"], true); - - $result = $responseJson['st']; - - //----------------------------------successfully reversed------------------------------------- - - if ($responseJson['st'] === AsanpardakhtStatusEnum::SuccessRequest->value) { - $this->log('successfully reversed', [], LogType::INFO->value); - $this->getTransaction()->setCallBackParameters($responseJson); - - $result = self::generalResponse( - code: AsanpardakhtStatusEnum::SuccessResponse->value, - ); - } - } catch (ServerException $ex) { - $this->log($ex->getMessage(), [], 'error'); - $errorJson = json_decode($ex->getResponse()->getBody()->getContents()); - $result = [AsanpardakhtStatusEnum::FailedResponse->value, $errorJson]; - } - - return $result; - } - - /** - * @return mixed - * @throws \JsonException + * @throws JsonException */ public function refundWalletPaymentResult(): mixed { - $time = time(); - - $arrayData = [ - "ao" => $this->getTransaction()->getPayableAmount(), - "mo" => $this->getCellNumber(), - "hi" => $this->getParameters('host_id'), - "walet" => 5, - "htran" => $this->getTransaction()->getCallbackParams()['htran'], - "hop" => AsanpardakhtStatusEnum::RefundRequestHop->value, - "htime" => $this->getTransaction()->getCallbackParams()['htime'], - "stime" => $time, - "stkn" => $this->getTransaction()->getCallbackParams()['stkn'], - "hkey" => $this->getParameters('api_key') - ]; - $hostRequest = $this->prepareJsonString($arrayData); + $hostRequest = $this->prepareJsonString([ + 'ao' => $this->getTransaction()->getPayableAmount(), + 'mo' => $this->getCellNumber(), + 'hi' => $this->getParameters('host_id'), + 'walet' => 5, + 'htran' => $this->getTransaction()->getCallbackParams()['htran'], + 'hop' => AsanpardakhtStatusEnum::RefundRequestHop->value, + 'htime' => $this->getTransaction()->getCallbackParams()['htime'], + 'stime' => time(), + 'stkn' => $this->getTransaction()->getCallbackParams()['stkn'], + 'hkey' => $this->getParameters('api_key'), + ]); $hostRequestSign = $this->signRequest($hostRequest); try { $rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl()); - $responseJson = json_decode($rawResponse["hresp"], true); + $responseJson = json_decode($rawResponse['hresp'], true); $result = $responseJson['st']; @@ -429,10 +391,10 @@ public function refundWalletPaymentResult(): mixed } } catch (ServerException $ex) { $this->log($ex->getMessage(), [], 'error'); - $errorJson = json_decode($ex->getResponse()->getBody()->getContents()); + $errorJson = json_decode($ex->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); $result = [ AsanpardakhtStatusEnum::FailedResponse->value, - $errorJson + $errorJson, ]; } diff --git a/src/WalletManager.php b/src/WalletManager.php index 1c393bd..8e73a8e 100644 --- a/src/WalletManager.php +++ b/src/WalletManager.php @@ -7,52 +7,31 @@ use InvalidArgumentException; use PhpMonsters\LaraWallet\Provider\AsanPardakhtProvider; -/** - * - */ -class WalletManager extends Manager/* implements Contracts\Factory*/ +class WalletManager extends Manager /* implements Contracts\Factory*/ { /** * runtime driver configuration - * - * @var array */ protected array $runtimeConfig; - /** - * @var string - */ protected string $environment; - /** - * @var string - */ protected string $cellNumber; - public $transaction; - - /** - * @param string $driver - * @param array $config - * @param $transaction - * @param string $mobileNumber - * @return mixed - */ public function with(string $driver, array $config, $transaction, string $mobileNumber): mixed { $this->transaction = $transaction; $this->cellNumber = $mobileNumber; - if (!empty($config)) { + if (! empty($config)) { $this->runtimeConfig = $config; } return $this->driver($driver); } - /** * @return AsanPardakhtProvider|mixed */ @@ -67,11 +46,7 @@ protected function createAsanPardakhtDriver(): mixed ); } - /** - * @param $provider - * @param array $config - * @param string $mobileNumber * @return mixed */ public function buildProvider($provider, array $config, string $mobileNumber) @@ -87,7 +62,6 @@ public function buildProvider($provider, array $config, string $mobileNumber) /** * Get the default driver name. * - * @return string * * @throws InvalidArgumentException */ @@ -98,10 +72,6 @@ public function getDefaultDriver(): string /** * get provider configuration runtime array or config based configuration - * - * @param string $driver - * - * @return array */ protected function getConfig(string $driver): array { @@ -112,14 +82,9 @@ protected function getConfig(string $driver): array return $this->runtimeConfig; } - /** - * @param string $message - * @param array $params - * @param string $level - */ public static function log(string $message, array $params = [], string $level = 'debug'): void { - $message = "WALLET -> " . $message; + $message = 'WALLET -> '.$message; forward_static_call(['PhpMonsters\Log\Facades\XLog', $level], $message, $params); }