App\Http\Controllers\AccountGatewayController::save PHP Method

save() public method

Stores new account
public save ( $accountGatewayPublicId = false )
    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
                    } elseif ($gatewayId == GATEWAY_CUSTOM) {
                        $config->{$field} = strip_tags($value);
                    } 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 {
                $this->testGateway($accountGateway);
                if ($accountGatewayPublicId) {
                    $message = trans('texts.updated_gateway');
                    Session::flash('message', $message);
                    return Redirect::to("gateways/{$accountGateway->public_id}/edit");
                } else {
                    $message = trans('texts.created_gateway');
                    Session::flash('message', $message);
                    return Redirect::to("/settings/online_payments");
                }
            }
        }
    }