Skip to content

Commit

Permalink
Allow basic listing of subscriptions to the admins
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 11, 2023
1 parent eb5953e commit 946a56d
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 3 deletions.
96 changes: 96 additions & 0 deletions Resources/templates/responsive/admin/subscriptions/list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

$filters = $this->filters;
$the_filters = array(
'projects' => array(
'label' => 'Proyecto',
'first' => 'Todos los proyectos'
),
'statuses' => array(
'label' => 'Estado de la subscripción',
'first' => 'Todos los estados'
)
);

?>
<?php $this->layout('admin/layout') ?>

<?php $this->section('admin-content') ?>

<div class="widget board">
<h3 class="title">Filtros</h3>
<form id="filter-form" action="/admin/subscriptions" method="get">
<?php foreach ($the_filters as $filter => $data) : ?>
<div style="float:left;margin:5px;">
<label for="<?= $filter ?>-filter"><?= $data['label'] ?></label><br />
<select id="<?= $filter ?>-filter" name="<?= $filter ?>">
<option>
<?= $data['first'] ?>
</option>
<?php foreach ($this->raw($filter) as $itemId => $itemName) : ?>
<option value="<?= $itemId ?>" <?php if ($filters[$filter] === (string) $itemId) echo ' selected="selected"'; ?>>
<?= $itemName ?>
</option>
<?php endforeach ?>
</select>
</div>
<?php endforeach ?>

<br clear="both" />

<div style="float:left;margin:5px;">
<label for="name-filter">Usuario:</label><br />
<input type="text" id="name-filter" name="name" value="<?= $filters['name'] ?>" />
</div>

<br clear="both" />

<div style="float:left;margin:5px;">
<input type="submit" value="filtrar" />
</div>
</form>
<br clear="both" />
<a href="/admin/accounts?reset=filters">[<?= $this->text('admin-remove-filters') ?>]</a>
</div>

<div class="widget board">
<?php if ($this->list) : ?>
<table width="100%">
<thead>
<tr>
<th>Proyecto</th>
<th>Usuario</th>
<th>Recompensa</th>
<th>Importe</th>
<th>Estado</th>
<th>Stripe</th>
</tr>
</thead>

<tbody>
<?php foreach ($this->list as $subscription) : ?>
<?php $plan = $subscription['plan'] ?>
<?php $price = $subscription['items']['data'][0]['price']; ?>
<tr>
<td><?= $subscription['metadata']['project'] ?></td>
<td><?= $subscription['metadata']['user'] ?></td>
<td><?= $subscription['metadata']['reward'] ?></td>
<td><?= \amount_format($price['unit_amount'] / 100) . '/' . $plan['interval'] ?></td>
<td><span class="label label-info"><?= $subscription['status'] ?></span></td>
<td>
<a class="btn btn-default" title="<?= $this->text('regular-view') ?>" href="https://dashboard.stripe.com/subscriptions/<?= $subscription['id'] ?>"><i class="fa fa-info-circle"></i></a>
</td>
</tr>
<?php endforeach ?>
</tbody>

</table>

<?php else : ?>
<p>No hay subscripciones que cumplan con los filtros.</p>
<?php endif; ?>
</div>

<?= $this->insert('partials/utils/paginator', ['total' => $this->total, 'limit' => $this->limit]) ?>

<?php $this->replace() ?>
83 changes: 80 additions & 3 deletions src/Goteo/Controller/Admin/SubscriptionsAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,97 @@

namespace Goteo\Controller\Admin;

use Goteo\Application\Config;
use Goteo\Model\Project;
use Stripe\StripeClient;
use Stripe\Subscription;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;

class SubscriptionsAdminController extends AbstractAdminController
{
public static string $label = 'admin-subscriptions';

private StripeClient $stripe;

public function __construct()
{
parent::__construct();

$this->stripe = new StripeClient(Config::get('payments.stripe.secretKey'));
}

public static function getGroup(): string
{
return 'activity';
}

public function indexAction(Request $request): Response
public static function getRoutes(): array
{
return [
new Route(
'/',
['_controller' => __CLASS__ . "::listAction"]
),
];
}

public function listAction(Request $request)
{
return new Response('test', 200);
$searchFilters = [sprintf('created<%d', time())];
$filters = $request->query->all();

if (!empty($filters)) {
foreach ($request->query->all() as $key => $value) {
if (empty($value)) continue;

switch ($key) {
case 'statuses':
if ($value === "Todos los estados") break;
$searchFilters[] = "status:'$value'";
break;
case 'projects':
$searchFilters[] = "metadata['project']:'$value'";
break;
case 'name':
$searchFilters[] = "metadata['user']:'$value'";
break;
}
}
}

$statuses = array_column(array_map(function ($status) {
return ['name' => $status, 'id' => $status];
}, [
Subscription::STATUS_ACTIVE,
Subscription::STATUS_CANCELED,
Subscription::STATUS_INCOMPLETE,
Subscription::STATUS_INCOMPLETE_EXPIRED,
Subscription::STATUS_PAST_DUE,
Subscription::STATUS_PAUSED,
Subscription::STATUS_TRIALING,
Subscription::STATUS_UNPAID
]), 'name', 'id');

$total_projects = Project::getList(['status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]], null, 0, 0, true);
$projects = Project::getList(['status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]], null, 0, $total_projects);
$projects = array_column($projects, 'name', 'id');

$result = $this->stripe->subscriptions->search([
'query' => implode(" AND ", $searchFilters)
]);

$subscriptions = [];
foreach ($result->data as $result) {
$subscriptions[] = $result->toArray();
}

return $this->viewResponse('admin/subscriptions/list', [
'list' => $subscriptions,
'result' => $result,
'projects' => $projects,
'statuses' => $statuses,
'filters' => $filters
]);
}
}

0 comments on commit 946a56d

Please sign in to comment.