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.
279 lines
11 KiB
PHP
279 lines
11 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudPaymentEffectResolutionService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$reject = static function (callable $callback, int $status) use ($assert): void {
|
|
try {
|
|
$callback();
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === $status,
|
|
'Unexpected payment resolution HTTP status.');
|
|
return;
|
|
}
|
|
throw new RuntimeException('Invalid payment resolution was accepted.');
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->getConnection('default')->getSchemaBuilder();
|
|
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('operation_id')->unique();
|
|
$table->string('request_idempotency_key');
|
|
$table->string('request_fingerprint');
|
|
$table->integer('parent_job_id')->nullable();
|
|
$table->integer('order_id')->nullable();
|
|
$table->integer('invoice_id')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->integer('snapshot_id')->nullable();
|
|
$table->integer('user_id');
|
|
$table->integer('host_id')->nullable();
|
|
$table->string('kind');
|
|
$table->string('execution_mode');
|
|
$table->string('phase');
|
|
$table->string('outcome')->nullable();
|
|
$table->string('status');
|
|
$table->text('sealed_payload');
|
|
$table->integer('attempts')->default(0);
|
|
$table->integer('dispatch_attempts')->default(0);
|
|
$table->integer('dispatch_deadline_at');
|
|
$table->integer('dispatch_started_at')->nullable();
|
|
$table->integer('agent_accepted_at')->nullable();
|
|
$table->integer('receipt_deadline_at')->nullable();
|
|
$table->integer('terminal_at')->nullable();
|
|
$table->boolean('effect_may_have_applied')->nullable();
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
$table->string('effect_resolution');
|
|
$table->integer('effect_resolved_at')->nullable();
|
|
$table->integer('effect_resolved_by')->nullable();
|
|
$table->text('effect_resolution_reason')->default('');
|
|
$table->boolean('finalization_incident')->default(false);
|
|
$table->string('finalization_error_code')->nullable();
|
|
$table->string('completion_event_id')->nullable();
|
|
$table->integer('read_at')->nullable();
|
|
$table->integer('dispatched_at')->nullable();
|
|
$table->string('lease_token')->nullable();
|
|
$table->integer('lease_until')->default(0);
|
|
$table->integer('available_at');
|
|
$table->string('error_code')->nullable();
|
|
$table->text('result_json')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['user_id', 'request_idempotency_key']);
|
|
});
|
|
$schema->create('cloud_invoice_payments', static function (Blueprint $table): void {
|
|
$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');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['provider', 'provider_order_no']);
|
|
$table->unique(['user_id', 'idempotency_key']);
|
|
});
|
|
$schema->create('paytaro_orders', static function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('order_no')->unique();
|
|
$table->string('gateway_trade_no')->nullable()->unique();
|
|
$table->integer('paylist_id')->unique();
|
|
$table->integer('user_id');
|
|
$table->decimal('amount', 12, 2);
|
|
$table->string('payment_kind')->default('');
|
|
$table->text('payment_value')->nullable();
|
|
$table->string('status');
|
|
$table->string('error_message')->default('');
|
|
$table->integer('paid_at')->default(0);
|
|
$table->integer('created_at')->default(0);
|
|
$table->integer('updated_at')->default(0);
|
|
});
|
|
|
|
$now = time();
|
|
$job = static function (int $id, int $invoiceId, string $key,
|
|
string $operationId) use ($now): array {
|
|
return [
|
|
'id' => $id,
|
|
'operation_id' => $operationId,
|
|
'request_idempotency_key' => $key,
|
|
'request_fingerprint' => hash('sha256', $key),
|
|
'invoice_id' => $invoiceId,
|
|
'user_id' => 7,
|
|
'host_id' => null,
|
|
'kind' => 'payment_create',
|
|
'execution_mode' => 'local',
|
|
'phase' => 'terminal',
|
|
'outcome' => 'failed',
|
|
'status' => 'failed',
|
|
'sealed_payload' => '',
|
|
'attempts' => 1,
|
|
'dispatch_attempts' => 1,
|
|
'dispatch_deadline_at' => $now - 120,
|
|
'dispatch_started_at' => $now - 90,
|
|
'receipt_deadline_at' => $now - 30,
|
|
'terminal_at' => $now - 1,
|
|
'effect_may_have_applied' => true,
|
|
'safe_to_resubmit' => false,
|
|
'effect_resolution' => 'unknown',
|
|
'effect_resolution_reason' => '',
|
|
'finalization_incident' => false,
|
|
'lease_until' => 0,
|
|
'available_at' => $now - 120,
|
|
'error_code' => 'payment_creation_unknown',
|
|
'result_json' => json_encode(['cause_code' => 'payment_creation_unknown']),
|
|
'created_at' => $now - 120,
|
|
'updated_at' => $now - 1,
|
|
];
|
|
};
|
|
Capsule::table('cloud_jobs')->insert([
|
|
$job(1, 101, 'payment-effect-applied-0001', str_repeat('a', 32)),
|
|
$job(2, 102, 'payment-effect-absent-0001', str_repeat('b', 32)),
|
|
]);
|
|
Capsule::table('cloud_invoice_payments')->insert([
|
|
[
|
|
'invoice_id' => 101,
|
|
'user_id' => 7,
|
|
'provider' => 'paytaro',
|
|
'provider_order_no' => 'PT-EFFECT-APPLIED-0001',
|
|
'idempotency_key' => 'payment-effect-applied-0001',
|
|
'amount' => '20.00',
|
|
'status' => 'pending',
|
|
'created_at' => $now - 120,
|
|
'updated_at' => $now - 60,
|
|
],
|
|
[
|
|
'invoice_id' => 102,
|
|
'user_id' => 7,
|
|
'provider' => 'paytaro',
|
|
'provider_order_no' => 'PT-EFFECT-ABSENT-0001',
|
|
'idempotency_key' => 'payment-effect-absent-0001',
|
|
'amount' => '30.00',
|
|
'status' => 'creation_unknown',
|
|
'created_at' => $now - 120,
|
|
'updated_at' => $now - 60,
|
|
],
|
|
]);
|
|
Capsule::table('paytaro_orders')->insert([
|
|
[
|
|
'order_no' => 'PT-EFFECT-APPLIED-0001',
|
|
'gateway_trade_no' => 'GATEWAY-EFFECT-APPLIED-0001',
|
|
'paylist_id' => 201,
|
|
'user_id' => 7,
|
|
'amount' => '20.00',
|
|
'status' => 'pending',
|
|
'created_at' => $now - 120,
|
|
'updated_at' => $now - 60,
|
|
],
|
|
[
|
|
'order_no' => 'PT-EFFECT-ABSENT-0001',
|
|
'gateway_trade_no' => null,
|
|
'paylist_id' => 202,
|
|
'user_id' => 7,
|
|
'amount' => '30.00',
|
|
'status' => 'creation_unknown',
|
|
'created_at' => $now - 120,
|
|
'updated_at' => $now - 60,
|
|
],
|
|
]);
|
|
|
|
$actor = [
|
|
'user_id' => 99,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
];
|
|
$assert(CloudPaymentEffectResolutionService::handles('1'),
|
|
'Local payment task was not recognized by the resolution service.');
|
|
$assert(!CloudPaymentEffectResolutionService::handles('invalid'),
|
|
'Invalid payment task identifier was accepted.');
|
|
|
|
$reject(static function (): void {
|
|
CloudPaymentEffectResolutionService::resolve([
|
|
'user_id' => 10,
|
|
'is_admin' => true,
|
|
'is_super_admin' => false,
|
|
'can_manage' => false,
|
|
], '1', [
|
|
'resolution' => 'applied',
|
|
'reason' => '管理员通过支付平台交易号核对确认该订单已经成功创建',
|
|
]);
|
|
}, 403);
|
|
$reject(static function () use ($actor): void {
|
|
CloudPaymentEffectResolutionService::resolve($actor, '1', [
|
|
'resolution' => 'applied',
|
|
'reason' => '证据太短',
|
|
]);
|
|
}, 400);
|
|
$assert((string) Capsule::table('cloud_jobs')->where('id', 1)
|
|
->value('effect_resolution') === 'unknown',
|
|
'Rejected payment resolution mutated the task.');
|
|
|
|
$providerOrderCount = Capsule::table('paytaro_orders')->count();
|
|
$appliedReason = '管理员通过支付平台交易号核对确认该订单已经成功创建';
|
|
$applied = CloudPaymentEffectResolutionService::resolve(
|
|
$actor,
|
|
'1',
|
|
['resolution' => 'applied', 'reason' => $appliedReason]
|
|
);
|
|
$appliedJob = Capsule::table('cloud_jobs')->where('id', 1)->first();
|
|
$assert(($applied['outcome'] ?? '') === 'applied'
|
|
&& ($applied['payment_status'] ?? '') === 'pending'
|
|
&& $appliedJob !== null
|
|
&& (string) $appliedJob->phase === 'terminal'
|
|
&& (string) $appliedJob->outcome === 'failed'
|
|
&& (string) $appliedJob->status === 'failed'
|
|
&& (string) $appliedJob->effect_resolution === 'applied'
|
|
&& (int) $appliedJob->effect_resolved_by === 99
|
|
&& (string) $appliedJob->effect_resolution_reason === $appliedReason
|
|
&& (int) $appliedJob->finalization_incident === 0,
|
|
'Applied payment effect did not preserve the terminal task envelope.');
|
|
|
|
$notAppliedReason = '管理员通过支付平台订单查询确认该请求从未创建任何支付订单';
|
|
$notApplied = CloudPaymentEffectResolutionService::resolve(
|
|
$actor,
|
|
'2',
|
|
['resolution' => 'not_applied', 'reason' => $notAppliedReason]
|
|
);
|
|
$notAppliedJob = Capsule::table('cloud_jobs')->where('id', 2)->first();
|
|
$assert(($notApplied['outcome'] ?? '') === 'not_applied'
|
|
&& ($notApplied['payment_status'] ?? '') === 'not_created'
|
|
&& $notAppliedJob !== null
|
|
&& (string) $notAppliedJob->phase === 'terminal'
|
|
&& (string) $notAppliedJob->outcome === 'failed'
|
|
&& (string) $notAppliedJob->status === 'failed'
|
|
&& (string) $notAppliedJob->effect_resolution === 'not_applied'
|
|
&& (int) $notAppliedJob->effect_resolved_by === 99
|
|
&& (string) $notAppliedJob->effect_resolution_reason === $notAppliedReason
|
|
&& (string) Capsule::table('cloud_invoice_payments')->where('invoice_id', 102)
|
|
->value('status') === 'not_created'
|
|
&& (string) Capsule::table('paytaro_orders')
|
|
->where('order_no', 'PT-EFFECT-ABSENT-0001')->value('status') === 'not_created',
|
|
'Not-applied payment effect did not atomically close the local ledgers.');
|
|
|
|
$source = file_get_contents(dirname(__DIR__)
|
|
. '/app/Services/Cloud/CloudPaymentEffectResolutionService.php');
|
|
$assert($source !== false
|
|
&& !str_contains($source, 'CloudAgent')
|
|
&& !str_contains($source, 'CloudEngineClient')
|
|
&& !str_contains($source, 'gatewayClient(')
|
|
&& !str_contains($source, 'createOrder(')
|
|
&& Capsule::table('paytaro_orders')->count() === $providerOrderCount,
|
|
'Manual payment resolution can contact Agent/provider or create another order.');
|
|
|
|
echo 'Cloud payment effect resolution checks passed.' . PHP_EOL;
|