mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
add Payment Refunded handler
This commit is contained in:
parent
c3dd94520a
commit
34aab23f4e
2 changed files with 92 additions and 0 deletions
|
@ -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),
|
||||
];
|
||||
}
|
||||
];
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PaymentCaptureRefunded implements RequestHandler
|
||||
{
|
||||
|
||||
private $logger;
|
||||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue