Introduce payment capture denied webhook (WIP)

This commit is contained in:
dinamiko 2022-07-18 12:03:52 +02:00
parent abf61a14b8
commit 2b8281e0f4
3 changed files with 83 additions and 2 deletions

View file

@ -20,6 +20,7 @@ use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulationStateEndpoint;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderApproved;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureDenied;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCapturePending;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed;
@ -80,6 +81,7 @@ return array(
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor ),
new VaultCreditCardCreated( $logger, $prefix ),
new PaymentCapturePending( $logger ),
new PaymentCaptureDenied($logger),
);
},

View file

@ -0,0 +1,81 @@
<?php
/**
* Handles the Webhook PAYMENT.CAPTURE.DENIED
*
* @package WooCommerce\PayPalCommerce\Webhooks\Handler
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use Psr\Log\LoggerInterface;
use WP_REST_Request;
use WP_REST_Response;
class PaymentCaptureDenied implements RequestHandler
{
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* PaymentCaptureDenied constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* The event types a handler handles.
*
* @return string[]
*/
public function event_types(): array
{
return array( 'PAYMENT.CAPTURE.DENIED' );
}
/**
* Whether a handler is responsible for a given request or not.
*
* @param \WP_REST_Request $request The request.
*
* @return bool
*/
public function responsible_for_request(WP_REST_Request $request): bool
{
return in_array( $request['event_type'], $this->event_types(), true );
}
/**
* Responsible for handling the request.
*
* @param \WP_REST_Request $request The request.
*
* @return WP_REST_Response
*/
public function handle_request(WP_REST_Request $request): WP_REST_Response
{
$response = array( 'success' => false );
$resource = $request['resource'];
if ( ! is_array( $resource ) ) {
$message = 'Resource data not found in webhook request.';
$this->logger->warning( $message, array( 'request' => $request ) );
$response['message'] = $message;
return new WP_REST_Response( $response );
}
$this->logger->info( (string) wc_print_r( $resource, true ) );
$response['success'] = true;
return new WP_REST_Response( $response );
}
}

View file

@ -74,8 +74,6 @@ class PaymentCapturePending implements RequestHandler {
return new WP_REST_Response( $response );
}
$this->logger->info( (string) wc_print_r( $resource, true ) );
$order_id = isset( $request['resource']['custom_id'] )
? $this->sanitize_custom_id( $request['resource']['custom_id'] )
: 0;