Files
unsupervised-scheduler/src/Payment/PaymentRepository.php
T
thatguygriffandClaude Fable 5 c49171695a
CI / Coding Standards (pull_request) Successful in 2m47s
CI / PHPStan (pull_request) Successful in 2m56s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m39s
CI / Build Plugin Zip (pull_request) Skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 43s
CI / Tests (PHP 8.1) (pull_request) Successful in 44s
CI / No Debug Code (pull_request) Successful in 2s
Add policy, intake, and payment history to the admin student detail view
The student-administration spec deferred three detail-view sections until
Payments landed. Adds them now: policy-acceptance history (title, version,
context, date), intake answers (label, answer, context), and — gated on
manage_billing — payment history with HST breakdown and receipt numbers.

New Auth\StudentHistory builds the display rows from per-student queries
added to AcceptanceRepository, AnswerRepository, and PaymentRepository;
the Payment model now carries created_at so unpaid rows still have a date.

Closes #69

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-18 18:17:20 -03:00

210 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Payment;
class PaymentRepository {
private string $table;
public function __construct( private \wpdb $db ) {
$this->table = $db->prefix . 'us_payments';
}
public function insert( Payment $payment ): int {
$this->db->insert(
$this->table,
[
'student_id' => $payment->studentId,
'instructor_id' => $payment->instructorId,
'registration_type' => $payment->registrationType,
'registration_id' => $payment->registrationId,
'amount' => $payment->amount,
'currency' => $payment->currency,
'method' => $payment->method,
'status' => $payment->status,
'tax_rate' => $payment->taxRate,
'tax_amount' => $payment->taxAmount,
'etransfer_email' => $payment->etransferEmail,
'stripe_payment_intent_id' => $payment->stripePaymentIntentId,
'receipt_number' => $payment->receiptNumber,
'receipt_sent_at' => $payment->receiptSentAt,
'paid_at' => $payment->paidAt,
'created_at' => current_time( 'mysql' ),
],
[ '%d', '%d', '%s', '%d', '%f', '%s', '%s', '%s', '%f', '%f', '%s', '%s', '%s', '%s', '%s', '%s' ]
);
return $this->db->insert_id;
}
/**
* Attach the Stripe PaymentIntent id created for a card payment so the webhook
* can later reconcile the charge back to this row.
*/
public function setStripeIntentId( int $id, string $intentId ): bool {
return false !== $this->db->update(
$this->table,
[ 'stripe_payment_intent_id' => $intentId ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
public function findByStripeIntentId( string $intentId ): ?Payment {
$row = $this->db->get_row(
$this->db->prepare(
'SELECT * FROM %i WHERE stripe_payment_intent_id = %s ORDER BY id DESC LIMIT 1',
$this->table,
$intentId
)
);
return $row ? Payment::fromRow( $row ) : null;
}
public function updateEtransferEmail( int $id, ?string $email ): bool {
return false !== $this->db->update(
$this->table,
[ 'etransfer_email' => $email ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
/**
* Set a payment's tax rate and recompute the tax amount from its subtotal.
*/
public function updateTax( int $id, float $rate ): bool {
$sql = $this->db->prepare(
'UPDATE %i SET tax_rate = %f, tax_amount = ROUND( amount * %f / 100, 2 ) WHERE id = %d',
$this->table,
$rate,
$rate,
$id
);
return null !== $sql && false !== $this->db->query( $sql );
}
/**
* Paid payments in a month (`Y-m` bounds), optionally for one instructor —
* the reporting source.
*
* @return list<Payment>
*/
public function findPaidBetween( string $from, string $to, int $instructorId = 0 ): array {
$sql = 'SELECT * FROM %i WHERE status = %s AND paid_at >= %s AND paid_at < %s';
$params = [ $this->table, Payment::STATUS_PAID, $from, $to ];
if ( $instructorId > 0 ) {
$sql .= ' AND instructor_id = %d';
$params[] = $instructorId;
}
$sql .= ' ORDER BY paid_at ASC';
$rows = $this->db->get_results( $this->db->prepare( $sql, $params ) );
return array_map( Payment::fromRow( ... ), $rows ?? [] );
}
public function findById( int $id ): ?Payment {
$row = $this->db->get_row(
$this->db->prepare( 'SELECT * FROM %i WHERE id = %d', $this->table, $id )
);
return $row ? Payment::fromRow( $row ) : null;
}
public function findByRegistration( string $registrationType, int $registrationId ): ?Payment {
$row = $this->db->get_row(
$this->db->prepare(
'SELECT * FROM %i WHERE registration_type = %s AND registration_id = %d ORDER BY id DESC LIMIT 1',
$this->table,
$registrationType,
$registrationId
)
);
return $row ? Payment::fromRow( $row ) : null;
}
/**
* Every payment for a student, newest first (admin payment history).
*
* @return list<Payment>
*/
public function findByStudent( int $studentId ): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE student_id = %d ORDER BY created_at DESC, id DESC',
$this->table,
$studentId
)
);
return array_map( Payment::fromRow( ... ), $rows ?? [] );
}
/**
* Pending payments, newest first (studio-admin confirmation queue).
*
* @return list<Payment>
*/
public function findPending(): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE status = %s ORDER BY created_at DESC',
$this->table,
Payment::STATUS_PENDING
)
);
return array_map( Payment::fromRow( ... ), $rows ?? [] );
}
/**
* Mark a payment paid, stamping the paid time and receipt number.
*/
public function markPaid( int $id, string $receiptNumber ): bool {
return false !== $this->db->update(
$this->table,
[
'status' => Payment::STATUS_PAID,
'paid_at' => current_time( 'mysql' ),
'receipt_number' => $receiptNumber,
],
[ 'id' => $id ],
[ '%s', '%s', '%s' ],
[ '%d' ]
);
}
public function markReceiptSent( int $id ): bool {
return false !== $this->db->update(
$this->table,
[ 'receipt_sent_at' => current_time( 'mysql' ) ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
public function updateStatus( int $id, string $status ): bool {
if ( ! in_array( $status, Payment::VALID_STATUSES, true ) ) {
return false;
}
return (bool) $this->db->update(
$this->table,
[ 'status' => $status ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
}