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.
331 lines
14 KiB
PHP
331 lines
14 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\User;
|
|
use App\Services\Payment\TronAddress;
|
|
use App\Services\Payment\UsdtConfig;
|
|
use App\Services\Payment\UsdtPaymentService;
|
|
use App\Services\Payment\UsdtScannerService;
|
|
use App\Services\Payment\UsdtWalletService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
function failUsdtTest($message)
|
|
{
|
|
fwrite(STDERR, $message . PHP_EOL);
|
|
exit(1);
|
|
}
|
|
|
|
$walletAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
|
|
$senderAddress = 'T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb';
|
|
if (!TronAddress::isValid($walletAddress)
|
|
|| !TronAddress::isValid($senderAddress)
|
|
|| TronAddress::isValid(substr($walletAddress, 0, -1) . 'x')) {
|
|
failUsdtTest('TRON Base58Check address validation failed.');
|
|
}
|
|
|
|
$adminActor = new class {
|
|
public function isAdmin() { return true; }
|
|
};
|
|
$userActor = new class {
|
|
public function isAdmin() { return false; }
|
|
};
|
|
$accessCases = [
|
|
[$adminActor, ['usdt_enabled' => 'false', 'usdt_test_mode' => 'false'], false],
|
|
[$userActor, ['usdt_enabled' => 'true', 'usdt_test_mode' => 'false'], true],
|
|
[$userActor, ['usdt_enabled' => 'true', 'usdt_test_mode' => 'true'], false],
|
|
[$adminActor, ['usdt_enabled' => 'true', 'usdt_test_mode' => 'true'], true],
|
|
];
|
|
foreach ($accessCases as [$actor, $settings, $expected]) {
|
|
if (UsdtConfig::canUserAccess($actor, $settings) !== $expected) {
|
|
failUsdtTest('Native USDT access policy failed.');
|
|
}
|
|
}
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->getConnection('default')->getSchemaBuilder();
|
|
$schema->create('configurations', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('name')->nullable();
|
|
$table->text('value')->nullable();
|
|
});
|
|
$schema->create('user', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('email')->default('');
|
|
$table->integer('is_admin')->default(0);
|
|
$table->decimal('money', 12, 2)->default(0);
|
|
$table->integer('enable')->default(1);
|
|
$table->integer('restrict_balance_purchase')->default(0);
|
|
$table->integer('restrict_recharge')->default(0);
|
|
$table->integer('ref_by')->default(0);
|
|
$table->dateTime('reg_date');
|
|
$table->decimal('aff_rate', 8, 2)->default(0);
|
|
$table->decimal('aff_money', 12, 2)->default(0);
|
|
});
|
|
$schema->create('paylist', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->integer('userid');
|
|
$table->decimal('total', 12, 2);
|
|
$table->integer('status')->default(0);
|
|
$table->string('tradeno');
|
|
$table->integer('datetime')->default(0);
|
|
});
|
|
$schema->create('code', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->string('code');
|
|
$table->integer('type');
|
|
$table->decimal('number', 12, 2);
|
|
$table->integer('isused');
|
|
$table->integer('userid');
|
|
$table->dateTime('usedatetime');
|
|
});
|
|
$schema->create('payback', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->decimal('total', 12, 2);
|
|
$table->integer('userid');
|
|
$table->integer('ref_by');
|
|
$table->decimal('ref_get', 12, 2);
|
|
$table->integer('datetime');
|
|
});
|
|
$schema->create('usdt_wallets', function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('address')->unique();
|
|
$table->string('label')->default('');
|
|
$table->integer('enabled')->default(1);
|
|
$table->integer('sort')->default(0);
|
|
$table->integer('allocation_cursor')->default(0);
|
|
$table->integer('last_assigned_at')->default(0);
|
|
$table->integer('last_scanned_at')->default(0);
|
|
$table->string('last_error')->default('');
|
|
$table->integer('created_at')->default(0);
|
|
$table->integer('updated_at')->default(0);
|
|
});
|
|
$schema->create('usdt_payment_orders', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->string('order_no')->unique();
|
|
$table->string('business_type');
|
|
$table->string('business_order_no');
|
|
$table->integer('paylist_id')->nullable()->unique();
|
|
$table->integer('user_id')->nullable();
|
|
$table->integer('wallet_id');
|
|
$table->decimal('amount', 12, 2);
|
|
$table->decimal('exchange_rate', 12, 4);
|
|
$table->decimal('actual_amount', 24, 6);
|
|
$table->bigInteger('amount_atomic');
|
|
$table->string('currency');
|
|
$table->string('token');
|
|
$table->string('network');
|
|
$table->string('contract_address');
|
|
$table->string('receive_address');
|
|
$table->string('status');
|
|
$table->string('txid')->nullable()->unique();
|
|
$table->bigInteger('block_number')->default(0);
|
|
$table->bigInteger('block_timestamp')->default(0);
|
|
$table->bigInteger('expires_at')->default(0);
|
|
$table->bigInteger('paid_at')->default(0);
|
|
$table->bigInteger('created_at')->default(0);
|
|
$table->bigInteger('updated_at')->default(0);
|
|
$table->string('error_message')->default('');
|
|
$table->unique(['business_type', 'business_order_no']);
|
|
$table->unique(['wallet_id', 'amount_atomic']);
|
|
});
|
|
$schema->create('usdt_chain_transactions', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->string('event_key')->unique();
|
|
$table->string('txid');
|
|
$table->string('contract_address');
|
|
$table->string('from_address');
|
|
$table->string('to_address');
|
|
$table->bigInteger('amount_atomic');
|
|
$table->bigInteger('block_number')->default(0);
|
|
$table->bigInteger('block_timestamp')->default(0);
|
|
$table->text('payload');
|
|
$table->string('status');
|
|
$table->string('order_no')->nullable();
|
|
$table->bigInteger('detected_at')->default(0);
|
|
$table->bigInteger('processed_at')->default(0);
|
|
$table->string('error_message')->default('');
|
|
});
|
|
$schema->create('cloud_orders', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->integer('user_id');
|
|
$table->integer('plan_id');
|
|
$table->decimal('amount', 12, 2);
|
|
$table->string('status');
|
|
});
|
|
$schema->create('cloud_invoices', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->string('purpose')->default('renewal');
|
|
$table->integer('order_id')->nullable();
|
|
$table->integer('user_id');
|
|
$table->integer('plan_id')->nullable();
|
|
$table->integer('due_at')->default(0);
|
|
$table->decimal('amount', 12, 2)->default(0);
|
|
$table->string('status')->default('open');
|
|
});
|
|
$schema->create('cloud_invoice_payments', function (Blueprint $table) {
|
|
$table->bigIncrements('id');
|
|
$table->integer('invoice_id');
|
|
$table->integer('user_id');
|
|
$table->string('provider');
|
|
$table->string('provider_order_no');
|
|
$table->string('idempotency_key');
|
|
$table->decimal('amount', 12, 2);
|
|
$table->string('status')->default('creating');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['provider', 'provider_order_no']);
|
|
$table->unique(['user_id', 'idempotency_key']);
|
|
});
|
|
|
|
Capsule::table('configurations')->insert([
|
|
['name' => 'usdt_enabled', 'value' => 'true'],
|
|
['name' => 'usdt_test_mode', 'value' => 'false'],
|
|
['name' => 'usdt_trongrid_base_url', 'value' => 'https://api.trongrid.io'],
|
|
['name' => 'usdt_cny_per_token', 'value' => '7.2000'],
|
|
['name' => 'usdt_min_amount', 'value' => '1.00'],
|
|
['name' => 'usdt_order_expire_minutes', 'value' => '30'],
|
|
['name' => 'usdt_late_grace_minutes', 'value' => '10'],
|
|
['name' => 'usdt_request_timeout_seconds', 'value' => '15'],
|
|
]);
|
|
UsdtWalletService::sync([$walletAddress => 'test wallet']);
|
|
Capsule::table('user')->insert([
|
|
['id' => 1, 'email' => 'buyer@example.com', 'money' => 0, 'ref_by' => 2, 'reg_date' => date('Y-m-d H:i:s'), 'aff_rate' => 0, 'aff_money' => 0],
|
|
['id' => 2, 'email' => 'referrer@example.com', 'money' => 0, 'ref_by' => 0, 'reg_date' => date('Y-m-d H:i:s'), 'aff_rate' => 10, 'aff_money' => 0],
|
|
]);
|
|
Capsule::table('cloud_orders')->insert([
|
|
'id' => 1999, 'user_id' => 1, 'plan_id' => 1,
|
|
'amount' => '50.00', 'status' => 'awaiting_payment',
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 999, 'purpose' => 'provision', 'order_id' => 1999,
|
|
'user_id' => 1, 'plan_id' => 1, 'due_at' => time() + 1800,
|
|
'amount' => '50.00', 'status' => 'open',
|
|
]);
|
|
|
|
$user = User::where('id', 1)->first();
|
|
if (method_exists(UsdtPaymentService::class, 'createPaidRegistrationOrder')) {
|
|
failUsdtTest('Retired paid registration is still exposed by native USDT payments.');
|
|
}
|
|
$order = (new UsdtPaymentService())->createRechargeOrder($user, '100.00');
|
|
$expectedBase = (int) ceil(100 / 7.2 * 1000000);
|
|
if ((int) $order->amount_atomic <= $expectedBase
|
|
|| (int) $order->amount_atomic > $expectedBase + 50000
|
|
|| (string) $order->receive_address !== $walletAddress
|
|
|| (string) $order->status !== 'pending') {
|
|
failUsdtTest('Native USDT order allocation failed.');
|
|
}
|
|
|
|
$transfer = [
|
|
'transaction_id' => str_repeat('a', 64),
|
|
'token_info' => ['address' => UsdtConfig::CONTRACT_ADDRESS, 'decimals' => 6, 'symbol' => 'USDT'],
|
|
'from' => $senderAddress,
|
|
'to' => $walletAddress,
|
|
'value' => (string) $order->amount_atomic,
|
|
'block_timestamp' => (int) $order->created_at * 1000,
|
|
'block_number' => 123456,
|
|
];
|
|
$fakeClient = new class($transfer) {
|
|
private $transfer;
|
|
public function __construct(array $transfer) { $this->transfer = $transfer; }
|
|
public function transfersTo($address, $minimum, $maximum) { return [$this->transfer]; }
|
|
};
|
|
$scanner = new class($fakeClient) extends UsdtScannerService {
|
|
private $fakeClient;
|
|
public function __construct($fakeClient) { $this->fakeClient = $fakeClient; }
|
|
protected function client(array $settings) { return $this->fakeClient; }
|
|
};
|
|
$stats = $scanner->run();
|
|
|
|
if ((int) $stats['credited'] !== 1
|
|
|| (float) Capsule::table('user')->where('id', 1)->value('money') !== 100.0
|
|
|| (float) Capsule::table('user')->where('id', 2)->value('aff_money') !== 10.0
|
|
|| Capsule::table('code')->where('type', UsdtPaymentService::LEDGER_TYPE)->count() !== 1
|
|
|| Capsule::table('payback')->count() !== 1
|
|
|| Capsule::table('usdt_chain_transactions')->where('status', 'credited')->count() !== 1
|
|
|| Capsule::table('usdt_payment_orders')->where('status', 'paid')->count() !== 1) {
|
|
failUsdtTest('Native USDT confirmed transfer settlement failed.');
|
|
}
|
|
|
|
$scanner->run();
|
|
if ((float) Capsule::table('user')->where('id', 1)->value('money') !== 100.0
|
|
|| Capsule::table('code')->where('type', UsdtPaymentService::LEDGER_TYPE)->count() !== 1) {
|
|
failUsdtTest('Native USDT settlement was not idempotent.');
|
|
}
|
|
|
|
// A recovery retry must not credit a paylist that was already committed.
|
|
Capsule::table('usdt_payment_orders')->where('id', $order->id)->update(['status' => 'pending']);
|
|
Capsule::table('usdt_chain_transactions')->where('txid', $transfer['transaction_id'])->update(['status' => 'detected']);
|
|
$retryStats = $scanner->run();
|
|
if ((int) $retryStats['credited'] !== 0
|
|
|| (int) $retryStats['duplicates'] !== 1
|
|
|| (float) Capsule::table('user')->where('id', 1)->value('money') !== 100.0
|
|
|| (float) Capsule::table('user')->where('id', 2)->value('aff_money') !== 10.0
|
|
|| Capsule::table('code')->where('type', UsdtPaymentService::LEDGER_TYPE)->count() !== 1
|
|
|| Capsule::table('payback')->count() !== 1
|
|
|| Capsule::table('usdt_payment_orders')->where('id', $order->id)->value('status') !== 'paid'
|
|
|| Capsule::table('usdt_chain_transactions')->where('txid', $transfer['transaction_id'])->value('status') !== 'credited') {
|
|
failUsdtTest('Native USDT recovery retry credited the same recharge twice or failed to reconcile the event.');
|
|
}
|
|
|
|
$invoiceOrder = (new UsdtPaymentService())->createInvoiceOrder($user, '50.00', [
|
|
'invoice_id' => 999,
|
|
'idempotency_key' => 'usdt-invoice-payment-0001',
|
|
'provider' => 'usdt',
|
|
]);
|
|
$invoiceAttempt = Capsule::table('cloud_invoice_payments')
|
|
->where('provider_order_no', (string) $invoiceOrder->order_no)->first();
|
|
if ($invoiceAttempt === null
|
|
|| (string) $invoiceAttempt->status !== 'creating'
|
|
|| (string) $invoiceOrder->business_type !== 'recharge'
|
|
|| (string) $invoiceOrder->business_order_no !== (string) $invoiceOrder->order_no) {
|
|
failUsdtTest('Native USDT invoice order did not preserve recharge scanning semantics.');
|
|
}
|
|
// The scanner bridge must retain a provider credit if its linked business
|
|
// invoice disappeared after the payment order was created.
|
|
Capsule::table('cloud_invoices')->where('id', 999)->delete();
|
|
Capsule::table('cloud_invoice_payments')->where('id', $invoiceAttempt->id)
|
|
->update(['status' => 'pending', 'updated_at' => time()]);
|
|
$invoiceTransfer = [
|
|
'transaction_id' => str_repeat('b', 64),
|
|
'token_info' => ['address' => UsdtConfig::CONTRACT_ADDRESS, 'decimals' => 6, 'symbol' => 'USDT'],
|
|
'from' => $senderAddress,
|
|
'to' => $walletAddress,
|
|
'value' => (string) $invoiceOrder->amount_atomic,
|
|
'block_timestamp' => (int) $invoiceOrder->created_at * 1000,
|
|
'block_number' => 123457,
|
|
];
|
|
$invoiceClient = new class($invoiceTransfer) {
|
|
private $transfer;
|
|
public function __construct(array $transfer) { $this->transfer = $transfer; }
|
|
public function transfersTo($address, $minimum, $maximum) { return [$this->transfer]; }
|
|
};
|
|
$invoiceScanner = new class($invoiceClient) extends UsdtScannerService {
|
|
private $fakeClient;
|
|
public function __construct($fakeClient) { $this->fakeClient = $fakeClient; }
|
|
protected function client(array $settings) { return $this->fakeClient; }
|
|
};
|
|
$invoiceStats = $invoiceScanner->run();
|
|
if ((int) $invoiceStats['credited'] !== 1
|
|
|| (string) Capsule::table('cloud_invoice_payments')->where('id', $invoiceAttempt->id)
|
|
->value('status') !== 'credited_balance'
|
|
|| (string) Capsule::table('usdt_payment_orders')->where('id', $invoiceOrder->id)
|
|
->value('status') !== 'paid'
|
|
|| (float) Capsule::table('user')->where('id', 1)->value('money') !== 150.0
|
|
|| Capsule::table('code')->where('type', UsdtPaymentService::LEDGER_TYPE)->count() !== 2) {
|
|
failUsdtTest('Native USDT invoice settlement bridge failed.');
|
|
}
|
|
|
|
$controllerSource = file_get_contents(dirname(__DIR__) . '/app/Controllers/UsdtController.php');
|
|
if ($controllerSource === false
|
|
|| strpos($controllerSource, "->whereNotExists(function (\$query)") === false
|
|
|| strpos($controllerSource, "cloud_invoice_payments.provider_order_no") === false) {
|
|
failUsdtTest('Native USDT invoice orders are exposed by the recharge status endpoint.');
|
|
}
|
|
|
|
echo 'Native USDT address, invoice bridge, order allocation and idempotent recovery checks passed.' . PHP_EOL;
|