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

242 lines
10 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2016-05-23 18:52:20 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Ninja\Datatables;
use App\Models\AccountGateway;
2016-09-15 12:41:09 +02:00
use App\Models\AccountGatewaySettings;
use App\Models\GatewayType;
use Cache;
2017-01-30 20:40:43 +01:00
use URL;
2016-09-15 12:41:09 +02:00
use Utils;
2016-05-23 18:52:20 +02:00
class AccountGatewayDatatable extends EntityDatatable
{
2016-09-26 11:33:30 +02:00
private static $accountGateways;
2017-03-14 22:27:09 +01:00
private static $accountGatewaySettings;
2016-09-26 11:33:30 +02:00
2016-05-23 18:52:20 +02:00
public $entityType = ENTITY_ACCOUNT_GATEWAY;
public function columns()
{
return [
[
2017-03-16 21:56:13 +01:00
'gateway',
2016-05-23 18:52:20 +02:00
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') . ']';
2017-01-30 20:40:43 +01:00
2016-09-26 11:33:30 +02:00
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;
2017-01-30 20:40:43 +01:00
$wepayState = isset($config->state) ? $config->state : null;
2016-05-23 18:52:20 +02:00
$linkText = $model->name;
$url = $endpoint.'account/'.$wepayAccountId;
2017-01-30 20:40:43 +01:00
$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();
}
2017-01-30 17:05:31 +01:00
} catch (\WePayException $ex) {
}
2016-05-23 18:52:20 +02:00
return $html;
}
2017-01-30 20:40:43 +01:00
},
2016-05-23 18:52:20 +02:00
],
2016-09-15 12:41:09 +02:00
[
'limit',
function ($model) {
2017-03-14 22:27:09 +01:00
$gatewayTypes = $this->getGatewayTypes($model->id, $model->gateway_id);
2016-09-15 12:41:09 +02:00
$html = '';
foreach ($gatewayTypes as $gatewayTypeId) {
2017-03-14 22:27:09 +01:00
$accountGatewaySettings = $this->getAccountGatewaySetting($gatewayTypeId);
$gatewayType = Utils::getFromCache($gatewayTypeId, 'gatewayTypes');
2016-09-15 12:41:09 +02:00
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',
2017-01-30 20:40:43 +01:00
['min' => Utils::formatMoney($accountGatewaySettings->min_limit)]
2016-09-15 12:41:09 +02:00
);
} elseif ($accountGatewaySettings && $accountGatewaySettings->max_limit !== null) {
$html .= trans('texts.max_limit',
2017-01-30 20:40:43 +01:00
['max' => Utils::formatMoney($accountGatewaySettings->max_limit)]
2016-09-15 12:41:09 +02:00
);
} else {
$html .= trans('texts.no_limit');
}
}
return $html;
2017-01-30 20:40:43 +01:00
},
2016-09-15 12:41:09 +02:00
],
2017-03-14 14:18:31 +01:00
[
'fees',
function ($model) {
if (! $model->gateway_fee_enabled) {
2017-03-16 15:03:17 +01:00
return trans('texts.fees_disabled');
}
2017-03-14 22:27:09 +01:00
$gatewayTypes = $this->getGatewayTypes($model->id, $model->gateway_id);
$html = '';
foreach ($gatewayTypes as $gatewayTypeId) {
$accountGatewaySettings = $this->getAccountGatewaySetting($gatewayTypeId);
if (! $accountGatewaySettings || ! $accountGatewaySettings->areFeesEnabled()) {
continue;
}
$gatewayType = Utils::getFromCache($gatewayTypeId, 'gatewayTypes');
if (count($gatewayTypes) > 1) {
if ($html) {
$html .= '<br>';
}
$html .= $gatewayType->name . ' &mdash; ';
}
$html .= $accountGatewaySettings->feesToString();
2017-03-17 11:13:27 +01:00
if ($accountGatewaySettings->hasTaxes()) {
$html .= ' + ' . trans('texts.tax');
2017-03-17 11:13:27 +01:00
}
2017-03-14 22:27:09 +01:00
};
return $html ?: trans('texts.no_fees');
2017-03-14 14:18:31 +01:00
},
],
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;
},
2017-01-30 17:05:31 +01:00
function ($model) {
2017-01-30 20:40:43 +01:00
return ! $model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && ! empty($model->resendConfirmationUrl);
},
], [
2016-05-23 18:52:20 +02:00
uctrans('texts.edit_gateway'),
function ($model) {
return URL::to("gateways/{$model->public_id}/edit");
},
2017-01-30 17:05:31 +01:00
function ($model) {
2017-01-30 20:40:43 +01:00
return ! $model->deleted_at;
},
2016-05-23 18:52:20 +02:00
], [
2016-07-16 22:51:18 +02:00
uctrans('texts.finish_setup'),
function ($model) {
return $model->setupUrl;
},
2017-01-30 17:05:31 +01:00
function ($model) {
2017-01-30 20:40:43 +01:00
return ! $model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && ! empty($model->setupUrl);
},
2016-07-16 22:51:18 +02:00
], [
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/';
2017-01-30 20:40:43 +01:00
return [
2016-05-23 18:52:20 +02:00
'url' => $endpoint.'account/'.$accountGateway->getConfig()->accountId,
2017-01-30 20:40:43 +01:00
'attributes' => 'target="_blank"',
];
2016-05-23 18:52:20 +02:00
},
2017-01-30 17:05:31 +01:00
function ($model) {
2017-01-30 20:40:43 +01:00
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';
},
2017-01-30 17:05:31 +01:00
function ($model) {
2016-07-18 21:58:56 +02:00
return $model->gateway_id == GATEWAY_WEPAY;
2017-01-30 20:40:43 +01:00
},
],
2016-05-23 18:52:20 +02:00
];
2016-09-15 12:41:09 +02:00
foreach (Cache::get('gatewayTypes') as $gatewayType) {
$actions[] = [
2017-03-14 14:18:31 +01:00
trans('texts.set_limits_fees', ['gateway_type' => $gatewayType->name]),
2016-09-15 12:41:09 +02:00
function () use ($gatewayType) {
2017-03-14 16:45:36 +01:00
return "javascript:showLimitsModal('{$gatewayType->name}', {$gatewayType->id})";
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);
2017-03-14 14:18:31 +01:00
return $accountGateway->paymentDriver()->supportsGatewayType($gatewayType->id);
2016-09-26 11:33:30 +02:00
}
2017-01-30 20:40:43 +01:00
},
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];
}
2017-03-14 22:27:09 +01:00
private function getAccountGatewaySetting($gatewayTypeId)
{
if (isset(static::$accountGatewaySettings[$gatewayTypeId])) {
return static::$accountGatewaySettings[$gatewayTypeId];
}
static::$accountGatewaySettings[$gatewayTypeId] = AccountGatewaySettings::scope()
->where('account_gateway_settings.gateway_type_id', '=', $gatewayTypeId)->first();
return static::$accountGatewaySettings[$gatewayTypeId];
}
private function getGatewayTypes($id, $gatewayId)
{
if ($gatewayId == GATEWAY_CUSTOM) {
$gatewayTypes = [GATEWAY_TYPE_CUSTOM];
} else {
$accountGateway = $this->getAccountGateway($id);
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = $paymentDriver->gatewayTypes();
$gatewayTypes = array_diff($gatewayTypes, [GATEWAY_TYPE_TOKEN]);
}
return $gatewayTypes;
}
2016-05-23 18:52:20 +02:00
}