Inheritance: extends EntityModel, use trait Illuminate\Database\Eloquent\SoftDeletes, use trait Laracasts\Presenter\PresentableTrait
 /**
  * Stores new account
  *
  */
 public function save($accountGatewayPublicId = false)
 {
     $rules = array();
     $paymentType = Input::get('payment_type_id');
     $gatewayId = Input::get('gateway_id');
     if ($paymentType == PAYMENT_TYPE_PAYPAL) {
         $gatewayId = GATEWAY_PAYPAL_EXPRESS;
     } elseif ($paymentType == PAYMENT_TYPE_BITCOIN) {
         $gatewayId = GATEWAY_BITPAY;
     } elseif ($paymentType == PAYMENT_TYPE_DIRECT_DEBIT) {
         $gatewayId = GATEWAY_GOCARDLESS;
     } elseif ($paymentType == PAYMENT_TYPE_DWOLLA) {
         $gatewayId = GATEWAY_DWOLLA;
     }
     if (!$gatewayId) {
         Session::flash('error', trans('validation.required', ['attribute' => 'gateway']));
         return Redirect::to('gateways/create')->withInput();
     }
     $gateway = Gateway::findOrFail($gatewayId);
     $fields = $gateway->getFields();
     $optional = array_merge(Gateway::$hiddenFields, Gateway::$optionalFields);
     if ($gatewayId == GATEWAY_DWOLLA) {
         $optional = array_merge($optional, ['key', 'secret']);
     } elseif ($gatewayId == GATEWAY_STRIPE) {
         $rules['publishable_key'] = 'required';
     }
     foreach ($fields as $field => $details) {
         if (!in_array($field, $optional)) {
             if (strtolower($gateway->name) == 'beanstream') {
                 if (in_array($field, ['merchant_id', 'passCode'])) {
                     $rules[$gateway->id . '_' . $field] = 'required';
                 }
             } else {
                 $rules[$gateway->id . '_' . $field] = 'required';
             }
         }
     }
     $creditcards = Input::get('creditCardTypes');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('gateways/create')->withErrors($validator)->withInput();
     } else {
         $account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
         $oldConfig = null;
         if ($accountGatewayPublicId) {
             $accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
             $oldConfig = $accountGateway->getConfig();
         } else {
             $accountGateway = AccountGateway::createNew();
             $accountGateway->gateway_id = $gatewayId;
         }
         $config = new stdClass();
         foreach ($fields as $field => $details) {
             $value = trim(Input::get($gateway->id . '_' . $field));
             // if the new value is masked use the original value
             if ($oldConfig && $value && $value === str_repeat('*', strlen($value))) {
                 $value = $oldConfig->{$field};
             }
             if (!$value && ($field == 'testMode' || $field == 'developerMode')) {
                 // do nothing
             } else {
                 $config->{$field} = $value;
             }
         }
         $publishableKey = Input::get('publishable_key');
         if ($publishableKey = str_replace('*', '', $publishableKey)) {
             $config->publishableKey = $publishableKey;
         } elseif ($oldConfig && property_exists($oldConfig, 'publishableKey')) {
             $config->publishableKey = $oldConfig->publishableKey;
         }
         $cardCount = 0;
         if ($creditcards) {
             foreach ($creditcards as $card => $value) {
                 $cardCount += intval($value);
             }
         }
         $accountGateway->accepted_credit_cards = $cardCount;
         $accountGateway->show_address = Input::get('show_address') ? true : false;
         $accountGateway->update_address = Input::get('update_address') ? true : false;
         $accountGateway->setConfig($config);
         if ($accountGatewayPublicId) {
             $accountGateway->save();
         } else {
             $account->account_gateways()->save($accountGateway);
         }
         if (Input::get('token_billing_type_id')) {
             $account->token_billing_type_id = Input::get('token_billing_type_id');
             $account->save();
         }
         if ($accountGatewayPublicId) {
             $message = trans('texts.updated_gateway');
         } else {
             $message = trans('texts.created_gateway');
         }
         Session::flash('message', $message);
         return Redirect::to("gateways/{$accountGateway->public_id}/edit");
     }
 }
 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->setConfig(json_decode(env(NINJA_GATEWAY_CONFIG)));
         $account->account_gateways()->save($accountGateway);
     }
     return $account;
 }
 /**
  * @return \Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
  */
 private function showOnlinePayments()
 {
     $account = Auth::user()->account;
     $account->load('account_gateways');
     $count = count($account->account_gateways);
     $trashedCount = AccountGateway::scope()->withTrashed()->count();
     if ($accountGateway = $account->getGatewayConfig(GATEWAY_STRIPE)) {
         if (!$accountGateway->getPublishableStripeKey()) {
             Session::flash('warning', trans('texts.missing_publishable_key'));
         }
     }
     if ($trashedCount == 0) {
         return Redirect::to('gateways/create');
     } else {
         $tokenBillingOptions = [];
         for ($i = 1; $i <= 4; $i++) {
             $tokenBillingOptions[$i] = trans("texts.token_billing_{$i}");
         }
         return View::make('accounts.payments', ['showAdd' => $count < count(Gateway::$alternate) + 1, 'title' => trans('texts.online_payments'), 'tokenBillingOptions' => $tokenBillingOptions, 'currency' => Utils::getFromCache(Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY), 'currencies'), 'account' => $account]);
     }
 }
 public function resendConfirmation($publicId = false)
 {
     $accountGateway = AccountGateway::scope($publicId)->firstOrFail();
     if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
         try {
             $wepay = Utils::setupWePay($accountGateway);
             $wepay->request('user/send_confirmation', []);
             Session::flash('message', trans('texts.resent_confirmation_email'));
         } catch (\WePayException $e) {
             Session::flash('error', $e->getMessage());
         }
     }
     return Redirect::to("gateways/{$accountGateway->public_id}/edit");
 }
 private function getAccountGateway($id)
 {
     if (isset(static::$accountGateways[$id])) {
         return static::$accountGateways[$id];
     }
     static::$accountGateways[$id] = AccountGateway::find($id);
     return static::$accountGateways[$id];
 }
