ctrlpanel/app/Listeners/UserPayment.php

109 lines
4.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Listeners;
use App\Enums\PaymentStatus;
use App\Events\PaymentEvent;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use App\Models\PartnerDiscount;
2023-02-06 20:16:54 +00:00
use App\Settings\GeneralSettings;
use App\Settings\ReferralSettings;
use App\Settings\UserSettings;
class UserPayment
{
2023-02-06 20:16:54 +00:00
private $server_limit_after_irl_purchase;
private $referral_mode;
private $referral_percentage;
private $referral_always_give_commission;
private $credits_display_name;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(UserSettings $user_settings, ReferralSettings $referral_settings, GeneralSettings $general_settings)
{
$this->server_limit_after_irl_purchase = $user_settings->server_limit_after_irl_purchase;
$this->referral_mode = $referral_settings->mode;
$this->referral_percentage = $referral_settings->percentage;
$this->referral_always_give_commission = $referral_settings->always_give_commission;
$this->credits_display_name = $general_settings->credits_display_name;
}
2023-05-03 14:05:55 +00:00
/**
* Handle the event.
*
* @param \App\Events\PaymentEvent $event
* @return void
*/
public function handle(PaymentEvent $event)
{
$user = $event->user;
$shopProduct = $event->shopProduct;
// only update user if payment is paid
2023-09-17 17:01:37 +00:00
if ($event->payment->status != PaymentStatus::PAID->value) {
return;
}
//update server limit
2023-02-06 20:16:54 +00:00
if ($this->server_limit_after_irl_purchase !== 0 && $user->server_limit < $this->server_limit_after_irl_purchase) {
$user->update(['server_limit' => $this->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
2023-02-06 20:16:54 +00:00
if (($this->referral_mode === "commission" || $this->referral_mode === "both") && $shopProduct->type == "Credits" && $this->referral_always_give_commission) {
if ($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()) {
$ref_user = User::findOrFail($ref_user->referral_id);
2023-02-06 20:16:54 +00:00
$increment = number_format($shopProduct->quantity * (PartnerDiscount::getCommission($ref_user->id, $this->referral_percentage)) / 100, 0, "", "");
$ref_user->increment('credits', $increment);
//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
2023-02-06 20:16:54 +00:00
->log('gained ' . $increment . ' ' . $this->credits_display_name . ' for commission-referral of ' . $user->name . ' (ID:' . $user->id . ')');
}
}
//update role give Referral-reward
2023-05-03 14:05:55 +00:00
if ($user->hasRole(4)) {
$user->syncRoles(3);
//give referral commission only on first purchase
2023-02-06 20:16:54 +00:00
if (($this->referral_mode === "commission" || $this->referral_mode === "both") && $shopProduct->type == "Credits" && !$this->referral_always_give_commission) {
if ($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()) {
$ref_user = User::findOrFail($ref_user->referral_id);
2023-02-06 20:16:54 +00:00
$increment = number_format($shopProduct->quantity * (PartnerDiscount::getCommission($ref_user->id, $this->referral_percentage)) / 100, 0, "", "");
$ref_user->increment('credits', $increment);
//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
2023-02-06 20:16:54 +00:00
->log('gained ' . $increment . ' ' . $this->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);
}
}