Security review fixes: keep pending-capture ids, ship uninstall.php (+verify step), webhook guard, IPv6 /64 + global order throttle, token-safe redirects, honest refund wording, request-id and overlay-token hardening
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
68bd917660
commit
473fe192cd
28 files changed
+747
-34
No files matched your search
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Core\Maintenance;
|
||||
use StreamGoals\Core\Money;
|
||||
use StreamGoals\PayPal\CaptureFinalizer;
|
||||
use StreamGoals\PayPal\WebhookEvent;
|
||||
|
||||
/** M5: a PENDING PayPal capture must keep its capture id, survive the purge, and be finalized exactly once by the webhook. */
|
||||
final class PendingCaptureFlowTest extends TestCase {
|
||||
|
||||
/** @var array<int,array<string,mixed>> */
|
||||
private array $rows = array();
|
||||
private int $notified = 0;
|
||||
|
||||
protected function setUp(): void {
|
||||
foreach ( array( 'HOUR_IN_SECONDS' => 3600, 'DAY_IN_SECONDS' => 86400 ) as $k => $v ) {
|
||||
if ( ! defined( $k ) ) {
|
||||
define( $k, $v );
|
||||
}
|
||||
}
|
||||
$this->notified = 0;
|
||||
$this->rows = array(
|
||||
1 => array( 'id' => 1, 'goal_id' => 3, 'status' => 'pending', 'total_minor' => 1099, 'currency' => 'USD', 'paypal_order_id' => 'ORD1', 'paypal_capture_id' => null, 'created_at' => gmdate( 'Y-m-d H:i:s', time() - 30 * 86400 ) ),
|
||||
);
|
||||
}
|
||||
|
||||
private function record(): callable {
|
||||
return function ( int $id, string $cap ): bool {
|
||||
if ( 'pending' === $this->rows[ $id ]['status'] && empty( $this->rows[ $id ]['paypal_capture_id'] ) ) {
|
||||
$this->rows[ $id ]['paypal_capture_id'] = $cap;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
private function mark(): callable {
|
||||
return function ( int $id, string $cap ): bool {
|
||||
if ( 'pending' !== $this->rows[ $id ]['status'] ) {
|
||||
return false;
|
||||
}
|
||||
$this->rows[ $id ]['status'] = 'completed';
|
||||
$this->rows[ $id ]['paypal_capture_id'] = $cap;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
private function notify(): callable {
|
||||
return function ( int $id ): void { ++$this->notified; };
|
||||
}
|
||||
|
||||
private function purge(): void {
|
||||
foreach ( $this->rows as $id => $r ) {
|
||||
if ( Maintenance::isPurgeable( $r, time() ) ) {
|
||||
unset( $this->rows[ $id ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function webhook(): string {
|
||||
$ev = WebhookEvent::fromArray( array(
|
||||
'id' => 'WH-1', 'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
|
||||
'resource' => array( 'id' => 'CAP1', 'custom_id' => '1', 'amount' => array( 'value' => '10.99', 'currency_code' => 'USD' ), 'supplementary_data' => array( 'related_ids' => array( 'order_id' => 'ORD1' ) ) ),
|
||||
) );
|
||||
$row = $this->rows[ (int) $ev->customId ] ?? null;
|
||||
if ( ! $row ) {
|
||||
foreach ( $this->rows as $r ) {
|
||||
if ( $r['paypal_capture_id'] === $ev->captureId || $r['paypal_order_id'] === $ev->orderId ) {
|
||||
$row = $r;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $row ? CaptureFinalizer::finalize( $row, 'COMPLETED', $ev->captureId, $ev->amount, $this->mark(), $this->notify() ) : 'ignored';
|
||||
}
|
||||
|
||||
public function test_pending_capture_is_stored_survives_purge_and_webhook_finalizes_once(): void {
|
||||
$res = CaptureFinalizer::finalize( $this->rows[1], 'PENDING', 'CAP1', new Money( 1099, 'USD' ), $this->mark(), $this->notify(), $this->record() );
|
||||
$this->assertSame( CaptureFinalizer::PENDING, $res );
|
||||
$this->assertSame( 'CAP1', $this->rows[1]['paypal_capture_id'], 'PENDING capture id must be persisted' );
|
||||
$this->assertSame( 'pending', $this->rows[1]['status'] );
|
||||
|
||||
$this->purge();
|
||||
$this->assertArrayHasKey( 1, $this->rows, 'row with capture evidence must survive the purge even when old' );
|
||||
|
||||
$this->assertSame( CaptureFinalizer::COMPLETED, $this->webhook() );
|
||||
$this->assertSame( 'completed', $this->rows[1]['status'] );
|
||||
$this->assertSame( CaptureFinalizer::DUPLICATE, $this->webhook(), 'replayed webhook is a no-op' );
|
||||
$this->assertSame( 1, $this->notified, 'side effects exactly once' );
|
||||
}
|
||||
|
||||
public function test_without_recording_the_old_behaviour_would_lose_the_row(): void {
|
||||
CaptureFinalizer::finalize( $this->rows[1], 'PENDING', 'CAP1', new Money( 1099, 'USD' ), $this->mark(), $this->notify() );
|
||||
$this->assertNull( $this->rows[1]['paypal_capture_id'] );
|
||||
$this->purge();
|
||||
$this->assertArrayNotHasKey( 1, $this->rows );
|
||||
}
|
||||
|
||||
public function test_amount_mismatch_also_records_capture_id(): void {
|
||||
CaptureFinalizer::finalize( $this->rows[1], 'COMPLETED', 'CAP1', new Money( 5, 'USD' ), $this->mark(), $this->notify(), $this->record() );
|
||||
$this->assertSame( 'CAP1', $this->rows[1]['paypal_capture_id'] );
|
||||
$this->assertSame( 'pending', $this->rows[1]['status'] );
|
||||
}
|
||||
|
||||
public function test_no_capture_id_means_nothing_recorded(): void {
|
||||
$called = false;
|
||||
CaptureFinalizer::finalize( $this->rows[1], 'PENDING', '', null, $this->mark(), $this->notify(), function () use ( &$called ) { $called = true; return true; } );
|
||||
$this->assertFalse( $called );
|
||||
}
|
||||
|
||||
public function test_is_purgeable_rules(): void {
|
||||
$old = gmdate( 'Y-m-d H:i:s', time() - 8 * 86400 );
|
||||
$new = gmdate( 'Y-m-d H:i:s', time() - 6 * 86400 );
|
||||
$b = array( 'status' => 'pending', 'goal_id' => 1, 'paypal_capture_id' => null, 'created_at' => $old );
|
||||
$this->assertTrue( Maintenance::isPurgeable( $b, time() ) );
|
||||
$this->assertTrue( Maintenance::isPurgeable( array( 'paypal_capture_id' => '' ) + $b, time() ) );
|
||||
$this->assertFalse( Maintenance::isPurgeable( array( 'created_at' => $new ) + $b, time() ) );
|
||||
$this->assertFalse( Maintenance::isPurgeable( array( 'paypal_capture_id' => 'X' ) + $b, time() ) );
|
||||
$this->assertFalse( Maintenance::isPurgeable( array( 'status' => 'completed' ) + $b, time() ) );
|
||||
$this->assertFalse( Maintenance::isPurgeable( array( 'goal_id' => 0 ) + $b, time() ) );
|
||||
$this->assertFalse( Maintenance::isPurgeable( array( 'created_at' => 'garbage' ) + $b, time() ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user