1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Services/AccountGatewayService.php

135 lines
5.2 KiB
PHP
Raw Normal View History

2015-11-05 23:37:04 +01:00
<?php namespace App\Services;
use URL;
use App\Models\Gateway;
2016-05-12 04:55:37 +02:00
use App\Models\AccountGateway;
2015-11-05 23:37:04 +01:00
use App\Services\BaseService;
use App\Ninja\Repositories\AccountGatewayRepository;
class AccountGatewayService extends BaseService
{
protected $accountGatewayRepo;
protected $datatableService;
public function __construct(AccountGatewayRepository $accountGatewayRepo, DatatableService $datatableService)
{
$this->accountGatewayRepo = $accountGatewayRepo;
$this->datatableService = $datatableService;
}
protected function getRepo()
{
return $this->accountGatewayRepo;
}
/*
public function save()
{
return null;
}
*/
public function getDatatable($accountId)
{
$query = $this->accountGatewayRepo->find($accountId);
return $this->createDatatable(ENTITY_ACCOUNT_GATEWAY, $query, false);
}
protected function getDatatableColumns($entityType, $hideClient)
{
return [
[
'name',
function ($model) {
2016-05-14 23:23:20 +02:00
if ($model->deleted_at) {
return $model->name;
} elseif ($model->gateway_id != GATEWAY_WEPAY) {
2016-05-12 04:55:37 +02:00
return link_to("gateways/{$model->public_id}/edit", $model->name)->toHtml();
} else {
$accountGateway = AccountGateway::find($model->id);
2016-05-18 15:51:20 +02:00
$config = $accountGateway->getConfig();
$endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/';
2016-05-18 15:51:20 +02:00
$wepayAccountId = $config->accountId;
$wepayState = isset($config->state)?$config->state:null;
2016-05-12 04:55:37 +02:00
$linkText = $model->name;
$url = $endpoint.'account/'.$wepayAccountId;
$wepay = \Utils::setupWepay($accountGateway);
$html = link_to($url, $linkText, array('target'=>'_blank'))->toHtml();
try {
2016-05-18 15:51:20 +02:00
if ($wepayState == 'action_required') {
2016-05-12 04:55:37 +02:00
$updateUri = $wepay->request('/account/get_update_uri', array(
'account_id' => $wepayAccountId,
'redirect_uri' => URL::to('gateways'),
));
$linkText .= ' <span style="color:#d9534f">('.trans('texts.action_required').')</span>';
$url = $updateUri->uri;
$html = "<a href=\"{$url}\">{$linkText}</a>";
$model->setupUrl = $url;
2016-05-18 15:51:20 +02:00
} elseif ($wepayState == 'pending') {
2016-05-12 04:55:37 +02:00
$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;
}
2015-11-05 23:37:04 +01:00
}
],
[
'payment_type',
function ($model) {
return Gateway::getPrettyPaymentType($model->gateway_id);
}
],
];
}
protected function getDatatableActions($entityType)
{
return [
[
2016-05-12 04:55:37 +02:00
uctrans('texts.resend_confirmation_email'),
function ($model) {
return $model->resendConfirmationUrl;
},
function($model) {
2016-05-14 23:23:20 +02:00
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->resendConfirmationUrl);
2016-05-12 04:55:37 +02:00
}
], [
uctrans('texts.finish_setup'),
function ($model) {
return $model->setupUrl;
},
function($model) {
2016-05-14 23:23:20 +02:00
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->setupUrl);
2016-05-12 04:55:37 +02:00
}
] , [
2016-05-14 23:23:20 +02:00
uctrans('texts.edit_gateway'),
function ($model) {
return URL::to("gateways/{$model->public_id}/edit");
},
function($model) {
return !$model->deleted_at;
}
], [
2016-05-12 04:55:37 +02:00
uctrans('texts.manage_wepay_account'),
function ($model) {
$accountGateway = AccountGateway::find($model->id);
$endpoint = WEPAY_ENVIRONMENT == WEPAY_STAGE ? 'https://stage.wepay.com/' : 'https://www.wepay.com/';
2016-05-12 04:55:37 +02:00
return array(
'url' => $endpoint.'account/'.$accountGateway->getConfig()->accountId,
'attributes' => 'target="_blank"'
);
},
function($model) {
2016-05-14 23:23:20 +02:00
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY;
2015-11-05 23:37:04 +01:00
}
]
];
}
}