<?php
declare(strict_types=1);
/**
* BalanceController.php File.
* This file is part of the Payment.net Project.
*
* PHP version 8
*
* @category Public Api
* @package Bdm\PublicApiBundle\Controller\Api\Mock
* @author
* @link http://www.payment.net/
*
* FEATURES :
* ==========
*
* TODO-LIST :
* ===========
*
* HISTORY :
* =========
**/
namespace Bdm\PublicApiBundle\Controller\Api\Mock\V1;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class BalanceController extends AbstractFOSRestController
{
const BALANCE_NOT_FOUND = 'Balance not found';
const DEFAULT_BALANCE_REFERENCE = 'b44991d6-8329-11e7-af92-0050568b4e00';
public function getAction(string $sReference, Request $oRequest)
{
$aBalanceReferences = $this->getAllReferences();
if(!in_array($sReference, $aBalanceReferences)) {
throw $this->createNotFoundException(self::BALANCE_NOT_FOUND);
}
$oAllBalanceData = $this->getAllData();
$aData = $oAllBalanceData->$sReference;
return $this->handleView(View::create($aData, 200));
}
public function getListAction($iPage = 1, Request $oRequest)
{
$iPage = \abs((int) $iPage);
if ($iPage === 0) {
$iPage = 1;
}
$iItemNumberPerPage = 10;
$aPagination = [];
$aPagination['iTotalCount'] = count($this->getAllReferences());
$aPagination['iItemNumberPerPage'] = $iItemNumberPerPage;
$iNumberOfPages = (int) ceil($aPagination['iTotalCount'] / $aPagination['iItemNumberPerPage']);
if ($iPage > $iNumberOfPages) {
$iPage = $iNumberOfPages;
}
// $aPagination['iNumberOfPages'] = $iNumberOfPages;
$iQ = $aPagination['iItemNumberPerPage'];
$iP = $iPage;
// First item to display for the page
$iMin = ($iPage-1)*$iItemNumberPerPage + 1;
// Last item to display for the page
$iMax = $iPage*$iItemNumberPerPage;
$oAllBalanceData = $this->getAllData();
$iIndex = 0;
foreach ($oAllBalanceData as $oItem) {
$iIndex++;
if($iIndex >= $iMin && $iIndex <= $iMax) {
$aPagination['oItems'][] = $oItem;
}
}
$aPagination['iCurrentPage'] = $iPage;
return $aPagination;
}
public function createAction(Request $oRequest)
{
$aData = $this->getJsonBody($oRequest);
$aRequired = ['sCurrencyIsoCode', 'sType'];
foreach ($aRequired as $sKey) {
if (!array_key_exists($sKey, $aData)) {
throw new BadRequestHttpException("Missing required field: {$sKey}");
}
}
$sCurrencyIsoCode = $aData['sCurrencyIsoCode'];
if (!is_string($sCurrencyIsoCode)) {
throw new BadRequestHttpException('sCurrencyIsoCode must be a string.');
}
if (strlen($sCurrencyIsoCode) !== 3) {
throw new BadRequestHttpException('sCurrencyIsoCode must be a 3-letter ISO currency code.');
}
$sCurrencyIsoCode = strtoupper($sCurrencyIsoCode);
if ($sCurrencyIsoCode !== 'EUR') {
throw new BadRequestHttpException('sCurrencyIsoCode must be EUR for a SEPA balance.');
}
$sType = $aData['sType'];
if (!is_string($sType)) {
throw new BadRequestHttpException('sType must be a string.');
}
if ($sType !== 'payment') {
throw new BadRequestHttpException('sType value should be payment.');
}
$sReference = uuid_create(UUID_TYPE_RANDOM);
$aData = [
'sReference' => $sReference,
'sCreationDate' => (new \DateTimeImmutable())->format('Y-m-d h:i:s'),
'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
'bActive' => true,
'fMainBalance' => 0,
'sCurrencyIsoCode' => 'EUR',
'bDefault' => false,
'sType' => 'payment'
];
return $this->handleView(View::create($aData, 200));
}
public function createIbanAction(string $sReference, Request $oRequest)
{
if($sReference !== 'c41fa8de-7345-4b29-9f5c-2f98f88c3921') {
throw $this->createNotFoundException(self::BALANCE_NOT_FOUND);
}
$sReference = 'c41fa8de-7345-4b29-9f5c-2f98f88c3921';
$aData = [
'sReference' => $sReference,
'sCreationDate' => '2025-11-17 09:14:21',
'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
'bActive' => true,
'fMainBalance' => 0,
'sCurrencyIsoCode' => 'EUR',
'bDefault' => false,
'sIban' => 'FR7630003000406598442574I73',
'sBic' => 'BUMDFRP2XXX',
'sType' => 'payment'
];
return $this->handleView(View::create($aData, 200));
}
protected function getAllReferences() {
$oAllBalanceData=$this->getAllData();
$aAllBalanceReferences = array_keys(get_object_vars($oAllBalanceData));
return $aAllBalanceReferences;
}
protected function getAllData() {
$oAllBalanceData = (object)[];
// Balance #1
// --------------------
$sReference = self::DEFAULT_BALANCE_REFERENCE;
$aData = [
'sReference' => $sReference,
'sCreationDate' => '2025-08-17 10:54:39',
'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
'bActive' => true,
'fMainBalance' => 7824.54,
'sCurrencyIsoCode' => 'EUR',
'bDefault' => true,
'sIban' => 'FR7617569000307417898225F82',
'sBic' => 'BUMDFRP2XXX',
'sType' => 'payment'
];
$oAllBalanceData->$sReference = $aData;
// Balance #2
// --------------------
$sReference = 'c41fa8de-7345-4b29-9f5c-2f98f88c3921';
$aData = [
'sReference' => $sReference,
'sCreationDate' => '2025-08-18 12:23:13',
'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
'bActive' => true,
'fMainBalance' => 0,
'sCurrencyIsoCode' => 'EUR',
'bDefault' => false,
'sType' => 'payment'
];
$oAllBalanceData->$sReference = $aData;
return $oAllBalanceData;
}
protected function getJsonBody(Request $oRequest)
{
$sContent = $oRequest->getContent();
if ($sContent === '' || $sContent === null) {
throw new BadRequestHttpException('Request body must not be empty.');
}
$aData = json_decode($sContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new BadRequestHttpException('Invalid JSON payload.');
}
if (!is_array($aData)) {
throw new BadRequestHttpException('JSON payload must be an object.');
}
return $aData;
}
}