High-Volume POS and Database Integration REST API

Contributed, as part of the internal backend team, to a high-throughput REST API integrating physical POS terminals with core databases for an amusement park hosting 185k+ yearly guests — developed in close collaboration with the POS vendor.

  • Complex Business Logic & Entitlements: Implemented key backend features for order creation and cancellation across single tickets, subscriptions, and gift cards.
  • Resilient Architecture: Implemented atomic database transactions paired with deferred third-party API calls to prevent rollbacks on paid orders and ensure data consistency during peak season loads.

  • PHP
  • REST API
  • Complex SQL
  • Atomic Transactions
  • System Integration
  • Business Logic
  • Cross-Team Collaboration

Code Snippet

PHP
<?php
/**
 * Amusement park API — order cancellation endpoint.
 * Handles two distinct cases: cancelling an existing order (tickets, discounts,
 * gift-card payments to refund) and cancelling a standalone gift-card transaction
 * with no associated order. Anonymized/condensed for portfolio purposes.
 *
 * Note: follows the same pattern used across the API — local DB transaction
 * first, external payment-provider calls (credit/delete) only after commit,
 * with alerting rather than rollback if an external call fails post-commit.
 */


if ($request['action'] === 'cancelOrder') {


    if (empty($data['idTransaction']) || !isIDTransactionValid($data['idTransaction'])) {
        json(['message' => 'Missing data to cancel the order', 'requestCode' => 0]);
        exit();
    }


    $idTransaction = $data['idTransaction'];
    $externalApiWarnings = [];


    $order = new Order();
    $order->setTransaction($idTransaction);


    $externalCreditsToProcess = [];
    $externalCardDeletesToProcess = [];


    if ($order->showByTransaction()) {


        // --- Business guardrails, checked before touching anything ---


        $client = new Client();
        $client->setId($order->getClientId());
        if ($client->show() && $client->getType() === CLIENT_TYPE[1]) {
            $corporateClient = new CorporateClient();
            if ($corporateClient->showByClientId($client->getId())) {
                if (in_array($corporateClient->getCompanyType(), [COMPANY_TYPE[0], COMPANY_TYPE[1]])) {
                    json(['message' => 'An order placed under a corporate/committee contract cannot be cancelled.', 'requestCode' => 0]);
                    exit();
                }
            }
        }


        $paymentMethods = $order->getPaymentMethod();
        $giftCard = new GiftCard();


        if ($giftCard->showByOrder($order->getId()) && str_contains($paymentMethods, 'GIFT_CARD')) {
            json(['message' => 'An order paid by gift card and containing a gift card cannot be cancelled in bulk. Each ticket must be cancelled individually with a chosen refund method.', 'requestCode' => 0]);
            exit();
        }


        if (!$paymentMethods) {
            json(['message' => 'Unknown payment method(s) for this order', 'requestCode' => 0]);
            exit();
        }


        // --- Local transaction: archive order, tickets, discounts ---


        DB::beginTransaction();


        if (str_contains($paymentMethods, 'GIFT_CARD')) {
            $giftCardTransaction = new GiftCardTransaction();
            $transactions = $giftCardTransaction->showByOrderId($order->getId());


            foreach ($transactions ?: [] as $code => $amount) {
                $externalCreditsToProcess[] = ['code' => $code, 'amountCents' => euroToCents($amount)];
            }


            if (!$giftCardTransaction->archiveTransaction($order->getId())) {
                DB::rollBack();
                json(['message' => 'Error cancelling the gift card transaction', 'requestCode' => 0]);
                exit();
            }
        }


        if (!$order->archive()) {
            DB::rollBack();
            json(['message' => 'Error archiving the order', 'requestCode' => 0]);
            exit();
        }


        // Cancel every ticket tied to the order, plus its associated visit/subscription card
        $ticket = new Ticket();
        $ticket->setOrderId($order->getId());


        foreach ($ticket->showByOrder() ?: [] as $ticketItem) {
            $ticketLocal = new Ticket();
            $ticketLocal->setId($ticketItem->id);

            $wasAlreadyCancelled = ($ticketItem->status == '0' && $ticketItem->statusReason == 'Transaction cancelled');

            if (!$wasAlreadyCancelled) {
                $ticketLocal->setStatusReason('Transaction cancelled');
                if (!$ticketLocal->cancel()) {
                    DB::rollBack();
                    json(['message' => 'Error cancelling ticket ' . $ticketItem->id, 'requestCode' => 0]);
                    exit();
                }

                $isSubscription = preg_match('/^(KK|XX|AA|XAA)/', $ticketItem->code);
                if (!(new Visit())->delete($ticketItem->code, $isSubscription)) {
                    errorLogPerso('Visit for ticket ' . $ticketItem->code . ' was not cancelled in cancelOrder');
                }

                if ($ticketLocal->show() && !empty($ticketLocal->getCardNumber()) && $ticketLocal->getCategory() != 'INFANT') {
                    $externalCardDeletesToProcess[] = $ticketLocal->getCardNumber();
                }
            }
        }


        DB::commit();


        // --- External provider calls, only after local commit succeeded ---


        foreach ($externalCreditsToProcess as $credit) {
            if (!externalCreditWallet($credit['code'], $credit['amountCents'])) {
                sendErrorMail("Failed to credit gift card in cancelOrder: {$credit['code']}, order #{$order->getId()}");
                $externalApiWarnings[] = "Failed to re-credit gift card {$credit['code']}.";
            }
        }

        foreach ($externalCardDeletesToProcess as $cardNumber) {
            if (!externalDeleteCard($cardNumber)) {
                sendErrorMail("Failed to delete subscription card in cancelOrder: {$cardNumber}, order #{$order->getId()}");
                $externalApiWarnings[] = "Failed to cancel subscription card {$cardNumber}.";
            }
        }

        json([
            'message' => 'Order archived successfully.',
            'requestCode' => 1,
            'warnings' => $externalApiWarnings,
        ]);
        exit();


    } else {
        // No order found — this may be a standalone gift-card transaction (no tickets involved)
        // ... same commit-then-notify pattern, omitted here for brevity
    }
}