completed transition. */ private function store( string $status = 'pending' ): object { return new class( $status ) { public array $row; public int $applied = 0; public function __construct( string $status ) { $this->row = array( 'id' => 7, 'status' => $status, 'total_minor' => 500, 'currency' => 'USD', 'paypal_capture_id' => '' ); } public function mark( int $id, string $cap ): bool { if ( 'pending' !== $this->row['status'] ) { return false; } $this->row['status'] = 'completed'; $this->row['paypal_capture_id'] = $cap; return true; } public function notify( int $id ): void { $this->applied++; } }; } private function run_finalize( object $s, string $status, string $cap, ?Money $amt ): string { return CaptureFinalizer::finalize( $s->row, $status, $cap, $amt, array( $s, 'mark' ), array( $s, 'notify' ) ); } public function test_pending_capture_status_stays_pending(): void { $s = $this->store(); $this->assertSame( 'pending', $this->run_finalize( $s, 'PENDING', 'CAP1', new Money( 500, 'USD' ) ) ); $this->assertSame( 'pending', $s->row['status'] ); $this->assertSame( 0, $s->applied ); } public function test_amount_or_currency_mismatch_or_missing_id_stays_pending(): void { foreach ( array( array( 'CAP1', new Money( 499, 'USD' ) ), array( 'CAP1', new Money( 500, 'EUR' ) ), array( 'CAP1', null ), array( '', new Money( 500, 'USD' ) ) ) as [ $cap, $amt ] ) { $s = $this->store(); $this->assertSame( 'pending', $this->run_finalize( $s, 'COMPLETED', $cap, $amt ) ); $this->assertSame( 'pending', $s->row['status'] ); $this->assertSame( 0, $s->applied ); } } public function test_completed_webhook_finalizes_pending_donation_exactly_once(): void { $s = $this->store(); // Capture endpoint saw PENDING first... $this->assertSame( 'pending', $this->run_finalize( $s, 'PENDING', '', null ) ); // ...then the COMPLETED webhook arrives, and is replayed twice. $ev = WebhookEvent::fromArray( array( 'id' => 'E1', 'event_type' => 'PAYMENT.CAPTURE.COMPLETED', 'resource' => array( 'id' => 'CAP1', 'custom_id' => '7', 'amount' => array( 'value' => '5.00', 'currency_code' => 'USD' ) ), ) ); $this->assertSame( 'completed', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) ); $this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) ); $this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) ); $this->assertSame( 'completed', $s->row['status'] ); $this->assertSame( 'CAP1', $s->row['paypal_capture_id'] ); $this->assertSame( 1, $s->applied ); } public function test_lost_race_does_not_apply_twice(): void { $s = $this->store(); $lost = CaptureFinalizer::finalize( $s->row, 'COMPLETED', 'CAP1', new Money( 500, 'USD' ), static fn(): bool => false, array( $s, 'notify' ) ); $this->assertSame( 'duplicate', $lost ); $this->assertSame( 0, $s->applied ); } public function test_non_pending_rows_are_never_touched(): void { foreach ( array( 'completed', 'refunded', 'reversed' ) as $st ) { $s = $this->store( $st ); $this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', 'CAP1', new Money( 500, 'USD' ) ) ); $this->assertSame( $st, $s->row['status'] ); $this->assertSame( 0, $s->applied ); } } public function test_refund_events_are_not_registered_or_parsed_specially(): void { $this->assertSame( array( 'PAYMENT.CAPTURE.COMPLETED' ), \StreamGoals\PayPal\Client::WEBHOOK_EVENTS ); $ev = WebhookEvent::fromArray( array( 'id' => 'E2', 'event_type' => 'PAYMENT.CAPTURE.REFUNDED', 'resource' => array( 'id' => 'REF1' ) ) ); $this->assertSame( 'PAYMENT.CAPTURE.REFUNDED', $ev->type ); // controller returns 'ignored' (HTTP 200) for it } }