Skip to content

Commit

Permalink
Update webhook handler method
Browse files Browse the repository at this point in the history
Make only `event_type` prop required in the event body. This allows
processing events without `data` prop. Handlers for specific event types
still may require additional props, e.g. `data.object` is required for
handling `charge.refund.updated` event.
  • Loading branch information
vbelolapotkov committed Apr 27, 2020
1 parent e2651a1 commit a653bd7
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions includes/admin/class-wc-rest-payments-webhook-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,14 @@ public function handle_webhook( $request ) {

try {
// Extract information about the webhook event.
$event_type = $this->read_rest_property( $body, 'type' );
$event_data = $this->read_rest_property( $body, 'data' );
$event_object = $this->read_rest_property( $event_data, 'object' );
$event_type = $this->read_rest_property( $body, 'type' );

Logger::debug( 'Webhook recieved: ' . $event_type );
Logger::debug( 'Webhook body: ' . var_export( $body, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export

switch ( $event_type ) {
case 'charge.refund.updated':
$this->process_webhook_refund_updated( $event_object );
$this->process_webhook_refund_updated( $body );
break;
case 'account.updated':
$this->account->refresh_account_data();
Expand All @@ -113,12 +111,15 @@ public function handle_webhook( $request ) {
/**
* Process webhook refund updated.
*
* @param array $event_object The event that triggered the webhook.
* @param array $event_body The event that triggered the webhook.
*
* @throws WC_Payments_Rest_Request_Exception Required parameters not found.
* @throws Exception Unable to resolve charge ID to order.
*/
private function process_webhook_refund_updated( $event_object ) {
private function process_webhook_refund_updated( $event_body ) {
$event_data = $this->read_rest_property( $event_body, 'data' );
$event_object = $this->read_rest_property( $event_data, 'object' );

// First, check the reason for the update. We're only interesting in a status of failed.
$status = $this->read_rest_property( $event_object, 'status' );
if ( 'failed' !== $status ) {
Expand Down

0 comments on commit a653bd7

Please sign in to comment.