public function getNinjaAccount()
 {
     $account = Account::whereAccountKey(NINJA_ACCOUNT_KEY)->first();
     if ($account) {
         return $account;
     } else {
         $account = new Account();
         $account->name = 'Invoice Ninja';
         $account->work_email = '*****@*****.**';
         $account->work_phone = '(800) 763-1948';
         $account->account_key = NINJA_ACCOUNT_KEY;
         $account->save();
         $random = str_random(RANDOM_KEY_LENGTH);
         $user = new User();
         $user->registered = true;
         $user->confirmed = true;
         $user->email = '*****@*****.**';
         $user->password = $random;
         $user->username = $random;
         $user->first_name = 'Invoice';
         $user->last_name = 'Ninja';
         $user->notify_sent = true;
         $user->notify_paid = true;
         $account->users()->save($user);
         $accountGateway = new AccountGateway();
         $accountGateway->user_id = $user->id;
         $accountGateway->gateway_id = NINJA_GATEWAY_ID;
         $accountGateway->public_id = 1;
         $accountGateway->config = env(NINJA_GATEWAY_CONFIG);
         $account->account_gateways()->save($accountGateway);
     }
     return $account;
 }
 public function handleBuyNow(ClientRepository $clientRepo, InvoiceService $invoiceService, $gatewayTypeAlias = false)
 {
     if (Crawler::isCrawler()) {
         return redirect()->to(NINJA_WEB_URL, 301);
     }
     $account = Account::whereAccountKey(Input::get('account_key'))->first();
     $redirectUrl = Input::get('redirect_url', URL::previous());
     if (!$account || !$account->enable_buy_now_buttons || !$account->hasFeature(FEATURE_BUY_NOW_BUTTONS)) {
         return redirect()->to("{$redirectUrl}/?error=invalid account");
     }
     Auth::onceUsingId($account->users[0]->id);
     $product = Product::scope(Input::get('product_id'))->first();
     if (!$product) {
         return redirect()->to("{$redirectUrl}/?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("{$redirectUrl}/?error=" . $validator->errors()->first());
     }
     $data = ['currency_id' => $account->currency_id, 'contact' => Input::all()];
     $client = $clientRepo->save($data);
     $data = ['client_id' => $client->id, 'tax_rate1' => $account->default_tax_rate ? $account->default_tax_rate->rate : 0, 'tax_name1' => $account->default_tax_rate ? $account->default_tax_rate->name : '', 'invoice_items' => [['product_key' => $product->product_key, 'notes' => $product->notes, 'cost' => $product->cost, 'qty' => 1, 'tax_rate1' => $product->default_tax_rate ? $product->default_tax_rate->rate : 0, 'tax_name1' => $product->default_tax_rate ? $product->default_tax_rate->name : '']]];
     $invoice = $invoiceService->save($data);
     $invitation = $invoice->invitations[0];
     $link = $invitation->getLink();
     if ($gatewayTypeAlias) {
         return redirect()->to($invitation->getLink('payment') . "/{$gatewayTypeAlias}");
     } else {
         return redirect()->to($invitation->getLink());
     }
 }
 public function findByKey($key)
 {
     $account = Account::whereAccountKey($key)->with('clients.invoices.invoice_items', 'clients.contacts')->firstOrFail();
     return $account;
 }