Use gateway supports to check if refunds are allowed

This commit is contained in:
Daniel Hüsken 2024-11-04 12:24:09 +01:00
parent 19fea3c81e
commit cfe61421f3
No known key found for this signature in database
GPG key ID: 9F732DA37FA709E8
4 changed files with 15 additions and 53 deletions

View file

@ -185,18 +185,6 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
2 2
); );
add_filter(
'woocommerce_paypal_payments_allowed_refund_payment_methods',
function( array $payment_methods ) use ( $c ): array {
$local_payment_methods = $c->get( 'ppcp-local-apms.payment-methods' );
foreach ( $local_payment_methods as $payment_method ) {
$payment_methods[] = $payment_method['id'];
}
return $payment_methods;
}
);
return true; return true;
} }

View file

@ -520,17 +520,10 @@ return array(
$container->get( 'api.endpoint.order' ), $container->get( 'api.endpoint.order' ),
$container->get( 'api.endpoint.payments' ), $container->get( 'api.endpoint.payments' ),
$container->get( 'wcgateway.helper.refund-fees-updater' ), $container->get( 'wcgateway.helper.refund-fees-updater' ),
$container->get( 'wcgateway.allowed_refund_payment_methods' ),
$container->get( 'api.prefix' ), $container->get( 'api.prefix' ),
$container->get( 'woocommerce.logger.woocommerce' ) $container->get( 'woocommerce.logger.woocommerce' )
); );
}, },
'wcgateway.allowed_refund_payment_methods' => static function ( ContainerInterface $container ): array {
return apply_filters(
'woocommerce_paypal_payments_allowed_refund_payment_methods',
array( PayPalGateway::ID, CreditCardGateway::ID, CardButtonGateway::ID, PayUponInvoiceGateway::ID, GooglePayGateway::ID, ApplePayGateway::ID )
);
},
'wcgateway.processor.authorized-payments' => static function ( ContainerInterface $container ): AuthorizedPaymentsProcessor { 'wcgateway.processor.authorized-payments' => static function ( ContainerInterface $container ): AuthorizedPaymentsProcessor {
$order_endpoint = $container->get( 'api.endpoint.order' ); $order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' ); $payments_endpoint = $container->get( 'api.endpoint.payments' );
@ -1933,8 +1926,7 @@ return array(
$container->get( 'wcgateway.url' ), $container->get( 'wcgateway.url' ),
$container->get( 'ppcp.asset-version' ), $container->get( 'ppcp.asset-version' ),
$container->get( 'api.endpoint.order' ), $container->get( 'api.endpoint.order' ),
$container->get( 'wcgateway.processor.refunds' ), $container->get( 'wcgateway.processor.refunds' )
$container->get( 'wcgateway.allowed_refund_payment_methods' )
); );
}, },
'wcgateway.void-button.endpoint' => function( ContainerInterface $container ) : VoidOrderEndpoint { 'wcgateway.void-button.endpoint' => function( ContainerInterface $container ) : VoidOrderEndpoint {

View file

@ -51,13 +51,6 @@ class VoidButtonAssets {
*/ */
private $refund_processor; private $refund_processor;
/**
* The methods that can be refunded.
*
* @var array
*/
private $allowed_refund_payment_methods;
/** /**
* VoidButtonAssets constructor. * VoidButtonAssets constructor.
* *
@ -65,20 +58,17 @@ class VoidButtonAssets {
* @param string $version The assets version. * @param string $version The assets version.
* @param OrderEndpoint $order_endpoint The order endpoint. * @param OrderEndpoint $order_endpoint The order endpoint.
* @param RefundProcessor $refund_processor The Refund Processor. * @param RefundProcessor $refund_processor The Refund Processor.
* @param array $allowed_refund_payment_methods The methods that can be refunded.
*/ */
public function __construct( public function __construct(
string $module_url, string $module_url,
string $version, string $version,
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
RefundProcessor $refund_processor, RefundProcessor $refund_processor
array $allowed_refund_payment_methods
) { ) {
$this->module_url = $module_url; $this->module_url = $module_url;
$this->version = $version; $this->version = $version;
$this->order_endpoint = $order_endpoint; $this->order_endpoint = $order_endpoint;
$this->refund_processor = $refund_processor; $this->refund_processor = $refund_processor;
$this->allowed_refund_payment_methods = $allowed_refund_payment_methods;
} }
/** /**
@ -103,7 +93,8 @@ class VoidButtonAssets {
return false; return false;
} }
if ( ! in_array( $theorder->get_payment_method(), $this->allowed_refund_payment_methods, true ) ) { $payment_gateways = WC()->payment_gateways()->payment_gateways();
if ( ! isset( $payment_gateways[ $theorder->get_payment_method() ] ) || ! $payment_gateways[ $theorder->get_payment_method() ]->supports( 'refunds' ) ) {
return false; return false;
} }

View file

@ -72,20 +72,12 @@ class RefundProcessor {
*/ */
private $refund_fees_updater; private $refund_fees_updater;
/**
* The methods that can be refunded.
*
* @var array
*/
private $allowed_refund_payment_methods;
/** /**
* RefundProcessor constructor. * RefundProcessor constructor.
* *
* @param OrderEndpoint $order_endpoint The order endpoint. * @param OrderEndpoint $order_endpoint The order endpoint.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint. * @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param RefundFeesUpdater $refund_fees_updater The refund fees updater. * @param RefundFeesUpdater $refund_fees_updater The refund fees updater.
* @param array $allowed_refund_payment_methods The methods that can be refunded.
* @param string $prefix The prefix. * @param string $prefix The prefix.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
*/ */
@ -93,7 +85,6 @@ class RefundProcessor {
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_endpoint, PaymentsEndpoint $payments_endpoint,
RefundFeesUpdater $refund_fees_updater, RefundFeesUpdater $refund_fees_updater,
array $allowed_refund_payment_methods,
string $prefix, string $prefix,
LoggerInterface $logger LoggerInterface $logger
) { ) {
@ -101,7 +92,6 @@ class RefundProcessor {
$this->order_endpoint = $order_endpoint; $this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint; $this->payments_endpoint = $payments_endpoint;
$this->refund_fees_updater = $refund_fees_updater; $this->refund_fees_updater = $refund_fees_updater;
$this->allowed_refund_payment_methods = $allowed_refund_payment_methods;
$this->prefix = $prefix; $this->prefix = $prefix;
$this->logger = $logger; $this->logger = $logger;
} }
@ -119,7 +109,8 @@ class RefundProcessor {
*/ */
public function process( WC_Order $wc_order, float $amount = null, string $reason = '' ) : bool { public function process( WC_Order $wc_order, float $amount = null, string $reason = '' ) : bool {
try { try {
if ( ! in_array( $wc_order->get_payment_method(), $this->allowed_refund_payment_methods, true ) ) { $payment_gateways = WC()->payment_gateways()->payment_gateways();
if ( ! isset( $payment_gateways[ $wc_order->get_payment_method() ] ) || ! $payment_gateways[ $wc_order->get_payment_method() ]->supports( 'refunds' ) ) {
return true; return true;
} }