From 197636dbdb648499d2947b75ce77d145b864cb98 Mon Sep 17 00:00:00 2001 From: ilestis Date: Mon, 21 Oct 2024 11:32:30 -0600 Subject: [PATCH 1/7] Fix ad display information --- app/Services/Tracking/DatalayerService.php | 49 ++++++++++++++----- .../views/layouts/tracking/tracking.blade.php | 2 +- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/Services/Tracking/DatalayerService.php b/app/Services/Tracking/DatalayerService.php index 52473d397..9b3034da4 100644 --- a/app/Services/Tracking/DatalayerService.php +++ b/app/Services/Tracking/DatalayerService.php @@ -2,10 +2,14 @@ namespace App\Services\Tracking; +use App\Facades\AdCache; +use App\Models\Campaign; +use Carbon\Carbon; + class DatalayerService { - /** @var bool|string */ - protected $group = false; + /** Group name: a|b */ + protected string $group; /** @var array Extra parameters to pass */ protected array $additional = []; @@ -14,10 +18,12 @@ class DatalayerService protected bool $newAccount = false; /** @var bool If the user is newly registered */ - protected bool $newSubcriber = false; + protected bool $newSubscriber = false; /** @var bool If the user is newly cancelled */ - protected bool $newCancelledSubcriber = false; + protected bool $newCancelledSubscriber = false; + + protected ?Campaign $campaign; /** */ @@ -30,32 +36,53 @@ public function base(): string 'userSubbed' => false, 'route' => $this->route(), 'newAccount' => $this->newAccount ? '1' : '0', - 'newSubscriber' => $this->newSubcriber ? '1' : '0', + 'newSubscriber' => $this->newSubscriber ? '1' : '0', 'userID' => null, ], $this->additional); - if (auth()->check()) { $data['userType'] = 'registered'; $data['userTier'] = !empty(auth()->user()->pledge) ? auth()->user()->pledge : null; $data['userSubbed'] = !empty(auth()->user()->pledge) ? 'true' : 'false'; $data['userID'] = auth()->user()->id; - if ($this->newCancelledSubcriber) { + if ($this->newCancelledSubscriber) { $data['newCancelled'] = '1'; } - if ($this->newAccount || $this->newSubcriber) { + if ($this->newAccount || $this->newSubscriber) { $data['userEmail'] = auth()->user()->email; } } + + $data['showAds'] = $this->showAds(); return json_encode($data); } + public function campaign(?Campaign $campaign) + { + $this->campaign = $campaign; + return $this; + } + + protected function showAds(): bool + { + if ($this->campaign && $this->campaign->boosted()) { + return false; + } elseif (!AdCache::canHaveAds()) { + return false; + } elseif (auth()->guest()) { + return true; + } elseif (auth()->user()->isSubscriber()) { + return false; + } + return auth()->user()->created_at->diffInHours(Carbon::now()) > 24; + } + /** */ public function userGroup(): string { - if ($this->group !== false) { + if (isset($this->group)) { return $this->group; } // Set in session? Use that @@ -107,7 +134,7 @@ protected function route(): string */ public function newSubscriber(): self { - $this->newSubcriber = true; + $this->newSubscriber = true; return $this; } @@ -117,7 +144,7 @@ public function newSubscriber(): self */ public function newCancelledSubscriber(): self { - $this->newCancelledSubcriber = true; + $this->newCancelledSubscriber = true; return $this; } diff --git a/resources/views/layouts/tracking/tracking.blade.php b/resources/views/layouts/tracking/tracking.blade.php index a95e7607c..fe29d4237 100644 --- a/resources/views/layouts/tracking/tracking.blade.php +++ b/resources/views/layouts/tracking/tracking.blade.php @@ -3,7 +3,7 @@ @endif + +@includeWhen(config('tracking.consent'), 'partials.cookieconsent') diff --git a/resources/views/layouts/tracking/tracking.blade.php b/resources/views/layouts/tracking/tracking.blade.php index fe29d4237..a19402c89 100644 --- a/resources/views/layouts/tracking/tracking.blade.php +++ b/resources/views/layouts/tracking/tracking.blade.php @@ -22,8 +22,3 @@ function gtag(){dataLayer.push(arguments);} @endif @endif - - -@if (\App\Facades\AdCache::canHaveAds()) - -@endif From e834818e1984695e5300d764f77ea50196cad30a Mon Sep 17 00:00:00 2001 From: ilestis Date: Wed, 23 Oct 2024 11:57:33 -0600 Subject: [PATCH 5/7] Track paypal sub value --- app/Http/Controllers/PayPalController.php | 18 ++++++++++++++- .../Settings/SubscriptionController.php | 23 +++++++++++++++++-- app/Services/SubscriptionService.php | 2 +- .../views/layouts/tracking/tracking.blade.php | 14 +++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/PayPalController.php b/app/Http/Controllers/PayPalController.php index b494ae83b..a30a79d7e 100644 --- a/app/Http/Controllers/PayPalController.php +++ b/app/Http/Controllers/PayPalController.php @@ -2,10 +2,13 @@ namespace App\Http\Controllers; +use App\Enums\PricingPeriod; use App\Models\Tier; +use App\Models\TierPrice; use Illuminate\Http\Request; use App\Services\PayPalService; use App\Http\Requests\ValidatePledge; +use Illuminate\Support\Facades\Log; use Srmklive\PayPal\Services\PayPal as PayPalClient; class PayPalController extends Controller @@ -62,16 +65,29 @@ public function successTransaction(Request $request) if (isset($response['status']) && $response['status'] == 'COMPLETED') { $pledge = $response['purchase_units']['0']['reference_id']; + Log::info('Paypal', $response); $this->service ->user($request->user()) ->subscribe($pledge); $routeOptions = ['success' => 1]; $flash = 'subscribed'; + /** @var ?Tier $tier */ + $tier = Tier::where('name', $pledge)->first(); + /** @var ?TierPrice $tierPrice */ + $tierPrice = $tier->prices() + ->where('currency', $request->user()->currency()) + ->where('period', PricingPeriod::Yearly) + ->first(); + + return redirect() ->route('settings.subscription', $routeOptions) ->with('success', __('settings.subscription.success.subscribed')) - ->with('sub_tracking', $flash); + ->with('sub_tracking', $flash) + ->with('sub_id', $tierPrice?->id) + ->with('sub_value', $response['purchase_units']['0']['payments']['captures'][0]['amount']['value']) + ; } else { return redirect() ->route('settings.subscription') diff --git a/app/Http/Controllers/Settings/SubscriptionController.php b/app/Http/Controllers/Settings/SubscriptionController.php index 3d8253c12..1957a7c65 100644 --- a/app/Http/Controllers/Settings/SubscriptionController.php +++ b/app/Http/Controllers/Settings/SubscriptionController.php @@ -9,6 +9,7 @@ use App\Http\Requests\Settings\UserSubscribeStore; use App\Jobs\Users\AbandonedCart; use App\Models\Tier; +use App\Models\TierPrice; use App\Services\SubscriptionService; use App\Services\SubscriptionUpgradeService; use App\Services\Users\CurrencyService; @@ -59,10 +60,11 @@ public function index() $currency = $user->currencySymbol(); $invoices = !empty($user->stripe_id) ? $user->invoices(true, ['limit' => 3]) : []; $tracking = session()->get('sub_tracking'); + $newSubPricingId = session()->get('sub_id'); $tiers = Tier::with('prices')->ordered()->get(); $isPayPal = $user->hasPayPal(); $hasManual = $user->hasManualSubscription(); - $gaTrackingEvent = null; + $gaTrackingEvent = $gaPurchase = null; if (!empty($tracking)) { $gaTrackingEvent = 'TJhYCMDErpYDEOaOq7oC'; if ($tracking === 'subscribed') { @@ -73,6 +75,19 @@ public function index() } } + if (!empty($newSubPricingId)) { + /** @var TierPrice $pricing */ + $pricing = TierPrice::find($newSubPricingId); + $gaPurchase = [ + 'value' => $pricing->cost, + 'currency' => $pricing->currency, + 'coupon' => session()->get('sub_coupon'), + 'item_id' => $pricing->tier->id, + 'item_name' => $pricing->tier->name . ($pricing->isYearly() ? ' Yearly' : null), + ]; + } + + return view('settings.subscription.index', compact( 'stripeApiToken', 'status', @@ -83,6 +98,7 @@ public function index() 'invoices', 'tracking', 'gaTrackingEvent', + 'gaPurchase', 'tiers', 'isPayPal', 'hasManual', @@ -184,7 +200,10 @@ public function subscribe(UserSubscribeStore $request, Tier $tier) ->route('settings.subscription', $routeOptions) ->withSuccess(__('settings.subscription.success.' . $flash)) ->with('sub_tracking', $flash) - ->with('sub_value', $this->subscription->subscriptionValue()); + ->with('sub_value', $this->subscription->subscriptionValue()) + ->with('sub_coupon', $request->get('coupon')) + ->with('sub_id', $this->subscription->tierPrice()->id) + ; } catch (IncompletePayment $exception) { session()->put('subscription_callback', $request->get('payment_id')); return redirect()->route( diff --git a/app/Services/SubscriptionService.php b/app/Services/SubscriptionService.php index 3949c890f..532fb04a4 100644 --- a/app/Services/SubscriptionService.php +++ b/app/Services/SubscriptionService.php @@ -392,7 +392,7 @@ protected function isYearly(): bool return $this->period === PricingPeriod::Yearly; } - protected function tierPrice(): TierPrice + public function tierPrice(): TierPrice { if (isset($this->tierPrice)) { return $this->tierPrice; diff --git a/resources/views/layouts/tracking/tracking.blade.php b/resources/views/layouts/tracking/tracking.blade.php index a19402c89..14d03a5a0 100644 --- a/resources/views/layouts/tracking/tracking.blade.php +++ b/resources/views/layouts/tracking/tracking.blade.php @@ -20,5 +20,19 @@ function gtag(){dataLayer.push(arguments);} @if (isset($gaTrackingEvent) && !empty($gaTrackingEvent)) @endif + @if (isset($gaPurchase) && !empty($gaPurchase)) + + @endif @endif From 7e5a9227a9232c95c8bb85be0aa7e2f948c71fed Mon Sep 17 00:00:00 2001 From: ilestis Date: Thu, 24 Oct 2024 09:50:58 -0600 Subject: [PATCH 6/7] More entity log details --- app/Models/EntityLog.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Models/EntityLog.php b/app/Models/EntityLog.php index 183a4c49a..4d9dc7ff8 100644 --- a/app/Models/EntityLog.php +++ b/app/Models/EntityLog.php @@ -175,6 +175,7 @@ public function attributeKey(string $transKey, string $attribute): string // Custom mapping $custom = [ 'header_uuid' => 'fields.header-image.title', + 'image_uuid' => 'crud.fields.image', 'is_template' => 'entities/actions.templates.toggle', 'is_attributes_private' => 'entities/attributes.fields.is_private', ]; @@ -182,9 +183,9 @@ public function attributeKey(string $transKey, string $attribute): string return __($custom[$name]); } if (app()->isProduction()) { - return '' . __('crud.users.unknown') . ''; + return '' . __('crud.users.unknown') . ''; } - return '' . $name . ''; + return '' . $name . ''; } /** From 6ef34933bcaa6216c45c49a7c8eb080612b1ec7c Mon Sep 17 00:00:00 2001 From: ilestis Date: Thu, 24 Oct 2024 12:25:55 -0600 Subject: [PATCH 7/7] Fix paypal upgrade broken --- app/Http/Controllers/Settings/SubscriptionController.php | 2 +- app/Services/PayPalService.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Settings/SubscriptionController.php b/app/Http/Controllers/Settings/SubscriptionController.php index 1957a7c65..f3b6e3997 100644 --- a/app/Http/Controllers/Settings/SubscriptionController.php +++ b/app/Http/Controllers/Settings/SubscriptionController.php @@ -115,7 +115,7 @@ public function change(Request $request, Tier $tier) $period = $request->get('period') === 'yearly' ? PricingPeriod::Yearly : PricingPeriod::Monthly; // If the user has a cancelled sub still ending - if ($user->subscribed('kanka') && $user->subscription('kanka')->onGracePeriod()) { + if ($user->subscribed('kanka') && $user->subscription('kanka')->onGracePeriod() && !$user->hasPayPal()) { return view('settings.subscription.change_blocked') ->with('user', $user); } diff --git a/app/Services/PayPalService.php b/app/Services/PayPalService.php index 2c4a0d6a8..ea68d2063 100644 --- a/app/Services/PayPalService.php +++ b/app/Services/PayPalService.php @@ -47,9 +47,9 @@ public function process(): mixed $oldPrice = $tier->yearly; } // @phpstan-ignore-next-line - $price = (floatval($price) - ($oldPrice)) * ($this->user->subscriptions()->first()->ends_at->diffInDays(Carbon::now()) / 365); - $price = number_format($price, 2); + $price = round(($price - ($oldPrice)) * ($this->user->subscriptions()->first()->ends_at->diffInDays(Carbon::now(), true) / 365), 2); } + $price = max(0, $price); $provider = new PayPal(); $provider->setApiCredentials(config('paypal'));