PaymentConfirmController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CommodityModel;
  5. use Models\CprModel;
  6. use Models\PaymentModel;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Services\B3CprService;
  9. use Services\TokenCreateService;
  10. class PaymentConfirmController
  11. {
  12. private PaymentModel $paymentModel;
  13. private CprModel $cprModel;
  14. private B3CprService $b3Service;
  15. private CommodityModel $commodityModel;
  16. private TokenCreateService $tokenCreateService;
  17. private \PDO $pdo;
  18. public function __construct()
  19. {
  20. if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
  21. throw new \RuntimeException('Global PDO connection not initialized');
  22. }
  23. $this->pdo = $GLOBALS['pdo'];
  24. $this->paymentModel = new PaymentModel();
  25. $this->cprModel = new CprModel();
  26. $this->b3Service = new B3CprService();
  27. $this->commodityModel = new CommodityModel();
  28. $this->tokenCreateService = new TokenCreateService();
  29. }
  30. public function __invoke(ServerRequestInterface $request)
  31. {
  32. $body = json_decode((string)$request->getBody(), true) ?? [];
  33. $paymentId = isset($body['payment_id']) ? (int)$body['payment_id'] : 0;
  34. if ($paymentId <= 0) {
  35. return ResponseLib::sendFail('payment_id inválido', [], 'E_VALIDATE')->withStatus(400);
  36. }
  37. $payment = $this->paymentModel->findById($paymentId);
  38. if (!$payment) {
  39. return ResponseLib::sendFail('Pagamento não encontrado', [], 'E_NOT_FOUND')->withStatus(404);
  40. }
  41. $statusId = (int)($payment['status_id'] ?? 0);
  42. if ($statusId === 0) {
  43. return ResponseLib::sendFail('Pagamento ainda não confirmado', ['payment_id' => $paymentId], 'E_PAYMENT_PENDING')->withStatus(409);
  44. }
  45. if ($statusId !== 1) {
  46. return ResponseLib::sendFail('Pagamento em status inválido', ['status_id' => $statusId], 'E_PAYMENT_STATUS')->withStatus(409);
  47. }
  48. $cpr = $this->cprModel->findByPaymentId($paymentId);
  49. if (!$cpr) {
  50. return ResponseLib::sendFail('Nenhuma CPR vinculada ao pagamento', [], 'E_CPR_NOT_FOUND')->withStatus(404);
  51. }
  52. try {
  53. $payload = $this->b3Service->mapToB3($cpr);
  54. } catch (\Throwable $e) {
  55. return ResponseLib::sendFail('Falha ao montar payload para a B3: ' . $e->getMessage(), [], 'E_B3_MAP')->withStatus(500);
  56. }
  57. try {
  58. $token = $this->resolveB3Token($request, $body);
  59. } catch (\Throwable $e) {
  60. return ResponseLib::sendFail('Falha ao obter token de acesso da B3: ' . $e->getMessage(), [], 'E_B3_TOKEN')->withStatus(502);
  61. }
  62. try {
  63. $result = $this->b3Service->postCpr($token, $payload);
  64. } catch (\Throwable $e) {
  65. return ResponseLib::sendFail('Falha ao enviar CPR à B3: ' . $e->getMessage(), [], 'E_EXTERNAL')->withStatus(502);
  66. }
  67. if (isset($result['error'])) {
  68. return ResponseLib::sendFail('cURL error during B3 CPR request', ['error' => $result['error']], 'E_EXTERNAL')->withStatus(502);
  69. }
  70. try {
  71. $tokenResult = $this->createTokenFromCpr($cpr);
  72. } catch (\Throwable $e) {
  73. return ResponseLib::sendFail(
  74. 'Falha ao gerar token: ' . $e->getMessage(),
  75. [],
  76. 'E_TOKEN_CREATE'
  77. )->withStatus(500);
  78. }
  79. try {
  80. $this->cprModel->updateTokenId((int)$cpr['cpr_id'], (int)$tokenResult['token_id']);
  81. } catch (\Throwable $e) {
  82. return ResponseLib::sendFail(
  83. 'Falha ao vincular token à CPR: ' . $e->getMessage(),
  84. [],
  85. 'E_CPR_UPDATE'
  86. )->withStatus(500);
  87. }
  88. return ResponseLib::sendOk([
  89. 'message' => 'CPR enviada e token criado com sucesso',
  90. 'payment_id' => $paymentId,
  91. 'b3_response' => $result['json'] ?? ($result['raw'] ?? null),
  92. 'token_id' => $tokenResult['token_id'],
  93. 'token_external_id' => $tokenResult['token_external_id'],
  94. 'tx_hash' => $tokenResult['tx_hash'],
  95. ], 'S_CPR_SENT');
  96. }
  97. private function resolveB3Token(ServerRequestInterface $request, array $body): string
  98. {
  99. $token = $body['b3_access_token'] ?? ($body['access_token'] ?? null);
  100. if (!$token) {
  101. $b3Auth = $request->getHeaderLine('X-B3-Authorization') ?: '';
  102. if (stripos($b3Auth, 'Bearer ') === 0) {
  103. $token = trim(substr($b3Auth, 7));
  104. }
  105. }
  106. if (!$token) {
  107. $token = $request->getHeaderLine('X-B3-Access-Token') ?: null;
  108. }
  109. if (!$token) {
  110. $token = $this->b3Service->getAccessToken();
  111. }
  112. return $token;
  113. }
  114. private function createTokenFromCpr(array $cpr): array
  115. {
  116. $inputs = $this->prepareTokenInputs($cpr);
  117. return $this->tokenCreateService->createToken(
  118. $inputs['token_commodities_amount'],
  119. $inputs['token_commodities_value'],
  120. $inputs['token_uf'],
  121. $inputs['token_city'],
  122. $inputs['token_content'],
  123. $inputs['token_flag'],
  124. $inputs['wallet_id'],
  125. $inputs['chain_id'],
  126. $inputs['commodities_id'],
  127. $inputs['cpr_id'],
  128. $inputs['user_id']
  129. );
  130. }
  131. /**
  132. * @return array{
  133. * token_commodities_amount:int,
  134. * token_commodities_value:int,
  135. * token_uf:string,
  136. * token_city:string,
  137. * token_content:string,
  138. * token_flag:string,
  139. * wallet_id:int,
  140. * chain_id:int,
  141. * commodities_id:int,
  142. * cpr_id:int,
  143. * user_id:int
  144. * }
  145. */
  146. private function prepareTokenInputs(array $cpr): array
  147. {
  148. $cprId = (int)($cpr['cpr_id'] ?? 0);
  149. if ($cprId <= 0) {
  150. throw new \InvalidArgumentException('CPR sem identificador válido.');
  151. }
  152. $userId = (int)($cpr['user_id'] ?? 0);
  153. if ($userId <= 0) {
  154. throw new \InvalidArgumentException('CPR sem usuário associado.');
  155. }
  156. $companyId = 1;
  157. $wallet = $this->findWalletByCompanyId($companyId);
  158. $commoditiesName = $this->requireStringField($cpr, ['cpr_product_name'], 'cpr_product_name');
  159. $commoditiesId = $this->resolveCommodityId($commoditiesName);
  160. $tokenCommoditiesAmount = $this->requireNumericField(
  161. $cpr,
  162. ['cpr_product_quantity', 'cpr_issue_quantity'],
  163. 'quantidade do produto'
  164. );
  165. $tokenCommoditiesValue = $this->requireNumericField(
  166. $cpr,
  167. ['cpr_issue_value', 'cpr_issue_financial_value'],
  168. 'valor do produto'
  169. );
  170. $tokenUf = $this->requireStringField(
  171. $cpr,
  172. ['cpr_deliveryPlace_state_acronym', 'cpr_issuers_state_acronym'],
  173. 'UF'
  174. );
  175. $tokenCity = $this->requireStringField(
  176. $cpr,
  177. ['cpr_deliveryPlace_city_name', 'cpr_issuers_city_name'],
  178. 'cidade'
  179. );
  180. return [
  181. 'token_commodities_amount' => $tokenCommoditiesAmount,
  182. 'token_commodities_value' => $tokenCommoditiesValue,
  183. 'token_uf' => $tokenUf,
  184. 'token_city' => $tokenCity,
  185. 'token_content' => (string)$cprId,
  186. 'token_flag' => '',
  187. 'wallet_id' => $wallet['wallet_id'],
  188. 'chain_id' => $wallet['chain_id'],
  189. 'commodities_id' => $commoditiesId,
  190. 'cpr_id' => $cprId,
  191. 'user_id' => $userId,
  192. ];
  193. }
  194. private function findWalletByCompanyId(int $companyId): array
  195. {
  196. $stmt = $this->pdo->prepare(
  197. 'SELECT wallet_id, chain_id
  198. FROM "wallet"
  199. WHERE company_id = :company_id
  200. ORDER BY wallet_id ASC
  201. LIMIT 1'
  202. );
  203. $stmt->execute(['company_id' => $companyId]);
  204. $wallet = $stmt->fetch(\PDO::FETCH_ASSOC);
  205. if (!$wallet) {
  206. throw new \RuntimeException('Nenhuma carteira encontrada para a empresa informada.');
  207. }
  208. return [
  209. 'wallet_id' => (int)$wallet['wallet_id'],
  210. 'chain_id' => (int)$wallet['chain_id'],
  211. ];
  212. }
  213. private function resolveCommodityId(string $name): int
  214. {
  215. $commodityId = $this->commodityModel->getIdByName($name);
  216. if ($commodityId === null) {
  217. throw new \RuntimeException('Commodity não encontrada para o produto: ' . $name);
  218. }
  219. return $commodityId;
  220. }
  221. private function requireStringField(array $cpr, array $candidates, string $label): string
  222. {
  223. foreach ($candidates as $field) {
  224. if (!array_key_exists($field, $cpr)) {
  225. continue;
  226. }
  227. $value = $this->normalizeStringValue($cpr[$field]);
  228. if ($value !== '') {
  229. return $value;
  230. }
  231. }
  232. throw new \InvalidArgumentException("Campo {$label} ausente ou inválido na CPR.");
  233. }
  234. private function requireNumericField(array $cpr, array $candidates, string $label): int
  235. {
  236. foreach ($candidates as $field) {
  237. if (!array_key_exists($field, $cpr)) {
  238. continue;
  239. }
  240. $value = $this->normalizeNumericValue($cpr[$field]);
  241. if ($value !== null) {
  242. return $value;
  243. }
  244. }
  245. throw new \InvalidArgumentException("Campo {$label} ausente ou inválido na CPR.");
  246. }
  247. private function normalizeStringValue($value): string
  248. {
  249. if (is_array($value)) {
  250. $value = reset($value);
  251. }
  252. if (!is_scalar($value)) {
  253. return '';
  254. }
  255. $stringValue = trim((string)$value);
  256. if ($stringValue === '') {
  257. return '';
  258. }
  259. $parts = preg_split('/\s*;\s*/', $stringValue) ?: [];
  260. $first = $parts[0] ?? $stringValue;
  261. return trim((string)$first);
  262. }
  263. private function normalizeNumericValue($value): ?int
  264. {
  265. if (is_array($value)) {
  266. $value = reset($value);
  267. }
  268. if (is_string($value)) {
  269. $value = str_replace([' ', ','], ['', '.'], $value);
  270. }
  271. if (is_numeric($value)) {
  272. return (int)round((float)$value);
  273. }
  274. return null;
  275. }
  276. }