Made notifications queueble, added payment confirmation notification

This commit is contained in:
AVMG20 2021-06-22 23:22:57 +02:00
parent fff45d4f75
commit 9c6317d32f
9 changed files with 144 additions and 20 deletions

View file

@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
use App\Models\Configuration;
use App\Models\Payment;
use App\Models\PaypalProduct;
use App\Models\User;
use App\Notifications\ConfirmPaymentNotification;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
@ -117,7 +119,10 @@ class PaymentController extends Controller
*/
public function success(Request $laravelRequest)
{
/** @var PaypalProduct $paypalProduct */
$paypalProduct = PaypalProduct::findOrFail($laravelRequest->input('product'));
/** @var User $user */
$user = Auth::user();
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
$request->prefer('return=representation');
@ -127,32 +132,35 @@ class PaymentController extends Controller
if ($response->statusCode == 201 || $response->statusCode == 200) {
//update credits
Auth::user()->increment('credits', $paypalProduct->quantity);
$user->increment('credits', $paypalProduct->quantity);
//update server limit
if (Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10) !== 0) {
if (Auth::user()->server_limit < Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10)) {
Auth::user()->update(['server_limit' => 10]);
if ($user->server_limit < Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10)) {
$user->update(['server_limit' => 10]);
}
}
//update role
if (Auth::user()->role == 'member') {
Auth::user()->update(['role' => 'client']);
if ($user->role == 'member') {
$user->update(['role' => 'client']);
}
//store payment
Payment::create([
'user_id' => Auth::user()->id,
$payment = Payment::create([
'user_id' => $user->id,
'payment_id' => $response->result->id,
'payer_id' => $laravelRequest->input('PayerID'),
'type' => 'Credits',
'status' => $response->result->status,
'amount' => $paypalProduct->quantity,
'price' => $paypalProduct->price,
'price' => $paypalProduct->formatCurrency(),
'payer' => json_encode($response->result->payer),
]);
//payment notification
$user->notify(new ConfirmPaymentNotification($payment));
//redirect back to home
return redirect()->route('home')->with('success', 'Credits have been increased!');
}

View file

@ -56,21 +56,14 @@ class RegisterController extends Controller
$data['ip'] = session()->get('ip') ?? request()->ip();
if (User::where('ip', '=', request()->ip())->exists()) session()->put('ip', request()->ip());
//check if registered cookie exists as extra defense
if (isset($_COOKIE['4b3403665fea6'])) {
$data['registered'] = env('APP_ENV') == 'local' ? false : true;
}
return Validator::make($data, [
'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'g-recaptcha-response' => ['recaptcha'],
'ip' => ['unique:users'],
'registered' => ['nullable', 'boolean', 'in:true']
], [
'ip.unique' => "You have already made an account with us! Please contact support if you think this is incorrect.",
'registered.in' => "You have already made an account with us! Please contact support if you think this is incorrect."
'ip.unique' => "You have already made an account with us! Please contact support if you think this is incorrect."
]);
}

View file

@ -18,9 +18,6 @@ class HomeController extends Controller
/** Show the application dashboard. */
public function index(Request $request)
{
//set cookie as extra layer of defense against users that make multiple accounts
setcookie('4b3403665fea6' , base64_encode(1) , time() + (20 * 365 * 24 * 60 * 60));
$usage = 0;
foreach (Auth::user()->Servers as $server){

View file

@ -0,0 +1,64 @@
<?php
namespace App\Notifications;
use App\Models\Payment;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ConfirmPaymentNotification extends Notification implements ShouldQueue
{
use Queueable;
private Payment $payment;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Payment $payment)
{
$this->payment = $payment;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Payment Confirmation')
->markdown('mail.payment.confirmed' , ['payment' => $this->payment]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'title' => "Payment Confirmed!",
'content' => "Payment Confirmed!",
];
}
}

View file

@ -8,6 +8,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class ServerCreationError extends Notification
{
use Queueable;
/**

View file

@ -5,9 +5,10 @@ namespace App\Notifications;
use App\Models\Configuration;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class WelcomeMessage extends Notification
class WelcomeMessage extends Notification implements ShouldQueue
{
use Queueable;

View file

@ -0,0 +1,37 @@
<?php
namespace Database\Factories;
use App\Models\Payment;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PaymentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Payment::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'payment_id' => Str::random(30),
'payer_id' => Str::random(30),
'user_id' => User::factory(),
'type' => "Credits",
'status' => "Completed",
'amount' => $this->faker->numberBetween(10, 10000),
'price' => '€' . $this->faker->numerify('##.##'),
'payer' => '{}',
];
}
}

View file

@ -0,0 +1,18 @@
@component('mail::message')
# Thank you for your purchase!
Your payment has been confirmed; Your balance has been updated.
# Details
___
### Payment ID: **{{$payment->id}}**
### Status: **{{$payment->status}}**
### Price: **{{$payment->price}}**
### Type: **{{$payment->type}}**
### Amount: **{{$payment->amount}}**
### Balance **{{$payment->user->credits}}**
### User ID: **{{$payment->user_id}}**
<br>
Thanks,<br>
{{ config('app.name') }}
@endcomponent

View file

@ -112,6 +112,11 @@ Route::middleware('auth')->group(function () {
Route::resource('api', ApplicationApiController::class)->parameters([
'api' => 'applicationApi',
]);
#Testing route to preview new ConfirmedPaymentNotification email
Route::get('test' , function(Request $request){
return (new \App\Notifications\ConfirmPaymentNotification(\App\Models\Payment::factory()->create()))->toMail($request->user());
});
});
Route::get('/home', [HomeController::class, 'index'])->name('home');