Skip to content

Commit ed8fe1d

Browse files
committed
fix pay now option
1 parent 3ab8aaf commit ed8fe1d

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

app/Http/Controllers/PaymentController.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ public function index(): View
2121
{
2222
return view('frontend.payment.index');
2323
}
24-
public function create(string $model, string $id): View
24+
public function create(string $model, string $id, $purchase_id = null): View
2525
{
2626
$table = str(str($model)->snake())->plural();
2727
$record = DB::table($table)->find($id);
2828
$incomeSources = IncomeSource::latest()->get();
2929
$paymentMethods = Setting::select('payment')->first()->pluck('payment')[ 0 ];
30-
return view('frontend.pages.payment.create', compact('model', 'id', 'record', 'incomeSources', 'paymentMethods'));
30+
$purchase = Purchase::find($purchase_id);
31+
return view('frontend.pages.payment.create', compact('model', 'id', 'record', 'incomeSources', 'paymentMethods', 'purchase'));
3132
}
32-
public function store(Request $request)
33+
public function store(Request $request, $purchase_id = null)
3334
{
3435
$request->validate([
3536
'purchasable_type' => 'required',
@@ -125,7 +126,10 @@ public function store(Request $request)
125126
$status = 'partial';
126127
}
127128

128-
Purchase::create([
129+
Purchase::updateOrCreate([
130+
'id' => $request->purchase_id
131+
],
132+
[
129133
'user_id' => auth()->id(),
130134
'name' => $request->name,
131135
'promo_code_id' => $promoCode->id ?? null,

app/Providers/RouteServiceProvider.php

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use App\Models\Purchase;
56
use Illuminate\Http\Request;
67
use Illuminate\Support\Facades\Route;
78
use Illuminate\Cache\RateLimiting\Limit;
@@ -41,6 +42,16 @@ public function boot(): void
4142
Route::middleware('web')
4243
->group(base_path('routes/web.php'));
4344
});
45+
46+
Route::bind('purchase_id', function (string $value) {
47+
$purchaseId = $value;
48+
try{
49+
$purchaseId = decrypt($value);
50+
}catch(\Exception $e){
51+
$purchaseId = $value;
52+
}
53+
return $purchaseId;
54+
});
4455
}
4556

4657
/**

resources/views/frontend/pages/payment/create.blade.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</style>
3434
@endPushOnce
3535
@section('main')
36-
<form action="{{ route('payment.store') }}" method="post" class="my-3">
36+
<form action="{{ route('payment.store', ['purchase_id' => encrypt($purchase?->id)]) }}" method="post" class="my-3">
3737
@csrf
3838

3939
@if ($model === Product::class)
@@ -143,7 +143,7 @@ class="badge bg-soft-success text-success mb-0">{{ $model }}</span>
143143
<div class="row my-2">
144144
<label for="amoutn-to-pay" class="d-block fs-3 fw-medium">Amount To Pay</label>
145145
<input id="amount-to-pay" name="paid_amount" type="text"
146-
value="{{ $record->price }}"
146+
value="{{ $purchase ? $purchase->due : $record->price }}"
147147
class=" text-center border-success rounded-3 fs-2 fw-bold py-5">
148148
</div>
149149

@@ -154,7 +154,8 @@ class=" text-center border-success rounded-3 fs-2 fw-bold py-5">
154154
<div class="row">
155155
<div class="col-12">
156156
@if (auth()->user() === null)
157-
<h6 class="text-success text-center fw-bold fs-5">Please create an account to
157+
<h6 class="text-success text-center fw-bold fs-5">Please create an
158+
account to
158159
perform this action</h6>
159160
@endif
160161
</div>

resources/views/frontend/pages/purchasesItems/purchases.blade.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,20 @@
1313
@forelse ($purchaseItem as $item)
1414
<div class="h-100" style="width: max-content;">
1515
<div class="w-100 h-100 rounded-2 h-100" style="overflow: hidden;">
16-
<div class="align-items-center border p-3">
16+
<div class="align-items-center border p-3 h-100">
1717
<div class="row gap-4 align-items-center justify-content-center mb-3">
18-
<div style="width: max-content;">
18+
<div
19+
class="d-flex {{ $item->due >= 1 ? 'justify-content-between gap-2' : 'justify-content-center' }} align-items-center">
1920
<div
20-
class="ms-auto fw-bold mb-2 text-center {{ $item->approved == 1 ? 'text-success' : 'text-danger' }}">
21-
{{ $item->approved == 1 ? 'Approved.' : 'Waiting for Approval.' }}
21+
class="py-2 fw-bold text-center {{ $item->approved == 1 ? 'text-success' : 'text-danger' }}">
22+
{{ $item->approved == 1 ? 'Approved' : ($item->due >= 1 ? 'Waiting for Payment' : 'Waiting for Approval') }}
2223
</div>
23-
@if ($item->due > 0)
24-
<form action="{{ route('payment.pay.now', $item) }}" method="post"
25-
class="d-flex gap-4 align-items-center">
26-
@csrf
27-
<input class="form-control px-3 py-2 border-focus-2" style="width: 150px;"
28-
name="trx_id" label="Trx ID" placeholder="Trx ID" required />
29-
@error('trx_id')
30-
<div class="text-danger">{{ $message }}</div>
31-
@enderror
32-
<button class="btn btn-primary text-white" style="font-weight: 500;">Pay
33-
Now</button>
34-
</form>
24+
@if ($item->due >= 1)
25+
<a href="{{ route('payment.create', ['model' => $item->purchasable_type, 'id' => $item->purchasable_id, 'purchase_id' => encrypt($item->id)]) }}"
26+
class="btn btn-primary text-white" style="font-weight: 500;">Pay
27+
Now</a>
3528
@endif
29+
3630
</div>
3731
</div>
3832
<div class="text-muted text-start">

routes/frontend.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@
173173
->name('payment.')
174174
->controller(PaymentController::class)
175175
->group(function () {
176-
Route::get('create/{model}/{id}', 'create')->name('create');
177-
Route::post('store/', 'store')->name('store');
176+
Route::get('create/{model}/{id}/{purchase_id?}', 'create')->name('create');
177+
Route::post('store/{purchase_id?}', 'store')->name('store');
178178
Route::post('later/', 'later')->name('later');
179179
Route::get('success/{model}/{id}', 'success')->name('success');
180180
Route::get('cancel', 'cancel')->name('cancel');
181-
Route::post('pay-now/{purchase}', 'payNow')->name('pay.now');
181+
// Route::post('pay-now/{purchase_id?}', 'payNow')->name('pay.now');
182182
});
183183

184184

0 commit comments

Comments
 (0)