Phase 6 of the tenant series. GlobalTables::DEFERRED is now empty and the
coverage test asserts it stays that way.
payments carries the (entity_type, entity_id) pair and belongs to the
receiving side, never the payer: an appointment payment takes the
appointment's environment, a subscription takes the environment its buyer
owns, and an SMS wallet top-up takes the wallet's. The patient never chose
an environment, so TenantFilter stays off for them and they still see their
own payment.
Three corrections to the analysis the phase was planned on, each backed by
the code or the data rather than the plan:
- A third payment type exists. Payment::TYPE_SMS_WALLET is created in
SmsWalletController and already carries its environment in the metadata;
without assigning it the write would fail at flush.
- clinic_subscriptions has no user_id, and its trial rows carry no payment,
so it cannot drive the subscription backfill. The environment is derived
the way handleSubscriptionActivation derives it — and that method now
reads the pair off the payment instead of re-deriving it, so a payment and
the subscription it buys can no longer land on different environments.
- WalletTransaction is not a child of Payment. payment_id is nullable and
none of the four creation sites set it; the wallet is a person's, with a
running balance per user. It and Settlement, which withdraws from that same
wallet, are global with a recorded reason instead.
bank_accounts and pos_devices move from the registering user to the
environment. Their pair is deliberately nullable: nothing in the existing
data says which of a multi-environment owner's cards belongs where, and
guessing would point real money at the wrong account. Ambiguous rows stay
unassigned and the migration reports how many. The cost is that such a row
is invisible in every environment, so the owner reaches it through a
user-scoped lookup that runs outside the filter, and assigns it with
PATCH .../{uuid}/environment. The admin panel marks those rows and offers
the assignment.
Tests: 896 backend (+11), 570 frontend (+4). PHPStan unchanged at its 17
pre-existing errors.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Payment;
|
|
|
|
use App\Config\Entity\SiteConfig;
|
|
use App\Payment\Entity\Payment;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* The payment callback must confirm an order only when the gateway-reported
|
|
* settled amount matches what we charged. Guards against underpayment / a
|
|
* replayed RefNum from another (cheaper) order marking an expensive order paid.
|
|
*/
|
|
class PaymentCallbackAmountTest extends ApiTestCase
|
|
{
|
|
private function enableTestMode(): void
|
|
{
|
|
$cfg = $this->em->getRepository(SiteConfig::class)->findOneBy(['configKey' => 'payment_test_mode']);
|
|
if ($cfg === null) {
|
|
$cfg = new SiteConfig('payment_test_mode', '1');
|
|
$this->em->persist($cfg);
|
|
} else {
|
|
$cfg->setValue('1');
|
|
}
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function makePayment(int $amountRials): Payment
|
|
{
|
|
$user = $this->createUser();
|
|
$payment = $this->stampTenant(new Payment($user, $amountRials, 'mock', Payment::TYPE_SMS_WALLET));
|
|
$this->em->persist($payment);
|
|
$this->em->flush();
|
|
|
|
return $payment;
|
|
}
|
|
|
|
private function fireCallback(Payment $payment, int $reportedAmount): void
|
|
{
|
|
$this->client->request('POST', '/api/v1/payment/callback/mock?' . http_build_query([
|
|
'order_id' => $payment->getOrderId(),
|
|
'mock' => '1',
|
|
'ResCode' => '0',
|
|
'mock_amount' => (string) $reportedAmount,
|
|
]));
|
|
}
|
|
|
|
private function reload(Payment $payment): Payment
|
|
{
|
|
$this->em->clear();
|
|
|
|
return $this->em->getRepository(Payment::class)->find($payment->getId());
|
|
}
|
|
|
|
public function testUnderpaymentIsRejected(): void
|
|
{
|
|
$this->enableTestMode();
|
|
$payment = $this->makePayment(50000);
|
|
|
|
$this->fireCallback($payment, 10000);
|
|
|
|
$this->assertSame(Payment::STATUS_FAILED, $this->reload($payment)->getStatus());
|
|
}
|
|
|
|
public function testMatchingAmountSucceeds(): void
|
|
{
|
|
$this->enableTestMode();
|
|
$payment = $this->makePayment(50000);
|
|
|
|
$this->fireCallback($payment, 50000);
|
|
|
|
$this->assertSame(Payment::STATUS_SUCCESS, $this->reload($payment)->getStatus());
|
|
}
|
|
|
|
public function testReplayedGatewayReferenceIsRejected(): void
|
|
{
|
|
$this->enableTestMode();
|
|
|
|
// Unique per run — db_test is shared and not reset between runs.
|
|
$ref = 'REF-' . bin2hex(random_bytes(8));
|
|
|
|
$first = $this->makePayment(50000);
|
|
$this->fireCallbackWithRef($first, $ref, 50000);
|
|
$this->assertSame(Payment::STATUS_SUCCESS, $this->reload($first)->getStatus());
|
|
|
|
// Same gateway reference replayed onto a different (same-amount) order.
|
|
$second = $this->makePayment(50000);
|
|
$this->fireCallbackWithRef($second, $ref, 50000);
|
|
$this->assertSame(Payment::STATUS_FAILED, $this->reload($second)->getStatus());
|
|
}
|
|
|
|
private function fireCallbackWithRef(Payment $payment, string $refId, int $reportedAmount): void
|
|
{
|
|
$this->client->request('POST', '/api/v1/payment/callback/mock?' . http_build_query([
|
|
'order_id' => $payment->getOrderId(),
|
|
'mock' => '1',
|
|
'ResCode' => '0',
|
|
'RefId' => $refId,
|
|
'mock_amount' => (string) $reportedAmount,
|
|
]));
|
|
}
|
|
}
|