PaymentModel.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Models;
  3. class PaymentModel
  4. {
  5. private \PDO $pdo;
  6. public const STATUS_PENDING = 0;
  7. public const STATUS_COMPLETED = 1;
  8. public function __construct()
  9. {
  10. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  11. $this->pdo = $GLOBALS['pdo'];
  12. return;
  13. }
  14. throw new \RuntimeException('Global PDO connection not initialized');
  15. }
  16. public function create(
  17. string $paymentExternalId,
  18. int $statusId,
  19. int $userId,
  20. ?int $paymentTs = null,
  21. ?string $paymentE2e = null,
  22. ?string $paymentFlag = null
  23. ): int {
  24. $stmt = $this->pdo->prepare(
  25. 'INSERT INTO "payment" (
  26. payment_external_id,
  27. status_id,
  28. user_id,
  29. payment_ts,
  30. payment_e2e,
  31. payment_flag
  32. ) VALUES (
  33. :external_id,
  34. :status_id,
  35. :user_id,
  36. :payment_ts,
  37. :payment_e2e,
  38. :payment_flag
  39. ) RETURNING payment_id'
  40. );
  41. $stmt->execute([
  42. 'external_id' => $paymentExternalId,
  43. 'status_id' => $statusId,
  44. 'user_id' => $userId,
  45. 'payment_ts' => $paymentTs ?? 0,
  46. 'payment_e2e' => $paymentE2e ?? '',
  47. 'payment_flag' => $paymentFlag ?? '',
  48. ]);
  49. return (int)$stmt->fetchColumn();
  50. }
  51. public function findById(int $paymentId): ?array
  52. {
  53. $stmt = $this->pdo->prepare('SELECT * FROM "payment" WHERE payment_id = :payment_id');
  54. $stmt->execute(['payment_id' => $paymentId]);
  55. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  56. return $record ?: null;
  57. }
  58. public function findByExternalId(string $paymentExternalId): ?array
  59. {
  60. $stmt = $this->pdo->prepare(
  61. 'SELECT * FROM "payment" WHERE payment_external_id = :external_id LIMIT 1'
  62. );
  63. $stmt->execute(['external_id' => $paymentExternalId]);
  64. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  65. return $record ?: null;
  66. }
  67. public function updateStatusId(int $paymentId, int $statusId): void
  68. {
  69. $stmt = $this->pdo->prepare(
  70. 'UPDATE "payment" SET status_id = :status_id WHERE payment_id = :payment_id'
  71. );
  72. $stmt->execute([
  73. 'status_id' => $statusId,
  74. 'payment_id' => $paymentId,
  75. ]);
  76. }
  77. }