Skip to content

Commit 3b08b13

Browse files
committed
Add laravel-paytabs dependency
1 parent b586c5f commit 3b08b13

File tree

8 files changed

+282
-3
lines changed

8 files changed

+282
-3
lines changed

app/Billing/PaytabsBilling.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Billing;
4+
5+
use Devinweb\LaravelPaytabs\Contracts\BillingInterface;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Facades\Http;
8+
9+
class PaytabsBilling implements BillingInterface
10+
{
11+
protected $ip;
12+
protected $cordinates;
13+
14+
public function __construct()
15+
{
16+
$this->ip = \Request::ip();
17+
$this->cordinates = $this->getCordinates();
18+
}
19+
/**
20+
* Get the billing data.
21+
*
22+
* @return array
23+
*/
24+
public function getData(): array
25+
{
26+
return $this->GetAddressFromCordinates();
27+
}
28+
29+
protected function GetAddressFromCordinates()
30+
{
31+
$response = Http::get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $this->cordinates . '&key=AIzaSyAFHz-7hKCyzYx2kWfY-S_kSi6Hm8pZ8jk');
32+
33+
$array = $this->getBillingFromGeocode($response->json());
34+
35+
return $array;
36+
}
37+
38+
protected function getCordinates()
39+
{
40+
$response = Http::withHeaders(['Accept' => 'application/json'])->get('https://ipinfo.io/' . $this->ip);
41+
$response = $response->json();
42+
if (Arr::has($response, 'loc')) {
43+
return Arr::get($response, 'loc');
44+
}
45+
return '21.4901,39.1862';
46+
}
47+
48+
protected function getBillingFromGeocode(array $response): array
49+
{
50+
$billing = [
51+
'street1' => '',
52+
'city' => '',
53+
'state' => '',
54+
'country' => '',
55+
'zip' => '',
56+
'ip' => $this->ip,
57+
];
58+
59+
if (Arr::has($response, 'results')) {
60+
$result = $response['results'];
61+
$address_components = Arr::has($result[1], 'address_components') ? $result[1]['address_components'] : [];
62+
63+
$billing['street1'] = Arr::has($result[0], 'formatted_address') ? $result[0]['formatted_address'] : '';
64+
65+
for ($i = 0; $i < count($address_components); $i++) {
66+
$child_address_components = $address_components[$i];
67+
switch (Arr::get($child_address_components, 'types')[0]) {
68+
case 'locality':
69+
$billing['city'] = Arr::get($child_address_components, 'long_name');
70+
break;
71+
case 'administrative_area_level_1':
72+
$billing['state'] = Arr::get($child_address_components, 'long_name');
73+
break;
74+
case 'country':
75+
$billing['country'] = Arr::get($child_address_components, 'short_name');
76+
break;
77+
case 'postal_code':
78+
$billing['zip'] = Arr::get($child_address_components, 'long_name');
79+
break;
80+
}
81+
}
82+
}
83+
84+
return collect($billing)->filter()->all();
85+
}
86+
}

