From 0257bf4e855b4ecd08afe00c2a2bfe5f7bdc4620 Mon Sep 17 00:00:00 2001 From: IceToast <> Date: Sun, 15 Jan 2023 00:41:31 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Added=20new=20events=20to?= =?UTF-8?q?=20extract=20payment=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Events/PaymentEvent.php | 12 +-- ...{PaymentListener.php => CreateInvoice.php} | 4 +- app/Listeners/UserPayment.php | 78 +++++++++++++++++++ app/Providers/EventServiceProvider.php | 6 +- 4 files changed, 91 insertions(+), 9 deletions(-) rename app/Listeners/{PaymentListener.php => CreateInvoice.php} (93%) create mode 100644 app/Listeners/UserPayment.php diff --git a/app/Events/PaymentEvent.php b/app/Events/PaymentEvent.php index 3157de69..11711200 100644 --- a/app/Events/PaymentEvent.php +++ b/app/Events/PaymentEvent.php @@ -3,6 +3,8 @@ namespace App\Events; use App\Models\Payment; +use App\Models\ShopProduct; +use App\Models\User; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; @@ -11,19 +13,19 @@ class PaymentEvent { use Dispatchable, InteractsWithSockets, SerializesModels; - /** - * @var User - */ + public User $user; public Payment $payment; - + public ShopProduct $shopProduct; /** * Create a new event instance. * * @return void */ - public function __construct(Payment $payment) + public function __construct(User $user, Payment $payment, ShopProduct $shopProduct) { + $this->user = $user; $this->payment = $payment; + $this->shopProduct = $shopProduct; } } diff --git a/app/Listeners/PaymentListener.php b/app/Listeners/CreateInvoice.php similarity index 93% rename from app/Listeners/PaymentListener.php rename to app/Listeners/CreateInvoice.php index af7ab63d..08d772c0 100644 --- a/app/Listeners/PaymentListener.php +++ b/app/Listeners/CreateInvoice.php @@ -7,7 +7,7 @@ use App\Traits\Invoiceable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; -class PaymentListener +class CreateInvoice implements ShouldQueue { use Invoiceable; @@ -23,7 +23,7 @@ class PaymentListener if (config('SETTINGS::INVOICE:ENABLED') == 'true') { // get user from payment which does hold the user_id $user = $event->payment->user; - + // create invoice using the trait $this->createInvoice($user, $event->payment); } diff --git a/app/Listeners/UserPayment.php b/app/Listeners/UserPayment.php new file mode 100644 index 00000000..e6729d9c --- /dev/null +++ b/app/Listeners/UserPayment.php @@ -0,0 +1,78 @@ +user; + $shopProduct = $event->shopProduct; + + + //update server limit + if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) { + if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) { + $user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]); + } + } + //update User with bought item + if ($shopProduct->type == "Credits") { + $user->increment('credits', $shopProduct->quantity); + } elseif ($shopProduct->type == "Server slots") { + $user->increment('server_limit', $shopProduct->quantity); + } + //give referral commission always + if ((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type == "Credits" && config("SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION") == "true") { + if ($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()) { + $ref_user = User::findOrFail($ref_user->referral_id); + $increment = number_format($shopProduct->quantity * (PartnerDiscount::getCommission($ref_user->id)) / 100, 0, "", ""); + $ref_user->increment('credits', $increment); + + //LOGS REFERRALS IN THE ACTIVITY LOG + activity() + ->performedOn($user) + ->causedBy($ref_user) + ->log('gained ' . $increment . ' ' . config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME") . ' for commission-referral of ' . $user->name . ' (ID:' . $user->id . ')'); + } + } + //update role give Referral-reward + if ($user->role == 'member') { + $user->update(['role' => 'client']); + + //give referral commission only on first purchase + if ((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type == "Credits" && config("SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION") == "false") { + if ($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()) { + $ref_user = User::findOrFail($ref_user->referral_id); + $increment = number_format($shopProduct->quantity * (PartnerDiscount::getCommission($ref_user->id)) / 100, 0, "", ""); + $ref_user->increment('credits', $increment); + + //LOGS REFERRALS IN THE ACTIVITY LOG + activity() + ->performedOn($user) + ->causedBy($ref_user) + ->log('gained ' . $increment . ' ' . config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME") . ' for commission-referral of ' . $user->name . ' (ID:' . $user->id . ')'); + } + } + } + + // LOGS PAYMENT IN THE ACTIVITY LOG + activity() + ->performedOn($user) + ->causedBy($user) + ->log('bought ' . $shopProduct->quantity . ' ' . $shopProduct->type . ' for ' . $shopProduct->price . $shopProduct->currency_code); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a63efdd5..585b3e0f 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -4,8 +4,9 @@ namespace App\Providers; use App\Events\PaymentEvent; use App\Events\UserUpdateCreditsEvent; -use App\Listeners\PaymentListener; +use App\Listeners\CreateInvoice; use App\Listeners\UnsuspendServers; +use App\Listeners\UserPayment; use App\Listeners\Verified; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; @@ -28,7 +29,8 @@ class EventServiceProvider extends ServiceProvider UnsuspendServers::class, ], PaymentEvent::class => [ - PaymentListener::class, + CreateInvoice::class, + UserPayment::class, ], SocialiteWasCalled::class => [ // ... other providers