2023-01-14 23:41:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2023-05-28 00:34:59 +00:00
|
|
|
use App\Enums\PaymentStatus;
|
2023-01-14 23:41:31 +00:00
|
|
|
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;
|
2023-01-14 23:41:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-01-14 23:41:31 +00:00
|
|
|
/**
|
|
|
|
* Handle the event.
|
|
|
|
*
|
|
|
|
* @param \App\Events\PaymentEvent $event
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle(PaymentEvent $event)
|
|
|
|
{
|
|
|
|
$user = $event->user;
|
|
|
|
$shopProduct = $event->shopProduct;
|
|
|
|
|
2023-01-15 00:41:42 +00:00
|
|
|
// only update user if payment is paid
|
2023-09-17 17:01:37 +00:00
|
|
|
if ($event->payment->status != PaymentStatus::PAID->value) {
|
2023-01-15 00:41:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-14 23:41:31 +00:00
|
|
|
|
|
|
|
//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]);
|
2023-01-14 23:41:31 +00:00
|
|
|
}
|
2023-01-15 00:41:42 +00:00
|
|
|
|
2023-01-14 23:41:31 +00:00
|
|
|
//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);
|
|
|
|
}
|
2023-01-15 00:41:42 +00:00
|
|
|
|
2023-01-14 23:41:31 +00:00
|
|
|
//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) {
|
2023-01-14 23:41:31 +00:00
|
|
|
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, "", "");
|
2023-01-14 23:41:31 +00:00
|
|
|
$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 . ')');
|
2023-01-14 23:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//update role give Referral-reward
|
2023-05-03 14:05:55 +00:00
|
|
|
if ($user->hasRole(4)) {
|
|
|
|
$user->syncRoles(3);
|
2023-01-14 23:41:31 +00:00
|
|
|
|
|
|
|
//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) {
|
2023-01-14 23:41:31 +00:00
|
|
|
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, "", "");
|
2023-01-14 23:41:31 +00:00
|
|
|
$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 . ')');
|
2023-01-14 23:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LOGS PAYMENT IN THE ACTIVITY LOG
|
|
|
|
activity()
|
|
|
|
->performedOn($user)
|
|
|
|
->causedBy($user)
|
|
|
|
->log('bought ' . $shopProduct->quantity . ' ' . $shopProduct->type . ' for ' . $shopProduct->price . $shopProduct->currency_code);
|
|
|
|
}
|
|
|
|
}
|