app/Http/Controllers/TransactionController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Billing\PaytabsBilling;
56
use Devinweb\LaravelPaytabs\Enums\TransactionClass;
67
use Devinweb\LaravelPaytabs\Enums\TransactionType;
78
use Devinweb\LaravelPaytabs\Facades\LaravelPaytabsFacade;
@@ -24,6 +25,8 @@ public function create()
2425
->setCart($this->prepareCartData())
2526
->framedPage()
2627
->hideShipping()
28+
->hideBilling()
29+
->addBilling(new PaytabsBilling)
2730
->setRedirectUrl(config('app.url') . "/transaction/finalized", )
2831
->initiate(TransactionType::SALE, TransactionClass::ECOM);
2932

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"require": {
88
"php": "^7.3|^8.0",
9+
"devinweb/laravel-paytabs": "^1.0",
910
"fruitcake/laravel-cors": "^2.0",
1011
"guzzlehttp/guzzle": "^7.0.1",
1112
"laravel/framework": "^8.75",

composer.lock

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/paytabs.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Merchant profile id
7+
|--------------------------------------------------------------------------
8+
|
9+
| Your merchant profile id , you can find the profile id on your PayTabs Merchant’s Dashboard- profile.
10+
|
11+
*/
12+
13+
'profile_id' => env('PAYTABS_PROFILE_ID', null),
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Server Key
18+
|--------------------------------------------------------------------------
19+
|
20+
| You can find the Server key on your PayTabs Merchant’s Dashboard - Developers - Key management.
21+
|
22+
*/
23+
24+
'server_key' => env('PAYTABS_SERVER_KEY', null),
25+
26+
/*
27+
|--------------------------------------------------------------------------
28+
| Currency
29+
|--------------------------------------------------------------------------
30+
|
31+
| The currency you registered in with PayTabs account
32+
you must pass value from this array ['AED','EGP','SAR','OMR','JOD','US']
33+
|
34+
*/
35+
36+
'currency' => env('CURRENCY', 'SAR'),
37+
38+
/*
39+
|--------------------------------------------------------------------------
40+
| Region
41+
|--------------------------------------------------------------------------
42+
|
43+
| The region you registered in with PayTabs
44+
you must pass value from this array ['ARE','EGY','SAU','OMN','JOR','GLOBAL']
45+
|
46+
*/
47+
48+
'region' => env('PAYTABS_REGION', 'SAU'),
49+
50+
/*
51+
|--------------------------------------------------------------------------
52+
| The API endpoint
53+
|--------------------------------------------------------------------------
54+
|
55+
| The transaction request API
56+
|
57+
*/
58+
'paytabs_api' => env('PAYTABS_API', 'https://secure.paytabs.sa/'),
59+
60+
/*
61+
|--------------------------------------------------------------------------
62+
| Redirect URL
63+
|--------------------------------------------------------------------------
64+
|
65+
| The redirect url after payment
66+
|
67+
*/
68+
'redirect_url' => env('PAYTABS_REDIRECT_URL', null),
69+
70+
/*
71+
|--------------------------------------------------------------------------
72+
| Lang
73+
|--------------------------------------------------------------------------
74+
|
75+
| The hosted payment page lang
76+
|
77+
*/
78+
'lang' => '',
79+
'model' => env('PAYMENT_MODEL', class_exists(App\Models\User::class) ? App\Models\User::class : App\User::class),
80+
];
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateTransactionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('transactions', function (Blueprint $table) {
17+
$table->string('id')->primary();
18+
$table->integer('user_id');
19+
$table->string('transaction_ref')->unique();
20+
$table->string('type');
21+
$table->string('class');
22+
$table->string('status')->default('pending');
23+
$table->float('amount');
24+
$table->string('currency');
25+
$table->string('parent')->nullable();
26+
$table->json('data')->nullable();
27+
$table->foreign('user_id')->references('id')->on('users')
28+
->onUpdate('cascade')->onDelete('set null');
29+
$table->foreign('parent')->references('transaction_ref')->on('transactions')
30+
->onUpdate('cascade')->onDelete('set null');
31+
32+
$table->timestamps();
33+
});
34+
}
35+
36+
/**
37+
* Reverse the migrations.
38+
*
39+
* @return void
40+
*/
41+
public function down()
42+
{
43+
Schema::drop('transactions');
44+
}
45+
};

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="600" src="{{$transaction['redirect_url']}}" frameborder="0"></iframe>
16+
<iframe class="w-full" height="400" src="{{$transaction['redirect_url']}}" frameborder="0"></iframe>
1717
</div>
1818
</div>
1919
</div>

routes/web.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Http\Controllers\TransactionController;
4+
use Illuminate\Http\Request;
45
use Illuminate\Support\Facades\Route;
56

67
/*
@@ -21,7 +22,7 @@
2122
Route::get('/transactions', [TransactionController::class, 'index'])->middleware(['auth'])->name('transactions');
2223
Route::get('/transactions/create', [TransactionController::class, 'create'])->middleware(['auth'])->name('new-transaction');
2324
Route::post('/transactions/{transactionRef}/refund', [TransactionController::class, 'refund'])->middleware(['auth'])->name('refund-transaction');
24-
Route::get('/transaction/finalized', function () {
25+
Route::get('/transaction/finalized', function (Request $request) {
2526
return view('transaction-done');
2627
});
2728

0 commit comments

Comments
 (0)