Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UpdateUserPlanAction | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| getNewUserType | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Actions\Users; |
| 4 | |
| 5 | use App\Http\Models\Auth\User; |
| 6 | use App\Http\Models\Plans; |
| 7 | use Carbon\Carbon; |
| 8 | |
| 9 | class UpdateUserPlanAction |
| 10 | { |
| 11 | // private function matchPlanName(HubspotProperties $hubspotProperty) { |
| 12 | // return match (true) { |
| 13 | // $hubspotProperty->flymsg_starter_subscription_status == "Active" => 'starter', |
| 14 | // $hubspotProperty->flymsg_growth_subscription_status == "Active" => 'growth', |
| 15 | // $hubspotProperty->flymsg_professional_subscription_status == "Active" => 'professional', |
| 16 | // $hubspotProperty->flymsg_sales_pro_teams_subscription_status == "Active" => 'sales_pro_teams', |
| 17 | // $hubspotProperty->flymsg_freemium_subscription_status == "Active" => 'freemium', |
| 18 | // default => 'freemium', |
| 19 | // }; |
| 20 | // } |
| 21 | |
| 22 | // private function getNewSubscriptionType(string $identifier) { |
| 23 | // return match ($identifier) { |
| 24 | // 'starter' => 'Starter', |
| 25 | // 'growth' => 'Growth', |
| 26 | // 'professional' => 'Sales Pro', |
| 27 | // 'sales_pro_teams' => 'Sales Pro Teams - ENT', |
| 28 | // default => 'Freemium', |
| 29 | // }; |
| 30 | // } |
| 31 | |
| 32 | // private function getNewPlanName(?Subscription $newSubscription) { |
| 33 | // return match (true) { |
| 34 | // !$newSubscription || $newSubscription?->plan?->identifier == Plans::FREEMIUM_IDENTIFIER => 'freemium', |
| 35 | // $newSubscription?->plan?->identifier == Plans::STARTER_MONTHLY_IDENTIFIER => 'starter', |
| 36 | // $newSubscription?->plan?->identifier == Plans::STARTER_YEARLY_IDENTIFIER => 'starter', |
| 37 | // $newSubscription?->plan?->identifier == Plans::GROWTH_YEARLY_IDENTIFIER => 'growth', |
| 38 | // $newSubscription?->plan?->identifier == Plans::GROWTH_MONTHLY_IDENTIFIER => 'growth', |
| 39 | // $newSubscription?->plan?->identifier == Plans::PROFESSIONAL_YEARLY_IDENTIFIER => 'professional', |
| 40 | // $newSubscription?->plan?->identifier == Plans::PROFESSIONAL_MONTHLY_IDENTIFIER => 'professional', |
| 41 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsSMB => 'sales_pro_teams', |
| 42 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsENT => 'sales_pro_teams', |
| 43 | // default => null, |
| 44 | // }; |
| 45 | // } |
| 46 | |
| 47 | // private function getNewPlanInterval(?Subscription $newSubscription) { |
| 48 | // return match (true) { |
| 49 | // !$newSubscription || $newSubscription?->plan?->identifier == Plans::FREEMIUM_IDENTIFIER => 'Lifetime', |
| 50 | // $newSubscription?->plan?->identifier == Plans::STARTER_MONTHLY_IDENTIFIER => 'Monthly', |
| 51 | // $newSubscription?->plan?->identifier == Plans::STARTER_YEARLY_IDENTIFIER => 'Annual', |
| 52 | // $newSubscription?->plan?->identifier == Plans::GROWTH_YEARLY_IDENTIFIER => 'Annual', |
| 53 | // $newSubscription?->plan?->identifier == Plans::GROWTH_MONTHLY_IDENTIFIER => 'Monthly', |
| 54 | // $newSubscription?->plan?->identifier == Plans::PROFESSIONAL_YEARLY_IDENTIFIER => 'Annual', |
| 55 | // $newSubscription?->plan?->identifier == Plans::PROFESSIONAL_MONTHLY_IDENTIFIER => 'Monthly', |
| 56 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsSMB => 'Annual', |
| 57 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsENT => 'Annual', |
| 58 | // $newSubscription?->plan?->identifier == Plans::APPSUMO_IDENTIFIER => 'Lifetime - AppSumo', |
| 59 | // $newSubscription?->plan?->identifier == Plans::DEALFUEL_IDENTIFIER => 'Lifetime - DealFuel', |
| 60 | // default => null, |
| 61 | // }; |
| 62 | // } |
| 63 | |
| 64 | // private function getNewPlanProType(?Subscription $newSubscription) { |
| 65 | // return match (true) { |
| 66 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsSMB => 'SMB', |
| 67 | // $newSubscription?->plan?->identifier == Plans::ProPlanTeamsENT => 'Enterprise', |
| 68 | // default => null, |
| 69 | // }; |
| 70 | // } |
| 71 | |
| 72 | private function getNewUserType(User $user) |
| 73 | { |
| 74 | $userRoles = $user->roles(); |
| 75 | |
| 76 | // map the user roles to change some names and return a new string split byt comma |
| 77 | return implode(",", array_map(function ($role) { |
| 78 | return match ($role) { |
| 79 | 'Reporting Admin' => 'Reporting POC', |
| 80 | 'Group Admin' => 'Group Admin Manager', |
| 81 | default => $role, |
| 82 | }; |
| 83 | }, $userRoles)); |
| 84 | } |
| 85 | |
| 86 | public function execute(User $user, ?Plans $newPlan, $endDate, bool $hasCorporatePlan) |
| 87 | { |
| 88 | $cancellation_date = Carbon::now(); |
| 89 | |
| 90 | if ($user->subscribed('main')) { |
| 91 | try { |
| 92 | $user->subscription('main')->cancel(); |
| 93 | } catch (\Exception $e) { |
| 94 | $userSub = $user->subscriptions()->latest()->first(); |
| 95 | if ($userSub) { |
| 96 | $userSub->markAsCancelledOnlyInDB(); |
| 97 | } |
| 98 | // do nothing please remove this try catch asap |
| 99 | } |
| 100 | } else { |
| 101 | $userSub = $user->subscriptions()->latest()->first(); |
| 102 | if ($userSub) { |
| 103 | $userSub->markAsCancelledOnlyInDB(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $newSubscription = $user->subscriptions()->create([ |
| 108 | 'name' => 'main', |
| 109 | 'stripe_status' => 'active', |
| 110 | 'stripe_plan' => $newPlan->stripe_id, |
| 111 | 'quantity' => "1", |
| 112 | 'ends_at' => $endDate, |
| 113 | 'starts_at' => now()->toDateTimeString() |
| 114 | ]); |
| 115 | |
| 116 | // $hubspotProperty = HubspotProperties::whereEmail($user->email)->first(); |
| 117 | |
| 118 | // if ($hubspotProperty) { |
| 119 | // $planName = $this->matchPlanName($hubspotProperty); |
| 120 | // $newPlanName = $this->getNewPlanName($newSubscription); |
| 121 | // $subscription_type = $this->getNewSubscriptionType($newPlanName); |
| 122 | // $interval = $this->getNewPlanInterval($newSubscription); |
| 123 | // $proType = $this->getNewPlanProType($newSubscription); |
| 124 | // $userType = $this->getNewUserType($user); |
| 125 | |
| 126 | // if ($planName != null) { |
| 127 | // $data = $this->cancelHubspotSubscription($hubspotProperty, $cancellation_date, $planName); |
| 128 | // $data["subscription_type"] = $subscription_type; |
| 129 | |
| 130 | // if ($user->instancy_id) { |
| 131 | // $this->updateInstancySubscription($user, $cancellation_date); |
| 132 | // } |
| 133 | // } |
| 134 | |
| 135 | // if ($newPlanName != null) { |
| 136 | // $newData = $this->updateHubspotSubscription( |
| 137 | // $newSubscription?->starts_at ? Carbon::parse($newSubscription->starts_at) : $cancellation_date, |
| 138 | // $newSubscription?->ends_at ? Carbon::parse($newSubscription->ends_at) : null, |
| 139 | // $newPlanName, |
| 140 | // $interval, |
| 141 | // $proType, |
| 142 | // $userType, |
| 143 | // ); |
| 144 | |
| 145 | // $data = array_merge($data, $newData); |
| 146 | // $data["subscription_type"] = $subscription_type; |
| 147 | |
| 148 | // if (strpos($newPlanName, 'pro') !== false) { |
| 149 | // $this->updateInstancySubscription($user, $cancellation_date); |
| 150 | // } |
| 151 | // } |
| 152 | |
| 153 | // $hubspotProperty->update($data); |
| 154 | // (new HubspotService())->batchUpdate($user->hubspot_id, $data); |
| 155 | // } |
| 156 | } |
| 157 | |
| 158 | // private function updateHubspotSubscription( |
| 159 | // Carbon $date, |
| 160 | // ?Carbon $endDate, |
| 161 | // string $subscriptionType, |
| 162 | // string $interval, |
| 163 | // ?string $proType, |
| 164 | // ?string $userType, |
| 165 | // ) |
| 166 | // { |
| 167 | // $subscriptionStatusField = "flymsg_{$subscriptionType}_subscription_status"; |
| 168 | // $cancelSubscriptionDateField = "{$subscriptionType}_cancel_subscription_date"; |
| 169 | // $subscriptionStatusUpdatedOnField = "{$subscriptionType}_subscription_status_updated_on"; |
| 170 | // $subscriptionChurnDateField = "{$subscriptionType}_subscription_churn_date"; |
| 171 | // $subscriptionExpirationDateField = "{$subscriptionType}_subscription_expiration_date"; |
| 172 | // $subscriptionIntervalField = "{$subscriptionType}_subscription_frequency"; |
| 173 | // $subscriptionProTypeField = "{$subscriptionType}_subscription_plan_type"; |
| 174 | // $subscriptionUserTypeField = "{$subscriptionType}_user_type"; |
| 175 | |
| 176 | // return [ |
| 177 | // $subscriptionStatusField => 'Active', |
| 178 | // $subscriptionStatusUpdatedOnField => $date->toDateString(), |
| 179 | // $cancelSubscriptionDateField => "", |
| 180 | // $subscriptionChurnDateField => "", |
| 181 | // $subscriptionExpirationDateField => $endDate ? $endDate->toDateString() : null, |
| 182 | // $subscriptionIntervalField => $interval, |
| 183 | // $subscriptionProTypeField => $proType, |
| 184 | // $subscriptionUserTypeField => $userType, |
| 185 | // ]; |
| 186 | // } |
| 187 | |
| 188 | // private function cancelHubspotSubscription(HubspotProperties $hubspotProperty, Carbon $cancellation_date, string $subscriptionType) |
| 189 | // { |
| 190 | // $subscriptionStatusField = "flymsg_{$subscriptionType}_subscription_status"; |
| 191 | // $cancelSubscriptionDateField = "{$subscriptionType}_cancel_subscription_date"; |
| 192 | // $subscriptionStatusUpdatedOnField = "{$subscriptionType}_subscription_status_updated_on"; |
| 193 | // $subscriptionChurnDateField = "{$subscriptionType}_subscription_churn_date"; |
| 194 | |
| 195 | // if ($hubspotProperty->$subscriptionStatusField == "Active" || $hubspotProperty->$subscriptionStatusField == "Canceled") { |
| 196 | // if ($hubspotProperty->$cancelSubscriptionDateField) { |
| 197 | // return [ |
| 198 | // $subscriptionStatusField => 'Canceled', |
| 199 | // $subscriptionStatusUpdatedOnField => $hubspotProperty->$subscriptionStatusUpdatedOnField ?? $cancellation_date->toDateString(), |
| 200 | // $cancelSubscriptionDateField => $hubspotProperty->$cancelSubscriptionDateField, |
| 201 | // $subscriptionChurnDateField => $hubspotProperty->$subscriptionChurnDateField ?? $cancellation_date->toDateString(), |
| 202 | // ]; |
| 203 | // } else { |
| 204 | // return [ |
| 205 | // $subscriptionStatusField => 'Canceled', |
| 206 | // $subscriptionStatusUpdatedOnField => $cancellation_date->toDateString(), |
| 207 | // $cancelSubscriptionDateField => $cancellation_date->toDateString(), |
| 208 | // $subscriptionChurnDateField => $cancellation_date->toDateString(), |
| 209 | // ]; |
| 210 | // } |
| 211 | // } |
| 212 | // } |
| 213 | |
| 214 | // private function updateInstancySubscription(User $user, Carbon $date) |
| 215 | // { |
| 216 | // $instancyService = new InstancyServiceV2(); |
| 217 | // $instancyService->updateMembership($user->email, $date->subDay()->toDateString()); |
| 218 | // } |
| 219 | } |