mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
add CANCELLED webhook
This commit is contained in:
parent
c04187338a
commit
21cfd7c091
6 changed files with 110 additions and 14 deletions
|
@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Webhooks;
|
||||||
|
|
||||||
use Inpsyde\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted;
|
use Inpsyde\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted;
|
||||||
use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
|
use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
|
||||||
|
use Inpsyde\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -33,6 +34,7 @@ return [
|
||||||
return [
|
return [
|
||||||
new CheckoutOrderCompleted($logger),
|
new CheckoutOrderCompleted($logger),
|
||||||
new PaymentCaptureRefunded($logger),
|
new PaymentCaptureRefunded($logger),
|
||||||
|
new PaymentCaptureReversed($logger),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
@ -14,14 +14,14 @@ class CheckoutOrderCompleted implements RequestHandler
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventType(): string
|
public function eventTypes(): array
|
||||||
{
|
{
|
||||||
return 'CHECKOUT.ORDER.COMPLETED';
|
return ['CHECKOUT.ORDER.COMPLETED'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||||
{
|
{
|
||||||
return $request['event_type'] === $this->eventType();
|
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
|
|
@ -14,14 +14,14 @@ class PaymentCaptureRefunded implements RequestHandler
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function eventType(): string
|
public function eventTypes(): array
|
||||||
{
|
{
|
||||||
return 'PAYMENT.CAPTURE.REFUNDED';
|
return ['PAYMENT.CAPTURE.REFUNDED'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function responsibleForRequest(\WP_REST_Request $request): bool
|
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||||
{
|
{
|
||||||
return $request['event_type'] === $this->eventType();
|
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||||
|
|
||||||
|
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
class PaymentCaptureReversed implements RequestHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
private $logger;
|
||||||
|
public function __construct(LoggerInterface $logger)
|
||||||
|
{
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function eventTypes(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'PAYMENT.CAPTURE.REVERSED',
|
||||||
|
'PAYMENT.ORDER.CANCELLED',
|
||||||
|
'PAYMENT.CAPTURE.DENIED',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function responsibleForRequest(\WP_REST_Request $request): bool
|
||||||
|
{
|
||||||
|
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
{
|
||||||
|
$response = ['success' => false];
|
||||||
|
$orderId = isset($request['resource']['custom_id']) ? (int) $request['resource']['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);
|
||||||
|
}
|
||||||
|
|
||||||
|
$wcOrder = wc_get_order($orderId);
|
||||||
|
if (! is_a($wcOrder, \WC_Order::class)) {
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \WC_Order $wcOrder
|
||||||
|
*/
|
||||||
|
$response['success'] = (bool) $wcOrder->update_status('cancelled');
|
||||||
|
|
||||||
|
$message = $response['success'] ? sprintf(
|
||||||
|
//translators: %1$s is the order id.
|
||||||
|
__('Order %1$s has been cancelled through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
(string) $wcOrder->get_id()
|
||||||
|
) : sprintf(
|
||||||
|
//translators: %1$s is the order id.
|
||||||
|
__('Failed to cancel order %1$s through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||||
|
(string) $wcOrder->get_id()
|
||||||
|
);
|
||||||
|
$this->logger->log(
|
||||||
|
$response['success'] ? 'info' : 'warning',
|
||||||
|
$message,
|
||||||
|
[
|
||||||
|
'request' => $request,
|
||||||
|
'order' => $wcOrder,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
return rest_ensure_response($response);
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||||
interface RequestHandler
|
interface RequestHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
public function eventType() : string;
|
public function eventTypes() : array;
|
||||||
|
|
||||||
public function responsibleForRequest(\WP_REST_Request $request) : bool;
|
public function responsibleForRequest(\WP_REST_Request $request) : bool;
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ class IncomingWebhookEndpoint
|
||||||
'info',
|
'info',
|
||||||
sprintf(
|
sprintf(
|
||||||
__('Webhook has been handled by %s', 'woocommerce-paypal-commerce-gateway'),
|
__('Webhook has been handled by %s', 'woocommerce-paypal-commerce-gateway'),
|
||||||
$handler->eventType()
|
($handler->eventTypes())?current($handler->eventTypes()):''
|
||||||
),
|
),
|
||||||
[
|
[
|
||||||
'request' => $request,
|
'request' => $request,
|
||||||
|
@ -93,11 +93,10 @@ class IncomingWebhookEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handledEventTypes() : array {
|
public function handledEventTypes() : array {
|
||||||
return array_map(
|
$eventTypes = [];
|
||||||
function(RequestHandler $handler) : string {
|
foreach ($this->handlers as $handler) {
|
||||||
return $handler->eventType();
|
$eventTypes = array_merge($eventTypes, $handler->eventTypes());
|
||||||
},
|
}
|
||||||
$this->handlers
|
return array_unique($eventTypes);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue