PaymentModel.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Models;
  3. class PaymentModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  9. $this->pdo = $GLOBALS['pdo'];
  10. return;
  11. }
  12. throw new \RuntimeException('Global PDO connection not initialized');
  13. }
  14. public function create(
  15. string $paymentExternalId,
  16. int $statusId,
  17. int $userId,
  18. ?int $paymentTs = null,
  19. ?string $paymentE2e = null,
  20. ?string $paymentFlag = null
  21. ): int {
  22. $stmt = $this->pdo->prepare(
  23. 'INSERT INTO "payment" (
  24. payment_external_id,
  25. status_id,
  26. user_id,
  27. payment_ts,
  28. payment_e2e,
  29. payment_flag
  30. ) VALUES (
  31. :external_id,
  32. :status_id,
  33. :user_id,
  34. :payment_ts,
  35. :payment_e2e,
  36. :payment_flag
  37. ) RETURNING payment_id'
  38. );
  39. $stmt->execute([
  40. 'external_id' => $paymentExternalId,
  41. 'status_id' => $statusId,
  42. 'user_id' => $userId,
  43. 'payment_ts' => $paymentTs ?? 0,
  44. 'payment_e2e' => $paymentE2e ?? '',
  45. 'payment_flag' => $paymentFlag ?? '',
  46. ]);
  47. return (int)$stmt->fetchColumn();
  48. }
  49. }