Skip to content

Commit 13d9188

Browse files
committed
Add payment page options
1 parent 3b08b13 commit 13d9188

File tree

9 files changed

+174
-37
lines changed

9 files changed

+174
-37
lines changed

app/Actions/CreatePaymentPage.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use App\Billing\PaytabsBilling;
6+
use Devinweb\LaravelPaytabs\Enums\TransactionClass;
7+
use Devinweb\LaravelPaytabs\Enums\TransactionType;
8+
use Devinweb\LaravelPaytabs\Facades\LaravelPaytabsFacade;
9+
use Illuminate\Support\Facades\Auth;
10+
use Illuminate\Http\Request;
11+
12+
class CreatePaymentPage
13+
{
14+
public function __invoke(Request $request)
15+
{
16+
$cart = app(PrepareCartData::class)();
17+
$transaction = LaravelPaytabsFacade::setCustomer(Auth::user())
18+
->setCart($cart);
19+
20+
if ($request->framed) {
21+
$transaction = $transaction->framedPage();
22+
}
23+
24+
if ($request->add_billing) {
25+
$transaction = $transaction->addBilling(new PaytabsBilling());
26+
if ($request->hide_billing) {
27+
$transaction = $transaction->hideBilling();
28+
}
29+
}
30+
31+
if ($request->add_shipping) {
32+
$transaction = $transaction->addShipping(new PaytabsBilling());
33+
}
34+
35+
if ($request->hide_shipping) {
36+
$transaction = $transaction->hideShipping();
37+
}
38+
$transaction = $transaction->setRedirectUrl(config('app.url') . "/transaction/finalized", )
39+
->initiate(TransactionType::SALE, TransactionClass::ECOM);
40+
return $transaction;
41+
}
42+
}

app/Actions/PrepareCartData.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use Illuminate\Support\Str;
6+
7+
class PrepareCartData
8+
{
9+
public function __invoke($amount = null): array
10+
{
11+
return [
12+
'id' => Str::random(6),
13+
'amount' => $amount ?: random_int(10, 100),
14+
'description' => "Cart description",
15+
];
16+
}
17+
}

app/Actions/RefundTransaction.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use Devinweb\LaravelPaytabs\Enums\TransactionClass;
6+
use Devinweb\LaravelPaytabs\Enums\TransactionType;
7+
use Devinweb\LaravelPaytabs\Facades\LaravelPaytabsFacade;
8+
use Illuminate\Support\Facades\Auth;
9+
use Devinweb\LaravelPaytabs\Models\Transaction;
10+
11+
class RefundTransaction
12+
{
13+
public function __invoke($transactionRef)
14+
{
15+
$transaction = Transaction::where('transaction_ref', $transactionRef)->first();
16+
$cart = app(PrepareCartData::class)($transaction->amount);
17+
$response = LaravelPaytabsFacade::setTransactionRef($transactionRef)
18+
->setCart($cart)
19+
->setCustomer(Auth::user())
20+
->followUpTransaction(TransactionType::REFUND, TransactionClass::ECOM);
21+
22+
return $response;
23+
}
24+
}

app/Http/Controllers/TransactionController.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,27 @@
99
use Devinweb\LaravelPaytabs\Models\Transaction;
1010
use Illuminate\Support\Facades\Auth;
1111
use Illuminate\Support\Str;
12+
use App\Actions\CreatePaymentPage;
13+
use App\Actions\RefundTransaction;
14+
use Illuminate\Http\Request;
1215

