/**
  * Stores new account
  *
  */
 public function save($accountGatewayPublicId = false)
 {
     $gatewayId = Input::get('primary_gateway_id') ?: Input::get('secondary_gateway_id');
     $gateway = Gateway::findOrFail($gatewayId);
     $rules = [];
     $fields = $gateway->getFields();
     $optional = array_merge(Gateway::$hiddenFields, Gateway::$optionalFields);
     if ($gatewayId == GATEWAY_DWOLLA) {
         $optional = array_merge($optional, ['key', 'secret']);
     } elseif ($gatewayId == GATEWAY_STRIPE) {
         if (Utils::isNinjaDev()) {
             // do nothing - we're unable to acceptance test with StripeJS
         } else {
             $rules['publishable_key'] = 'required';
             $rules['enable_ach'] = 'boolean';
         }
     }
     if ($gatewayId != GATEWAY_WEPAY) {
         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?other_providers=' . ($gatewayId == GATEWAY_WEPAY ? 'false' : 'true'))->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 {
             // check they don't already have an active gateway for this provider
             // TODO complete this
             $accountGateway = AccountGateway::scope()->whereGatewayId($gatewayId)->first();
             if ($accountGateway) {
                 Session::flash('error', trans('texts.gateway_exists'));
                 return Redirect::to("gateways/{$accountGateway->public_id}/edit");
             }
             $accountGateway = AccountGateway::createNew();
             $accountGateway->gateway_id = $gatewayId;
             if ($gatewayId == GATEWAY_WEPAY) {
                 if (!$this->setupWePay($accountGateway, $wepayResponse)) {
                     return $wepayResponse;
                 }
                 $oldConfig = $accountGateway->getConfig();
             }
         }
         $config = new stdClass();
         if ($gatewayId != GATEWAY_WEPAY) {
             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;
                 }
             }
         } elseif ($oldConfig) {
             $config = clone $oldConfig;
         }
         $publishableKey = Input::get('publishable_key');
         if ($publishableKey = str_replace('*', '', $publishableKey)) {
             $config->publishableKey = $publishableKey;
         } elseif ($oldConfig && property_exists($oldConfig, 'publishableKey')) {
             $config->publishableKey = $oldConfig->publishableKey;
         }
         $plaidClientId = Input::get('plaid_client_id');
         if ($plaidClientId = str_replace('*', '', $plaidClientId)) {
             $config->plaidClientId = $plaidClientId;
         } elseif ($oldConfig && property_exists($oldConfig, 'plaidClientId')) {
             $config->plaidClientId = $oldConfig->plaidClientId;
         }
         $plaidSecret = Input::get('plaid_secret');
         if ($plaidSecret = str_replace('*', '', $plaidSecret)) {
             $config->plaidSecret = $plaidSecret;
         } elseif ($oldConfig && property_exists($oldConfig, 'plaidSecret')) {
             $config->plaidSecret = $oldConfig->plaidSecret;
         }
         $plaidPublicKey = Input::get('plaid_public_key');
         if ($plaidPublicKey = str_replace('*', '', $plaidPublicKey)) {
             $config->plaidPublicKey = $plaidPublicKey;
         } elseif ($oldConfig && property_exists($oldConfig, 'plaidPublicKey')) {
             $config->plaidPublicKey = $oldConfig->plaidPublicKey;
         }
         if ($gatewayId == GATEWAY_STRIPE || $gatewayId == GATEWAY_WEPAY) {
             $config->enableAch = boolval(Input::get('enable_ach'));
         }
         if ($gatewayId == GATEWAY_BRAINTREE) {
             $config->enablePayPal = boolval(Input::get('enable_paypal'));
         }
         $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 (isset($wepayResponse)) {
             return $wepayResponse;
         } else {
             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");
         }
     }
 }
 /**
  * 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");
     }
 }
 /**
  * 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');
     }
 }