Support coupon codes

This commit is contained in:
Hillel Coren 2017-08-25 13:46:32 +03:00
parent 7c1ab1bfca
commit 83ab8f0f60
5 changed files with 61 additions and 10 deletions

View File

@ -50,6 +50,8 @@ class StartupCheck
return $next($request); return $next($request);
} }
$company = Auth::user()->account->company;
// Check if a new version was installed // Check if a new version was installed
if (! Utils::isNinja()) { if (! Utils::isNinja()) {
$file = storage_path() . '/version.txt'; $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()) { if (Auth::check()) {
$count = Session::get(SESSION_COUNTER, 0); $count = Session::get(SESSION_COUNTER, 0);
Session::put(SESSION_COUNTER, ++$count); 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')) { if (isset($_SERVER['REQUEST_URI']) && ! Utils::startsWith($_SERVER['REQUEST_URI'], '/news_feed') && ! Session::has('news_feed_id')) {
$data = false; $data = false;
if (Utils::isNinja()) { if (Utils::isNinja()) {
@ -144,7 +172,6 @@ class StartupCheck
if ($data == RESULT_FAILURE) { if ($data == RESULT_FAILURE) {
Session::flash('error', trans('texts.invalid_white_label_license')); Session::flash('error', trans('texts.invalid_white_label_license'));
} elseif ($data) { } elseif ($data) {
$company = Auth::user()->account->company;
$company->plan_term = PLAN_TERM_YEARLY; $company->plan_term = PLAN_TERM_YEARLY;
$company->plan_paid = $data; $company->plan_paid = $data;
$date = max(date_create($data), date_create($company->plan_expires)); $date = max(date_create($data), date_create($company->plan_expires));

View File

@ -185,6 +185,22 @@ class Company extends Eloquent
return false; 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) Company::deleted(function ($company)

View File

@ -42,17 +42,11 @@ class AccountRepository
if (Input::get('utm_campaign')) { if (Input::get('utm_campaign')) {
if (env('PROMO_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PROMO_CAMPAIGN'))) { if (env('PROMO_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PROMO_CAMPAIGN'))) {
$company->discount = .75; $company->applyDiscount(.75);
$company->promo_expires = date_create()->modify('14 days')->format('Y-m-d');
} }
if (env('PARTNER_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PARTNER_CAMPAIGN'))) { if (env('PARTNER_CAMPAIGN') && hash_equals(Input::get('utm_campaign'), env('PARTNER_CAMPAIGN'))) {
$company->plan = PLAN_PRO; $company->applyFreeYear();
$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');
} }
} }

10
config/ninja.php Normal file
View File

@ -0,0 +1,10 @@
<?php
return [
// Hosted plan coupons
'coupon_50_off' => env('COUPON_50_OFF', false),
'coupon_75_off' => env('COUPON_75_OFF', false),
'coupon_free_year' => env('COUPON_FREE_YEAR', false),
];

View File

@ -2418,6 +2418,10 @@ $LANG = array(
'delete_company_help' => 'Permanently delete the company along with all data and setting.', '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.', '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; return $LANG;