1316
class TransactionController extends Controller
1417
{
15-
1618
public function index()
1719
{
1820
$transactions = Transaction::paid()->withCount('children')->latest()->paginate(30);
1921
return view('transactions')->with('transactions', $transactions);
2022
}
2123

22-
public function create()
24+
public function create(Request $request)
2325
{
24-
$transaction = LaravelPaytabsFacade::setCustomer(Auth::user())
25-
->setCart($this->prepareCartData())
26-
->framedPage()
27-
->hideShipping()
28-
->hideBilling()
29-
->addBilling(new PaytabsBilling)
30-
->setRedirectUrl(config('app.url') . "/transaction/finalized", )
31-
->initiate(TransactionType::SALE, TransactionClass::ECOM);
26+
$transaction = app(CreatePaymentPage::class)($request);
3227

3328
return view('new-transaction')->with('transaction', $transaction);
34-
3529
}
3630
public function refund($transactionRef)
3731
{
38-
$response = LaravelPaytabsFacade::setTransactionRef($transactionRef)
39-
->setCart($this->prepareCartData())
40-
->setCustomer(Auth::user())
41-
->followUpTransaction(TransactionType::REFUND, TransactionClass::ECOM);
32+
$response = app(RefundTransaction::class)($transactionRef);
4233
return $response;
4334
}
44-
45-
private function prepareCartData(): array
46-
{
47-
return [
48-
'id' => Str::random(6),
49-
'amount' => 80,
50-
'description' => "Cart description",
51-
];
52-
}
53-
5435
}

