From 9c6317d32f178153cae61a9cba309cea077d09d5 Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Tue, 22 Jun 2021 23:22:57 +0200 Subject: [PATCH] Made notifications queueble, added payment confirmation notification --- .../Controllers/Admin/PaymentController.php | 24 ++++--- .../Controllers/Auth/RegisterController.php | 9 +-- app/Http/Controllers/HomeController.php | 3 - .../ConfirmPaymentNotification.php | 64 +++++++++++++++++++ app/Notifications/ServerCreationError.php | 1 + app/Notifications/WelcomeMessage.php | 3 +- database/factories/PaymentFactory.php | 37 +++++++++++ .../views/mail/payment/confirmed.blade.php | 18 ++++++ routes/web.php | 5 ++ 9 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 app/Notifications/ConfirmPaymentNotification.php create mode 100644 database/factories/PaymentFactory.php create mode 100644 resources/views/mail/payment/confirmed.blade.php diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index 9a554ad4..4040eff2 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -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!'); } diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 5d99fdb7..85789a60 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -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." ]); } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 2657d60d..c09a2c37 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -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){ diff --git a/app/Notifications/ConfirmPaymentNotification.php b/app/Notifications/ConfirmPaymentNotification.php new file mode 100644 index 00000000..efd133ab --- /dev/null +++ b/app/Notifications/ConfirmPaymentNotification.php @@ -0,0 +1,64 @@ +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!", + ]; + } +} diff --git a/app/Notifications/ServerCreationError.php b/app/Notifications/ServerCreationError.php index 534f52fa..9555f86a 100644 --- a/app/Notifications/ServerCreationError.php +++ b/app/Notifications/ServerCreationError.php @@ -8,6 +8,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; class ServerCreationError extends Notification + { use Queueable; /** diff --git a/app/Notifications/WelcomeMessage.php b/app/Notifications/WelcomeMessage.php index d74390a2..6f30e032 100644 --- a/app/Notifications/WelcomeMessage.php +++ b/app/Notifications/WelcomeMessage.php @@ -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; diff --git a/database/factories/PaymentFactory.php b/database/factories/PaymentFactory.php new file mode 100644 index 00000000..f3d6e9cc --- /dev/null +++ b/database/factories/PaymentFactory.php @@ -0,0 +1,37 @@ + 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' => '{}', + ]; + } +} diff --git a/resources/views/mail/payment/confirmed.blade.php b/resources/views/mail/payment/confirmed.blade.php new file mode 100644 index 00000000..754ee7e6 --- /dev/null +++ b/resources/views/mail/payment/confirmed.blade.php @@ -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}}** + +
+Thanks,
+{{ config('app.name') }} +@endcomponent diff --git a/routes/web.php b/routes/web.php index 71940edb..fa6df30f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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');