Browse Source

Made notifications queueble, added payment confirmation notification

AVMG20 4 years ago
parent
commit
9c6317d32f

+ 16 - 8
app/Http/Controllers/Admin/PaymentController.php

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

+ 1 - 8
app/Http/Controllers/Auth/RegisterController.php

@@ -56,21 +56,14 @@ class RegisterController extends Controller
         $data['ip'] = session()->get('ip') ?? request()->ip();
         $data['ip'] = session()->get('ip') ?? request()->ip();
         if (User::where('ip', '=', request()->ip())->exists()) session()->put('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, [
         return Validator::make($data, [
             'name'                 => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
             'name'                 => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
             'email'                => ['required', 'string', 'email', 'max:64', 'unique:users'],
             'email'                => ['required', 'string', 'email', 'max:64', 'unique:users'],
             'password'             => ['required', 'string', 'min:8', 'confirmed'],
             'password'             => ['required', 'string', 'min:8', 'confirmed'],
             'g-recaptcha-response' => ['recaptcha'],
             'g-recaptcha-response' => ['recaptcha'],
             'ip'                   => ['unique:users'],
             '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."
         ]);
         ]);
     }
     }
 
 

+ 0 - 3
app/Http/Controllers/HomeController.php

@@ -18,9 +18,6 @@ class HomeController extends Controller
     /** Show the application dashboard. */
     /** Show the application dashboard. */
     public function index(Request $request)
     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;
         $usage = 0;
 
 
         foreach (Auth::user()->Servers as $server){
         foreach (Auth::user()->Servers as $server){

+ 64 - 0
app/Notifications/ConfirmPaymentNotification.php

@@ -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!",
+        ];
+    }
+}

+ 1 - 0
app/Notifications/ServerCreationError.php

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

+ 2 - 1
app/Notifications/WelcomeMessage.php

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

+ 37 - 0
database/factories/PaymentFactory.php

@@ -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' => '{}',
+        ];
+    }
+}

+ 18 - 0
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}}**
+
+<br>
+Thanks,<br>
+{{ config('app.name') }}
+@endcomponent

+ 5 - 0
routes/web.php

@@ -112,6 +112,11 @@ Route::middleware('auth')->group(function () {
         Route::resource('api', ApplicationApiController::class)->parameters([
         Route::resource('api', ApplicationApiController::class)->parameters([
             'api' => 'applicationApi',
             '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');
     Route::get('/home', [HomeController::class, 'index'])->name('home');