diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index d3e3246..8f4642f 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -2,7 +2,11 @@ namespace App\Http\Controllers; +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 { @@ -13,25 +17,59 @@ class OnlinePaymentController extends Controller */ public function store(Request $request) { - return $request->all(); + // Validate the order id + $request->validate(['orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId']]); - // $response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [ - // 'language' => 'ru', - // 'orderId' => $request->orderId, - // 'userName' => 301161000067, - // 'password' => 'E3vb2SR3dgTPdff' - // ]); + // Find order from history + $paymentHistory = OnlinePaymentHistory::where('orderId', $request->orderId)->first(); - // if ($response['depositAmount'] > 0) { - // $payment_history = OnlinePaymentHistory::where('orderId', $request->orderId)->first(); + // Find related resource + $resource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id); - // $booking = Booking::where('id', $payment_history->online_paymantable_id)->update([ - // 'status' => Settings::PAID - // ]); + 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, + ], + ]); + } - // $payment_history->update([ - // 'paymentStatus' => Settings::PAID - // ]); - // } + $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['depositAmount'] > 0; + + if ($payment_status) { + $resource->update([ + 'paid' => true, + ]); + + $paymentHistory->update([ + 'paymentStatus' => OnlinePaymentRepo::PAID, + ]); + } else { + $paymentHistory->update([ + 'paymentStatus' => OnlinePaymentRepo::FAILED, + ]); + } + + return view(OnlinePaymentRepo::statusView(), [ + 'status' => $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(), + ]); } } diff --git a/app/Models/Order/Card/CardOrder.php b/app/Models/Order/Card/CardOrder.php index 38dc4f7..cc66b8b 100644 --- a/app/Models/Order/Card/CardOrder.php +++ b/app/Models/Order/Card/CardOrder.php @@ -120,4 +120,14 @@ class CardOrder extends Model return 1; // return floatval($this->cardType->price); } + + /** + * Panel url + */ + public function panelUrl(string $type = 'index'): string + { + return match ($type) { + 'index' => sprintf('resources/card-orders'), + }; + } } diff --git a/app/Repos/Payment/OnlinePaymentRepo.php b/app/Repos/Payment/OnlinePaymentRepo.php index 107fe1c..ea0071c 100644 --- a/app/Repos/Payment/OnlinePaymentRepo.php +++ b/app/Repos/Payment/OnlinePaymentRepo.php @@ -4,13 +4,26 @@ namespace App\Repos\Payment; use App\Models\Payment\ApiKeyHalkbank; use App\Models\Payment\OnlinePaymentHistory; -use App\Repos\Order\OrderRepo; -use Exception; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class OnlinePaymentRepo { + /** + * Pending orders are brand new orders that have not been processed yet. + */ + public const PENDING = 'pending'; + + /** + * Failed orders are existing orders that could be paid + */ + public const FAILED = 'failed'; + + /** + * Paid orders are existing order that could be paid successfully + */ + public const PAID = 'paid'; + /** * Pay card order * @@ -58,7 +71,7 @@ class OnlinePaymentRepo 'errorUrl' => route('online-payment-store'), 'api_client' => config('app.url'), 'username' => $resource->branch->billing_username, - 'paymentStatus' => OrderRepo::PENDING, + 'paymentStatus' => self::PENDING, ]); return [ @@ -74,4 +87,12 @@ class OnlinePaymentRepo { return ApiKeyHalkbank::generateOrderNumber($resource); } + + /** + * Status view + */ + public static function statusView(): string + { + return 'orders.cards.online-payment.status'; + } } diff --git a/config/logging.php b/config/logging.php index 5421fe9..b45aaed 100644 --- a/config/logging.php +++ b/config/logging.php @@ -133,6 +133,13 @@ return [ 'level' => env('LOG_LEVEL', 'error'), 'replace_placeholders' => true, ], + + 'halkbank_payment_check_error' => [ + 'driver' => 'single', + 'path' => storage_path('logs/halkbank_payment_check_error.log'), + 'level' => env('LOG_LEVEL', 'error'), + 'replace_placeholders' => true, + ], ], ]; diff --git a/database/migrations/2024_01_28_210813_make_default_false_for_paid_on_card_orders_table.php b/database/migrations/2024_01_28_210813_make_default_false_for_paid_on_card_orders_table.php new file mode 100644 index 0000000..2880027 --- /dev/null +++ b/database/migrations/2024_01_28_210813_make_default_false_for_paid_on_card_orders_table.php @@ -0,0 +1,28 @@ +boolean('paid')->default(false)->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('card_orders', function (Blueprint $table) { + $table->boolean('paid')->default(true)->change(); + }); + } +}; diff --git a/lang/tk.json b/lang/tk.json index edd5036..55600c7 100644 --- a/lang/tk.json +++ b/lang/tk.json @@ -254,5 +254,8 @@ "I accept terms of contract": "Şertnama bilen razylaşýaryn", "Click to read": "Okamak üçin bas", "Agree": "Razylaşýaryn", - "I have read the contract": "Şertnama bilen tanyşdym" + "I have read the contract": "Şertnama bilen tanyşdym", + "Go to home": "Baş sahypa git", + "Payment is successful": "Töleg geçdi", + "Payment has failed": "Töleg geçmedi" } diff --git a/resources/views/orders/cards/online-payment/status.blade.php b/resources/views/orders/cards/online-payment/status.blade.php new file mode 100644 index 0000000..816b314 --- /dev/null +++ b/resources/views/orders/cards/online-payment/status.blade.php @@ -0,0 +1,36 @@ + + + + + + P + + + +
+
+
+
+ + + + +

+ {{ $title }} +

+ PNR: {{ $pnr }} +
+
+
+ {{ $branch_name }} + {{ $price_amount }} +
+ {{ __('Go to home') }} +
+
+
+
+ + diff --git a/routes/web.php b/routes/web.php index cd685ce..a0430d7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -33,6 +33,8 @@ Route::middleware(['auth', 'un_verified'])->group(function () { Route::post('sms-verification', [RegisterController::class, 'verifySmsCode']); }); +Route::view('test', 'orders.cards.online-payment.status'); + Route::get('online-payment-store', [OnlinePaymentController::class, 'store'])->name('online-payment-store'); Route::redirect('/', config('nova.path'));