A refund tool in 40 lines of PHP

Dec 01, 2026 6 min read 32 views
A refund tool in 40 lines of PHP

The first two posts showed the idea of Script UI and what one click does with a hello-world. This one walks through a tool that changes data, the kind that makes people careful: refunds.

It is the refund tool of the demo, as it lies in the repository. $shop stands in for your own code.

<?php
// Refund orders
// Find a customer's orders, tick the ones to refund, confirm.

require_once __DIR__ . '/_shop.php';

use function Runio\ScriptUi\{title, text, select, table, button, success, info, rerun};

$shop = Shop::open();

title('Refund orders');

$email = text('Customer email', placeholder: 'ava@example.com', help: 'Try ava@example.com or omar@example.com.');
$status = select('Status', ['paid', 'shipped']);

if ($email === '') {
    info('Type a customer email to see their orders.');

    return;
}

$orders = $shop->orders(email: $email, status: $status);

$picked = table(
    $orders,
    selectable: true,
    columns: ['id' => 'Order', 'created_at' => 'Placed on', 'items' => 'Items', 'total' => 'Total'],
    empty: 'No ' . $status . ' orders for ' . $email . '.',
);

$count = count($picked) === 1 ? '1 order' : count($picked) . ' orders';

if ($picked && button('Refund selected', style: 'danger', confirm: 'Refund ' . $count . '?')) {
    foreach ($picked as $order) {
        $shop->refund($order['id']);
    }

    success('Refunded ' . $count . '.');
    rerun();
}

The refund tool: two orders of a customer are ticked, and a question asks "Refund 2 orders?" with the buttons "Yes" and "Cancel".

Nothing to do yet: lines 16 to 20

Without an email there is nothing to show. info() says what to type, and an early return ends the page there. The table and the button are not on the page, because their lines never run.

The table returns the ticked rows: lines 24 to 29

table() draws the orders with a tick box in every row, and returns the rows that are ticked. They come back exactly as you passed them in. The browser only says which rows are ticked, so $order['id'] is an id of yours, never something that was typed into a request.

columns: names the columns and their headers, and empty: is what the table says when it has no rows.

The button asks first: line 33

$picked && stands in front of the button, so it is on the page only while something is ticked. confirm: makes it ask before it acts, and the question says what will happen: "Refund 2 orders?"

button() returns true only after the user answered yes to exactly that question. When the selection changes in between, the question changes with it, and an old yes does not count.

The work: lines 34 to 39

It runs once for each yes. rerun() then runs the script again from the top, so that the table shows the orders that are left. The message that was written before it survives that:

After the refund: the message "Refunded 2 orders." stands above a table with the one order that is left.

Behind your own login

So far the tool runs on your computer. php vendor/bin/script-ui serve tools starts PHP's own server, and without a login check a tool answers nobody else.

For colleagues, one script in the web root serves the folder of tools:

// public/admin.php: the only file of all this that lies in the web root
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../bootstrap.php';         // starts your session, connects your database

Runio\ScriptUi\serve(
    __DIR__ . '/../tools',
    auth: fn () => $_SESSION['admin_email'] ?? null,
    allow: fn (mixed $user, string $tool) => $permissions->has($user, 'tool.' . $tool),
    log: fn (array $entry) => $auditLog->write($entry),
    name: 'Back office',
);
  • auth: says who is asking, or null to refuse. It is your session and your login. The library has none of its own.
  • allow: decides for one user and one tool. A tool that is not allowed is left out of the list, and its address answers "Not allowed" before anything of the tool runs.
  • log: is called for every button that a user presses and for every file that a user downloads. An entry says who it was, which tool and which button, whether a question was answered with yes, and what every input held.

Keep the folder of tools out of the web root, next to vendor/. A file that the web server runs by itself never passes serve(), so nobody would ask auth: or allow: about it.

Inside Symfony the same folder runs behind the login of the framework. A controller calls respond(), which works out the same page and hands it back instead of sending it:

// src/Controller/ToolsController.php
use function Runio\ScriptUi\respond;

final class ToolsController extends AbstractController
{
    #[Route('/admin/tools/{tool}', defaults: ['tool' => ''], methods: ['GET', 'POST'])]
    public function tools(Request $request): Response
    {
        $page = respond(
            $this->getParameter('kernel.project_dir') . '/tools',
            base: '/admin/tools',
            auth: fn () => $this->getUser(),
            allow: fn ($user, string $tool) => $tool !== 'payroll' || $this->isGranted('ROLE_PAYROLL'),
            session: $request->getSession(),
        );

        return new Response($page->body, $page->status, $page->headers);
    }
}

Laravel does the same with a route. The third tutorial has both.

Three rules for a tool that changes data

  1. Change data only inside if (button(...)). The script also runs when somebody only changes a filter.
  2. Ask with confirm: before something that cannot be undone, and say in the question what will happen.
  3. Check what was typed behind the button, and ask valid() before the work. field_error() writes under an input what is wrong with it.

The demo has nine more tools, among them a coupon form with checks, an import with a preview, an export that takes half a minute, a dashboard, and an Arabic page that runs from right to left. The examples show each of them with a picture and its source, and the code is on GitHub.

AM

Author Amer Malik Mohammed

Full-Stack Developer with 2+ years of experience in object-oriented PHP development (Symfony), JavaScript and MySQL. Specialized in e-commerce solutions (Shopware 5/6), REST API development and process automation in agile teams.

Contact author