lets check online payment

This commit is contained in:
2024-11-23 11:01:17 +05:00
parent 61b6634fce
commit 134fb8507e
5 changed files with 131 additions and 62 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\OnlinePaymentStoreRequest;
use App\Models\Payment\OnlinePaymentHistory;
use App\Repos\Payment\OnlinePaymentRepo;
use Illuminate\Http\Request;
@@ -76,70 +77,11 @@ class OnlinePaymentController extends Controller
]);
}
public function visaMaster(Request $request)
public function visaMaster(OnlinePaymentStoreRequest $request)
{
// Validate the order id
if (
validator(
$request->all(),
['orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId']]
)->fails()
) {
return ['wrong order id'];
}
$data = OnlinePaymentRepo::checkPayment($request);
// 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_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'),
]);
return view(OnlinePaymentRepo::statusView(), $data);
}
public function sber(Request $request)

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class OnlinePaymentStoreRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId'],
];
}
}

View File

@@ -9,6 +9,7 @@ use App\Repos\Order\OrderRepo;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
@@ -16,6 +17,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media;
class VisaMasterPaymentOrder extends Model implements HasMedia
{
use InteractsWithMedia;
use SoftDeletes;
/**
* Table

View File

@@ -3,6 +3,8 @@
namespace App\Repos\Payment;
use App\Models\Payment\OnlinePaymentHistory;
use App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrderItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Laravel\Nova\Makeable;
@@ -153,4 +155,79 @@ class OnlinePaymentRepo
{
return 'orders.cards.online-payment.status';
}
public static function checkPayment(Request $request)
{
// Find order from history
$paymentHistory = OnlinePaymentHistory::where('orderId', $request->orderId)->first();
// Find related resource
$resource = VisaMasterPaymentOrderItem::where('online_payment_history_id', $paymentHistory->id)->first();
// If resource could not be found or does not exist, then inform it via logs
if (! $resource) {
static::logResourceNotFound($request, $paymentHistory);
return static::resourceNotFound();
}
$resource->load('branch');
$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,
]);
$payment_status = $response['ErrorCode'] == '0';
if ($payment_status) {
$resource->update([
'paid' => true,
]);
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepo::PAID,
]);
} else {
$paymentHistory->update([
'paymentStatus' => OnlinePaymentRepo::FAILED,
]);
}
return [
'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'),
];
}
protected static function logResourceNotFound($request, $paymentHistory): void
{
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,
],
]);
}
protected static function resourceNotFound(): array
{
return [
'success' => false,
'title' => __('Payment has failed').' '.'(RESOURCE NOT FOUND)',
'pnr' => '',
'branch_name' => '',
'price_amount' => '',
'return_url' => url('/'),
];
}
}