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 +

1 RUB = 0.038611 TMT

+

TVEB USD tutumy: 18 USD

-

GBÜS tutumy: 3 TMT

+

TVEB tutumy TMT = $tvebTaxTMT TMT

+
+

Bank tutumy: $bankTax TMT

HTML))->asHtml(), Text::make(sprintf('%s (%s)', __('Töleg möçberi'), __('TMT')), 'payment_amount') @@ -62,9 +93,11 @@ class MakeSberPaymentAction extends Action Text::make(__('USD ekwalendi'), 'usd_rate') ->fullWidth() ->readonly() - ->dependsOn('payment_amount', function ($field, $request, $formData) use ($usd_to_tmt) { + ->dependsOn('payment_amount', function ($field, $request, $formData) { if ($formData->payment_amount) { - $field->setValue(number_format($formData->payment_amount / $usd_to_tmt->value, 2, '.', '')); + $usdValue = number_format($formData->payment_amount / (90.6 * 0.038611), 2, '.', ''); + + $field->setValue($usdValue); } else { $field->setValue(''); } @@ -73,10 +106,10 @@ class MakeSberPaymentAction extends Action Text::make(__('Jemi (TMT)'), 'total_amount') ->fullWidth() ->readonly() - ->dependsOn('payment_amount', function ($field, $request, $formData) { + ->dependsOn('payment_amount', function ($field, $request, $formData) use ($tvebTaxTMT, $bankTax) { if (is_numeric($formData->payment_amount)) { $field->setValue( - floatval(number_format($formData->payment_amount, 2, '.', '')) + 23 + number_format($formData->payment_amount + $tvebTaxTMT + $bankTax, 2, '.', '') ); } else { $field->setValue(''); @@ -84,4 +117,60 @@ class MakeSberPaymentAction extends Action }), ]; } + + /** + * Order a payment page + */ + public function order($resource, $amount) + { + $onlinePaymentRepo = OnlinePaymentRepo::make(); + + $orderNumber = $onlinePaymentRepo->generateOrderNumber($resource); + + $paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [ + 'orderNumber' => $orderNumber, + 'amount' => number_format($amount, 2, '', ''), + 'currency' => 934, + 'language' => 'ru', + 'userName' => $resource->branch->billing_sber_username, + 'password' => $resource->branch->billing_visa_master_password, + 'returnUrl' => route('online-payment-store-sber'), + 'pageView' => 'DESKTOP', + 'description' => 'Sber tölegi', + ])->onError(function ($response) { + Log::channel('halkbank_payment_error') + ->error('Payment error', [ + 'response' => [ + 'body' => $response->body(), + ], + ]); + }); + + if ($paymentResponse->failed()) { + return [ + 'status' => 'failed', + 'url' => '', + ]; + } + + OnlinePaymentHistory::create([ + 'online_paymantable_id' => $resource->id, + 'online_paymantable_type' => SberPaymentOrder::class, + 'amount' => number_format($amount, 2, '', ''), + 'orderNumber' => $orderNumber, + 'description' => 'Sber tölegi', + 'orderId' => $paymentResponse['orderId'], + 'formUrl' => $paymentResponse['formUrl'], + 'successUrl' => route('online-payment-store-sber'), + 'errorUrl' => route('online-payment-store-sber'), + 'api_client' => 'billing_sber_username', + 'username' => $resource->branch->billing_sber_username, + 'paymentStatus' => OnlinePaymentRepo::PENDING, + ]); + + return [ + 'status' => 'success', + 'url' => $paymentResponse['formUrl'], + ]; + } } diff --git a/routes/web.php b/routes/web.php index e0b7323..089b8a4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -38,5 +38,6 @@ Route::middleware(['auth', 'unVerified'])->group(function () { Route::get('online-payment-store', [OnlinePaymentController::class, 'store'])->name('online-payment-store'); Route::get('online-payment-store-visa-master', [OnlinePaymentController::class, 'visaMaster'])->name('online-payment-store-visa-master'); +Route::get('online-payment-store-sber', [OnlinePaymentController::class, 'sber'])->name('online-payment-store-sber'); Route::redirect('/', config('nova.path'));