From 83ab8f0f607a601689fdc043eb8f9cd82c82a4b4 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 25 Aug 2017 13:46:32 +0300 Subject: [PATCH] Support coupon codes --- app/Http/Middleware/StartupCheck.php | 31 ++++++++++++++++++-- app/Models/Company.php | 16 ++++++++++ app/Ninja/Repositories/AccountRepository.php | 10 ++----- config/ninja.php | 10 +++++++ resources/lang/en/texts.php | 4 +++ 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 config/ninja.php diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index de8f7be8a580..c23381a87400 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -50,6 +50,8 @@ class StartupCheck return $next($request); } + $company = Auth::user()->account->company; + // Check if a new version was installed if (! Utils::isNinja()) { $file = storage_path() . '/version.txt'; @@ -72,11 +74,37 @@ class StartupCheck } } - // Check the application is up to date and for any news feed messages if (Auth::check()) { $count = Session::get(SESSION_COUNTER, 0); Session::put(SESSION_COUNTER, ++$count); + if (Utils::isNinja()) { + if ($coupon = request()->coupon) { + if ($code = config('ninja.coupon_50_off')) { + if (hash_equals($coupon, $code)) { + $company->applyDiscount(.5); + $company->save(); + Session::flash('message', trans('texts.applied_discount', ['discount' => 50])); + } + } + if ($code = config('ninja.coupon_75_off')) { + if (hash_equals($coupon, $code)) { + $company->applyDiscount(.75); + $company->save(); + Session::flash('message', trans('texts.applied_discount', ['discount' => 75])); + } + } + if ($code = config('ninja.coupon_free_year')) { + if (hash_equals($coupon, $code)) { + $company->applyFreeYear(); + $company->save(); + Session::flash('message', trans('texts.applied_free_year')); + } + } + } + } + + // Check the application is up to date and for any news feed messages if (isset($_SERVER['REQUEST_URI']) && ! Utils::startsWith($_SERVER['REQUEST_URI'], '/news_feed') && ! Session::has('news_feed_id')) { $data = false; if (Utils::isNinja()) { @@ -144,7 +172,6 @@ class StartupCheck if ($data == RESULT_FAILURE) { Session::flash('error', trans('texts.invalid_white_label_license')); } elseif ($data) { - $company = Auth::user()->account->company; $company->plan_term = PLAN_TERM_YEARLY; $company->plan_paid = $data; $date = max(date_create($data), date_create($company->plan_expires)); diff --git a/app/Models/Company.php b/app/Models/Company.php index 1d5589b5cf56..d2f6fa6e6d34 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -185,6 +185,22 @@ class Company extends Eloquent return false; } + + public function applyDiscount($amount) + { + $this->discount = $amount; + $this->promo_expires = date_create()->modify('14 days')->format('Y-m-d'); + } + + public function applyFreeYear() + { + $this->plan = PLAN_PRO; + $this->plan_term = PLAN_TERM_YEARLY; + $this->plan_price = PLAN_PRICE_PRO_MONTHLY; + $this->plan_started = date_create()->format('Y-m-d'); + $this->plan_paid = date_create()->format('Y-m-d'); + $this->plan_expires = date_create()->modify('1 year')->format('Y-m-d'); + } } Company::deleted(function ($company) diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 39d3475030e7..092493794f01 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -42,17 +42,11 @@ class AccountRepository if (Input::get('utm_campaign')) { if (env('PROMO_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PROMO_CAMPAIGN'))) { - $company->discount = .75; - $company->promo_expires = date_create()->modify('14 days')->format('Y-m-d'); + $company->applyDiscount(.75); } if (env('PARTNER_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PARTNER_CAMPAIGN'))) { - $company->plan = PLAN_PRO; - $company->plan_term = PLAN_TERM_YEARLY; - $company->plan_price = PLAN_PRICE_PRO_MONTHLY; - $company->plan_started = date_create()->format('Y-m-d'); - $company->plan_paid = date_create()->format('Y-m-d'); - $company->plan_expires = date_create()->modify('1 year')->format('Y-m-d'); + $company->applyFreeYear(); } } diff --git a/config/ninja.php b/config/ninja.php new file mode 100644 index 000000000000..3b428f9f3724 --- /dev/null +++ b/config/ninja.php @@ -0,0 +1,10 @@ + env('COUPON_50_OFF', false), + 'coupon_75_off' => env('COUPON_75_OFF', false), + 'coupon_free_year' => env('COUPON_FREE_YEAR', false), + +]; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index e735f868d531..8d3fa1ee6725 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2418,6 +2418,10 @@ $LANG = array( 'delete_company_help' => 'Permanently delete the company along with all data and setting.', 'delete_company_message' => 'Warning: This will permanently delete your company, there is no undo.', + 'applied_discount' => 'The coupon has been applied, the plan price has been reduced by :discount%.', + 'applied_free_year' => 'The coupon has been applied, your account has been upgraded to pro for one year.', + + ); return $LANG;