From c14d46cc32f8a296a3800c1aefc17a20e27ac66f Mon Sep 17 00:00:00 2001 From: David Remer Date: Mon, 20 Jul 2020 12:06:45 +0300 Subject: [PATCH] add PaymentCaptureCompleted Webhook --- modules.local/ppcp-webhooks/services.php | 2 + .../src/Handler/PaymentCaptureCompleted.php | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 modules.local/ppcp-webhooks/src/Handler/PaymentCaptureCompleted.php diff --git a/modules.local/ppcp-webhooks/services.php b/modules.local/ppcp-webhooks/services.php index 4fc05eda0..59241ac2e 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\PaymentCaptureCompleted; use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded; use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed; use Psr\Container\ContainerInterface; @@ -42,6 +43,7 @@ return [ new CheckoutOrderCompleted($logger), new PaymentCaptureRefunded($logger), new PaymentCaptureReversed($logger), + new PaymentCaptureCompleted($logger), ]; } ]; diff --git a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureCompleted.php b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureCompleted.php new file mode 100644 index 000000000..3a17075d1 --- /dev/null +++ b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureCompleted.php @@ -0,0 +1,81 @@ +logger = $logger; + } + + public function eventTypes(): array + { + return ['PAYMENT.CAPTURE.COMPLETED']; + } + + public function responsibleForRequest(\WP_REST_Request $request): bool + { + return in_array($request['event_type'], $this->eventTypes(), true); + } + + // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong + public function handleRequest(\WP_REST_Request $request): \WP_REST_Response + { + $response = ['success' => false]; + $orderId = (int) $request['resource']['custom_id']; + $wcOrder = wc_get_order($orderId); + + if (! is_a($wcOrder, \WC_Order::class)) { + $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); + } + + if ($wcOrder->get_status() !== 'on-hold') { + $response['success'] = true; + return rest_ensure_response($response); + } + $wcOrder->add_order_note( + __('Payment successfully captured.', 'woocommerce-paypal-commerce-gateway') + ); + + $wcOrder->set_status('processing'); + $wcOrder->update_meta_data(WcGateway::CAPTURED_META_KEY, 'true'); + $wcOrder->save(); + $this->logger->log( + 'info', + sprintf( + // translators: %s is the order ID. + __('Order %s has been updated through PayPal', 'woocommerce-paypal-commerce-gateway'), + (string)$wcOrder->get_id() + ), + [ + 'request' => $request, + 'order' => $wcOrder, + ] + ); + $response['success'] = true; + return rest_ensure_response($response); + } + // phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong +}