diff --git a/app/Helpers/helpers.php b/app/Helpers/helpers.php index 03383c8..5e2e8a3 100644 --- a/app/Helpers/helpers.php +++ b/app/Helpers/helpers.php @@ -4,6 +4,7 @@ use App\Events\EventType; use App\Models\Payment\OnlinePaymentHistory; use App\Models\System\Roles\Permission; use App\Models\System\Verification; +use App\Modules\SberPaymentOrder\Models\SberPaymentOrderItem; use App\Nova\Resources\Order\Card\CardTransaction\Actions\DownloadCardTransaction; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request as GuzzleRequest; @@ -401,7 +402,7 @@ function emptyClass(...$arguments): object * @param string $orderId * @param int|string $username * @param string $password - * + * * @return mixed */ function checkOnlinePayment(string $orderId, int|string $username, string $password): mixed @@ -426,19 +427,18 @@ function checkOnlinePayment(string $orderId, int|string $username, string $passw * @param string $online_payment_auth_ref_num * @param string $online_payment_tmt_amount * @param string $pay_purpose - * + * * @return mixed */ function syncWithBankSystem( - $online_payment_order_uuid, - $bank_unique_code, - $online_payment_terminal_id, - $user_deposit_account, - $online_payment_auth_ref_num, - $online_payment_tmt_amount, + $online_payment_order_uuid, + $bank_unique_code, + $online_payment_terminal_id, + $user_deposit_account, + $online_payment_auth_ref_num, + $online_payment_tmt_amount, $pay_purpose -): mixed -{ +): mixed { $ecomId = $online_payment_order_uuid; $agentId = $bank_unique_code; $eposId = $online_payment_terminal_id; @@ -479,3 +479,43 @@ function syncWithBankSystem( return $response; } + +/** + * Get Sber credentials for a given payment item. + * + * @param SberPaymentOrderItem $item + * @return array{username?: string, password?: string, onlinePaymentResource?: OnlinePaymentHistory, error?: string, type?: 'danger'|'modal'} + */ +function getSberCredentials(SberPaymentOrderItem $item): array +{ + /** @var \App\Models\Payment\OnlinePaymentHistory|null $onlinePaymentResource */ + $onlinePaymentResource = OnlinePaymentHistory::find($item->online_payment_history_id); + + if (! $onlinePaymentResource) { + return ['error' => 'Online payment resource tapylmady', 'type' => 'danger']; + } + + $relatedResource = (new $onlinePaymentResource->online_paymantable_type) + ->find(id: $onlinePaymentResource->online_paymantable_id); + + if (! $relatedResource) { + return ['error' => 'Bu resource tapylmady', 'type' => 'danger']; + } + + if (! $relatedResource->branch) { + return ['error' => 'Şahamça bilen birikdirilmedik', 'type' => 'danger']; + } + + $username = $relatedResource->branch->billing_sber_username; + $password = $relatedResource->branch->billing_sber_password; + + if (empty($username) || empty($password)) { + return ['error' => 'Ulanyjy ady bilen açar sözi gabat gelmedi', 'type' => 'modal']; + } + + return [ + 'username' => $username, + 'password' => $password, + 'onlinePaymentResource' => $onlinePaymentResource, + ]; +} diff --git a/app/Nova/Actions/Sber/SyncWithSystem.php b/app/Nova/Actions/Sber/SyncWithSystem.php index a21ead1..3acb6d0 100644 --- a/app/Nova/Actions/Sber/SyncWithSystem.php +++ b/app/Nova/Actions/Sber/SyncWithSystem.php @@ -2,7 +2,6 @@ namespace App\Nova\Actions\Sber; -use App\Models\Payment\OnlinePaymentHistory; use App\Nova\Actions\CheckOnlinePayment; use Illuminate\Bus\Queueable; use Illuminate\Queue\InteractsWithQueue; @@ -36,35 +35,16 @@ class SyncWithSystem extends Action /** @var \App\Modules\SberPaymentOrder\Models\SberPaymentOrderItem $item */ $item = $models->first(); + $result = getSberCredentials($item); + + if (isset($result['error'])) { + $this->handleError($result); + } + /** @var \App\Models\Payment\OnlinePaymentHistory $onlinePaymentResource */ - $onlinePaymentResource = OnlinePaymentHistory::find($item->online_payment_history_id); + $onlinePaymentResource = $result['onlinePaymentResource']; - if (! $onlinePaymentResource) { - return ActionResponse::danger('Online payment resource tapylmady'); - } - - $relatedResource = (new $onlinePaymentResource->online_paymantable_type) - ->find(id: $onlinePaymentResource->online_paymantable_id); - - if (! $relatedResource) { - return ActionResponse::danger('Bu resource tapylmady'); - } - - if (! $relatedResource->branch) { - return ActionResponse::danger('Şahamça bilen birikdirilmedik'); - } - - $username = $relatedResource->branch->billing_sber_username; - $password = $relatedResource->branch->billing_sber_password; - - if ($username == '') { - return Action::modal('modal-response', [ - 'title' => 'HALKBANK API', - 'body' => 'Ulanyjy ady bilen açar sözi gabat gelmedi', - ]); - } - - $response = checkOnlinePayment($onlinePaymentResource->orderId, $username, $password); + $response = checkOnlinePayment($onlinePaymentResource->orderId, $result['username'], $result['password']); // syncWithBankSystem(); @@ -84,4 +64,21 @@ class SyncWithSystem extends Action { return []; } + + /** + * Handle error + * + * @param array $result + */ + private function handleError(array $result): mixed + { + if ($result['type'] === 'modal') { + return Action::modal('modal-response', [ + 'title' => 'HALKBANK API', + 'body' => $result['error'], + ]); + } + + return ActionResponse::danger($result['error']); + } }