bundles/PublicApiBundle/Controller/Api/Mock/V1/BalanceController.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * BalanceController.php File.
  5. * This file is part of the Payment.net Project.
  6. *
  7. * PHP version 8
  8. *
  9. * @category Public Api
  10. * @package Bdm\PublicApiBundle\Controller\Api\Mock
  11. * @author
  12. * @link http://www.payment.net/
  13. *
  14. * FEATURES :
  15. * ==========
  16. *
  17. * TODO-LIST :
  18. * ===========
  19. *
  20. * HISTORY :
  21. * =========
  22. **/
  23. namespace Bdm\PublicApiBundle\Controller\Api\Mock\V1;
  24. use FOS\RestBundle\Controller\AbstractFOSRestController;
  25. use FOS\RestBundle\View\View;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  28. class BalanceController extends AbstractFOSRestController
  29. {
  30. const BALANCE_NOT_FOUND = 'Balance not found';
  31. const DEFAULT_BALANCE_REFERENCE = 'b44991d6-8329-11e7-af92-0050568b4e00';
  32. public function getAction(string $sReference, Request $oRequest)
  33. {
  34. $aBalanceReferences = $this->getAllReferences();
  35. if(!in_array($sReference, $aBalanceReferences)) {
  36. throw $this->createNotFoundException(self::BALANCE_NOT_FOUND);
  37. }
  38. $oAllBalanceData = $this->getAllData();
  39. $aData = $oAllBalanceData->$sReference;
  40. return $this->handleView(View::create($aData, 200));
  41. }
  42. public function getListAction($iPage = 1, Request $oRequest)
  43. {
  44. $iPage = \abs((int) $iPage);
  45. if ($iPage === 0) {
  46. $iPage = 1;
  47. }
  48. $iItemNumberPerPage = 10;
  49. $aPagination = [];
  50. $aPagination['iTotalCount'] = count($this->getAllReferences());
  51. $aPagination['iItemNumberPerPage'] = $iItemNumberPerPage;
  52. $iNumberOfPages = (int) ceil($aPagination['iTotalCount'] / $aPagination['iItemNumberPerPage']);
  53. if ($iPage > $iNumberOfPages) {
  54. $iPage = $iNumberOfPages;
  55. }
  56. // $aPagination['iNumberOfPages'] = $iNumberOfPages;
  57. $iQ = $aPagination['iItemNumberPerPage'];
  58. $iP = $iPage;
  59. // First item to display for the page
  60. $iMin = ($iPage-1)*$iItemNumberPerPage + 1;
  61. // Last item to display for the page
  62. $iMax = $iPage*$iItemNumberPerPage;
  63. $oAllBalanceData = $this->getAllData();
  64. $iIndex = 0;
  65. foreach ($oAllBalanceData as $oItem) {
  66. $iIndex++;
  67. if($iIndex >= $iMin && $iIndex <= $iMax) {
  68. $aPagination['oItems'][] = $oItem;
  69. }
  70. }
  71. $aPagination['iCurrentPage'] = $iPage;
  72. return $aPagination;
  73. }
  74. public function createAction(Request $oRequest)
  75. {
  76. $aData = $this->getJsonBody($oRequest);
  77. $aRequired = ['sCurrencyIsoCode', 'sType'];
  78. foreach ($aRequired as $sKey) {
  79. if (!array_key_exists($sKey, $aData)) {
  80. throw new BadRequestHttpException("Missing required field: {$sKey}");
  81. }
  82. }
  83. $sCurrencyIsoCode = $aData['sCurrencyIsoCode'];
  84. if (!is_string($sCurrencyIsoCode)) {
  85. throw new BadRequestHttpException('sCurrencyIsoCode must be a string.');
  86. }
  87. if (strlen($sCurrencyIsoCode) !== 3) {
  88. throw new BadRequestHttpException('sCurrencyIsoCode must be a 3-letter ISO currency code.');
  89. }
  90. $sCurrencyIsoCode = strtoupper($sCurrencyIsoCode);
  91. if ($sCurrencyIsoCode !== 'EUR') {
  92. throw new BadRequestHttpException('sCurrencyIsoCode must be EUR for a SEPA balance.');
  93. }
  94. $sType = $aData['sType'];
  95. if (!is_string($sType)) {
  96. throw new BadRequestHttpException('sType must be a string.');
  97. }
  98. if ($sType !== 'payment') {
  99. throw new BadRequestHttpException('sType value should be payment.');
  100. }
  101. $sReference = uuid_create(UUID_TYPE_RANDOM);
  102. $aData = [
  103. 'sReference' => $sReference,
  104. 'sCreationDate' => (new \DateTimeImmutable())->format('Y-m-d h:i:s'),
  105. 'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
  106. 'bActive' => true,
  107. 'fMainBalance' => 0,
  108. 'sCurrencyIsoCode' => 'EUR',
  109. 'bDefault' => false,
  110. 'sType' => 'payment'
  111. ];
  112. return $this->handleView(View::create($aData, 200));
  113. }
  114. public function createIbanAction(string $sReference, Request $oRequest)
  115. {
  116. if($sReference !== 'c41fa8de-7345-4b29-9f5c-2f98f88c3921') {
  117. throw $this->createNotFoundException(self::BALANCE_NOT_FOUND);
  118. }
  119. $sReference = 'c41fa8de-7345-4b29-9f5c-2f98f88c3921';
  120. $aData = [
  121. 'sReference' => $sReference,
  122. 'sCreationDate' => '2025-11-17 09:14:21',
  123. 'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
  124. 'bActive' => true,
  125. 'fMainBalance' => 0,
  126. 'sCurrencyIsoCode' => 'EUR',
  127. 'bDefault' => false,
  128. 'sIban' => 'FR7630003000406598442574I73',
  129. 'sBic' => 'BUMDFRP2XXX',
  130. 'sType' => 'payment'
  131. ];
  132. return $this->handleView(View::create($aData, 200));
  133. }
  134. protected function getAllReferences() {
  135. $oAllBalanceData=$this->getAllData();
  136. $aAllBalanceReferences = array_keys(get_object_vars($oAllBalanceData));
  137. return $aAllBalanceReferences;
  138. }
  139. protected function getAllData() {
  140. $oAllBalanceData = (object)[];
  141. // Balance #1
  142. // --------------------
  143. $sReference = self::DEFAULT_BALANCE_REFERENCE;
  144. $aData = [
  145. 'sReference' => $sReference,
  146. 'sCreationDate' => '2025-08-17 10:54:39',
  147. 'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
  148. 'bActive' => true,
  149. 'fMainBalance' => 7824.54,
  150. 'sCurrencyIsoCode' => 'EUR',
  151. 'bDefault' => true,
  152. 'sIban' => 'FR7617569000307417898225F82',
  153. 'sBic' => 'BUMDFRP2XXX',
  154. 'sType' => 'payment'
  155. ];
  156. $oAllBalanceData->$sReference = $aData;
  157. // Balance #2
  158. // --------------------
  159. $sReference = 'c41fa8de-7345-4b29-9f5c-2f98f88c3921';
  160. $aData = [
  161. 'sReference' => $sReference,
  162. 'sCreationDate' => '2025-08-18 12:23:13',
  163. 'sUpdateDate' => (new \DateTimeImmutable())->format('Y-m-d'),
  164. 'bActive' => true,
  165. 'fMainBalance' => 0,
  166. 'sCurrencyIsoCode' => 'EUR',
  167. 'bDefault' => false,
  168. 'sType' => 'payment'
  169. ];
  170. $oAllBalanceData->$sReference = $aData;
  171. return $oAllBalanceData;
  172. }
  173. protected function getJsonBody(Request $oRequest)
  174. {
  175. $sContent = $oRequest->getContent();
  176. if ($sContent === '' || $sContent === null) {
  177. throw new BadRequestHttpException('Request body must not be empty.');
  178. }
  179. $aData = json_decode($sContent, true);
  180. if (json_last_error() !== JSON_ERROR_NONE) {
  181. throw new BadRequestHttpException('Invalid JSON payload.');
  182. }
  183. if (!is_array($aData)) {
  184. throw new BadRequestHttpException('JSON payload must be an object.');
  185. }
  186. return $aData;
  187. }
  188. }