diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index 9ccc7e7..e6a1d5b 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -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) diff --git a/app/Http/Requests/OnlinePaymentStoreRequest.php b/app/Http/Requests/OnlinePaymentStoreRequest.php new file mode 100644 index 0000000..12eb69f --- /dev/null +++ b/app/Http/Requests/OnlinePaymentStoreRequest.php @@ -0,0 +1,20 @@ +|string> + */ + public function rules(): array + { + return [ + 'orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId'], + ]; + } +} diff --git a/app/Modules/VisaMasterPaymentOrder/Models/VisaMasterPaymentOrder.php b/app/Modules/VisaMasterPaymentOrder/Models/VisaMasterPaymentOrder.php index 05a3aa6..905ffef 100644 --- a/app/Modules/VisaMasterPaymentOrder/Models/VisaMasterPaymentOrder.php +++ b/app/Modules/VisaMasterPaymentOrder/Models/VisaMasterPaymentOrder.php @@ -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 diff --git a/app/Repos/Payment/OnlinePaymentRepo.php b/app/Repos/Payment/OnlinePaymentRepo.php index be2fd5b..81a13a5 100644 --- a/app/Repos/Payment/OnlinePaymentRepo.php +++ b/app/Repos/Payment/OnlinePaymentRepo.php @@ -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('/'), + ]; + } } diff --git a/database/migrations/2024_11_23_104500_add_soft_deletes_to_visa_master_payment_orders_table.php b/database/migrations/2024_11_23_104500_add_soft_deletes_to_visa_master_payment_orders_table.php new file mode 100644 index 0000000..fbfea6d --- /dev/null +++ b/database/migrations/2024_11_23_104500_add_soft_deletes_to_visa_master_payment_orders_table.php @@ -0,0 +1,28 @@ +softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('visa_master_payment_orders', function (Blueprint $table) { + $table->dropSoftDeletes(); + }); + } +};