1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/AccountGatewayController.php

298 lines
10 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-03-26 07:24:02 +01:00
use Auth;
use Datatable;
use DB;
use Input;
use Redirect;
use Session;
use View;
2015-04-01 21:57:02 +02:00
use Validator;
use stdClass;
2015-04-02 15:12:12 +02:00
use URL;
2015-06-03 19:55:48 +02:00
use Utils;
2015-03-26 07:24:02 +01:00
use App\Models\Gateway;
2015-04-01 21:57:02 +02:00
use App\Models\Account;
use App\Models\AccountGateway;
use App\Ninja\Repositories\AccountRepository;
2015-11-05 23:37:04 +01:00
use App\Services\AccountGatewayService;
2015-03-16 22:45:25 +01:00
class AccountGatewayController extends BaseController
{
2015-11-05 23:37:04 +01:00
protected $accountGatewayService;
public function __construct(AccountGatewayService $accountGatewayService)
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-11-05 23:37:04 +01:00
$this->accountGatewayService = $accountGatewayService;
}
2015-10-21 13:11:08 +02:00
public function index()
{
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
}
2015-03-16 22:45:25 +01:00
public function getDatatable()
{
2015-11-05 23:37:04 +01:00
return $this->accountGatewayService->getDatatable(Auth::user()->account_id);
2015-03-16 22:45:25 +01:00
}
public function edit($publicId)
{
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
2015-11-01 19:21:11 +01:00
$config = $accountGateway->getConfig();
2016-01-18 10:13:39 +01:00
2015-11-01 19:21:11 +01:00
foreach ($config as $field => $value) {
$config->$field = str_repeat('*', strlen($value));
2015-03-16 22:45:25 +01:00
}
$data = self::getViewModel($accountGateway);
$data['url'] = 'gateways/'.$publicId;
$data['method'] = 'PUT';
$data['title'] = trans('texts.edit_gateway') . ' - ' . $accountGateway->gateway->name;
2015-11-01 19:21:11 +01:00
$data['config'] = $config;
2015-05-31 14:37:29 +02:00
$data['hiddenFields'] = Gateway::$hiddenFields;
2015-04-15 18:35:41 +02:00
$data['paymentTypeId'] = $accountGateway->getPaymentType();
2015-04-16 19:12:56 +02:00
$data['selectGateways'] = Gateway::where('id', '=', $accountGateway->gateway_id)->get();
2015-03-16 22:45:25 +01:00
return View::make('accounts.account_gateway', $data);
}
public function update($publicId)
{
return $this->save($publicId);
}
public function store()
{
return $this->save();
}
/**
* Displays the form for account creation
*
*/
public function create()
{
$data = self::getViewModel();
$data['url'] = 'gateways';
$data['method'] = 'POST';
$data['title'] = trans('texts.add_gateway');
2015-10-02 10:32:13 +02:00
$data['selectGateways'] = Gateway::where('payment_library_id', '=', 1)
->where('id', '!=', GATEWAY_PAYPAL_EXPRESS)
->where('id', '!=', GATEWAY_BITPAY)
2016-01-18 10:13:39 +01:00
->where('id', '!=', GATEWAY_GOCARDLESS)
2015-10-02 10:32:13 +02:00
->where('id', '!=', GATEWAY_DWOLLA)
->orderBy('name')->get();
2015-05-31 14:37:29 +02:00
$data['hiddenFields'] = Gateway::$hiddenFields;
2015-03-16 22:45:25 +01:00
2016-02-03 19:59:07 +01:00
if ( ! \Request::secure() && ! Utils::isNinjaDev()) {
Session::flash('warning', trans('texts.enable_https'));
}
2015-03-16 22:45:25 +01:00
return View::make('accounts.account_gateway', $data);
}
private function getViewModel($accountGateway = false)
{
$selectedCards = $accountGateway ? $accountGateway->accepted_credit_cards : 0;
$account = Auth::user()->account;
2015-04-15 18:35:41 +02:00
$paymentTypes = [];
2015-05-31 14:37:29 +02:00
foreach (Gateway::$paymentTypes as $type) {
2015-04-15 18:35:41 +02:00
if ($accountGateway || !$account->getGatewayByType($type)) {
$paymentTypes[$type] = trans('texts.'.strtolower($type));
if ($type == PAYMENT_TYPE_BITCOIN) {
$paymentTypes[$type] .= ' - BitPay';
}
2016-01-18 10:13:39 +01:00
if ($type == PAYMENT_TYPE_DIRECT_DEBIT) {
$paymentTypes[$type] .= ' - GoCardless';
}
2015-04-15 18:35:41 +02:00
}
}
2015-03-16 22:45:25 +01:00
$creditCardsArray = unserialize(CREDIT_CARDS);
$creditCards = [];
foreach ($creditCardsArray as $card => $name) {
if ($selectedCards > 0 && ($selectedCards & $card) == $card) {
$creditCards[$name['text']] = ['value' => $card, 'data-imageUrl' => asset($name['card']), 'checked' => 'checked'];
} else {
$creditCards[$name['text']] = ['value' => $card, 'data-imageUrl' => asset($name['card'])];
}
}
$account->load('account_gateways');
$currentGateways = $account->account_gateways;
2015-04-15 18:35:41 +02:00
$gateways = Gateway::where('payment_library_id', '=', 1)->orderBy('name')->get();
2015-03-16 22:45:25 +01:00
foreach ($gateways as $gateway) {
2015-05-31 14:37:29 +02:00
$fields = $gateway->getFields();
asort($fields);
$gateway->fields = $fields;
2015-03-16 22:45:25 +01:00
if ($accountGateway && $accountGateway->gateway_id == $gateway->id) {
$accountGateway->fields = $gateway->fields;
}
}
$tokenBillingOptions = [];
for ($i=1; $i<=4; $i++) {
$tokenBillingOptions[$i] = trans("texts.token_billing_{$i}");
}
return [
2015-04-15 18:35:41 +02:00
'paymentTypes' => $paymentTypes,
2015-03-16 22:45:25 +01:00
'account' => $account,
'accountGateway' => $accountGateway,
'config' => false,
'gateways' => $gateways,
'creditCardTypes' => $creditCards,
'tokenBillingOptions' => $tokenBillingOptions,
'countGateways' => count($currentGateways)
];
}
2015-11-05 23:37:04 +01:00
public function bulk()
{
$action = Input::get('bulk_action');
$ids = Input::get('bulk_public_id');
$count = $this->accountGatewayService->bulk($ids, $action);
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
Session::flash('message', trans('texts.archived_account_gateway'));
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
2015-03-16 22:45:25 +01:00
}
/**
* Stores new account
*
*/
public function save($accountGatewayPublicId = false)
{
$rules = array();
2015-04-16 19:12:56 +02:00
$paymentType = Input::get('payment_type_id');
2015-04-06 13:46:02 +02:00
$gatewayId = Input::get('gateway_id');
2015-03-16 22:45:25 +01:00
2015-04-16 19:12:56 +02:00
if ($paymentType == PAYMENT_TYPE_PAYPAL) {
$gatewayId = GATEWAY_PAYPAL_EXPRESS;
} elseif ($paymentType == PAYMENT_TYPE_BITCOIN) {
$gatewayId = GATEWAY_BITPAY;
2016-01-18 10:13:39 +01:00
} elseif ($paymentType == PAYMENT_TYPE_DIRECT_DEBIT) {
$gatewayId = GATEWAY_GOCARDLESS;
} elseif ($paymentType == PAYMENT_TYPE_DWOLLA) {
2015-05-31 14:37:29 +02:00
$gatewayId = GATEWAY_DWOLLA;
2015-04-16 19:12:56 +02:00
}
2015-03-16 22:45:25 +01:00
if (!$gatewayId) {
Session::flash('error', trans('validation.required', ['attribute' => 'gateway']));
return Redirect::to('gateways/create')
->withInput();
}
$gateway = Gateway::findOrFail($gatewayId);
$fields = $gateway->getFields();
2015-06-03 19:55:48 +02:00
$optional = array_merge(Gateway::$hiddenFields, Gateway::$optionalFields);
if ($gatewayId == GATEWAY_DWOLLA) {
2015-06-03 19:55:48 +02:00
$optional = array_merge($optional, ['key', 'secret']);
} elseif ($gatewayId == GATEWAY_STRIPE) {
2016-02-18 18:00:21 +01:00
if (Utils::isNinjaDev()) {
// do nothing - we're unable to acceptance test with StripeJS
} else {
2016-02-18 18:00:21 +01:00
$rules['publishable_key'] = 'required';
}
2015-06-03 19:55:48 +02:00
}
2015-03-16 22:45:25 +01:00
foreach ($fields as $field => $details) {
2015-06-03 19:55:48 +02:00
if (!in_array($field, $optional)) {
2015-03-16 22:45:25 +01:00
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);
2015-04-16 19:12:56 +02:00
$oldConfig = null;
2015-03-16 22:45:25 +01:00
if ($accountGatewayPublicId) {
$accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
2015-11-01 19:21:11 +01:00
$oldConfig = $accountGateway->getConfig();
2015-03-16 22:45:25 +01:00
} else {
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = $gatewayId;
}
$config = new stdClass();
foreach ($fields as $field => $details) {
$value = trim(Input::get($gateway->id.'_'.$field));
2015-04-16 19:12:56 +02:00
// if the new value is masked use the original value
2015-09-17 21:01:06 +02:00
if ($oldConfig && $value && $value === str_repeat('*', strlen($value))) {
2015-04-16 19:12:56 +02:00
$value = $oldConfig->$field;
}
if (!$value && ($field == 'testMode' || $field == 'developerMode')) {
// do nothing
} else {
$config->$field = $value;
2015-03-16 22:45:25 +01:00
}
}
2015-11-29 21:13:50 +01:00
$publishableKey = Input::get('publishable_key');
if ($publishableKey = str_replace('*', '', $publishableKey)) {
$config->publishableKey = $publishableKey;
} elseif ($oldConfig && property_exists($oldConfig, 'publishableKey')) {
$config->publishableKey = $oldConfig->publishableKey;
}
2015-03-16 22:45:25 +01:00
$cardCount = 0;
if ($creditcards) {
foreach ($creditcards as $card => $value) {
$cardCount += intval($value);
}
}
2015-04-16 19:12:56 +02:00
$accountGateway->accepted_credit_cards = $cardCount;
2015-07-12 21:43:45 +02:00
$accountGateway->show_address = Input::get('show_address') ? true : false;
$accountGateway->update_address = Input::get('update_address') ? true : false;
2015-11-01 19:21:11 +01:00
$accountGateway->setConfig($config);
2015-04-16 19:12:56 +02:00
if ($accountGatewayPublicId) {
2015-03-16 22:45:25 +01:00
$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);
2015-07-12 21:43:45 +02:00
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
2015-03-16 22:45:25 +01:00
}
}
}