Example #6
0
 /**
  * @return array
  */
 public function availableGatewaysIds()
 {
     if (!$this->relationLoaded('account_gateways')) {
         $this->load('account_gateways');
     }
     $gatewayTypes = [];
     $gatewayIds = [];
     foreach ($this->account_gateways as $accountGateway) {
         $paymentDriver = $accountGateway->paymentDriver();
         $gatewayTypes = array_unique(array_merge($gatewayTypes, $paymentDriver->gatewayTypes()));
     }
     foreach (Cache::get('gateways') as $gateway) {
         $paymentDriverClass = AccountGateway::paymentDriverClass($gateway->provider);
         $paymentDriver = new $paymentDriverClass();
         $available = true;
         foreach ($gatewayTypes as $type) {
             if ($paymentDriver->handles($type)) {
                 $available = false;
                 break;
             }
         }
         if ($available) {
             $gatewayIds[] = $gateway->id;
         }
     }
     return $gatewayIds;
 }
 /**
  * Stores new account
  *
  */
 public function save($accountGatewayPublicId = false)
 {
     $rules = array();
     $paymentType = Input::get('payment_type_id');
     $gatewayId = Input::get('gateway_id');
     if ($paymentType == PAYMENT_TYPE_PAYPAL) {
         $gatewayId = GATEWAY_PAYPAL_EXPRESS;
     } elseif ($paymentType == PAYMENT_TYPE_BITCOIN) {
         $gatewayId = GATEWAY_BITPAY;
     }
     if (!$gatewayId) {
         Session::flash('error', trans('validation.required', ['attribute' => 'gateway']));
         return Redirect::to('gateways/create')->withInput();
     }
     $gateway = Gateway::findOrFail($gatewayId);
     $fields = $gateway->getFields();
     foreach ($fields as $field => $details) {
         if (!in_array($field, ['testMode', 'developerMode', 'headerImageUrl', 'solutionType', 'landingPage', 'brandName', 'logoImageUrl', 'borderColor'])) {
             if (strtolower($gateway->name) == 'beanstream') {
                 if (in_array($field, ['merchant_id', 'passCode'])) {
                     $rules[$gateway->id . '_' . $field] = 'required';
                 }
             } else {
                 $rules[$gateway->id . '_' . $field] = 'required';
             }
         }
     }
     $creditcards = Input::get('creditCardTypes');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('gateways/create')->withErrors($validator)->withInput();
     } else {
         $account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
         $oldConfig = null;
         if ($accountGatewayPublicId) {
             $accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
             $oldConfig = json_decode($accountGateway->config);
         } else {
             $accountGateway = AccountGateway::createNew();
             $accountGateway->gateway_id = $gatewayId;
         }
         $config = new stdClass();
         foreach ($fields as $field => $details) {
             $value = trim(Input::get($gateway->id . '_' . $field));
             // if the new value is masked use the original value
             if ($value && $value === str_repeat('*', strlen($value))) {
                 $value = $oldConfig->{$field};
             }
             if (!$value && ($field == 'testMode' || $field == 'developerMode')) {
                 // do nothing
             } else {
                 $config->{$field} = $value;
             }
         }
         $cardCount = 0;
         if ($creditcards) {
             foreach ($creditcards as $card => $value) {
                 $cardCount += intval($value);
             }
         }
         $accountGateway->accepted_credit_cards = $cardCount;
         $accountGateway->config = json_encode($config);
         if ($accountGatewayPublicId) {
             $accountGateway->save();
         } else {
             $account->account_gateways()->save($accountGateway);
         }
         if (Input::get('token_billing_type_id')) {
             $account->token_billing_type_id = Input::get('token_billing_type_id');
             $account->save();
         }
         if ($accountGatewayPublicId) {
             $message = trans('texts.updated_gateway');
         } else {
             $message = trans('texts.created_gateway');
         }
         Session::flash('message', $message);
         return Redirect::to('company/payments');
     }
 }
 public function actions()
 {
     $actions = [[uctrans('texts.resend_confirmation_email'), function ($model) {
         return $model->resendConfirmationUrl;
     }, function ($model) {
         return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->resendConfirmationUrl);
     }], [uctrans('texts.edit_gateway'), function ($model) {
         return URL::to("gateways/{$model->public_id}/edit");
     }, function ($model) {
         return !$model->deleted_at;
     }], [uctrans('texts.finish_setup'), function ($model) {
         return $model->setupUrl;
     }, function ($model) {
         return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->setupUrl);
     }], [uctrans('texts.manage_account'), function ($model) {
         $accountGateway = AccountGateway::find($model->id);
         $endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/';
         return ['url' => $endpoint . 'account/' . $accountGateway->getConfig()->accountId, 'attributes' => 'target="_blank"'];
     }, function ($model) {
         return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY;
     }], [uctrans('texts.terms_of_service'), function ($model) {
         return 'https://go.wepay.com/terms-of-service-us';
     }, function ($model) {
         return $model->gateway_id == GATEWAY_WEPAY;
     }]];
     foreach (Cache::get('gatewayTypes') as $gatewayType) {
         $actions[] = [trans('texts.set_limits', ['gateway_type' => $gatewayType->name]), function () use($gatewayType) {
             $accountGatewaySettings = AccountGatewaySettings::scope()->where('account_gateway_settings.gateway_type_id', '=', $gatewayType->id)->first();
             $min = $accountGatewaySettings && $accountGatewaySettings->min_limit !== null ? $accountGatewaySettings->min_limit : 'null';
             $max = $accountGatewaySettings && $accountGatewaySettings->max_limit !== null ? $accountGatewaySettings->max_limit : 'null';
             return "javascript:showLimitsModal('{$gatewayType->name}',{$gatewayType->id},{$min},{$max})";
         }, function ($model) use($gatewayType) {
             // Only show this action if the given gateway supports this gateway type
             $accountGateway = AccountGateway::find($model->id);
             $paymentDriver = $accountGateway->paymentDriver();
             $gatewayTypes = $paymentDriver->gatewayTypes();
             return in_array($gatewayType->id, $gatewayTypes);
         }];
     }
     return $actions;
 }