diff --git a/modules.local/ppcp-webhooks/services.php b/modules.local/ppcp-webhooks/services.php index 8ea0f2e3e..033d34d40 100644 --- a/modules.local/ppcp-webhooks/services.php +++ b/modules.local/ppcp-webhooks/services.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Inpsyde\PayPalCommerce\Webhooks; use Inpsyde\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted; +use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded; use Psr\Container\ContainerInterface; return [ @@ -29,6 +30,7 @@ return [ $logger = $container->get('woocommerce.logger.woocommerce'); return [ new CheckoutOrderCompleted($logger), + new PaymentCaptureRefunded($logger), ]; } ]; diff --git a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php new file mode 100644 index 000000000..b454ef9b2 --- /dev/null +++ b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php @@ -0,0 +1,90 @@ +logger = $logger; + } + + public function eventType(): string + { + return 'PAYMENT.CAPTURE.REFUNDED'; + } + + public function responsibleForRequest(\WP_REST_Request $request): bool + { + return $request['event_type'] === $this->eventType(); + } + + public function handleRequest(\WP_REST_Request $request): \WP_REST_Response + { + $response = ['success' => false]; + $orderId = isset($request['custom_id']) ? (int) $request['custom_id'] : 0; + if (! $orderId) { + $message = sprintf( + // translators: %s is the PayPal webhook Id. + __('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'), + isset($request['id']) ? $request['id'] : '' + ); + $this->logger->log( + 'warning', + $message, + [ + 'request' => $request, + ] + ); + $response['message'] = $message; + return rest_ensure_response($response); + } + + $args = [ + 'post__in' => [$orderId], + 'limit' => -1, + ]; + $wcOrders = wc_get_orders($args); + if (! $wcOrders) { + $message = sprintf( + // translators: %s is the PayPal refund Id. + __('Order for PayPal refund %s not found.', 'woocommerce-paypal-commerce-gateway'), + isset($request['resource']['id']) ? $request['resource']['id'] : '' + ); + $this->logger->log( + 'warning', + $message, + [ + 'request' => $request, + ] + ); + $response['message'] = $message; + return rest_ensure_response($response); + } + + foreach ($wcOrders as $wcOrder) { + /** + * @var \WC_Product $wcOrder + */ + $wcOrder->update_status( + 'refunded', + __('Payment Refunded.', 'woocommerce-paypal-gateway') + ); + $this->logger->log( + 'info', + __('Order ' . $wcOrder->get_id() . ' has been updated through PayPal' , 'woocommerce-paypal-gateway'), + [ + 'request' => $request, + 'order' => $wcOrder, + ] + ); + } + $response['success'] = true; + return rest_ensure_response($response); + } +} \ No newline at end of file