Skip to content

Commit d7c70de

Browse files
authored
Fraud Protection: Add event dispatching for pay-for-order page (#35)
* Add tracking for pay-for-order page in CheckoutEventTracker Enhance the CheckoutEventTracker to collect an event when the pay-for-order page is loaded. This change includes a new conditional check for the pay-for-order page and updates the corresponding unit tests to verify that the event is correctly collected during the checkout process.
1 parent df01e85 commit d7c70de

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/CheckoutEventTracker.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ public function register(): void {
6161
* @return void
6262
*/
6363
public function track_checkout_page_loaded(): void {
64-
if ( function_exists( 'is_checkout' ) && is_checkout() && ! is_order_received_page() ) {
65-
$this->session_data_collector->collect( 'checkout_page_loaded', array() );
64+
if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_order_received_page() ) {
65+
return;
6666
}
67+
68+
$event_name = is_checkout_pay_page() ? 'pay_for_order_page_loaded' : 'checkout_page_loaded';
69+
$this->session_data_collector->collect( $event_name, array() );
6770
}
6871

6972
/**

tests/php/src/CheckoutEventTrackerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ public function test_track_checkout_page_loaded_does_not_collect_when_not_checko
116116
$this->sut->track_checkout_page_loaded();
117117
}
118118

119+
/**
120+
* @testdox track_checkout_page_loaded() collects pay_for_order_page_loaded (not checkout_page_loaded) on the pay-for-order page.
121+
*/
122+
public function test_track_checkout_page_loaded_collects_pay_for_order_event(): void {
123+
global $wp;
124+
$wp->query_vars['order-pay'] = 123;
125+
add_filter( 'woocommerce_is_checkout', '__return_true' );
126+
127+
$collected_events = array();
128+
$this->mock_collector
129+
->expects( $this->atLeast( 1 ) )
130+
->method( 'collect' )
131+
->willReturnCallback(
132+
function ( $event_type, $event_data ) use ( &$collected_events ) {
133+
$collected_events[] = $event_type;
134+
return array();
135+
}
136+
);
137+
138+
$this->sut->track_checkout_page_loaded();
139+
140+
$this->assertContains( 'pay_for_order_page_loaded', $collected_events, 'pay_for_order_page_loaded event should be collected on the pay-for-order page' );
141+
$this->assertNotContains( 'checkout_page_loaded', $collected_events, 'checkout_page_loaded event should NOT be collected on the pay-for-order page' );
142+
143+
remove_filter( 'woocommerce_is_checkout', '__return_true' );
144+
unset( $wp->query_vars['order-pay'] );
145+
}
146+
119147
// ========================================
120148
// Blocks Checkout Tests
121149
// ========================================

0 commit comments

Comments
 (0)