Files
Kydoimosandanthropic/claude-opus-4-8 e389e40843
CI / Coding Standards (pull_request) Successful in 28s
CI / Tests (PHP 8.1) (pull_request) Successful in 35s
CI / No Debug Code (pull_request) Successful in 9s
CI / Tests (PHP 8.3) (pull_request) Successful in 44s
CI / Tests (PHP 8.2) (pull_request) Successful in 47s
CI / Tests (PHP 8.5) (pull_request) Successful in 48s
CI / Static Analysis (pull_request) Successful in 52s
CI / Build Plugin Zip (pull_request) Skipped
Send each scheduled payment's due notice exactly once
The daily billing scan runs on request via WP-Cron and can overlap
itself under concurrent traffic. Each run emailed the payments it
created with no record that a notice had gone out, so two overlapping
runs could send a payer two identical "Payment due" emails for one
charge — read by families as being billed twice, though only one row
exists.

Stamp us_payments.notice_sent_at atomically before emailing: the scan
now claims each payment with a conditional UPDATE ... WHERE
notice_sent_at IS NULL and only notices, credits and batches the rows
it won. A competing run finds them claimed and stays quiet, so exactly
one notice is sent regardless of how the scan is triggered. A one-time
backfill stamps existing scheduled rows on upgrade so already-noticed
charges are not re-emailed.

Co-authored-by: anthropic/claude-opus-4-8
2026-09-17 13:19:25 -03:00

116 lines
4.3 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,
'notice_sent_at' => '2026-06-07 08:00:00',
'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-07 08:00:00', $payment->noticeSentAt);
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);
}
}
}