diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index b6c9a280cecf..c1b6fe81ec5a 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -11,6 +11,7 @@ use Exception; use Validator; use App\Models\Invitation; use App\Models\Account; +use App\Models\Client; use App\Models\Payment; use App\Models\Product; use App\Models\PaymentMethod; @@ -283,22 +284,31 @@ class OnlinePaymentController extends BaseController return redirect()->to("{$failureUrl}/?error=invalid product"); } - $rules = [ - 'first_name' => 'string|max:100', - 'last_name' => 'string|max:100', - 'email' => 'email|string|max:100', - ]; - - $validator = Validator::make(Input::all(), $rules); - if ($validator->fails()) { - return redirect()->to("{$failureUrl}/?error=" . $validator->errors()->first()); + // check for existing client using contact_key + $client = false; + if ($contactKey = Input::get('contact_key')) { + $client = Client::scope()->whereHas('contacts', function ($query) use ($contactKey) { + $query->where('contact_key', $contactKey); + })->first(); } + if ( ! $client) { + $rules = [ + 'first_name' => 'string|max:100', + 'last_name' => 'string|max:100', + 'email' => 'email|string|max:100', + ]; - $data = [ - 'currency_id' => $account->currency_id, - 'contact' => Input::all() - ]; - $client = $clientRepo->save($data); + $validator = Validator::make(Input::all(), $rules); + if ($validator->fails()) { + return redirect()->to("{$failureUrl}/?error=" . $validator->errors()->first()); + } + + $data = [ + 'currency_id' => $account->currency_id, + 'contact' => Input::all() + ]; + $client = $clientRepo->save($data); + } $data = [ 'client_id' => $client->id, diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index e18c8148bb66..620cbbfac543 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -240,7 +240,7 @@ class UserController extends BaseController $user = User::where('confirmation_code', '=', $code)->get()->first(); if ($user) { - $notice_msg = trans('texts.security.confirmation'); + $notice_msg = trans('texts.security_confirmation'); $user->confirmed = true; $user->confirmation_code = ''; diff --git a/app/Models/Client.php b/app/Models/Client.php index 92cabf0bc89c..c7bb9102bb06 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -275,6 +275,7 @@ class Client extends EntityModel } else { $contact = Contact::createNew(); $contact->send_invoice = true; + $contact->contact_key = isset($data['contact_key']) ? $data['contact_key'] : str_random(RANDOM_KEY_LENGTH); } if (Utils::hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD) && $this->account->enable_portal_password){ diff --git a/app/Models/User.php b/app/Models/User.php index 72fe6e8d7876..19f3608fadfc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -403,6 +403,11 @@ class User extends Authenticatable return (($entity && $this->can('edit', $entity)) || (!$entity && $this->can('create', $entityType))); } + + public function primaryAccount() + { + return $this->account->company->accounts->sortBy('id')->first(); + } } User::updating(function ($user) { diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 2c819b95556b..14cf361462d0 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -8,6 +8,7 @@ use Omnipay; use Exception; use CreditCard; use DateTime; +use App\Models\License; use App\Models\AccountGatewayToken; use App\Models\AccountGatewaySettings; use App\Models\Account; @@ -133,7 +134,12 @@ class BasePaymentDriver Session::reflash(); } else { $this->completeOnsitePurchase(); - Session::flash('message', trans('texts.applied_payment')); + if ($redirectUrl = session('redirect_url:' . $this->invitation->invitation_key)) { + $separator = strpos($redirectUrl, '?') === false ? '?' : '&'; + return redirect()->to($redirectUrl . $separator . 'invoice_id=' . $this->invoice()->public_id); + } else { + Session::flash('message', trans('texts.applied_payment')); + } } return redirect()->to('view/' . $this->invitation->invitation_key); @@ -613,9 +619,13 @@ class BasePaymentDriver $payment->save(); + $accountKey = $invoice->account->account_key; + + if ($accountKey == env('NINJA_LICENSE_ACCOUNT_KEY')) { + $this->createLicense($payment); // TODO move this code // enable pro plan for hosted users - if ($invoice->account->account_key == NINJA_ACCOUNT_KEY) { + } elseif ($accountKey == NINJA_ACCOUNT_KEY) { foreach ($invoice->invoice_items as $invoice_item) { // Hacky, but invoices don't have meta fields to allow us to store this easily if (1 == preg_match('/^Plan - (.+) \((.+)\)$/', $invoice_item->product_key, $matches)) { @@ -679,6 +689,33 @@ class BasePaymentDriver return $payment; } + protected function createLicense($payment) + { + // TODO parse invoice to determine license + if ($payment->amount == 20) { + $affiliateId = 4; + $productId = PRODUCT_WHITE_LABEL; + } else { + $affiliateId = 1; + $productId = PRODUCT_ONE_CLICK_INSTALL; + } + + $license = new License(); + $license->first_name = $this->contact()->first_name; + $license->last_name = $this->contact()->last_name; + $license->email = $this->contact()->email; + $license->transaction_reference = $payment->transaction_reference; + $license->license_key = Utils::generateLicense(); + $license->affiliate_id = $affiliateId; + $license->product_id = $productId; + $license->save(); + + // Add the license key to the redirect URL + $key = 'redirect_url:' . $payment->invitation->invitation_key; + $redirectUrl = session($key); + session([$key => "{$redirectUrl}?license_key={$license->license_key}&product_id={$productId}"]); + } + protected function creatingPayment($payment, $paymentMethod) { return $payment; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 68e3478e5362..2b3cc98ef57f 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2284,7 +2284,8 @@ $LANG = array( 'debug' => 'Debug', 'https' => 'HTTPS', 'require' => 'Require', - 'license_expiring' => 'Note: Your license will expire in :count days, :link to renew it.' + 'license_expiring' => 'Note: Your license will expire in :count days, :link to renew it.', + 'security_confirmation' => 'Your email address has been confirmed.', ); diff --git a/resources/views/partials/white_label.blade.php b/resources/views/partials/white_label.blade.php index 2135e0913144..e8de823cab51 100644 --- a/resources/views/partials/white_label.blade.php +++ b/resources/views/partials/white_label.blade.php @@ -6,8 +6,11 @@ @if (Auth::user()->account->hasFeature(FEATURE_WHITE_LABEL)) {{ trans('texts.white_labeled') }} - @if ($company->hasActivePlan() && $company->daysUntilPlanExpires() <= 10) - - {{ trans('texts.license_expiring', ['count' => $company->daysUntilPlanExpires(), 'link' => 'link']) }} + @if (false && $company->hasActivePlan() && $company->daysUntilPlanExpires() <= 10) + - {!! trans('texts.license_expiring', [ + 'count' => $company->daysUntilPlanExpires(), + 'link' => '' . trans('texts.click_here') . '', + ]) !!} @endif @else {{ trans('texts.white_label_link') }} @@ -37,7 +40,7 @@
@@ -76,8 +79,13 @@ $('#whiteLabelModal').modal('show'); } + function buyWhiteLabel() { + buyProduct('{{ WHITE_LABEL_AFFILIATE_KEY }}', '{{ PRODUCT_WHITE_LABEL }}'); + } + function buyProduct(affiliateKey, productId) { window.open('{{ Utils::isNinjaDev() ? '' : NINJA_APP_URL }}/license?affiliate_key=' + affiliateKey + '&product_id=' + productId + '&return_url=' + window.location); + //window.open('{{ Utils::isNinjaDev() ? '' : NINJA_APP_URL }}/buy_now/?account_key={{ env('NINJA_LICENSE_ACCOUNT_KEY') }}&product_id=' + productId + '&contact_key={{ Auth::user()->primaryAccount()->account_key }}' + '&return_url=' + window.location); } function showApplyLicense() {