diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index 317bcc7..9ccc7e7 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -46,8 +46,8 @@ class OnlinePaymentController extends Controller $response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [ 'language' => 'ru', 'orderId' => $request->orderId, - 'userName' => $resource->branch->billing_visa_master_username, - 'password' => $resource->branch->billing_visa_master_password, + 'userName' => $resource->branch->billing_username, + 'password' => $resource->branch->billing_password, ]); $payment_status = $response['ErrorCode'] == '0'; @@ -112,8 +112,74 @@ class OnlinePaymentController extends Controller $response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [ 'language' => 'ru', 'orderId' => $request->orderId, - 'userName' => $resource->branch->billing_username, - 'password' => $resource->branch->billing_password, + 'userName' => $resource->branch->billing_visa_master_username, + 'password' => $resource->branch->billing_visa_master_password, + ]); + + $payment_status = $response['ErrorCode'] == '0'; + + if ($payment_status) { + $resource->update([ + 'paid' => true, + ]); + + $paymentHistory->update([ + 'paymentStatus' => OnlinePaymentRepo::PAID, + ]); + } else { + $paymentHistory->update([ + 'paymentStatus' => OnlinePaymentRepo::FAILED, + ]); + } + + return view(OnlinePaymentRepo::statusView(), [ + 'success' => $payment_status, + 'title' => $payment_status ? __('Payment is successful') : __('Payment has failed'), + 'pnr' => $paymentHistory->orderNumber, + 'branch_name' => $resource->branch->name, + 'price_amount' => $paymentHistory->amount, + 'return_url' => $resource->panelUrl('index'), + ]); + } + + public function sber(Request $request) + { + // Validate the order id + if ( + validator( + $request->all(), + ['orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId']] + )->fails() + ) { + return ['wrong order id']; + } + + // Find order from history + $paymentHistory = OnlinePaymentHistory::where('orderId', $request->orderId)->first(); + + // Find related resource + $resource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id); + + // If resource could not be found or does not exist, then inform it via logs + if (! $resource) { + Log::channel('halkbank_payment_check_error') + ->error('Related resource not found', [ + 'orderId' => $request->orderId, + 'onlinePaymentHistory_id' => $paymentHistory->id, + 'related_resource' => [ + 'type' => $paymentHistory->online_paymantable_type, + 'id' => $paymentHistory->online_paymantable_id, + ], + ]); + } + + $resource->load('branch'); + + $response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [ + 'language' => 'ru', + 'orderId' => $request->orderId, + 'userName' => $resource->branch->billing_sber_username, + 'password' => $resource->branch->billing_sber_password, ]); $payment_status = $response['ErrorCode'] == '0'; diff --git a/app/Modules/SberPaymentOrder/Nova/Resources/Concerns/SberPaymentOrderFieldsForDetail.php b/app/Modules/SberPaymentOrder/Nova/Resources/Concerns/SberPaymentOrderFieldsForDetail.php index 708ce49..e3e977c 100644 --- a/app/Modules/SberPaymentOrder/Nova/Resources/Concerns/SberPaymentOrderFieldsForDetail.php +++ b/app/Modules/SberPaymentOrder/Nova/Resources/Concerns/SberPaymentOrderFieldsForDetail.php @@ -2,7 +2,6 @@ namespace App\Modules\SberPaymentOrder\Nova\Resources\Concerns; -use App\Modules\SberPaymentOrder\Nova\Resources\SberPaymentOrderFileFields; use App\Nova\Resources\Branch\Branch; use App\Repos\Order\OrderRepo; use App\Repos\System\Settings\Legal\PassportRepo; diff --git a/app/Modules/SberPaymentOrder/Nova/Resources/NovaSberPaymentOrder.php b/app/Modules/SberPaymentOrder/Nova/Resources/NovaSberPaymentOrder.php index 6d174be..43ce448 100644 --- a/app/Modules/SberPaymentOrder/Nova/Resources/NovaSberPaymentOrder.php +++ b/app/Modules/SberPaymentOrder/Nova/Resources/NovaSberPaymentOrder.php @@ -5,7 +5,7 @@ namespace App\Modules\SberPaymentOrder\Nova\Resources; use App\Models\Branch\Branch; use App\Modules\SberPaymentOrder\Nova\Resources\Concerns\SberPaymentOrderFieldsForDetail; use App\Modules\SberPaymentOrder\Nova\Resources\Concerns\SberPaymentOrderFieldsForIndex; -use App\Nova\Actions\MakePaymentNovaVisaMaster; +use App\Nova\Actions\MakeSberPaymentAction; use App\Nova\Resource; use App\Repos\Order\Card\CardOrderRepo; use App\Repos\Order\OrderRepo; @@ -320,14 +320,14 @@ class NovaSberPaymentOrder extends Resource /** * Actions * - * @param NovaRequest $request + * @param NovaRequest $request */ public function actions(NovaRequest $request): array { return [ - MakePaymentNovaVisaMaster::make() - ->icon('credit-card') - ->sole() + MakeSberPaymentAction::make() + ->icon('credit-card') + ->sole(), ]; } } diff --git a/app/Nova/Actions/MakeSberPaymentAction.php b/app/Nova/Actions/MakeSberPaymentAction.php index 046560b..33f77c9 100644 --- a/app/Nova/Actions/MakeSberPaymentAction.php +++ b/app/Nova/Actions/MakeSberPaymentAction.php @@ -3,12 +3,17 @@ namespace App\Nova\Actions; use App\Models\CurrencyRate; +use App\Models\Payment\OnlinePaymentHistory; +use App\Modules\SberPaymentOrder\Models\SberPaymentOrder; +use App\Repos\Payment\OnlinePaymentRepo; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Blade; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Laravel\Nova\Actions\Action; +use Laravel\Nova\Actions\ActionResponse; use Laravel\Nova\Fields\ActionFields; use Laravel\Nova\Fields\Heading; use Laravel\Nova\Fields\Text; @@ -27,7 +32,27 @@ class MakeSberPaymentAction extends Action */ public function handle(ActionFields $fields, Collection $models) { - // + $usd_to_tmt = CurrencyRate::where('currency_from', 'USD')->where('currency_to', 'TMT')->first('value'); + + if (! $usd_to_tmt) { + return ActionResponse::danger('Walýuta hasaby girizilmedik, operator bilen habarlaşmagyňyzy haýyş edýärin.'); + } + + $resource = $models->first(); + + if (! $resource->branch || ! $resource->branch->billing_visa_master_username) { + return ActionResponse::danger('Şahamça visa/master tölegi kabul edip bilmeýär.'); + } + + $tvebTaxTMT = $usd_to_tmt->value * 18; + $bankTax = 120.75; + $total_amount = number_format($fields->payment_amount + $tvebTaxTMT + $bankTax, 2, '.', ''); + + $payment = $this->order($resource, $total_amount); + + return $payment['status'] === 'success' + ? ActionResponse::openInNewTab($payment['url']) + : ActionResponse::danger('Töleg ýerinde näsazlyk!'); } /** @@ -44,13 +69,19 @@ class MakeSberPaymentAction extends Action return []; } - $max_value = number_format($usd_to_tmt->value * 250, 2); + $max_value = number_format(90.6 * 250 * 0.038611, 2, '.', ''); + $tvebTaxTMT = $usd_to_tmt->value * 18; + $bankTax = 120.75; return [ Heading::make(Blade::render(<<1 USD = $usd_to_tmt->value TMT +