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

101 lines
5.1 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 App\Services\Payment\PaymentReconciliation;
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);
$schema->create('stripe_orders', static function (Blueprint $table): void {
$table->increments('id');
$table->string('order_no');
$table->string('payment_method');
$table->decimal('amount', 12, 2);
$table->string('status');
$table->integer('paid_at');
});
$schema->create('yipay_refunds', static function (Blueprint $table): void {
$table->increments('id');
$table->string('order_no');
$table->string('status');
$table->decimal('amount', 12, 2);
$table->decimal('gateway_amount', 12, 2);
});
$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.');
Capsule::table('stripe_orders')->insert([
'order_no' => 'ST-USD', 'payment_method' => 'card', 'amount' => '20.00', 'status' => 'paid', 'paid_at' => $now,
]);
PaymentRateService::override('USDT/USD', '1.00', $now + 3600, 7, 'USDT reconciliation fixture', $now);
$usdt = PaymentCheckoutQuotes::prepare('usdt', '72.00', $now, $now + 900);
PaymentCheckoutQuotes::persist($usdt, 'USDT-EXPORT', 21, 72_000_001);
$receipt('usdt', 'USDT-EXPORT', 72_000_001, $now + 12, $now + 10);
$totals = PaymentReconciliation::totals();
checkCheckoutQuote($totals['CNY'] === ['received' => '525.60', 'credited' => '73.00', 'review' => 2]
&& $totals['USDT'] === ['received' => '72.000001', 'credited' => '72.00', 'review' => 0]
&& $totals['USD'] === ['received' => '20.00', 'credited' => '20.00', 'review' => 0],
'Reconciliation mixed currencies, excluded overpayment or counted unresolved receipts.');
$csvFile = tmpfile();
PaymentReconciliation::csv($csvFile, 'CNY', true);
rewind($csvFile);
$csv = stream_get_contents($csvFile);
fclose($csvFile);
checkCheckoutQuote(str_contains($csv, 'PT-LEGACY,paytaro,CNY,review,legacy_quote')
&& str_contains($csv, 'YP-QUOTE-B,yipay_wxpay,CNY,review,payment_time_unconfirmed')
&& !str_contains($csv, 'PT-QUOTE-A') && !str_contains($csv, 'USDT-EXPORT'),
'CSV did not respect the requested status and currency scopes.');
echo "Checkout quote persistence, old receipts and deadlines passed.\n";