add PaymentCaptureCompleted Webhook

This commit is contained in:
David Remer 2020-07-20 12:06:45 +03:00
parent 9d974be95d
commit c14d46cc32
2 changed files with 83 additions and 0 deletions

View file

@ -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),
];
}
];

View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Psr\Log\LoggerInterface;
class PaymentCaptureCompleted implements RequestHandler
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->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
}