1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Ninja/Datatables/AccountGatewayDatatable.php

199 lines
8.9 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Ninja\Datatables;
2016-05-23 18:52:20 +02:00
2016-09-15 12:41:09 +02:00
use App\Models\AccountGatewaySettings;
use App\Models\GatewayType;
2016-05-23 18:52:20 +02:00
use URL;
2016-09-15 12:41:09 +02:00
use Cache;
use Utils;
use Session;
2016-05-23 18:52:20 +02:00
use App\Models\AccountGateway;
class AccountGatewayDatatable extends EntityDatatable
{
2016-09-26 11:33:30 +02:00
private static $accountGateways;
2016-05-23 18:52:20 +02:00
public $entityType = ENTITY_ACCOUNT_GATEWAY;
public function columns()
{
return [
[
'name',
function ($model) {
if ($model->deleted_at) {
return $model->name;
2016-09-26 11:33:30 +02:00
} elseif ($model->gateway_id == GATEWAY_CUSTOM) {
$accountGateway = $this->getAccountGateway($model->id);
$name = $accountGateway->getConfigField('name') . ' [' . trans('texts.custom') . ']';
return link_to("gateways/{$model->public_id}/edit", $name)->toHtml();
2016-05-23 18:52:20 +02:00
} elseif ($model->gateway_id != GATEWAY_WEPAY) {
return link_to("gateways/{$model->public_id}/edit", $model->name)->toHtml();
} else {
2016-09-26 11:33:30 +02:00
$accountGateway = $this->getAccountGateway($model->id);
2016-05-23 18:52:20 +02:00
$config = $accountGateway->getConfig();
$endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/';
$wepayAccountId = $config->accountId;
$wepayState = isset($config->state)?$config->state:null;
$linkText = $model->name;
$url = $endpoint.'account/'.$wepayAccountId;
$html = link_to($url, $linkText, ['target'=>'_blank'])->toHtml();
2016-05-23 18:52:20 +02:00
try {
if ($wepayState == 'action_required') {
2016-05-24 19:04:55 +02:00
$updateUri = $endpoint.'api/account_update/'.$wepayAccountId.'?redirect_uri='.urlencode(URL::to('gateways'));
2016-05-23 18:52:20 +02:00
$linkText .= ' <span style="color:#d9534f">('.trans('texts.action_required').')</span>';
2016-05-24 19:04:55 +02:00
$url = $updateUri;
2016-05-23 18:52:20 +02:00
$html = "<a href=\"{$url}\">{$linkText}</a>";
$model->setupUrl = $url;
} elseif ($wepayState == 'pending') {
$linkText .= ' ('.trans('texts.resend_confirmation_email').')';
$model->resendConfirmationUrl = $url = URL::to("gateways/{$accountGateway->public_id}/resend_confirmation");
$html = link_to($url, $linkText)->toHtml();
}
} catch(\WePayException $ex){}
return $html;
}
}
],
2016-09-15 12:41:09 +02:00
[
'limit',
function ($model) {
2016-09-26 11:33:30 +02:00
if ($model->gateway_id == GATEWAY_CUSTOM) {
$gatewayTypes = [GATEWAY_TYPE_CUSTOM];
} else {
$accountGateway = $this->getAccountGateway($model->id);
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = $paymentDriver->gatewayTypes();
$gatewayTypes = array_diff($gatewayTypes, array(GATEWAY_TYPE_TOKEN));
}
2016-09-15 12:41:09 +02:00
$html = '';
foreach ($gatewayTypes as $gatewayTypeId) {
$accountGatewaySettings = AccountGatewaySettings::scope()->where('account_gateway_settings.gateway_type_id',
'=', $gatewayTypeId)->first();
$gatewayType = GatewayType::find($gatewayTypeId);
if (count($gatewayTypes) > 1) {
if ($html) {
$html .= '<br>';
}
$html .= $gatewayType->name . ' &mdash; ';
}
if ($accountGatewaySettings && $accountGatewaySettings->min_limit !== null && $accountGatewaySettings->max_limit !== null) {
$html .= Utils::formatMoney($accountGatewaySettings->min_limit) . ' - ' . Utils::formatMoney($accountGatewaySettings->max_limit);
} elseif ($accountGatewaySettings && $accountGatewaySettings->min_limit !== null) {
$html .= trans('texts.min_limit',
array('min' => Utils::formatMoney($accountGatewaySettings->min_limit))
);
} elseif ($accountGatewaySettings && $accountGatewaySettings->max_limit !== null) {
$html .= trans('texts.max_limit',
array('max' => Utils::formatMoney($accountGatewaySettings->max_limit))
);
} else {
$html .= trans('texts.no_limit');
}
}
return $html;
}
],
2016-05-23 18:52:20 +02:00
];
}
public function actions()
{
2016-09-15 12:41:09 +02:00
$actions = [
2016-05-23 18:52:20 +02:00
[
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;
}
], [
2016-07-16 22:51:18 +02:00
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'),
2016-05-23 18:52:20 +02:00
function ($model) {
2016-09-26 11:33:30 +02:00
$accountGateway = $this->getAccountGateway($model->id);
2016-05-23 18:52:20 +02:00
$endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/';
return [
2016-05-23 18:52:20 +02:00
'url' => $endpoint.'account/'.$accountGateway->getConfig()->accountId,
'attributes' => 'target="_blank"'
];
2016-05-23 18:52:20 +02:00
},
function($model) {
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY;
}
2016-07-18 21:58:56 +02:00
], [
uctrans('texts.terms_of_service'),
function ($model) {
return 'https://go.wepay.com/terms-of-service-us';
},
function($model) {
return $model->gateway_id == GATEWAY_WEPAY;
}
2016-05-23 18:52:20 +02:00
]
];
2016-09-15 12:41:09 +02:00
foreach (Cache::get('gatewayTypes') as $gatewayType) {
$actions[] = [
trans('texts.set_limits', ['gateway_type' => $gatewayType->name]),
function () use ($gatewayType) {
2016-09-26 11:33:30 +02:00
$accountGatewaySettings = AccountGatewaySettings::scope()
->where('account_gateway_settings.gateway_type_id', '=', $gatewayType->id)
->first();
2016-09-15 12:41:09 +02:00
$min = $accountGatewaySettings && $accountGatewaySettings->min_limit !== null ? $accountGatewaySettings->min_limit : 'null';
$max = $accountGatewaySettings && $accountGatewaySettings->max_limit !== null ? $accountGatewaySettings->max_limit : 'null';
2016-09-26 11:33:30 +02:00
return "javascript:showLimitsModal('{$gatewayType->name}', {$gatewayType->id}, $min, $max)";
2016-09-15 12:41:09 +02:00
},
function ($model) use ($gatewayType) {
// Only show this action if the given gateway supports this gateway type
2016-09-26 11:33:30 +02:00
if ($model->gateway_id == GATEWAY_CUSTOM) {
return $gatewayType->id == GATEWAY_TYPE_CUSTOM;
} else {
$accountGateway = $this->getAccountGateway($model->id);
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = $paymentDriver->gatewayTypes();
2016-09-15 12:41:09 +02:00
2016-09-26 11:33:30 +02:00
return in_array($gatewayType->id, $gatewayTypes);
}
2016-09-15 12:41:09 +02:00
}
];
}
return $actions;
2016-05-23 18:52:20 +02:00
}
2016-09-26 11:33:30 +02:00
private function getAccountGateway($id)
{
if (isset(static::$accountGateways[$id])) {
return static::$accountGateways[$id];
}
static::$accountGateways[$id] = AccountGateway::find($id);
return static::$accountGateways[$id];
}
2016-05-23 18:52:20 +02:00
}