A parent registers once and manages lessons for one or more children, who need no login of their own. A child is a real wp_users row with the student role but no usable login — so student_id keeps meaning "a WordPress user" on every table, and booking, credits, policies and enrolments work unchanged. A us_guardians link table maps guardian to child. The signup form gains a parent/guardian tick that reveals a block per child, with the account-signup questions asked per child rather than per guardian — they describe the student, not the account holder. Signup policies are recorded once per child with the guardian as the acceptor, which is the record that actually means something. A family that half-creates is rolled back entirely rather than leaving a guardian who cannot re-register. The booking and enrolment forms gain a "Who is this for?" picker listing children first, so the default selection is never the parent — booking for the wrong child is correctable, quietly billing a parent for their kid's lesson is not. POST /bookings and POST /enrollments take an optional student_id honoured only for that child's guardian; anything else is a 403. That check is the authorisation boundary of the feature. Payments and credits gain a payer: the charge names the child it was for and the guardian who owes it, so per-child reporting is unchanged while notices, receipts and the payment step reach the parent. Credit is held by the payer, so one child's cancellation can settle a sibling's charge, and the daily billing scan sends a guardian one notice covering every child. Closes #132 Co-Authored-By: Claude Opus 5 <[email protected]>
112 lines
3.8 KiB
PHP
112 lines
3.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Payment;
|
|
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Payment\Payment;
|
|
use Unsupervised\Schedular\Payment\StripeGateway;
|
|
use Unsupervised\Schedular\Payment\StudioSettings;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class StripeGatewayTest extends TestCase
|
|
{
|
|
private StudioSettings $settings;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->settings = Mockery::mock(StudioSettings::class);
|
|
}
|
|
|
|
private function partialGateway(): StripeGateway
|
|
{
|
|
return Mockery::mock(StripeGateway::class, [$this->settings])
|
|
->makePartial()
|
|
->shouldAllowMockingProtectedMethods();
|
|
}
|
|
|
|
private function payment(): Payment
|
|
{
|
|
return new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, currency: 'CAD', method: Payment::METHOD_CARD, status: Payment::STATUS_PENDING, id: 90);
|
|
}
|
|
|
|
public function testCreateIntentReturnsNullWhenNotConfigured(): void
|
|
{
|
|
$this->settings->shouldReceive('isStripeConfigured')->andReturn(false);
|
|
|
|
$gateway = new StripeGateway($this->settings);
|
|
|
|
self::assertNull($gateway->createIntent($this->payment()));
|
|
}
|
|
|
|
public function testCreateIntentSendsAmountInCentsAndReturnsIntent(): void
|
|
{
|
|
$this->settings->shouldReceive('isStripeConfigured')->andReturn(true);
|
|
|
|
$intent = \Stripe\PaymentIntent::constructFrom(['id' => 'pi_1', 'client_secret' => 'cs_1']);
|
|
$gateway = $this->partialGateway();
|
|
$gateway->shouldReceive('paymentIntentsCreate')
|
|
->once()
|
|
->with(
|
|
Mockery::on(static fn (array $p): bool => $p['amount'] === 3500
|
|
&& $p['currency'] === 'cad'
|
|
&& $p['metadata']['payment_id'] === '90'),
|
|
Mockery::on(static fn (array $o): bool => $o['idempotency_key'] === 'usc-payment-90')
|
|
)
|
|
->andReturn($intent);
|
|
|
|
self::assertSame('pi_1', $gateway->createIntent($this->payment())->id);
|
|
}
|
|
|
|
public function testCreateIntentReturnsNullOnStripeError(): void
|
|
{
|
|
$this->settings->shouldReceive('isStripeConfigured')->andReturn(true);
|
|
|
|
$gateway = $this->partialGateway();
|
|
$gateway->shouldReceive('paymentIntentsCreate')->once()->andThrow(new \RuntimeException('declined'));
|
|
|
|
self::assertNull($gateway->createIntent($this->payment()));
|
|
}
|
|
|
|
public function testVerifyWebhookReturnsNullWithoutSecret(): void
|
|
{
|
|
$this->settings->shouldReceive('webhookSecret')->andReturn('');
|
|
|
|
$gateway = new StripeGateway($this->settings);
|
|
|
|
self::assertNull($gateway->verifyWebhook('{}', 'sig'));
|
|
}
|
|
|
|
public function testVerifyWebhookReturnsNullWithoutSignatureHeader(): void
|
|
{
|
|
$this->settings->shouldReceive('webhookSecret')->andReturn('whsec_123');
|
|
|
|
$gateway = new StripeGateway($this->settings);
|
|
|
|
self::assertNull($gateway->verifyWebhook('{}', ''));
|
|
}
|
|
|
|
public function testVerifyWebhookReturnsNullOnInvalidSignature(): void
|
|
{
|
|
$this->settings->shouldReceive('webhookSecret')->andReturn('whsec_123');
|
|
|
|
$gateway = $this->partialGateway();
|
|
$gateway->shouldReceive('constructEvent')->once()->andThrow(new \RuntimeException('bad signature'));
|
|
|
|
self::assertNull($gateway->verifyWebhook('{}', 'sig'));
|
|
}
|
|
|
|
public function testVerifyWebhookReturnsEventOnSuccess(): void
|
|
{
|
|
$this->settings->shouldReceive('webhookSecret')->andReturn('whsec_123');
|
|
|
|
$event = \Stripe\Event::constructFrom(['type' => 'payment_intent.succeeded']);
|
|
$gateway = $this->partialGateway();
|
|
$gateway->shouldReceive('constructEvent')->once()->with('{payload}', 'sig', 'whsec_123')->andReturn($event);
|
|
|
|
self::assertSame('payment_intent.succeeded', $gateway->verifyWebhook('{payload}', 'sig')->type);
|
|
}
|
|
}
|