Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow webhooks without livemode to be processed. #6646

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-3131-notification-webhooks-not-working
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Allow webhooks without livemode to be received.
16 changes: 16 additions & 0 deletions includes/class-wc-payments-webhook-processing-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ public function process( array $event_body ) {
* @throws Invalid_Webhook_Data_Exception Event mode does not match the gateway mode.
*/
private function is_webhook_mode_mismatch( array $event_body ): bool {
if ( ! $this->has_webhook_property( $event_body, 'livemode' ) ) {
return false;
}

$is_gateway_live_mode = WC_Payments::mode()->is_live();
$is_event_live_mode = $this->read_webhook_property( $event_body, 'livemode' );

Expand Down Expand Up @@ -689,6 +693,18 @@ private function read_webhook_property( $array, $key ) {
return $array[ $key ];
}

/**
* Safely check whether a webhook contains a property.
*
* @param array $array Array to read from.
* @param string $key ID to fetch on.
*
* @return bool
*/
private function has_webhook_property( $array, $key ) {
return isset( $array[ $key ] );
}

/**
* Gets the order related to the event intent id.
*
Expand Down
1 change: 0 additions & 1 deletion includes/core/server/request/class-paginated.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function set_page( int $page ) {
$this->set_param( 'page', $page );
}


/**
* Set page size.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test-class-wc-payments-webhook-processing-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WCPay\Exceptions\Invalid_Payment_Method_Exception;
use WCPay\Exceptions\Invalid_Webhook_Data_Exception;
use WCPay\Exceptions\Rest_Request_Exception;
use WCPay\Logger;

// Need to use WC_Mock_Data_Store.
require_once dirname( __FILE__ ) . '/helpers/class-wc-mock-wc-data-store.php';
Expand Down Expand Up @@ -1339,4 +1340,32 @@ public function test_dispute_funds_reinstated_order_note() {
// Run the test.
$this->webhook_processing_service->process( $this->event_body );
}

/**
* @dataProvider provider_mode_mismatch_detection
*/
public function test_mode_mismatch_detection( $livemode, $expectation ) {
$note = [ 'abc1234' ];

$this->event_body['type'] = 'wcpay.notification';
$this->event_body['id'] = 'evt_XYZ';
$this->event_body['data'] = $note;
if ( ! is_null( $livemode ) ) {
$this->event_body['livemode'] = $livemode;
}

$this->mock_remote_note_service->expects( $expectation )
->method( 'put_note' )
->with( $note );

$this->webhook_processing_service->process( $this->event_body );
}

public function provider_mode_mismatch_detection() {
return [
'Live mode webhook is processed.' => [ true, $this->once() ],
'Test mode is not processed.' => [ false, $this->never() ],
'No mode proceeds' => [ null, $this->once() ],
];
}
}