Skip to content

Commit aa5cc44

Browse files
authored
Skip payment processing for WooPay's order validation request (#5209)
* Skip payment processing for WooPay's order validation request * Add changelog entry * Add phpunit tests * Check whether it's a pre-flight request before removing the UPE intent from session
1 parent 9b29cc9 commit aa5cc44

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: update
3+
4+
Bail out before payment processing for WooPay's order validation request

includes/class-wc-payment-gateway-wcpay.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,20 @@ public function process_payment( $order_id ) {
688688
);
689689
}
690690

691+
// The request is a preflight check from WooPay.
692+
// phpcs:ignore WordPress.Security.NonceVerification.Missing
693+
if ( ! empty( $_POST['is-woopay-preflight-check'] ) ) {
694+
// Set the order status to "pending payment".
695+
$order->update_status( 'pending' );
696+
697+
// Bail out with success so we don't process the payment now,
698+
// but still let WooPay continue with the payment processing.
699+
return [
700+
'result' => 'success',
701+
'redirect' => '',
702+
];
703+
}
704+
691705
UPE_Payment_Gateway::remove_upe_payment_intent_from_session();
692706

693707
$check_response = $this->check_against_session_processing_order( $order );

tests/unit/test-class-wc-payment-gateway-wcpay-process-payment.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,49 @@ public function test_failed_transaction_rate_limiter_is_limited() {
610610
}
611611
}
612612

613+
/**
614+
* Tests that a draft order is updated to "pending" when the $_POST 'is-woopay-preflight-check` is present.
615+
*/
616+
public function test_draft_order_is_set_to_pending_for_woopay_preflight_check_request() {
617+
$_POST['is-woopay-preflight-check'] = true;
618+
619+
// Arrange: Create an order to test with.
620+
$order_data = [
621+
'status' => 'draft',
622+
'total' => '100',
623+
];
624+
625+
$order = wc_create_order( $order_data );
626+
627+
// Act: process payment.
628+
$result = $this->mock_wcpay_gateway->process_payment( $order->get_id(), false );
629+
630+
// Assert: Order status was updated.
631+
$this->assertEquals( 'pending', $order->get_status() );
632+
}
633+
634+
/**
635+
* Tests that a success response and no redirect is returned when the $_POST 'is-woopay-preflight-check` is present.
636+
*/
637+
public function test_successful_result_no_redirect_for_woopay_preflight_check_request() {
638+
$_POST['is-woopay-preflight-check'] = true;
639+
640+
// Arrange: Create an order to test with.
641+
$order_data = [
642+
'status' => 'draft',
643+
'total' => '100',
644+
];
645+
646+
$order = wc_create_order( $order_data );
647+
648+
// Act: process payment.
649+
$response = $this->mock_wcpay_gateway->process_payment( $order->get_id(), false );
650+
651+
// Assert: No payment was processed.
652+
$this->assertEquals( $response['result'], 'success' );
653+
$this->assertEmpty( $response['redirect'] );
654+
}
655+
613656
public function test_bad_request_exception_thrown() {
614657
$error_message = 'Test error.';
615658
$error_notice = 'We\'re not able to process this request. Please refresh the page and try again.';

tests/unit/test-class-wc-payment-gateway-wcpay.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,32 @@ private function get_partial_mock_for_gateway( array $methods = [] ) {
20612061
->getMock();
20622062
}
20632063

2064+
2065+
/**
2066+
* Tests that no payment is processed when the $_POST 'is-woopay-preflight-check` is present.
2067+
*/
2068+
public function test_no_payment_is_processed_for_woopay_preflight_check_request() {
2069+
$_POST['is-woopay-preflight-check'] = true;
2070+
2071+
// Arrange: Create an order to test with.
2072+
$order_data = [
2073+
'status' => 'draft',
2074+
'total' => '100',
2075+
];
2076+
2077+
$order = wc_create_order( $order_data );
2078+
2079+
$mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'process_payment_for_order' ] );
2080+
2081+
// Assert: No payment was processed.
2082+
$mock_wcpay_gateway
2083+
->expects( $this->never() )
2084+
->method( 'process_payment_for_order' );
2085+
2086+
// Act: process payment.
2087+
$response = $mock_wcpay_gateway->process_payment( $order->get_id() );
2088+
}
2089+
20642090
/**
20652091
* Mocks Fraud_Prevention_Service.
20662092
*

0 commit comments

Comments
 (0)