[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: SubscriptionCancelled.php
<?php /** --------------------------------------------------------------------------------------------------- * [Stripe Complete Onetime] * * - Cancels and subsccription that was initiated in teh stripe dashboard * @package Grow CRM * @author NextLoop *-----------------------------------------------------------------------------------------------------*/ namespace App\Cronjobs\Stripe; use App\Repositories\EventRepository; use App\Repositories\EventTrackingRepository; use App\Repositories\SubscriptionRepository; use App\Repositories\UserRepository; use Illuminate\Support\Facades\Mail; use Log; class SubscriptionCancelled { public function __invoke( EventRepository $eventrepo, EventTrackingRepository $trackingrepo, SubscriptionRepository $subscriptionrepo, UserRepository $userrepo ) { //[MT] - tenants only if (env('MT_TPYE')) { if (\Spatie\Multitenancy\Models\Tenant::current() == null) { return; } } //boot system settings middlewareBootSettings(); //[MT] boot mail settings env('MT_TPYE') ? middlewareSaaSBootMail() : middlewareBootMail(); /** * - Find webhhoks waiting to be completed * - mark the appropriate invoice as paid * - ecords timeline event & notifications * - Send thank you for your payment email to client * - Send new payment received email to admin * - Limit 20 emails at a time (for performance) */ $limit = 5; if ($webhooks = \App\Models\Webhook::Where('webhooks_gateway_name', 'stripe') ->where('webhooks_type', 'customer.subscription.deleted') ->where('webhooks_status', 'new')->take($limit)->get()) { //mark all emails in the batch as processing - to avoid batch duplicates/collisions foreach ($webhooks as $webhook) { $webhook->update([ 'webhooks_status' => 'processing', 'webhooks_started_at' => now(), ]); } //loop and process each webhook in the batch foreach ($webhooks as $webhook) { //get the subscription ID $subscription_gateway_id = $webhook->webhooks_matching_reference; Log::info("started process to cancel a subscription (ID: $subscription_gateway_id)", ['process' => '[cronjob][stripe-subscription-cancelled]', config('app.debug_ref'), 'function' => __function__, 'file' => basename(__FILE__), 'line' => __line__, 'path' => __file__, 'webhook' => $webhook]); //get the subscription if (!$subscription = \App\Models\Subscription::Where('subscription_gateway_id', $subscription_gateway_id)->first()) { Log::info("the subscription could not be found in the database", ['process' => '[cronjob][stripe-subscription-cancelled]', config('app.debug_ref'), 'function' => __function__, 'file' => basename(__FILE__), 'line' => __line__, 'path' => __file__, 'webhook' => $webhook]); //skip to next webhook in the batch $webhook->update([ 'webhooks_status' => 'completed', 'webhooks_completed_at' => now(), ]); continue; } //check if subscrption is not already marked as 'cancelled' - skip if ($subscription->subscription_status == 'cancelled') { $webhook->update([ 'webhooks_status' => 'completed', 'webhooks_completed_at' => now(), ]); continue; } //update subscription $subscription->subscription_status = 'cancelled'; $subscription->subscription_date_next_renewal = null; $subscription->save(); /** ---------------------------------------------- * record event [comment] * ----------------------------------------------*/ $data = [ 'event_creatorid' => 1, //set to admin 'event_item' => 'subscription', 'event_item_id' => $subscription->subscription_id, 'event_item_lang' => 'event_cancelled_subscription', 'event_item_content' => __('lang.subscription') . ' - ' . runtimeSubscriptionIdFormat($subscription->subscription_id), 'event_item_content2' => '', 'event_parent_type' => 'subscription', 'event_parent_id' => $subscription->subscription_id, 'event_parent_title' => $subscription->subscription_gateway_product_name, 'event_clientid' => $subscription->subscription_clientid, 'event_show_item' => 'yes', 'event_show_in_timeline' => 'yes', 'eventresource_type' => (is_numeric($subscription->subscription_projectid)) ? 'project' : 'subscription', 'eventresource_id' => (is_numeric($subscription->subscription_projectid)) ? $subscription->subscription_projectid : $subscription->subscription_id, 'event_notification_category' => 'notifications_billing_activity', ]; //record event $event_id = $eventrepo->create($data); /** -------------------------------------------------------------------------- * send email [team] [queued] * - invoice & payments users, with biling email preference enabled * --------------------------------------------------------------------------*/ $users = $userrepo->mailingListSubscriptions('email'); foreach ($users as $user) { //subscription started $mail = new \App\Mail\SubscriptionCancelled($user, [], $subscription); $mail->build(); } //track event if (is_numeric($event_id)) { $trackingrepo->recordEvent($data, $users, $event_id); } /** -------------------------------------------------------------------------- * send email [client] [queued] (no event tracking for initiator of the event) * - thank you email to user who placed order * - subscription has started * --------------------------------------------------------------------------*/ if ($user = $userrepo->getClientAccountOwner($subscription->subscription_clientid)) { //subscription started $mail = new \App\Mail\SubscriptionCancelled($user, [], $subscription); $mail->build(); } //mark webhook cronjob as done $webhook->update([ 'webhooks_status' => 'completed', 'webhooks_completed_at' => now(), ]); } } } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.87 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002