From 103fa1b974acdb79b93709516a7c2e8a43a91e15 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 19:48:54 +1100 Subject: [PATCH] Rename BillingSubscriptions to Subscriptions --- app/Console/Kernel.php | 4 +- ...pper.php => SubscriptionContextMapper.php} | 6 +- app/DataMapper/CompanySettings.php | 2 +- .../SubscriptionWasCreated.php} | 14 +- ...ionFactory.php => SubscriptionFactory.php} | 9 +- .../BillingSubscriptionPurchaseController.php | 8 +- .../OpenAPI/BillingSubscription.php | 2 +- ...troller.php => SubscriptionController.php} | 194 +++++++++--------- app/Http/Livewire/BillingPortalPurchase.php | 30 +-- .../CreateSubscriptionRequest.php} | 8 +- .../DestroySubscriptionRequest.php} | 6 +- .../EditSubscriptionRequest.php} | 6 +- .../ShowSubscriptionRequest.php} | 6 +- .../StoreSubscriptionRequest.php} | 8 +- .../UpdateSubscriptionRequest.php} | 6 +- ...scriptionCron.php => SubscriptionCron.php} | 15 +- app/Models/Company.php | 11 +- ...llingSubscription.php => Subscription.php} | 6 +- ...nObserver.php => SubscriptionObserver.php} | 24 +-- app/PaymentDrivers/BaseDriver.php | 6 +- ...ptionPolicy.php => SubscriptionPolicy.php} | 6 +- app/Providers/AppServiceProvider.php | 6 +- app/Providers/AuthServiceProvider.php | 8 +- ...ository.php => SubscriptionRepository.php} | 12 +- .../SubscriptionService.php} | 54 ++--- .../BillingSubscriptionTransformer.php | 76 ------- app/Transformers/CompanyTransformer.php | 17 +- app/Transformers/SubscriptionTransformer.php | 69 +++++++ ...ionFactory.php => SubscriptionFactory.php} | 6 +- ...2025_refactor_billing_scriptions_table.php | 29 +++ routes/api.php | 2 +- ...ionApiTest.php => SubscriptionApiTest.php} | 39 ++-- 32 files changed, 353 insertions(+), 342 deletions(-) rename app/DataMapper/Billing/{BillingContextMapper.php => SubscriptionContextMapper.php} (85%) rename app/Events/{BillingSubscription/BillingSubscriptionWasCreated.php => Subscription/SubscriptionWasCreated.php} (72%) rename app/Factory/{BillingSubscriptionFactory.php => SubscriptionFactory.php} (77%) rename app/Http/Controllers/{BillingSubscriptionController.php => SubscriptionController.php} (61%) rename app/Http/Requests/{BillingSubscription/CreateBillingSubscriptionRequest.php => Subscription/CreateSubscriptionRequest.php} (75%) rename app/Http/Requests/{BillingSubscription/EditBillingSubscriptionRequest.php => Subscription/DestroySubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/DestroyBillingSubscriptionRequest.php => Subscription/EditSubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/ShowBillingSubscriptionRequest.php => Subscription/ShowSubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/StoreBillingSubscriptionRequest.php => Subscription/StoreSubscriptionRequest.php} (88%) rename app/Http/Requests/{BillingSubscription/UpdateBillingSubscriptionRequest.php => Subscription/UpdateSubscriptionRequest.php} (80%) rename app/Jobs/Cron/{BillingSubscriptionCron.php => SubscriptionCron.php} (76%) rename app/Models/{BillingSubscription.php => Subscription.php} (91%) rename app/Observers/{BillingSubscriptionObserver.php => SubscriptionObserver.php} (56%) rename app/Policies/{BillingSubscriptionPolicy.php => SubscriptionPolicy.php} (79%) rename app/Repositories/{BillingSubscriptionRepository.php => SubscriptionRepository.php} (58%) rename app/Services/{BillingSubscription/BillingSubscriptionService.php => Subscription/SubscriptionService.php} (78%) delete mode 100644 app/Transformers/BillingSubscriptionTransformer.php create mode 100644 app/Transformers/SubscriptionTransformer.php rename database/factories/{BillingSubscriptionFactory.php => SubscriptionFactory.php} (81%) create mode 100644 database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php rename tests/Feature/{BillingSubscriptionApiTest.php => SubscriptionApiTest.php} (68%) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 0c908d25a7ee..2cf0371b23e4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -11,7 +11,7 @@ namespace App\Console; -use App\Jobs\Cron\BillingSubscriptionCron; +use App\Jobs\Cron\SubscriptionCron; use App\Jobs\Cron\RecurringInvoicesCron; use App\Jobs\Ninja\AdjustEmailQuota; use App\Jobs\Ninja\CompanySizeCheck; @@ -54,7 +54,7 @@ class Kernel extends ConsoleKernel $schedule->job(new UpdateExchangeRates)->daily()->withoutOverlapping(); - $schedule->job(new BillingSubscriptionCron)->daily()->withoutOverlapping(); + $schedule->job(new SubscriptionCron)->daily()->withoutOverlapping(); $schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping(); diff --git a/app/DataMapper/Billing/BillingContextMapper.php b/app/DataMapper/Billing/SubscriptionContextMapper.php similarity index 85% rename from app/DataMapper/Billing/BillingContextMapper.php rename to app/DataMapper/Billing/SubscriptionContextMapper.php index 3d878f38841e..fa5aa18a34c9 100644 --- a/app/DataMapper/Billing/BillingContextMapper.php +++ b/app/DataMapper/Billing/SubscriptionContextMapper.php @@ -13,12 +13,12 @@ namespace App\DataMapper\Billing; -class BillingContextMapper +class SubscriptionContextMapper { /** * @var int */ - public $billing_subscription_id; + public $subscription_id; /** * @var string @@ -39,7 +39,7 @@ class BillingContextMapper * @var string[] */ public $casts = [ - 'billing_subscription_id' => 'integer', + 'subscription_id' => 'integer', 'email' => 'string', 'client_id' => 'integer', 'invoice_id' => 'integer', diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index ea342769bd7e..049f807d5743 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -109,7 +109,7 @@ class CompanySettings extends BaseSettings public $shared_invoice_quote_counter = false; //@implemented public $shared_invoice_credit_counter = false; //@implemented - public $recurring_number_prefix = 'R'; //@implemented + public $recurring_number_prefix = ''; //@implemented public $reset_counter_frequency_id = '0'; //@implemented public $reset_counter_date = ''; //@implemented public $counter_padding = 4; //@implemented diff --git a/app/Events/BillingSubscription/BillingSubscriptionWasCreated.php b/app/Events/Subscription/SubscriptionWasCreated.php similarity index 72% rename from app/Events/BillingSubscription/BillingSubscriptionWasCreated.php rename to app/Events/Subscription/SubscriptionWasCreated.php index 73fc2d1f2f87..67d061f6738d 100644 --- a/app/Events/BillingSubscription/BillingSubscriptionWasCreated.php +++ b/app/Events/Subscription/SubscriptionWasCreated.php @@ -1,8 +1,8 @@ billing_subscription = $billing_subscription; + $this->subscription = $subscription; $this->company = $company; $this->event_vars = $event_vars; } diff --git a/app/Factory/BillingSubscriptionFactory.php b/app/Factory/SubscriptionFactory.php similarity index 77% rename from app/Factory/BillingSubscriptionFactory.php rename to app/Factory/SubscriptionFactory.php index f9f051293e9c..6e9c912bb776 100644 --- a/app/Factory/BillingSubscriptionFactory.php +++ b/app/Factory/SubscriptionFactory.php @@ -11,14 +11,13 @@ namespace App\Factory; +use App\Models\Subscription; -use App\Models\BillingSubscription; - -class BillingSubscriptionFactory +class SubscriptionFactory { - public static function create(int $company_id, int $user_id): BillingSubscription + public static function create(int $company_id, int $user_id): Subscription { - $billing_subscription = new BillingSubscription(); + $billing_subscription = new Subscription(); $billing_subscription->company_id = $company_id; $billing_subscription->user_id = $user_id; diff --git a/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php b/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php index b6a545e96b53..d39d8ca36111 100644 --- a/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php +++ b/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php @@ -13,23 +13,23 @@ namespace App\Http\Controllers\ClientPortal; use App\Http\Controllers\Controller; -use App\Models\BillingSubscription; +use App\Models\Subscription; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -class BillingSubscriptionPurchaseController extends Controller +class SubscriptionPurchaseController extends Controller { - public function index(BillingSubscription $billing_subscription, Request $request) + public function index(Subscription $subscription, Request $request) { if ($request->has('locale')) { $this->setLocale($request->query('locale')); } return view('billing-portal.purchase', [ - 'billing_subscription' => $billing_subscription, + 'subscription' => $subscription, 'hash' => Str::uuid()->toString(), 'request_data' => $request->all(), ]); diff --git a/app/Http/Controllers/OpenAPI/BillingSubscription.php b/app/Http/Controllers/OpenAPI/BillingSubscription.php index fad9b1eac32f..c088160781d6 100644 --- a/app/Http/Controllers/OpenAPI/BillingSubscription.php +++ b/app/Http/Controllers/OpenAPI/BillingSubscription.php @@ -1,7 +1,7 @@ billing_subscription_repo = $billing_subscription_repo; + $this->subscription_repo = $subscription_repo; } /** - * Show the list of BillingSubscriptions. + * Show the list of Subscriptions. * * @return Response * * @OA\Get( - * path="/api/v1/billing_subscriptions", - * operationId="getBillingSubscriptions", - * tags={"billing_subscriptions"}, - * summary="Gets a list of billing_subscriptions", - * description="Lists billing_subscriptions.", + * path="/api/v1/subscriptions", + * operationId="getSubscriptions", + * tags={"subscriptions"}, + * summary="Gets a list of subscriptions", + * description="Lists subscriptions.", * * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), @@ -58,11 +58,11 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="A list of billing_subscriptions", + * description="A list of subscriptions", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -79,24 +79,24 @@ class BillingSubscriptionController extends BaseController public function index(): \Illuminate\Http\Response { - $billing_subscriptions = BillingSubscription::query()->company(); + $subscriptions = Subscription::query()->company(); - return $this->listResponse($billing_subscriptions); + return $this->listResponse($subscriptions); } /** * Show the form for creating a new resource. * - * @param CreateBillingSubscriptionRequest $request The request + * @param CreateSubscriptionRequest $request The request * * @return Response * * * @OA\Get( - * path="/api/v1/billing_subscriptions/create", - * operationId="getBillingSubscriptionsCreate", - * tags={"billing_subscriptions"}, - * summary="Gets a new blank billing_subscriptions object", + * path="/api/v1/subscriptions/create", + * operationId="getSubscriptionsCreate", + * tags={"subscriptions"}, + * summary="Gets a new blank subscriptions object", * description="Returns a blank object with default values", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), @@ -104,11 +104,11 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="A blank billing_subscriptions object", + * description="A blank subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -123,38 +123,38 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function create(CreateBillingSubscriptionRequest $request): \Illuminate\Http\Response + public function create(CreateSubscriptionRequest $request): \Illuminate\Http\Response { - $billing_subscription = BillingSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); + $subscription = SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Store a newly created resource in storage. * - * @param StoreBillingSubscriptionRequest $request The request + * @param StoreSubscriptionRequest $request The request * * @return Response * * * @OA\Post( - * path="/api/v1/billing_subscriptions", - * operationId="storeBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Adds a billing_subscriptions", - * description="Adds an billing_subscriptions to the system", + * path="/api/v1/subscriptions", + * operationId="storeSubscription", + * tags={"subscriptions"}, + * summary="Adds a subscriptions", + * description="Adds an subscriptions to the system", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="Returns the saved billing_subscriptions object", + * description="Returns the saved subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -169,30 +169,30 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function store(StoreBillingSubscriptionRequest $request): \Illuminate\Http\Response + public function store(StoreSubscriptionRequest $request): \Illuminate\Http\Response { - $billing_subscription = $this->billing_subscription_repo->save($request->all(), BillingSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id)); + $subscription = $this->subscription_repo->save($request->all(), SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id)); - event(new BillingsubscriptionWasCreated($billing_subscription, $billing_subscription->company, Ninja::eventVars())); + event(new SubscriptionWasCreated($subscription, $subscription->company, Ninja::eventVars())); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Display the specified resource. * - * @param ShowBillingSubscriptionRequest $request The request - * @param Invoice $billing_subscription The invoice + * @param ShowSubscriptionRequest $request The request + * @param Invoice $subscription The invoice * * @return Response * * * @OA\Get( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="showBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Shows an billing_subscriptions", - * description="Displays an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="showSubscription", + * tags={"subscriptions"}, + * summary="Shows an subscriptions", + * description="Displays an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -200,7 +200,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -210,11 +210,11 @@ class BillingSubscriptionController extends BaseController * ), * @OA\Response( * response=200, - * description="Returns the BillingSubscription object", + * description="Returns the Subscription object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -229,25 +229,25 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function show(ShowBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function show(ShowSubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Show the form for editing the specified resource. * - * @param EditBillingSubscriptionRequest $request The request - * @param Invoice $billing_subscription The invoice + * @param EditSubscriptionRequest $request The request + * @param Invoice $subscription The invoice * * @return Response * * @OA\Get( - * path="/api/v1/billing_subscriptions/{id}/edit", - * operationId="editBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Shows an billing_subscriptions for editting", - * description="Displays an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}/edit", + * operationId="editSubscription", + * tags={"subscriptions"}, + * summary="Shows an subscriptions for editting", + * description="Displays an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -255,7 +255,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -269,7 +269,7 @@ class BillingSubscriptionController extends BaseController * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -284,26 +284,26 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function edit(EditBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function edit(EditSubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Update the specified resource in storage. * - * @param UpdateBillingSubscriptionRequest $request The request - * @param BillingSubscription $billing_subscription The invoice + * @param UpdateSubscriptionRequest $request The request + * @param Subscription $subscription The invoice * * @return Response * * * @OA\Put( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="updateBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Updates an billing_subscriptions", - * description="Handles the updating of an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="updateSubscription", + * tags={"subscriptions"}, + * summary="Updates an subscriptions", + * description="Handles the updating of an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -311,7 +311,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -321,11 +321,11 @@ class BillingSubscriptionController extends BaseController * ), * @OA\Response( * response=200, - * description="Returns the billing_subscriptions object", + * description="Returns the subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -340,32 +340,32 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function update(UpdateBillingSubscriptionRequest $request, BillingSubscription $billing_subscription) + public function update(UpdateSubscriptionRequest $request, Subscription $subscription) { - if ($request->entityIsDeleted($billing_subscription)) { + if ($request->entityIsDeleted($subscription)) { return $request->disallowUpdate(); } - $billing_subscription = $this->billing_subscription_repo->save($request->all(), $billing_subscription); + $subscription = $this->subscription_repo->save($request->all(), $subscription); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Remove the specified resource from storage. * - * @param DestroyBillingSubscriptionRequest $request - * @param BillingSubscription $invoice + * @param DestroySubscriptionRequest $request + * @param Subscription $invoice * * @return Response * * @throws \Exception * @OA\Delete( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="deleteBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Deletes a billing_subscriptions", - * description="Handles the deletion of an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="deleteSubscription", + * tags={"subscriptions"}, + * summary="Deletes a subscriptions", + * description="Handles the deletion of an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -373,7 +373,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -401,10 +401,10 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function destroy(DestroyBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function destroy(DestroySubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - $this->billing_subscription_repo->delete($billing_subscription); + $this->subscription_repo->delete($subscription); - return $this->itemResponse($billing_subscription->fresh()); + return $this->itemResponse($subscription->fresh()); } } diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index 6529c4cff065..ab4dc76cb341 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -12,7 +12,7 @@ namespace App\Http\Livewire; use App\Factory\ClientFactory; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\ClientContact; use App\Models\Invoice; use App\Repositories\ClientContactRepository; @@ -55,11 +55,11 @@ class BillingPortalPurchase extends Component public $password; /** - * Instance of billing subscription. + * Instance of subscription. * - * @var BillingSubscription + * @var Subscription */ - public $billing_subscription; + public $subscription; /** * Instance of client contact. @@ -186,8 +186,8 @@ class BillingPortalPurchase extends Component */ protected function createBlankClient() { - $company = $this->billing_subscription->company; - $user = $this->billing_subscription->user; + $company = $this->subscription->company; + $user = $this->subscription->user; $client_repo = new ClientRepository(new ClientContactRepository()); @@ -224,7 +224,7 @@ class BillingPortalPurchase extends Component */ protected function getPaymentMethods(ClientContact $contact): self { - if ($this->billing_subscription->trial_enabled) { + if ($this->subscription->trial_enabled) { $this->heading_text = ctrans('texts.plan_trial'); $this->steps['show_start_trial'] = true; @@ -274,11 +274,11 @@ class BillingPortalPurchase extends Component 'client_contact_id' => $this->contact->hashed_id, ]], 'user_input_promo_code' => $this->coupon, - 'coupon' => empty($this->billing_subscription->promo_code) ? '' : $this->coupon, + 'coupon' => empty($this->subscription->promo_code) ? '' : $this->coupon, 'quantity' => $this->quantity, ]; - $this->invoice = $this->billing_subscription + $this->invoice = $this->subscription ->service() ->createInvoice($data) ->service() @@ -287,12 +287,12 @@ class BillingPortalPurchase extends Component ->save(); Cache::put($this->hash, [ - 'billing_subscription_id' => $this->billing_subscription->id, + 'subscription_id' => $this->subscription->id, 'email' => $this->email ?? $this->contact->email, 'client_id' => $this->contact->client->id, 'invoice_id' => $this->invoice->id, 'quantity' => $this->quantity, - 'subscription_id' => $this->billing_subscription->id, + 'subscription_id' => $this->subscription->id, now()->addMinutes(60)] ); @@ -306,7 +306,7 @@ class BillingPortalPurchase extends Component */ public function handleTrial() { - return $this->billing_subscription->service()->startTrial([ + return $this->subscription->service()->startTrial([ 'email' => $this->email ?? $this->contact->email, 'quantity' => $this->quantity, 'contact_id' => $this->contact->id, @@ -325,17 +325,17 @@ class BillingPortalPurchase extends Component return $this->quantity; } - if ($this->quantity >= $this->billing_subscription->max_seats_limit && $option == 'increment') { + if ($this->quantity >= $this->subscription->max_seats_limit && $option == 'increment') { return $this->quantity; } if ($option == 'increment') { $this->quantity++; - return $this->price = (int)$this->price + $this->billing_subscription->product->price; + return $this->price = (int)$this->price + $this->subscription->product->price; } $this->quantity--; - $this->price = (int)$this->price - $this->billing_subscription->product->price; + $this->price = (int)$this->price - $this->subscription->product->price; return 0; } diff --git a/app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/CreateSubscriptionRequest.php similarity index 75% rename from app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/CreateSubscriptionRequest.php index f4dfbe25d4b0..c3572cb7be1f 100644 --- a/app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/CreateSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class CreateBillingSubscriptionRequest extends Request +class CreateSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class CreateBillingSubscriptionRequest extends Request */ public function authorize(): bool { - return auth()->user()->can('create', BillingSubscription::class); + return auth()->user()->can('create', Subscription::class); } /** diff --git a/app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/DestroySubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/DestroySubscriptionRequest.php index 94d285770b65..7cb4e16e51c4 100644 --- a/app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/DestroySubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class EditBillingSubscriptionRequest extends Request +class DestroySubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class EditBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/EditSubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/EditSubscriptionRequest.php index 399cdceb2f0b..aae8d1ea10e5 100644 --- a/app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/EditSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class DestroyBillingSubscriptionRequest extends Request +class EditSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class DestroyBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/ShowSubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/ShowSubscriptionRequest.php index 18ba6ab68f7d..1b24c7779018 100644 --- a/app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/ShowSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class ShowBillingSubscriptionRequest extends Request +class ShowSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class ShowBillingSubscriptionRequest extends Request */ public function authorize() : bool { - return auth()->user()->can('view', $this->billing_subscription); + return auth()->user()->can('view', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php similarity index 88% rename from app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/StoreSubscriptionRequest.php index 0c5a64c2ead6..e142d4578f1b 100644 --- a/app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class StoreBillingSubscriptionRequest extends Request +class StoreSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class StoreBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('create', BillingSubscription::class); + return auth()->user()->can('create', Subscription::class); } /** diff --git a/app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/UpdateSubscriptionRequest.php similarity index 80% rename from app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/UpdateSubscriptionRequest.php index 78d41deb35e6..4b7a0c4bbe46 100644 --- a/app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/UpdateSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use App\Utils\Traits\ChecksEntityStatus; -class UpdateBillingSubscriptionRequest extends Request +class UpdateSubscriptionRequest extends Request { use ChecksEntityStatus; @@ -25,7 +25,7 @@ class UpdateBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Jobs/Cron/BillingSubscriptionCron.php b/app/Jobs/Cron/SubscriptionCron.php similarity index 76% rename from app/Jobs/Cron/BillingSubscriptionCron.php rename to app/Jobs/Cron/SubscriptionCron.php index 3aef58c290cd..1fdd1812d82f 100644 --- a/app/Jobs/Cron/BillingSubscriptionCron.php +++ b/app/Jobs/Cron/SubscriptionCron.php @@ -12,11 +12,10 @@ namespace App\Jobs\Cron; use App\Libraries\MultiDB; -use App\Models\ClientSubscription; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Carbon; -class BillingSubscriptionCron +class SubscriptionCron { use Dispatchable; @@ -52,11 +51,13 @@ class BillingSubscriptionCron private function loopSubscriptions() { - $client_subs = ClientSubscription::whereNull('deleted_at') - ->cursor() - ->each(function ($cs){ - $this->processSubscription($cs); - }); + //looop recurring invoices with subscription id + + // $client_subs = ClientSubscription::whereNull('deleted_at') + // ->cursor() + // ->each(function ($cs){ + // $this->processSubscription($cs); + // }); } /* Our daily cron should check diff --git a/app/Models/Company.php b/app/Models/Company.php index 1982f6a3c72c..e2edb0d28314 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -159,15 +159,10 @@ class Company extends BaseModel { return $this->hasMany(ExpenseCategory::class)->withTrashed(); } - - public function client_subscriptions() + + public function subscriptions() { - return $this->hasMany(ClientSubscription::class)->withTrashed(); - } - - public function billing_subscriptions() - { - return $this->hasMany(BillingSubscription::class)->withTrashed(); + return $this->hasMany(Subscription::class)->withTrashed(); } public function task_statuses() diff --git a/app/Models/BillingSubscription.php b/app/Models/Subscription.php similarity index 91% rename from app/Models/BillingSubscription.php rename to app/Models/Subscription.php index 50b68eae796f..7b588bddfc97 100644 --- a/app/Models/BillingSubscription.php +++ b/app/Models/Subscription.php @@ -11,12 +11,12 @@ namespace App\Models; -use App\Services\BillingSubscription\BillingSubscriptionService; +use App\Services\Subscription\SubscriptionService; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -class BillingSubscription extends BaseModel +class Subscription extends BaseModel { use HasFactory, SoftDeletes; @@ -56,7 +56,7 @@ class BillingSubscription extends BaseModel public function service() { - return new BillingSubscriptionService($this); + return new SubscriptionService($this); } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/app/Observers/BillingSubscriptionObserver.php b/app/Observers/SubscriptionObserver.php similarity index 56% rename from app/Observers/BillingSubscriptionObserver.php rename to app/Observers/SubscriptionObserver.php index dc813c732544..7afa2f4c8da1 100644 --- a/app/Observers/BillingSubscriptionObserver.php +++ b/app/Observers/SubscriptionObserver.php @@ -11,17 +11,17 @@ namespace App\Observers; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class BillingSubscriptionObserver +class SubscriptionObserver { /** * Handle the billing_subscription "created" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function created(BillingSubscription $billing_subscription) + public function created(Subscription $subscription) { // } @@ -29,10 +29,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "updated" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function updated(BillingSubscription $billing_subscription) + public function updated(Subscription $subscription) { // } @@ -40,10 +40,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "deleted" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function deleted(BillingSubscription $billing_subscription) + public function deleted(Subscription $subscription) { // } @@ -51,10 +51,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "restored" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function restored(BillingSubscription $billing_subscription) + public function restored(Subscription $subscription) { // } @@ -62,10 +62,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "force deleted" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function forceDeleted(BillingSubscription $billing_subscription) + public function forceDeleted(Subscription $subscription) { // } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index cb639b5e219f..18f7cb08542e 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -30,7 +30,7 @@ use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; use App\Models\SystemLog; -use App\Services\BillingSubscription\BillingSubscriptionService; +use App\Services\Subscription\SubscriptionService; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use App\Utils\Traits\SystemLogTrait; @@ -242,9 +242,9 @@ class BaseDriver extends AbstractPaymentDriver event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); if (property_exists($this->payment_hash->data, 'billing_context')) { - $billing_subscription = \App\Models\BillingSubscription::find($this->payment_hash->data->billing_context->billing_subscription_id); + $billing_subscription = \App\Models\Subscription::find($this->payment_hash->data->billing_context->billing_subscription_id); - (new BillingSubscriptionService($billing_subscription))->completePurchase($this->payment_hash); + (new SubscriptionService($billing_subscription))->completePurchase($this->payment_hash); } return $payment->service()->applyNumber()->save(); diff --git a/app/Policies/BillingSubscriptionPolicy.php b/app/Policies/SubscriptionPolicy.php similarity index 79% rename from app/Policies/BillingSubscriptionPolicy.php rename to app/Policies/SubscriptionPolicy.php index aee6b5e190de..82f3b160651b 100644 --- a/app/Policies/BillingSubscriptionPolicy.php +++ b/app/Policies/SubscriptionPolicy.php @@ -14,9 +14,9 @@ namespace App\Policies; use App\Models\User; /** - * Class BillingSubscriptionPolicy. + * Class SubscriptionPolicy. */ -class BillingSubscriptionPolicy extends EntityPolicy +class SubscriptionPolicy extends EntityPolicy { /** * Checks if the user has create permissions. @@ -26,6 +26,6 @@ class BillingSubscriptionPolicy extends EntityPolicy */ public function create(User $user) : bool { - return $user->isAdmin() || $user->hasPermission('create_billing_subscription') || $user->hasPermission('create_all'); + return $user->isAdmin() || $user->hasPermission('create_subscription') || $user->hasPermission('create_all'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c017572692c3..058ec2835aa0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -12,7 +12,7 @@ namespace App\Providers; use App\Models\Account; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; use App\Models\ClientSubscription; use App\Models\Company; @@ -28,7 +28,7 @@ use App\Models\Quote; use App\Models\Task; use App\Models\User; use App\Observers\AccountObserver; -use App\Observers\BillingSubscriptionObserver; +use App\Observers\SubscriptionObserver; use App\Observers\ClientObserver; use App\Observers\ClientSubscriptionObserver; use App\Observers\CompanyGatewayObserver; @@ -80,7 +80,7 @@ class AppServiceProvider extends ServiceProvider Schema::defaultStringLength(191); Account::observe(AccountObserver::class); - BillingSubscription::observe(BillingSubscriptionObserver::class); + Subscription::observe(SubscriptionObserver::class); Client::observe(ClientObserver::class); ClientSubscription::observe(ClientSubscriptionObserver::class); Company::observe(CompanyObserver::class); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index d71a5ae1435f..5057e976c64b 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -12,9 +12,8 @@ namespace App\Providers; use App\Models\Activity; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; -use App\Models\ClientSubscription; use App\Models\Company; use App\Models\CompanyGateway; use App\Models\CompanyToken; @@ -39,7 +38,7 @@ use App\Models\User; use App\Models\Vendor; use App\Models\Webhook; use App\Policies\ActivityPolicy; -use App\Policies\BillingSubscriptionPolicy; +use App\Policies\SubscriptionPolicy; use App\Policies\ClientPolicy; use App\Policies\ClientSubscriptionPolicy; use App\Policies\CompanyGatewayPolicy; @@ -77,9 +76,8 @@ class AuthServiceProvider extends ServiceProvider */ protected $policies = [ Activity::class => ActivityPolicy::class, - BillingSubscription::class => BillingSubscriptionPolicy::class, + Subscription::class => SubscriptionPolicy::class, Client::class => ClientPolicy::class, - ClientSubscription::class => ClientSubscriptionPolicy::class, Company::class => CompanyPolicy::class, CompanyToken::class => CompanyTokenPolicy::class, CompanyGateway::class => CompanyGatewayPolicy::class, diff --git a/app/Repositories/BillingSubscriptionRepository.php b/app/Repositories/SubscriptionRepository.php similarity index 58% rename from app/Repositories/BillingSubscriptionRepository.php rename to app/Repositories/SubscriptionRepository.php index 68be2473468d..4a7a5469facc 100644 --- a/app/Repositories/BillingSubscriptionRepository.php +++ b/app/Repositories/SubscriptionRepository.php @@ -13,16 +13,16 @@ namespace App\Repositories; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class BillingSubscriptionRepository extends BaseRepository +class SubscriptionRepository extends BaseRepository { - public function save($data, BillingSubscription $billing_subscription): ?BillingSubscription + public function save($data, Subscription $subscription): ?Subscription { - $billing_subscription + $subscription ->fill($data) ->save(); - return $billing_subscription; + return $subscription; } -} +} \ No newline at end of file diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/Subscription/SubscriptionService.php similarity index 78% rename from app/Services/BillingSubscription/BillingSubscriptionService.php rename to app/Services/Subscription/SubscriptionService.php index acb39acf4635..74b83b4e667b 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -9,13 +9,13 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Services\BillingSubscription; +namespace App\Services\Subscription; use App\DataMapper\InvoiceItem; use App\Factory\InvoiceFactory; use App\Factory\InvoiceToRecurringInvoiceFactory; use App\Jobs\Util\SystemLogger; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\ClientContact; use App\Models\ClientSubscription; use App\Models\Invoice; @@ -27,20 +27,20 @@ use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\MakesHash; use GuzzleHttp\RequestOptions; -class BillingSubscriptionService +class SubscriptionService { use MakesHash; use CleanLineItems; - /** @var billing_subscription */ - private $billing_subscription; + /** @var subscription */ + private $subscription; /** @var client_subscription */ private $client_subscription; - public function __construct(BillingSubscription $billing_subscription) + public function __construct(Subscription $subscription) { - $this->billing_subscription = $billing_subscription; + $this->subscription = $subscription; } public function completePurchase(PaymentHash $payment_hash) @@ -70,16 +70,16 @@ class BillingSubscriptionService { // Redirects from here work just fine. Livewire will respect it. - if(!$this->billing_subscription->trial_enabled) + if(!$this->subscription->trial_enabled) return new \Exception("Trials are disabled for this product"); $contact = ClientContact::with('client')->find($data['contact_id']); $cs = new ClientSubscription(); - $cs->subscription_id = $this->billing_subscription->id; - $cs->company_id = $this->billing_subscription->company_id; + $cs->subscription_id = $this->subscription->id; + $cs->company_id = $this->subscription->company_id; $cs->trial_started = time(); - $cs->trial_ends = time() + $this->billing_subscription->trial_duration; + $cs->trial_ends = time() + $this->subscription->trial_duration; $cs->quantity = $data['quantity']; $cs->client_id = $contact->client->id; $cs->save(); @@ -89,8 +89,8 @@ class BillingSubscriptionService //execute any webhooks $this->triggerWebhook(); - if(strlen($this->billing_subscription->webhook_configuration->post_purchase_url) >=1) - return redirect($this->billing_subscription->webhook_configuration->post_purchase_url); + if(strlen($this->subscription->webhook_configuration->post_purchase_url) >=1) + return redirect($this->subscription->webhook_configuration->post_purchase_url); return redirect('/client/subscription/'.$cs->hashed_id); } @@ -102,7 +102,7 @@ class BillingSubscriptionService $data['line_items'] = $this->cleanItems($this->createLineItems($data)); - return $invoice_repo->save($data, InvoiceFactory::create($this->billing_subscription->company_id, $this->billing_subscription->user_id)); + return $invoice_repo->save($data, InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id)); } @@ -115,7 +115,7 @@ class BillingSubscriptionService $line_items = []; - $product = $this->billing_subscription->product; + $product = $this->subscription->product; $item = new InvoiceItem; $item->quantity = $data['quantity']; @@ -139,7 +139,7 @@ class BillingSubscriptionService //do we have a promocode? enter this as a line item. - if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->billing_subscription->promo_code) && $this->billing_subscription->promo_discount > 0) + if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0) $line_items[] = $this->createPromoLine($data); return $line_items; @@ -153,16 +153,16 @@ class BillingSubscriptionService private function createPromoLine($data) { - $product = $this->billing_subscription->product; + $product = $this->subscription->product; $discounted_amount = 0; $discount = 0; $amount = $data['quantity'] * $product->cost; - if ($this->billing_subscription->is_amount_discount == true) { - $discount = $this->billing_subscription->promo_discount; + if ($this->subscription->is_amount_discount == true) { + $discount = $this->subscription->promo_discount; } else { - $discount = round($amount * ($this->billing_subscription->promo_discount / 100), 2); + $discount = round($amount * ($this->subscription->promo_discount / 100), 2); } $discounted_amount = $amount - $discount; @@ -203,8 +203,8 @@ class BillingSubscriptionService //is this a recurring or one off subscription. $cs = new ClientSubscription(); - $cs->subscription_id = $this->billing_subscription->id; - $cs->company_id = $this->billing_subscription->company_id; + $cs->subscription_id = $this->subscription->id; + $cs->company_id = $this->subscription->company_id; $cs->invoice_id = $payment_hash->billing_context->invoice_id; $cs->client_id = $payment_hash->billing_context->client_id; @@ -212,10 +212,10 @@ class BillingSubscriptionService //if is_recurring //create recurring invoice from invoice - if($this->billing_subscription->is_recurring) + if($this->subscription->is_recurring) { $recurring_invoice = $this->convertInvoiceToRecurring($payment_hash); - $recurring_invoice->frequency_id = $this->billing_subscription->frequency_id; + $recurring_invoice->frequency_id = $this->subscription->frequency_id; $recurring_invoice->next_send_date = $recurring_invoice->nextDateByFrequency(now()->format('Y-m-d')); $recurring_invoice->save(); $cs->recurring_invoice_id = $recurring_invoice->id; @@ -236,15 +236,15 @@ class BillingSubscriptionService //hit the webhook to after a successful onboarding $body = [ - 'billing_subscription' => $this->billing_subscription, + 'subscription' => $this->subscription, 'client_subscription' => $this->client_subscription, 'client' => $this->client_subscription->client->toArray(), ]; - $client = new \GuzzleHttp\Client(['headers' => $this->billing_subscription->webhook_configuration->post_purchase_headers]); + $client = new \GuzzleHttp\Client(['headers' => $this->subscription->webhook_configuration->post_purchase_headers]); - $response = $client->{$this->billing_subscription->webhook_configuration->post_purchase_rest_method}($this->billing_subscription->post_purchase_url,[ + $response = $client->{$this->subscription->webhook_configuration->post_purchase_rest_method}($this->subscription->post_purchase_url,[ RequestOptions::JSON => ['body' => $body] ]); diff --git a/app/Transformers/BillingSubscriptionTransformer.php b/app/Transformers/BillingSubscriptionTransformer.php deleted file mode 100644 index 74ac5484ffe3..000000000000 --- a/app/Transformers/BillingSubscriptionTransformer.php +++ /dev/null @@ -1,76 +0,0 @@ - $this->encodePrimaryKey($billing_subscription->id), - 'user_id' => $this->encodePrimaryKey($billing_subscription->user_id), - 'product_id' => $this->encodePrimaryKey($billing_subscription->product_id), - 'assigned_user_id' => $this->encodePrimaryKey($billing_subscription->assigned_user_id), - 'company_id' => $this->encodePrimaryKey($billing_subscription->company_id), - 'is_recurring' => (bool)$billing_subscription->is_recurring, - 'frequency_id' => (string)$billing_subscription->frequency_id, - 'auto_bill' => (string)$billing_subscription->auto_bill, - 'promo_code' => (string)$billing_subscription->promo_code, - 'promo_discount' => (float)$billing_subscription->promo_discount, - 'is_amount_discount' => (bool)$billing_subscription->is_amount_discount, - 'allow_cancellation' => (bool)$billing_subscription->allow_cancellation, - 'per_seat_enabled' => (bool)$billing_subscription->per_set_enabled, - 'min_seats_limit' => (int)$billing_subscription->min_seats_limit, - 'max_seats_limit' => (int)$billing_subscription->max_seats_limit, - 'trial_enabled' => (bool)$billing_subscription->trial_enabled, - 'trial_duration' => (int)$billing_subscription->trial_duration, - 'allow_query_overrides' => (bool)$billing_subscription->allow_query_overrides, - 'allow_plan_changes' => (bool)$billing_subscription->allow_plan_changes, - 'plan_map' => (string)$billing_subscription->plan_map, - 'refund_period' => (int)$billing_subscription->refund_period, - 'webhook_configuration' => $billing_subscription->webhook_configuration ?: $std, - 'purchase_page' => (string)route('client.subscription.purchase', $billing_subscription->hashed_id), - 'is_deleted' => (bool)$billing_subscription->is_deleted, - 'created_at' => (int)$billing_subscription->created_at, - 'updated_at' => (int)$billing_subscription->updated_at, - 'archived_at' => (int)$billing_subscription->deleted_at, - ]; - } - - public function includeProduct(BillingSubscription $billing_subscription): \League\Fractal\Resource\Item - { - $transformer = new ProductTransformer($this->serializer); - - return $this->includeItem($billing_subscription->product, $transformer, Product::class); - } -} diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 8620fefd7b2a..5fd5857f5973 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -13,7 +13,7 @@ namespace App\Transformers; use App\Models\Account; use App\Models\Activity; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; use App\Models\ClientSubscription; use App\Models\Company; @@ -92,7 +92,7 @@ class CompanyTransformer extends EntityTransformer 'system_logs', 'expense_categories', 'task_statuses', - 'client_subscriptions', + 'billing_subscriptions', ]; /** @@ -362,17 +362,10 @@ class CompanyTransformer extends EntityTransformer return $this->includeCollection($company->system_logs, $transformer, SystemLog::class); } - public function includeClientSubscriptions(Company $company) + public function includeSubscriptions(Company $company) { - $transformer = new ClientSubscriptionTransformer($this->serializer); + $transformer = new SubscriptionTransformer($this->serializer); - return $this->includeCollection($company->client_subscriptions, $transformer, ClientSubscription::class); - } - - public function includeBillingSubscriptions(Company $company) - { - $transformer = new BillingSubscriptionTransformer($this->serializer); - - return $this->includeCollection($company->billing_subscriptions, $transformer, BillingSubscription::class); + return $this->includeCollection($company->billing_subscriptions, $transformer, Subscription::class); } } diff --git a/app/Transformers/SubscriptionTransformer.php b/app/Transformers/SubscriptionTransformer.php new file mode 100644 index 000000000000..4c0b59d2118b --- /dev/null +++ b/app/Transformers/SubscriptionTransformer.php @@ -0,0 +1,69 @@ + $this->encodePrimaryKey($subscription->id), + 'user_id' => $this->encodePrimaryKey($subscription->user_id), + 'product_id' => $this->encodePrimaryKey($subscription->product_id), + 'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id), + 'company_id' => $this->encodePrimaryKey($subscription->company_id), + 'is_recurring' => (bool)$subscription->is_recurring, + 'frequency_id' => (string)$subscription->frequency_id, + 'auto_bill' => (string)$subscription->auto_bill, + 'promo_code' => (string)$subscription->promo_code, + 'promo_discount' => (float)$subscription->promo_discount, + 'is_amount_discount' => (bool)$subscription->is_amount_discount, + 'allow_cancellation' => (bool)$subscription->allow_cancellation, + 'per_seat_enabled' => (bool)$subscription->per_set_enabled, + 'min_seats_limit' => (int)$subscription->min_seats_limit, + 'max_seats_limit' => (int)$subscription->max_seats_limit, + 'trial_enabled' => (bool)$subscription->trial_enabled, + 'trial_duration' => (int)$subscription->trial_duration, + 'allow_query_overrides' => (bool)$subscription->allow_query_overrides, + 'allow_plan_changes' => (bool)$subscription->allow_plan_changes, + 'plan_map' => (string)$subscription->plan_map, + 'refund_period' => (int)$subscription->refund_period, + 'webhook_configuration' => $subscription->webhook_configuration ?: $std, + 'purchase_page' => (string)route('client.subscription.purchase', $subscription->hashed_id), + 'is_deleted' => (bool)$subscription->is_deleted, + 'created_at' => (int)$subscription->created_at, + 'updated_at' => (int)$subscription->updated_at, + 'archived_at' => (int)$subscription->deleted_at, + ]; + } + +} diff --git a/database/factories/BillingSubscriptionFactory.php b/database/factories/SubscriptionFactory.php similarity index 81% rename from database/factories/BillingSubscriptionFactory.php rename to database/factories/SubscriptionFactory.php index 2fdf320cd5f4..a9b008244c7f 100644 --- a/database/factories/BillingSubscriptionFactory.php +++ b/database/factories/SubscriptionFactory.php @@ -12,17 +12,17 @@ namespace Database\Factories; -use App\Models\BillingSubscription; +use App\Models\Subscription; use Illuminate\Database\Eloquent\Factories\Factory; -class BillingSubscriptionFactory extends Factory +class SubscriptionFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ - protected $model = BillingSubscription::class; + protected $model = Subscription::class; /** * Define the model's default state. diff --git a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php new file mode 100644 index 000000000000..67a74f1f45ee --- /dev/null +++ b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php @@ -0,0 +1,29 @@ + ['api_db', 'token_auth', 'locale'], 'prefix' => 'a // Route::post('hooks', 'SubscriptionController@subscribe')->name('hooks.subscribe'); // Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe'); - Route::resource('billing_subscriptions', 'BillingSubscriptionController'); + Route::resource('subscriptions', 'SubscriptionController'); Route::resource('cliente_subscriptions', 'ClientSubscriptionController'); }); diff --git a/tests/Feature/BillingSubscriptionApiTest.php b/tests/Feature/SubscriptionApiTest.php similarity index 68% rename from tests/Feature/BillingSubscriptionApiTest.php rename to tests/Feature/SubscriptionApiTest.php index 54c6162ef5ff..c09e3c13c424 100644 --- a/tests/Feature/BillingSubscriptionApiTest.php +++ b/tests/Feature/SubscriptionApiTest.php @@ -11,7 +11,7 @@ namespace Tests\Feature; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Product; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; @@ -24,9 +24,9 @@ use Tests\TestCase; /** * @test - * @covers App\Http\Controllers\BillingSubscriptionController + * @covers App\Http\Controllers\SubscriptionController */ -class BillingSubscriptionApiTest extends TestCase +class SubscriptionApiTest extends TestCase { use MakesHash; use DatabaseTransactions; @@ -45,27 +45,30 @@ class BillingSubscriptionApiTest extends TestCase Model::reguard(); } - public function testBillingSubscriptionsGet() + public function testSubscriptionsGet() { $product = Product::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, ]); - $billing_subscription = BillingSubscription::factory()->create([ + $billing_subscription = Subscription::factory()->create([ 'product_id' => $product->id, 'company_id' => $this->company->id, ]); + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->get('/api/v1/billing_subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)); - + ])->get('/api/v1/subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)); + + nlog($response); + $response->assertStatus(200); } - public function testBillingSubscriptionsPost() + public function testSubscriptionsPost() { $product = Product::factory()->create([ 'company_id' => $this->company->id, @@ -75,12 +78,12 @@ class BillingSubscriptionApiTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/billing_subscriptions', ['product_id' => $product->id, 'allow_cancellation' => true]); + ])->post('/api/v1/subscriptions', ['product_id' => $product->id, 'allow_cancellation' => true]); $response->assertStatus(200); } - public function testBillingSubscriptionPut() + public function testSubscriptionPut() { $product = Product::factory()->create([ 'company_id' => $this->company->id, @@ -89,13 +92,13 @@ class BillingSubscriptionApiTest extends TestCase $response1 = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) - ->post('/api/v1/billing_subscriptions', ['product_id' => $product->id]) + ->post('/api/v1/subscriptions', ['product_id' => $product->id]) ->assertStatus(200) ->json(); $response2 = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) - ->put('/api/v1/billing_subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true]) + ->put('/api/v1/subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true]) ->assertStatus(200) ->json(); @@ -103,15 +106,15 @@ class BillingSubscriptionApiTest extends TestCase } /* - TypeError : Argument 1 passed to App\Transformers\BillingSubscriptionTransformer::transform() must be an instance of App\Models\BillingSubscription, bool given, called in /var/www/html/vendor/league/fractal/src/Scope.php on line 407 - /var/www/html/app/Transformers/BillingSubscriptionTransformer.php:35 + TypeError : Argument 1 passed to App\Transformers\SubscriptionTransformer::transform() must be an instance of App\Models\Subscription, bool given, called in /var/www/html/vendor/league/fractal/src/Scope.php on line 407 + /var/www/html/app/Transformers/SubscriptionTransformer.php:35 /var/www/html/vendor/league/fractal/src/Scope.php:407 /var/www/html/vendor/league/fractal/src/Scope.php:349 /var/www/html/vendor/league/fractal/src/Scope.php:235 /var/www/html/app/Http/Controllers/BaseController.php:395 - /var/www/html/app/Http/Controllers/BillingSubscriptionController.php:408 + /var/www/html/app/Http/Controllers/SubscriptionController.php:408 */ - public function testBillingSubscriptionDeleted() + public function testSubscriptionDeleted() { $product = Product::factory()->create([ @@ -119,14 +122,14 @@ class BillingSubscriptionApiTest extends TestCase 'user_id' => $this->user->id, ]); - $billing_subscription = BillingSubscription::factory()->create([ + $billing_subscription = Subscription::factory()->create([ 'product_id' => $product->id, 'company_id' => $this->company->id, ]); $response = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token]) - ->delete('/api/v1/billing_subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)) + ->delete('/api/v1/subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)) ->assertStatus(200) ->json();