153 lines
5.3 KiB
PHP
153 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\OnlinePaymentStoreRequest;
|
|
use App\Models\Payment\OnlinePaymentHistory;
|
|
use App\Repos\Payment\OnlinePaymentRepo;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class OnlinePaymentController extends Controller
|
|
{
|
|
/**
|
|
* Online payment
|
|
*
|
|
* @param Request $request
|
|
*/
|
|
public function store(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_username,
|
|
'password' => $resource->branch->billing_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 visaMaster(OnlinePaymentStoreRequest $request)
|
|
{
|
|
$data = OnlinePaymentRepo::checkPaymentVisaMaster($request);
|
|
|
|
return view(OnlinePaymentRepo::statusView(), $data);
|
|
}
|
|
|
|
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';
|
|
|
|
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'),
|
|
]);
|
|
}
|
|
}
|