Files
unsupervised-scheduler/tests/Unit/Payment/PaymentTest.php
T
thatguygriffandClaude Opus 4.8 e8e66eef3c
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / Tests (PHP 8.2) (pull_request) Successful in 47s
CI / PHPStan (pull_request) Successful in 3m12s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m52s
Credit students for cancelled paid lessons
Cancelling a lesson that was already paid for now credits the student
that money instead of leaving it as a manual refund, and the daily
scheduled-billing scan applies any available credit against their due
charges before emailing the notice.

- New us_credits ledger + us_payments.credit_applied column (Payment::netDue).
- PaymentService::creditForCancelledLesson issues a per-lesson share of the
  covering payment's total; wired into all three cancel paths (student
  self-cancel, instructor status update, admin student-detail cancel).
- PaymentService::applyCredits draws credit down FIFO across a run's charges,
  marking a fully-covered charge paid-by-credit; the notice shows the credit
  applied and reduced total, and the admin queue shows net due.
- Student detail page shows a student's credit balance and history.

Ships as part of the unreleased 1.2.0 (same release as scheduled billing).

Tests: composer test (585), composer lint, composer cs all pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-24 15:32:20 -03:00

114 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Payment;
use Unsupervised\Schedular\Payment\Payment;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class PaymentTest extends TestCase
{
public function testDefaults(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 35.00);
self::assertSame('CAD', $payment->currency);
self::assertSame(Payment::METHOD_ETRANSFER, $payment->method);
self::assertSame(Payment::STATUS_PENDING, $payment->status);
self::assertFalse($payment->isPaid());
self::assertNull($payment->id);
}
public function testConstants(): void
{
self::assertContains(Payment::METHOD_CARD, Payment::VALID_METHODS);
self::assertContains(Payment::METHOD_ETRANSFER, Payment::VALID_METHODS);
self::assertContains(Payment::METHOD_COMP, Payment::VALID_METHODS);
self::assertContains(Payment::STATUS_PAID, Payment::VALID_STATUSES);
}
public function testFromRowMapsCorrectly(): void
{
$payment = Payment::fromRow((object) [
'id' => '7',
'student_id' => '5',
'instructor_id' => '3',
'registration_type' => Payment::REG_ENROLLMENT,
'registration_id' => '12',
'amount' => '120.00',
'currency' => 'CAD',
'method' => Payment::METHOD_COMP,
'status' => Payment::STATUS_PAID,
'tax_rate' => '13.00',
'tax_amount' => '15.60',
'etransfer_email' => null,
'stripe_payment_intent_id' => null,
'receipt_number' => 'USC-7',
'receipt_sent_at' => null,
'paid_at' => '2026-06-08 10:00:00',
'created_at' => '2026-06-08 09:00:00',
]);
self::assertSame(7, $payment->id);
self::assertSame(120.00, $payment->amount);
self::assertSame(13.00, $payment->taxRate);
self::assertSame(15.60, $payment->taxAmount);
self::assertSame(Payment::METHOD_COMP, $payment->method);
self::assertTrue($payment->isPaid());
self::assertSame('USC-7', $payment->receiptNumber);
self::assertSame('2026-06-08 09:00:00', $payment->createdAt);
}
public function testTotalAddsTaxToAmount(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 100.00, taxRate: 13.0, taxAmount: 13.00);
self::assertSame(113.00, $payment->total());
}
public function testTotalEqualsAmountWhenUntaxed(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 100.00);
self::assertSame(100.00, $payment->total());
}
public function testNetDueSubtractsAppliedCredit(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 100.00, taxRate: 13.0, taxAmount: 13.00, creditApplied: 40.00);
self::assertSame(113.00, $payment->total());
self::assertSame(73.00, $payment->netDue());
}
public function testNetDueFloorsAtZeroWhenCreditExceedsTotal(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 30.00, creditApplied: 50.00);
self::assertSame(0.0, $payment->netDue());
}
public function testNetDueEqualsTotalWithoutCredit(): void
{
$payment = new Payment(5, 3, Payment::REG_LESSON, 12, 30.00);
self::assertSame(30.00, $payment->netDue());
}
public function testToSummaryArrayContainsOnlyClientFacingFields(): void
{
$summary = (new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, id: 7))->toSummaryArray();
self::assertSame(['id' => 7, 'method' => Payment::METHOD_ETRANSFER, 'status' => Payment::STATUS_PENDING], $summary);
}
public function testToArrayContainsExpectedKeys(): void
{
$arr = (new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, id: 7))->toArray();
foreach (['id', 'student_id', 'amount', 'tax_rate', 'tax_amount', 'total', 'currency', 'method', 'status', 'receipt_number'] as $key) {
self::assertArrayHasKey($key, $arr);
}
}
}