You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/PaymentCheckoutQuoteTest.php

62 lines
3.2 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
require __DIR__ . '/Support/PaymentCheckoutFixture.php';
use App\Services\Payment\PaymentCheckoutQuotes;
use App\Services\Payment\PaymentRateService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
function checkCheckoutQuote(bool $condition, string $message): void
{
if (!$condition) {
throw new RuntimeException($message);
}
}
$db = new Capsule();
$db->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$db->setAsGlobal();
$db->bootEloquent();
$connection = $db->getConnection();
$schema = $connection->getSchemaBuilder();
$schema->create('configurations', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->unique();
$table->text('value');
});
createPaymentCheckoutFixture($schema);
$now = 1_770_000_000;
PaymentRateService::override('USD/CNY', '7.20', $now + 3600, 7, 'First acceptance quote', $now);
$draft = PaymentCheckoutQuotes::prepare('paytaro', '72.00', $now, $now + 900);
$connection->transaction(fn () => PaymentCheckoutQuotes::persist($draft, 'PT-QUOTE-A', 21));
PaymentRateService::override('USD/CNY', '7.30', $now + 3600, 7, 'Second acceptance quote', $now + 1);
$later = PaymentCheckoutQuotes::prepare('yipay_wxpay', '72.00', $now + 1, $now + 900);
$connection->transaction(fn () => PaymentCheckoutQuotes::persist($later, 'YP-QUOTE-B', 21));
$old = PaymentCheckoutQuotes::find('paytaro', 'PT-QUOTE-A');
checkCheckoutQuote($old->expected_units === 51840 && $later->expected_units === 52560,
'Changing the rate affected an existing order or failed to price the next one.');
checkCheckoutQuote(PaymentCheckoutQuotes::find('yipay_wxpay', 'PT-QUOTE-A') === null,
'A quote was read using a different provider.');
$receipt = static fn (string $provider, string $order, int $units, int $observed, ?int $paidAt) =>
$connection->transaction(fn () => PaymentCheckoutQuotes::receive($provider, $order, 21, '72.00', 7200,
$units, 'receipt-' . $order, $observed, $paidAt));
$accepted = $receipt('paytaro', 'PT-QUOTE-A', 52560, $now + 1000, $now + 899);
checkCheckoutQuote($accepted->status === 'accepted' && $accepted->credit_cents === 7300 && $accepted->received_units === 52560,
'On-time payment evidence or the original rate for an attributable overpayment was lost.');
checkCheckoutQuote($receipt('paytaro', 'PT-QUOTE-A', 52560, $now + 1200, null)->credit_cents === 7300,
'Receipt replay did not reuse the original conclusion.');
checkCheckoutQuote($receipt('yipay_wxpay', 'YP-QUOTE-B', 52560, $now + 901, null)->review_reason === 'payment_time_unconfirmed',
'A late payment without a trusted timestamp was marked ready to credit.');
$legacy = $receipt('paytaro', 'PT-LEGACY', 7200, $now + 10, null);
checkCheckoutQuote($legacy->status === 'review' && $legacy->review_reason === 'legacy_quote' && $legacy->credit_cents === null,
'Money from a legacy underquoted order was released automatically.');
checkCheckoutQuote($receipt('paytaro', 'PT-LEGACY', 7200, $now + 20, null)->received_at === $now + 10,
'Repeated legacy evidence changed the recorded settlement.');
echo "Checkout quote persistence, old receipts and deadlines passed.\n";