public/css/app.css

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,12 @@ select {
662662
.z-50 {
663663
z-index: 50;
664664
}
665+
.float-left {
666+
float: left;
667+
}
668+
.m-3 {
669+
margin: 0.75rem;
670+
}
665671
.mx-auto {
666672
margin-left: auto;
667673
margin-right: auto;
@@ -729,9 +735,6 @@ select {
729735
.mt-5 {
730736
margin-top: 1.25rem;
731737
}
732-
.mb-8 {
733-
margin-bottom: 2rem;
734-
}
735738
.block {
736739
display: block;
737740
}
@@ -777,9 +780,6 @@ select {
777780
.h-12 {
778781
height: 3rem;
779782
}
780-
.h-full {
781-
height: 100%;
782-
}
783783
.min-h-screen {
784784
min-height: 100vh;
785785
}
@@ -810,6 +810,9 @@ select {
810810
.w-48 {
811811
width: 12rem;
812812
}
813+
.w-1\/2 {
814+
width: 50%;
815+
}
813816
.w-12 {
814817
width: 3rem;
815818
}
@@ -871,6 +874,9 @@ select {
871874
.flex-col {
872875
flex-direction: column;
873876
}
877+
.flex-wrap {
878+
flex-wrap: wrap;
879+
}
874880
.items-start {
875881
align-items: flex-start;
876882
}
@@ -1084,6 +1090,10 @@ select {
10841090
padding-top: 1.5rem;
10851091
padding-bottom: 1.5rem;
10861092
}
1093+
.px-12 {
1094+
padding-left: 3rem;
1095+
padding-right: 3rem;
1096+
}
10871097
.pt-2 {
10881098
padding-top: 0.5rem;
10891099
}
@@ -1126,9 +1136,6 @@ select {
11261136
.text-center {
11271137
text-align: center;
11281138
}
1129-
.align-middle {
1130-
vertical-align: middle;
1131-
}
11321139
.align-bottom {
11331140
vertical-align: bottom;
11341141
}
@@ -1244,6 +1251,10 @@ select {
12441251
--tw-text-opacity: 1;
12451252
color: rgb(220 38 38 / var(--tw-text-opacity));
12461253
}
1254+
.text-blue-600 {
1255+
--tw-text-opacity: 1;
1256+
color: rgb(37 99 235 / var(--tw-text-opacity));
1257+
}
12471258
.underline {
12481259
text-decoration-line: underline;
12491260
}
@@ -1459,6 +1470,10 @@ select {
14591470
--tw-ring-opacity: 1;
14601471
--tw-ring-color: rgb(99 102 241 / var(--tw-ring-opacity));
14611472
}
1473+
.focus\:ring-blue-500:focus {
1474+
--tw-ring-opacity: 1;
1475+
--tw-ring-color: rgb(59 130 246 / var(--tw-ring-opacity));
1476+
}
14621477
.focus\:ring-red-500:focus {
14631478
--tw-ring-opacity: 1;
14641479
--tw-ring-color: rgb(239 68 68 / var(--tw-ring-opacity));

resources/views/new-transaction.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
1414
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
1515
<div class="p-6 bg-white border-b border-gray-200">
16-
<iframe class="w-full" height="400" src="{{$transaction['redirect_url']}}" frameborder="0"></iframe>
16+
<iframe class="w-full" height="500" src="{{$transaction['redirect_url']}}" frameborder="0"></iframe>
1717
</div>
1818
</div>
1919
</div>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<x-app-layout>
2+
<x-slot name="header">
3+
<div class="flex justify-between">
4+
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
5+
{{ __('عملية دفع جديدة') }}
6+
</h2>
7+
8+
</div>
9+
</x-slot>
10+
11+
12+
<div class="py-12">
13+
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
14+
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
15+
<div class="p-6 bg-white border-b border-gray-200">
16+
17+
<div class="font-bold text-xl m-3">خيارات إنشاء صفحة الدفع</div>
18+
<form name="options-form" method="post" action="{{route('new-transaction')}}">
19+
@csrf
20+
<div class="flex flex-wrap">
21+
<div class="flex items-center mb-4 w-full">
22+
<input disabled checked type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 focus:ring-2">
23+
<input type="hidden" value="true" name="framed">
24+
<label class="mr-2 font-medium text-gray-700">وضع iframe</label>
25+
</div>
26+
<div class="flex items-center mb-4 w-1/2">
27+
<input checked type="checkbox" name="add_billing" value="true" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 focus:ring-2">
28+
<label class="mr-2 font-medium text-gray-700">إنشاء بيانات الفاتورة تلقائيًا</label>
29+
</div>
30+
<div class="flex items-center mb-4 w-1/2">
31+
<input checked type="checkbox" name="hide_billing" value="true" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 focus:ring-2">
32+
<label class="mr-2 font-medium text-gray-700">إخفاء بيانات الفاتورة</label>
33+
</div>
34+
<div class="flex items-center mb-4 w-1/2">
35+
<input type="checkbox" name="add_shipping" value="true" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 focus:ring-2">
36+
<label class="mr-2 font-medium text-gray-700">إنشاء بيانات الشحن تلقائيًا</label>
37+
</div>
38+
<div class="flex items-center mb-4 w-1/2">
39+
<input checked type="checkbox" value="true" name="hide_shipping" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300 focus:ring-blue-500 focus:ring-2">
40+
<label class="mr-2 font-medium text-gray-700">إخفاء بيانات الشحن</label>
41+
</div>
42+
<div class="w-full px-12">
43+
<button type="submit" class="px-4 py-1 rounded-md bg-gray-800 text-white float-left ">إنشاء الصفحة</button>
44+
</div>
45+
46+
</div>
47+
</form>
48+
</div>
49+
</div>
50+
</div>
51+
</div>
52+
</x-app-layout>
53+

resources/views/transactions.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
55
{{ __('سجل المدفوعات') }}
66
</h2>
7-
<a href="{{route('new-transaction')}}" class="px-4 py-1 rounded-md border border-gray-900">عملية
7+
<a href="{{route('transaction-options')}}" class="px-4 py-1 rounded-md border border-gray-900">عملية
88
جديدة</a>
99

1010
</div>

routes/web.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
});
2121

2222
Route::get('/transactions', [TransactionController::class, 'index'])->middleware(['auth'])->name('transactions');
23-
Route::get('/transactions/create', [TransactionController::class, 'create'])->middleware(['auth'])->name('new-transaction');
23+
Route::post('/transactions/create', [TransactionController::class, 'create'])->middleware(['auth'])->name('new-transaction');
2424
Route::post('/transactions/{transactionRef}/refund', [TransactionController::class, 'refund'])->middleware(['auth'])->name('refund-transaction');
2525
Route::get('/transaction/finalized', function (Request $request) {
2626
return view('transaction-done');
2727
});
28+
Route::get('/transactions/options', function (Request $request) {
29+
return view('transaction-options');
30+
})->name('transaction-options');
31+
32+
2833

2934
require __DIR__ . '/auth.php';

0 commit comments

Comments
 (0)