Skip to content

Commit

Permalink
update composer and add Auth API
Browse files Browse the repository at this point in the history
  • Loading branch information
arief-github committed Apr 28, 2024
1 parent 783565d commit 6854236
Show file tree
Hide file tree
Showing 11 changed files with 1,885 additions and 410 deletions.
Binary file added .rnd
Binary file not shown.
186 changes: 186 additions & 0 deletions app/Http/Controllers/Api/DonationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace App\Http\Controllers\Api;

use Midtrans\Snap;
use App\Models\Campaign;
use App\Models\Donation;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DonationController extends Controller
{
public function __construct()
{
\Midtrans\Config::$serverKey = config('services.midtrans.serverKey');
\Midtrans\Config::$isProduction = config('services.midtrans.isProduction');
\Midtrans\Config::$isSanitized = config('services.midtrans.isSanitized');
\Midtrans\Config::$is3ds = config('services.midtrans.is3ds');
}

public function index()
{
$donations = Donation::with('campaign')->where('donatur_id', auth()->guard('api')->user()->id)->latest()->paginate(5);

return response()->json([
'success' => true,
'message' => 'List Data Donations : '.auth()->guard('api')->user()->name,
'data' => $donations
], 200);
}

public function store(Request $request)
{
DB::transaction(function () use ($request) {
$length = 10;
$random = '';

for($i = 0; $i < $length; $i++) {
$random .= rand(0,1) ? rand(0, 9) : chr(rand(ord('a'), ord('z')));
}

$no_invoice = 'TRX-'.Str::upper($random);

// get data campaign
$campaign = Campaign::where('slug', $request->campaignSlug)->first();

$donation = Donations::create([
'no_invoice' => $no_invoice,
'campaign_id' => $campaign->id,
'donatur_id' => auth()->guard('api')->user()->id,
'amount' => $request->amount,
'pray' => $request->pray,
'status' => 'pending'
]);


// make transaction to midtrans and then save the snap token
$payload = [
'transaction_details' => [
'order_id' => $donation->invoice,
'gross_amount' => $donation->amount,
],
'customer_details' => [
'first_name' => auth()->guard('api')->user()->name,
'email' => auth()->guard('api')->user()->email,
]
];

// create snap token
$snapToken = Snap::getSnapToken($payload);
$donation->snap_token = $snapToken;
$donation->save();

$this->response['snap_token'] = $snapToken;
});

return response()->json([
'success' => true,
'message' => 'Donation Berhasil Dibuat',
$this->response
]);
}

public function notificationHandler(Request $request)
{
$payload = $request->getContent();
$notification = json_decode($payload);

$validateSignatureKey = hash("sha512", $notification->order_id . $notification->status_code . $notification->gross_amount . config('services.midtrans.serverKey'));

if($notification->signature_key !== $validateSignatureKey) {
return response(['message' => 'Invalid Signature'], 403);
}

$transaction = $notification->transaction_status;
$type = $notification->paymentType;
$orderId = $notification->order_id;
$fraud = $notification->fraud_status;

// data donation
$data_donation = Donation::where('invoice', $orderId)->first();

if ($transaction == 'capture') {
// For credit card transaction, we need to check whether transaction is challenge by FDS or not
if ($type == 'credit_card') {

if($fraud == 'challenge') {

/**
* update invoice to pending
*/
$data_donation->update([
'status' => 'pending'
]);

} else {

/**
* update invoice to success
*/
$data_donation->update([
'status' => 'success'
]);

}

}

} elseif ($transaction == 'settlement') {

/**
* update invoice to success
*/
$data_donation->update([
'status' => 'success'
]);


} elseif($transaction == 'pending'){


/**
* update invoice to pending
*/
$data_donation->update([
'status' => 'pending'
]);


} elseif ($transaction == 'deny') {


/**
* update invoice to failed
*/
$data_donation->update([
'status' => 'failed'
]);


} elseif ($transaction == 'expire') {


/**
* update invoice to expired
*/
$data_donation->update([
'status' => 'expired'
]);


} elseif ($transaction == 'cancel') {

/**
* update invoice to failed
*/
$data_donation->update([
'status' => 'failed'
]);

}

}
}
57 changes: 57 additions & 0 deletions app/Http/Controllers/Api/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Donatur;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
/**
* login
*
*/

public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required'
]);

if($validator->fails()) {
return response()->json($validator->errors(), 400);
}

$donatur = Donatur::where('email', $request->email)->first();

if(!$donatur || !Hash::check($request->password, $donatur->password)) {
return response()->json([
'success' => false,
'message' => 'Login Failed',
], 401);
}

return response()->json([
'success' => true,
'message' => 'Login Berhasil',
'data' => $donatur,
'token' => $donatur->createToken('authToken')->accessToken
], 200);
}

public function logout(Request $request)
{
$removeToken = $request->user()->tokens()->delete();

if ($removeToken) {
return response()->json([
'success' => true,
'message' => 'Logout Berhasil'
]);
}
}
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/Api/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Donatur;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
/**
* Register
*
* @param mixed $request
* @return void
*/

public function register(Request $request)
{
// set validation
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:donaturs',
'password' => 'required|min:8|confirmed'
]);

if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}

// Create Donatur
$donatur = Donatur::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password)
]);

// return JSON
return response()->json([
'success' => true,
'message' => 'Register Berhasil',
'data' => $donatur,
'token' => $donatur->createToken('authToken')->accessToken
], 201);
}
}
19 changes: 17 additions & 2 deletions app/Models/Donatur.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Donatur extends Model
class Donatur extends Authenticatable
{
use HasFactory;
use HasFactory, HasApiTokens;

/**
* fillable
Expand Down Expand Up @@ -36,4 +38,17 @@ public function donations()
{
return $this->hasMany(Donation::class);
}

/**
* getAvatarAttribute
*/

public function getAvatarAttribute($avatar)
{
if ($avatar != null) :
return asset('storage/donaturs'. $avatar);
else :
return 'https://ui-avatars.com/api/?name=' . str_replace('', '+', $this->name) . '&background=4e73df&color=ffffff&size=100';
endif;
}
}
5 changes: 4 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

Expand All @@ -25,6 +26,8 @@ public function boot()
{
$this->registerPolicies();

//
if (! $this->app->routesAreCached()) {
Passport::routes();
}
}
}
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
],
"license": "MIT",
"require": {
"php": "^7.3",
"php": "^7.3|^8.0",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/fortify": "1.7.7",
"laravel/framework": "^8.0",
"laravel/tinker": "^2.0"
"laravel/tinker": "^2.0",
"midtrans/midtrans-php": "2.3.2",
"laravel/passport": "10.1"
},
"require-dev": {
"facade/ignition": "^2.3.6",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.3.3"
},
"config": {
"optimize-autoloader": true,
Expand Down Expand Up @@ -62,4 +65,4 @@
"@php artisan key:generate --ansi"
]
}
}
}
Loading

0 comments on commit 6854236

Please sign in to comment.