payments = Mockery::mock(PaymentRepository::class); $this->resolver = Mockery::mock(BillingMethodResolver::class); $this->mailer = Mockery::mock(ReceiptMailer::class); $this->bookings = Mockery::mock(BookingRepository::class); $this->enrollments = Mockery::mock(EnrollmentRepository::class); $this->settings = Mockery::mock(StudioSettings::class); $this->stripe = Mockery::mock(StripeGateway::class); $this->credits = Mockery::mock(CreditRepository::class); $this->settings->shouldReceive('etransferEmail')->andReturn(''); $this->settings->shouldReceive('hstRate')->andReturn(0.0)->byDefault(); // Confirming a lesson looks it up to detect a weekly series; single // lessons (or a lookup miss) fall back to the per-lesson update. $this->bookings->shouldReceive('findById')->andReturn(null)->byDefault(); $this->service = new PaymentService( $this->payments, $this->resolver, $this->mailer, $this->bookings, $this->enrollments, $this->settings, $this->stripe, $this->credits ); Functions\when('get_userdata')->justReturn(false); } private function payment(string $method, string $status, int $id): Payment { return new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, currency: 'CAD', method: $method, status: $status, id: $id); } public function testFreeRegistrationCreatesNoPayment(): void { self::assertNull($this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 0.0, 'CAD')); } public function testVoidPendingMarksPendingPaymentFailed(): void { $this->payments->shouldReceive('findById')->with(50)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 50)); $this->payments->shouldReceive('updateStatus')->once()->with(50, Payment::STATUS_FAILED)->andReturn(true); $this->service->voidPending(50); } public function testVoidPendingLeavesScheduledPaymentAlone(): void { // A scheduled (weekly/monthly) payment can cover several lessons and may be // collected: cancelling one lesson must never void it or trigger a rebill. $scheduled = new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, dueDate: '2026-07-14', id: 60); $this->payments->shouldReceive('findById')->with(60)->andReturn($scheduled); $this->payments->shouldNotReceive('updateStatus'); $this->service->voidPending(60); } public function testVoidPendingLeavesPaidPaymentAlone(): void { // Refunds are manual: cancelling a paid lesson must not touch the ledger. $this->payments->shouldReceive('findById')->with(50)->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PAID, 50)); $this->payments->shouldNotReceive('updateStatus'); $this->service->voidPending(50); } public function testVoidPendingIgnoresNullPaymentId(): void { $this->payments->shouldNotReceive('findById'); $this->service->voidPending(null); } public function testEtransferStaysPending(): void { $this->resolver->shouldReceive('resolve')->with(5)->andReturn(Payment::METHOD_ETRANSFER); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->method === Payment::METHOD_ETRANSFER && $p->status === Payment::STATUS_PENDING)) ->andReturn(50); $this->bookings->shouldReceive('setPaymentId')->once()->with(12, 50); $this->payments->shouldReceive('findById')->with(50)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 50)); // No markPaid / confirm for a pending e-transfer. $result = $this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 35.00, 'CAD'); self::assertSame(Payment::STATUS_PENDING, $result->status); } public function testOfferingEtransferEmailIsFrozenOntoPayment(): void { $this->resolver->shouldReceive('resolve')->with(5)->andReturn(Payment::METHOD_ETRANSFER); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->etransferEmail === 'pay@studio.test')) ->andReturn(50); $this->bookings->shouldReceive('setPaymentId')->once()->with(12, 50); $this->payments->shouldReceive('findById')->with(50)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 50)); self::assertNotNull( $this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 35.00, 'CAD', 'pay@studio.test') ); } public function testHstIsComputedAndFrozenOntoPayment(): void { $this->settings->shouldReceive('hstRate')->andReturn(13.0); $this->resolver->shouldReceive('resolve')->with(5)->andReturn(Payment::METHOD_ETRANSFER); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->taxRate === 13.0 && $p->taxAmount === 13.00 && $p->total() === 113.00)) ->andReturn(50); $this->bookings->shouldReceive('setPaymentId')->once()->with(12, 50); $this->payments->shouldReceive('findById')->with(50)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 50)); self::assertNotNull($this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 100.00, 'CAD')); } public function testCompIsNotTaxed(): void { $this->settings->shouldReceive('hstRate')->andReturn(13.0); $this->resolver->shouldReceive('resolve')->with(5)->andReturn(Payment::METHOD_COMP); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->taxRate === 0.0 && $p->taxAmount === 0.0)) ->andReturn(61); $this->bookings->shouldReceive('setPaymentId')->once()->with(12, 61); $this->payments->shouldReceive('markPaid')->once()->with(61, 'USC-61')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->payments->shouldReceive('findById')->with(61)->andReturn($this->payment(Payment::METHOD_COMP, Payment::STATUS_PAID, 61)); $this->mailer->shouldReceive('send')->andReturn(false); self::assertNotNull($this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 100.00, 'CAD')); } public function testCompIsPaidAndConfirmsImmediately(): void { $this->resolver->shouldReceive('resolve')->with(5)->andReturn(Payment::METHOD_COMP); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->method === Payment::METHOD_COMP && $p->status === Payment::STATUS_PAID)) ->andReturn(60); $this->bookings->shouldReceive('setPaymentId')->once()->with(12, 60); $this->payments->shouldReceive('markPaid')->once()->with(60, 'USC-60')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->payments->shouldReceive('findById')->with(60)->andReturn($this->payment(Payment::METHOD_COMP, Payment::STATUS_PAID, 60)); $this->mailer->shouldReceive('send')->andReturn(false); $result = $this->service->createForRegistration(Payment::REG_LESSON, 12, 5, 3, 35.00, 'CAD'); self::assertSame(Payment::STATUS_PAID, $result->status); } public function testMarkPaidConfirmsAndReturnsTrue(): void { $this->payments->shouldReceive('findById')->with(70)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 70)); $this->payments->shouldReceive('markPaid')->once()->with(70, 'USC-70')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->mailer->shouldReceive('send')->andReturn(false); self::assertTrue($this->service->markPaid(70)); } public function testMarkPaidConfirmsEveryLessonInAWeeklySeries(): void { $this->payments->shouldReceive('findById')->with(70)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PENDING, 70)); $this->payments->shouldReceive('markPaid')->once()->with(70, 'USC-70')->andReturn(true); // The anchor lesson (registration_id 12) belongs to series 12: the whole // series is confirmed, not just the anchor row. $this->bookings->shouldReceive('findById')->with(12)->andReturn( new Lesson(slotId: 10, studentId: 5, instructorId: 3, recurrence: Lesson::RECURRENCE_WEEKLY, seriesId: 12, id: 12) ); $this->bookings->shouldReceive('updateStatusForSeries')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->bookings->shouldNotReceive('updateStatus'); $this->mailer->shouldReceive('send')->andReturn(false); self::assertTrue($this->service->markPaid(70)); } public function testMarkPaidReturnsFalseWhenMissing(): void { $this->payments->shouldReceive('findById')->with(99)->andReturn(null); self::assertFalse($this->service->markPaid(99)); } public function testMarkPaidIdempotentWhenAlreadyPaid(): void { $this->payments->shouldReceive('findById')->with(80)->andReturn($this->payment(Payment::METHOD_ETRANSFER, Payment::STATUS_PAID, 80)); // Already paid → no markPaid/confirm calls. self::assertTrue($this->service->markPaid(80)); } public function testCreateIntentForCardReturnsClientSecret(): void { $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 12) ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PENDING, 90)); $intent = \Stripe\PaymentIntent::constructFrom(['id' => 'pi_abc', 'client_secret' => 'pi_abc_secret']); $this->stripe->shouldReceive('createIntent')->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->id === 90)) ->andReturn($intent); $this->payments->shouldReceive('setStripeIntentId')->once()->with(90, 'pi_abc')->andReturn(true); $this->settings->shouldReceive('publishableKey')->andReturn('pk_test_123'); $result = $this->service->createIntent(Payment::REG_LESSON, 12, 5); self::assertSame('card', $result['method']); self::assertSame('pi_abc_secret', $result['client_secret']); self::assertSame('pk_test_123', $result['publishable_key']); } public function testCreateIntentForEtransferReturnsDisplayDataWithoutStripe(): void { $payment = new Payment(5, 3, Payment::REG_LESSON, 12, 35.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, etransferEmail: 'pay@studio.test', id: 91); $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 12)->andReturn($payment); $this->stripe->shouldNotReceive('createIntent'); $result = $this->service->createIntent(Payment::REG_LESSON, 12, 5); self::assertSame('etransfer', $result['method']); self::assertSame('pay@studio.test', $result['etransfer_email']); self::assertArrayNotHasKey('client_secret', $result); } public function testCreateIntentReturnsNullWhenNotOwner(): void { $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 12) ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PENDING, 90)); // Student 999 does not own payment whose studentId is 5. self::assertNull($this->service->createIntent(Payment::REG_LESSON, 12, 999)); } public function testCreateIntentReturnsNullWhenNoPayment(): void { $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 12)->andReturn(null); self::assertNull($this->service->createIntent(Payment::REG_LESSON, 12, 5)); } public function testCreateIntentReturnsNullWhenStripeFails(): void { $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 12) ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PENDING, 90)); $this->stripe->shouldReceive('createIntent')->once()->andReturn(null); self::assertNull($this->service->createIntent(Payment::REG_LESSON, 12, 5)); } public function testHandleWebhookInvalidSignatureReturnsFalse(): void { $this->stripe->shouldReceive('verifyWebhook')->with('{}', 'bad-sig')->andReturn(null); self::assertFalse($this->service->handleWebhook('{}', 'bad-sig')); } public function testHandleWebhookSucceededFinalizesPayment(): void { $event = $this->intentEvent('payment_intent.succeeded', 'pi_ok'); $this->stripe->shouldReceive('verifyWebhook')->andReturn($event); $this->payments->shouldReceive('findByStripeIntentId')->with('pi_ok') ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PENDING, 90)); $this->payments->shouldReceive('markPaid')->once()->with(90, 'USC-90')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->payments->shouldReceive('findById')->with(90)->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PAID, 90)); $this->mailer->shouldReceive('send')->andReturn(false); self::assertTrue($this->service->handleWebhook('{}', 'sig')); } public function testHandleWebhookSucceededIdempotentWhenAlreadyPaid(): void { $event = $this->intentEvent('payment_intent.succeeded', 'pi_ok'); $this->stripe->shouldReceive('verifyWebhook')->andReturn($event); $this->payments->shouldReceive('findByStripeIntentId')->with('pi_ok') ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PAID, 90)); // Already paid → no markPaid/confirm. self::assertTrue($this->service->handleWebhook('{}', 'sig')); } public function testHandleWebhookFailedMarksFailed(): void { $event = $this->intentEvent('payment_intent.payment_failed', 'pi_bad'); $this->stripe->shouldReceive('verifyWebhook')->andReturn($event); $this->payments->shouldReceive('findByStripeIntentId')->with('pi_bad') ->andReturn($this->payment(Payment::METHOD_CARD, Payment::STATUS_PENDING, 90)); $this->payments->shouldReceive('updateStatus')->once()->with(90, Payment::STATUS_FAILED)->andReturn(true); self::assertTrue($this->service->handleWebhook('{}', 'sig')); } public function testHandleWebhookAcknowledgesUnknownIntent(): void { $event = $this->intentEvent('payment_intent.succeeded', 'pi_unknown'); $this->stripe->shouldReceive('verifyWebhook')->andReturn($event); $this->payments->shouldReceive('findByStripeIntentId')->with('pi_unknown')->andReturn(null); self::assertTrue($this->service->handleWebhook('{}', 'sig')); } public function testCreditForCancelledLessonCreditsWholeTotalOfSingleLessonPayment(): void { // A paid single-lesson payment: the whole total (incl. tax) is credited. $paid = new Payment(5, 3, Payment::REG_LESSON, 77, 30.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PAID, taxRate: 10.0, taxAmount: 3.00, id: 12); $this->payments->shouldReceive('findById')->with(12)->andReturn($paid); $this->bookings->shouldReceive('countByPaymentId')->with(12)->andReturn(1); $this->credits->shouldReceive('existsForLesson')->with(77)->andReturn(false); $this->credits->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Credit $c): bool => $c->studentId === 5 && $c->amount === 33.00 && $c->remaining === 33.00 && $c->sourceLessonId === 77)) ->andReturn(300); $this->credits->shouldReceive('findById')->with(300)->andReturn( new Credit(5, 33.00, 33.00, currency: 'CAD', sourcePaymentId: 12, sourceLessonId: 77, id: 300) ); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, status: Lesson::STATUS_CANCELLED, paymentId: 12, id: 77); self::assertNotNull($this->service->creditForCancelledLesson($lesson)); } public function testCreditForCancelledLessonSplitsSharedMonthlyPayment(): void { // A monthly scheduled charge covering 3 lessons: one cancellation credits a third. $paid = new Payment(5, 3, Payment::REG_LESSON, 201, 90.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PAID, dueDate: '2026-07-01', id: 12); $this->payments->shouldReceive('findById')->with(12)->andReturn($paid); $this->bookings->shouldReceive('countByPaymentId')->with(12)->andReturn(3); $this->credits->shouldReceive('existsForLesson')->with(202)->andReturn(false); $this->credits->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Credit $c): bool => $c->amount === 30.00)) ->andReturn(301); $this->credits->shouldReceive('findById')->with(301)->andReturn(new Credit(5, 30.00, 30.00, currency: 'CAD', sourcePaymentId: 12, sourceLessonId: 202, id: 301)); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, status: Lesson::STATUS_CANCELLED, paymentId: 12, id: 202); self::assertNotNull($this->service->creditForCancelledLesson($lesson)); } public function testCreditForCancelledLessonSkipsUnpaidPayment(): void { $pending = new Payment(5, 3, Payment::REG_LESSON, 77, 30.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, id: 12); $this->payments->shouldReceive('findById')->with(12)->andReturn($pending); $this->credits->shouldNotReceive('insert'); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, paymentId: 12, id: 77); self::assertNull($this->service->creditForCancelledLesson($lesson)); } public function testCreditForCancelledLessonSkipsWhenNoPayment(): void { $this->credits->shouldNotReceive('insert'); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, id: 77); self::assertNull($this->service->creditForCancelledLesson($lesson)); } public function testCreditForCancelledLessonSkipsAlreadyCredited(): void { $paid = new Payment(5, 3, Payment::REG_LESSON, 77, 30.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PAID, id: 12); $this->payments->shouldReceive('findById')->with(12)->andReturn($paid); $this->bookings->shouldReceive('countByPaymentId')->with(12)->andReturn(1); $this->credits->shouldReceive('existsForLesson')->with(77)->andReturn(true); $this->credits->shouldNotReceive('insert'); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, paymentId: 12, id: 77); self::assertNull($this->service->creditForCancelledLesson($lesson)); } public function testCreditForCancelledLessonUsesSeriesSizeForUpfrontSeries(): void { // A non-anchor series lesson has no payment_id of its own; the anchor's // upfront (unscheduled) payment covers the whole 4-lesson series. $anchorPayment = new Payment(5, 3, Payment::REG_LESSON, 40, 120.00, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PAID, id: 12); $this->payments->shouldReceive('findByRegistration')->with(Payment::REG_LESSON, 40)->andReturn($anchorPayment); $this->payments->shouldReceive('findById')->with(12)->andReturn($anchorPayment); $this->bookings->shouldReceive('countBySeries')->with(40)->andReturn(4); $this->credits->shouldReceive('existsForLesson')->with(43)->andReturn(false); $this->credits->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Credit $c): bool => $c->amount === 30.00)) ->andReturn(302); $this->credits->shouldReceive('findById')->with(302)->andReturn(new Credit(5, 30.00, 30.00, currency: 'CAD', sourcePaymentId: 12, sourceLessonId: 43, id: 302)); $lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, recurrence: Lesson::RECURRENCE_WEEKLY, seriesId: 40, paymentId: null, id: 43); self::assertNotNull($this->service->creditForCancelledLesson($lesson)); } public function testApplyCreditsReturnsEmptyWhenNoBalance(): void { $this->credits->shouldReceive('availableBalance')->with(5)->andReturn(0.0); self::assertSame([], $this->service->applyCredits(5, [$this->pending(500, 40.00)])); } public function testApplyCreditsPartiallyCoversWithoutMarkingPaid(): void { // $30 credit against a $40 charge: applied but still owing, so it stays pending. $this->credits->shouldReceive('availableBalance')->with(5)->andReturn(30.0); $this->payments->shouldReceive('addCreditApplied')->once()->with(500, 30.0)->andReturn(true); $this->payments->shouldNotReceive('markPaid'); $this->credits->shouldReceive('consume')->once()->with(5, 30.0); $applied = $this->service->applyCredits(5, [$this->pending(500, 40.00)]); self::assertSame([500 => 30.0], $applied); } public function testApplyCreditsFullyCoversMarksPaidByCreditAndConfirms(): void { // $50 credit against a $40 charge: fully covered -> settled + registration confirmed. $this->credits->shouldReceive('availableBalance')->with(5)->andReturn(50.0); $this->payments->shouldReceive('addCreditApplied')->once()->with(500, 40.0)->andReturn(true); $this->payments->shouldReceive('markPaid')->once()->with(500, 'USC-500')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->credits->shouldReceive('consume')->once()->with(5, 40.0); $applied = $this->service->applyCredits(5, [$this->pending(500, 40.00)]); self::assertSame([500 => 40.0], $applied); } public function testApplyCreditsSpreadsAcrossChargesOldestFirst(): void { // $50 balance across two $40 charges: first fully covered, second partly. $this->credits->shouldReceive('availableBalance')->with(5)->andReturn(50.0); $this->payments->shouldReceive('addCreditApplied')->once()->with(500, 40.0)->andReturn(true); $this->payments->shouldReceive('markPaid')->once()->with(500, 'USC-500')->andReturn(true); $this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CONFIRMED)->andReturn(true); $this->payments->shouldReceive('addCreditApplied')->once()->with(501, 10.0)->andReturn(true); $this->credits->shouldReceive('consume')->once()->with(5, 50.0); $applied = $this->service->applyCredits(5, [$this->pending(500, 40.00), $this->pending(501, 40.00)]); self::assertSame([500 => 40.0, 501 => 10.0], $applied); } private function pending(int $id, float $amount): Payment { return new Payment(5, 3, Payment::REG_LESSON, 12, $amount, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, dueDate: '2026-07-14', id: $id); } private function intentEvent(string $type, string $intentId): \Stripe\Event { $intent = \Stripe\PaymentIntent::constructFrom(['id' => $intentId, 'object' => 'payment_intent']); return \Stripe\Event::constructFrom(['type' => $type, 'data' => ['object' => $intent]]); } /** * The billing method resolves against the payer, so comping or card-billing a * family is one setting on the guardian rather than one per child. */ public function testCreateForRegistrationResolvesTheBillingMethodAgainstThePayer(): void { $this->resolver->shouldReceive('resolve')->once()->with(5)->andReturn(Payment::METHOD_ETRANSFER); $this->settings->shouldReceive('etransferEmail')->andReturn(''); $this->settings->shouldReceive('hstRate')->andReturn(0.0); $this->payments->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Payment $p): bool => $p->studentId === 42 && $p->payerId === 5)) ->andReturn(90); $this->bookings->shouldReceive('setPaymentId')->once(); $this->payments->shouldReceive('findById')->with(90)->andReturn( new Payment(42, 3, Payment::REG_LESSON, 12, 35.00, payerId: 5, id: 90) ); $payment = $this->service->createForRegistration(Payment::REG_LESSON, 12, 42, 3, 35.00, 'CAD', payerId: 5); self::assertSame(5, $payment?->payerId); } /** * A credit earned by a child lands on the guardian's balance, so one child's * cancellation can settle a sibling's next charge. */ public function testCancelledChildLessonCreditsTheGuardiansBalance(): void { $lesson = new Lesson(slotId: 10, studentId: 42, instructorId: 3, paymentId: 12, id: 77); $paid = new Payment(42, 3, Payment::REG_LESSON, 77, 30.00, payerId: 5, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PAID, id: 12); $this->payments->shouldReceive('findById')->with(12)->andReturn($paid); $this->credits->shouldReceive('existsForLesson')->with(77)->andReturn(false); $this->bookings->shouldReceive('countByPaymentId')->with(12)->andReturn(1); $this->credits->shouldReceive('insert') ->once() ->with(Mockery::on(static fn (Credit $c): bool => $c->studentId === 42 && $c->payerId === 5 && $c->amount === 30.0)) ->andReturn(300); $this->credits->shouldReceive('findById')->with(300)->andReturn( new Credit(42, 30.00, 30.00, payerId: 5, id: 300) ); self::assertNotNull($this->service->creditForCancelledLesson($lesson)); } public function testApplyCreditsDrawsDownThePayersBalanceAcrossChildrensCharges(): void { $this->credits->shouldReceive('availableBalance')->with(5)->andReturn(60.0); $adasCharge = new Payment(42, 3, Payment::REG_LESSON, 12, 30.00, payerId: 5, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, dueDate: '2026-07-14', id: 91); $alansCharge = new Payment(43, 3, Payment::REG_LESSON, 13, 30.00, payerId: 5, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, dueDate: '2026-07-14', id: 92); $this->payments->shouldReceive('addCreditApplied')->once()->with(91, 30.0); $this->payments->shouldReceive('addCreditApplied')->once()->with(92, 30.0); $this->payments->shouldReceive('markPaid')->twice(); $this->bookings->shouldReceive('findById')->andReturn(null); $this->bookings->shouldReceive('updateStatus')->twice(); $this->credits->shouldReceive('consume')->once()->with(5, 60.0); $applied = $this->service->applyCredits(5, [$adasCharge, $alansCharge]); self::assertSame([91 => 30.0, 92 => 30.0], $applied); } /** * A guardian paying for their child's lesson must reach the payment step; * anyone else must not. */ public function testCreateIntentIsAllowedForBothTheStudentAndTheirPayer(): void { $payment = new Payment(42, 3, Payment::REG_LESSON, 12, 35.00, payerId: 5, currency: 'CAD', method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, id: 91); $this->payments->shouldReceive('findByRegistration')->andReturn($payment); self::assertNotNull($this->service->createIntent(Payment::REG_LESSON, 12, 42)); self::assertNotNull($this->service->createIntent(Payment::REG_LESSON, 12, 5)); self::assertNull($this->service->createIntent(Payment::REG_LESSON, 12, 99)); } }