woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/WCGatewayModule.php

812 lines
26 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-08-28 08:13:45 +03:00
/**
* The Gateway module.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\WcGateway
2020-08-28 08:13:45 +03:00
*/
2020-04-13 22:30:57 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\WcGateway;
2020-04-02 08:38:00 +03:00
use Psr\Log\LoggerInterface;
use Throwable;
2024-02-28 11:14:59 +00:00
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
2023-03-06 17:07:31 +02:00
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
2023-03-21 12:22:53 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Processor\CreditCardOrderInfoHandlingTrait;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
2022-02-14 13:53:37 +02:00
use WC_Order;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
2022-02-14 13:53:37 +02:00
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Onboarding\State;
2022-02-14 13:54:13 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction;
2022-11-29 17:06:17 +04:00
use WooCommerce\PayPalCommerce\WcGateway\Assets\FraudNetAssets;
use WooCommerce\PayPalCommerce\WcGateway\Assets\SettingsPageAssets;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset;
use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\GatewayRepository;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice;
2023-06-14 12:15:20 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Notice\UnsupportedCurrencyAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
2022-08-16 10:20:47 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\HeaderRenderer;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
2020-04-02 08:38:00 +03:00
2020-08-28 08:13:45 +03:00
/**
* Class WcGatewayModule
*/
class WCGatewayModule implements ModuleInterface {
2020-08-27 11:08:36 +03:00
use CreditCardOrderInfoHandlingTrait;
2020-08-28 08:13:45 +03:00
/**
2021-08-30 08:08:41 +02:00
* {@inheritDoc}
2020-08-28 08:13:45 +03:00
*/
2020-08-27 11:08:36 +03:00
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
2020-08-28 08:13:45 +03:00
/**
2021-08-30 08:08:41 +02:00
* {@inheritDoc}
2020-08-28 08:13:45 +03:00
*/
2021-08-30 08:10:43 +02:00
public function run( ContainerInterface $c ): void {
$this->register_payment_gateways( $c );
$this->register_order_functionality( $c );
$this->register_columns( $c );
$this->register_checkout_paypal_address_preset( $c );
2020-08-27 11:08:36 +03:00
2020-09-02 09:56:04 +03:00
add_action(
'woocommerce_sections_checkout',
2021-08-30 08:10:43 +02:00
function() use ( $c ) {
2022-08-16 10:20:47 +03:00
$header_renderer = $c->get( 'wcgateway.settings.header-renderer' );
assert( $header_renderer instanceof HeaderRenderer );
2021-08-30 08:10:43 +02:00
$section_renderer = $c->get( 'wcgateway.settings.sections-renderer' );
assert( $section_renderer instanceof SectionsRenderer );
// phpcs:ignore WordPress.Security.EscapeOutput
2022-08-16 10:20:47 +03:00
echo $header_renderer->render() . $section_renderer->render();
2022-08-10 09:10:38 +02:00
},
20
2020-09-02 09:56:04 +03:00
);
2022-02-14 13:53:37 +02:00
add_action(
'woocommerce_paypal_payments_order_captured',
function ( WC_Order $wc_order, Capture $capture ) use ( $c ) {
2022-02-14 13:53:37 +02:00
$breakdown = $capture->seller_receivable_breakdown();
if ( $breakdown ) {
$wc_order->update_meta_data( PayPalGateway::FEES_META_KEY, $breakdown->to_array() );
$paypal_fee = $breakdown->paypal_fee();
if ( $paypal_fee ) {
2022-10-10 15:17:05 +02:00
$wc_order->update_meta_data( 'PayPal Transaction Fee', (string) $paypal_fee->value() );
}
2022-10-10 15:14:34 +02:00
$wc_order->save_meta_data();
2022-02-14 13:53:37 +02:00
}
$order = $c->get( 'session.handler' )->order();
2024-02-06 17:05:36 +04:00
if ( ! $order ) {
return;
}
$fraud = $capture->fraud_processor_response();
if ( $fraud ) {
$this->handle_fraud( $fraud, $order, $wc_order );
2022-02-14 13:53:37 +02:00
}
$this->handle_three_d_secure( $order, $wc_order );
},
10,
2
);
add_action(
'woocommerce_paypal_payments_order_authorized',
function ( WC_Order $wc_order, Authorization $authorization ) use ( $c ) {
$order = $c->get( 'session.handler' )->order();
2024-02-06 17:05:36 +04:00
if ( ! $order ) {
return;
}
$fraud = $authorization->fraud_processor_response();
if ( $fraud ) {
$this->handle_fraud( $fraud, $order, $wc_order );
2022-02-14 13:53:37 +02:00
}
$this->handle_three_d_secure( $order, $wc_order );
2022-02-14 13:53:37 +02:00
},
10,
2
);
2022-02-14 13:54:13 +02:00
$fees_renderer = $c->get( 'wcgateway.admin.fees-renderer' );
assert( $fees_renderer instanceof FeesRenderer );
add_action(
'woocommerce_admin_order_totals_after_total',
function ( int $order_id ) use ( $fees_renderer ) {
$wc_order = wc_get_order( $order_id );
if ( ! $wc_order instanceof WC_Order ) {
return;
}
/**
* The filter can be used to remove the rows with PayPal fees in WC orders.
*/
if ( ! apply_filters( 'woocommerce_paypal_payments_show_fees_on_order_admin_page', true, $wc_order ) ) {
return;
}
2022-02-14 13:54:13 +02:00
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $fees_renderer->render( $wc_order );
}
);
2021-08-30 08:10:43 +02:00
if ( $c->has( 'wcgateway.url' ) ) {
$settings_status = $c->get( 'wcgateway.settings.status' );
assert( $settings_status instanceof SettingsStatus );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
2021-03-25 14:29:56 +02:00
$assets = new SettingsPageAssets(
2021-08-30 08:10:43 +02:00
$c->get( 'wcgateway.url' ),
$c->get( 'ppcp.asset-version' ),
$c->get( 'wc-subscriptions.helper' ),
$c->get( 'button.client_id_for_admin' ),
2022-10-19 11:38:49 +03:00
$c->get( 'api.shop.currency' ),
$c->get( 'api.shop.country' ),
$c->get( 'onboarding.environment' ),
$settings_status->is_pay_later_button_enabled(),
$settings->has( 'disable_funding' ) ? $settings->get( 'disable_funding' ) : array(),
$c->get( 'wcgateway.settings.funding-sources' ),
$c->get( 'wcgateway.is-ppcp-settings-page' ),
$settings->has( 'dcc_enabled' ) && $settings->get( 'dcc_enabled' ),
$c->get( 'api.endpoint.billing-agreements' )
2021-03-25 14:29:56 +02:00
);
$assets->register_assets();
}
2020-08-27 11:08:36 +03:00
add_filter(
Repository::NOTICES_FILTER,
2021-08-30 08:10:43 +02:00
static function ( $notices ) use ( $c ): array {
$notice = $c->get( 'wcgateway.notice.connect' );
2021-08-31 10:20:34 +03:00
assert( $notice instanceof ConnectAdminNotice );
2020-08-28 08:13:45 +03:00
$connect_message = $notice->connect_message();
if ( $connect_message ) {
$notices[] = $connect_message;
2020-08-27 11:08:36 +03:00
}
2023-06-14 12:15:20 +02:00
$notice = $c->get( 'wcgateway.notice.currency-unsupported' );
assert( $notice instanceof UnsupportedCurrencyAdminNotice );
$unsupported_currency_message = $notice->unsupported_currency_message();
if ( $unsupported_currency_message ) {
$notices[] = $unsupported_currency_message;
}
foreach ( array(
$c->get( 'wcgateway.notice.dcc-without-paypal' ),
$c->get( 'wcgateway.notice.card-button-without-paypal' ),
) as $gateway_without_paypal_notice ) {
assert( $gateway_without_paypal_notice instanceof GatewayWithoutPayPalAdminNotice );
$message = $gateway_without_paypal_notice->message();
if ( $message ) {
$notices[] = $message;
}
}
2021-08-30 08:10:43 +02:00
$authorize_order_action = $c->get( 'wcgateway.notice.authorize-order-action' );
2020-08-28 08:13:45 +03:00
$authorized_message = $authorize_order_action->message();
if ( $authorized_message ) {
$notices[] = $authorized_message;
2020-08-27 11:08:36 +03:00
}
2021-08-30 08:10:43 +02:00
$settings_renderer = $c->get( 'wcgateway.settings.render' );
2021-08-31 10:20:34 +03:00
assert( $settings_renderer instanceof SettingsRenderer );
$messages = $settings_renderer->messages();
$notices = array_merge( $notices, $messages );
2020-08-27 11:08:36 +03:00
return $notices;
}
);
add_action(
2020-09-17 16:50:04 -03:00
'woocommerce_paypal_commerce_gateway_deactivate',
2021-08-30 08:10:43 +02:00
static function () use ( $c ) {
2020-08-27 11:08:36 +03:00
delete_option( Settings::KEY );
delete_option( 'woocommerce_' . PayPalGateway::ID . '_settings' );
delete_option( 'woocommerce_' . CreditCardGateway::ID . '_settings' );
}
);
add_action(
'wc_ajax_' . ReturnUrlEndpoint::ENDPOINT,
2021-08-30 08:10:43 +02:00
static function () use ( $c ) {
$endpoint = $c->get( 'wcgateway.endpoint.return-url' );
2020-08-27 11:08:36 +03:00
/**
2020-08-28 08:13:45 +03:00
* The Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var ReturnUrlEndpoint $endpoint
*/
2020-08-28 08:13:45 +03:00
$endpoint->handle_request();
2020-08-27 11:08:36 +03:00
}
);
add_action(
'wc_ajax_' . RefreshFeatureStatusEndpoint::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'wcgateway.endpoint.refresh-feature-status' );
assert( $endpoint instanceof RefreshFeatureStatusEndpoint );
$endpoint->handle_request();
}
);
add_action(
'woocommerce_paypal_payments_gateway_migrate',
static function () use ( $c ) {
delete_option( 'ppcp-request-ids' );
$settings = $c->get( 'wcgateway.settings' );
2022-02-03 16:30:37 +01:00
assert( $settings instanceof Settings );
try {
2022-03-29 11:37:22 +02:00
if ( $settings->has( '3d_secure_contingency' ) && $settings->get( '3d_secure_contingency' ) === '3D_SECURE' ) {
$settings->set( '3d_secure_contingency', 'SCA_ALWAYS' );
$settings->persist();
}
} catch ( NotFoundException $exception ) {
return;
}
}
);
2023-03-21 12:22:53 +01:00
add_action(
'woocommerce_paypal_payments_gateway_migrate_on_update',
static function() use ( $c ) {
$dcc_status_cache = $c->get( 'dcc.status-cache' );
assert( $dcc_status_cache instanceof Cache );
$pui_status_cache = $c->get( 'pui.status-cache' );
assert( $pui_status_cache instanceof Cache );
$dcc_status_cache->delete( DCCProductStatus::DCC_STATUS_CACHE_KEY );
$pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY );
$settings = $c->get( 'wcgateway.settings' );
$settings->set( 'products_dcc_enabled', false );
$settings->set( 'products_pui_enabled', false );
$settings->persist();
2023-09-11 10:04:38 +01:00
do_action( 'woocommerce_paypal_payments_clear_apm_product_status', $settings );
// Update caches.
$dcc_status = $c->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_status instanceof DCCProductStatus );
$dcc_status->dcc_is_active();
$pui_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' );
assert( $pui_status instanceof PayUponInvoiceProductStatus );
$pui_status->pui_is_active();
2023-03-21 12:22:53 +01:00
}
);
add_action(
'wp_loaded',
2022-03-09 17:22:17 +01:00
function () use ( $c ) {
if ( 'DE' === $c->get( 'api.shop.country' ) ) {
2022-03-09 17:22:17 +01:00
( $c->get( 'wcgateway.pay-upon-invoice' ) )->init();
}
2022-07-05 11:30:20 +02:00
( $c->get( 'wcgateway.oxxo' ) )->init();
2022-11-29 17:06:17 +04:00
$fraudnet_assets = $c->get( 'wcgateway.fraudnet-assets' );
assert( $fraudnet_assets instanceof FraudNetAssets );
$fraudnet_assets->register_assets();
}
);
add_action(
'woocommerce_paypal_payments_check_pui_payment_captured',
function ( int $wc_order_id, string $order_id ) use ( $c ) {
$order_endpoint = $c->get( 'api.endpoint.order' );
$logger = $c->get( 'woocommerce.logger.woocommerce' );
$order = $order_endpoint->order( $order_id );
$order_status = $order->status();
$logger->info( "Checking payment captured webhook for WC order #{$wc_order_id}, PayPal order status: " . $order_status->name() );
$wc_order = wc_get_order( $wc_order_id );
if ( ! is_a( $wc_order, WC_Order::class ) || $wc_order->get_status() !== 'on-hold' ) {
return;
}
if ( $order_status->name() !== OrderStatus::COMPLETED ) {
$message = __(
'Could not process WC order because PAYMENT.CAPTURE.COMPLETED webhook not received.',
'woocommerce-paypal-payments'
);
$logger->error( $message );
$wc_order->update_status( 'failed', $message );
}
},
10,
2
);
2022-07-13 16:11:33 +02:00
add_action(
'woocommerce_order_status_changed',
static function ( int $order_id, string $from, string $to ) use ( $c ) {
$wc_order = wc_get_order( $order_id );
if ( ! $wc_order instanceof WC_Order ) {
return;
}
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
if ( ! $settings->has( 'capture_on_status_change' ) || ! $settings->get( 'capture_on_status_change' ) ) {
return;
}
$gateway_repository = $c->get( 'wcgateway.gateway-repository' );
assert( $gateway_repository instanceof GatewayRepository );
// Only allow to proceed if the payment method is one of our Gateways.
if ( ! $gateway_repository->exists( $wc_order->get_payment_method() ) ) {
return;
}
$intent = strtoupper( (string) $wc_order->get_meta( PayPalGateway::INTENT_META_KEY ) );
$captured = wc_string_to_bool( $wc_order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY ) );
if ( $intent !== 'AUTHORIZE' || $captured ) {
return;
}
/**
* The filter returning the WC order statuses which trigger capturing of payment authorization.
*/
$capture_statuses = apply_filters( 'woocommerce_paypal_payments_auto_capture_statuses', array( 'processing', 'completed' ), $wc_order );
if ( ! in_array( $to, $capture_statuses, true ) ) {
return;
}
$authorized_payment_processor = $c->get( 'wcgateway.processor.authorized-payments' );
assert( $authorized_payment_processor instanceof AuthorizedPaymentsProcessor );
try {
if ( $authorized_payment_processor->capture_authorized_payment( $wc_order ) ) {
return;
}
} catch ( Throwable $error ) {
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$logger->error( "Capture failed. {$error->getMessage()} {$error->getFile()}:{$error->getLine()}" );
}
$wc_order->update_status(
'failed',
__( 'Could not capture the payment.', 'woocommerce-paypal-payments' )
);
},
10,
3
);
add_action(
'woocommerce_paypal_payments_uninstall',
static function () use ( $c ) {
$listener = $c->get( 'wcgateway.settings.listener' );
assert( $listener instanceof SettingsListener );
$listener->listen_for_uninstall();
}
);
if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command(
'pcp settings',
$c->get( 'wcgateway.cli.settings.command' )
);
}
// Clears product status when appropriate.
add_action(
'woocommerce_paypal_payments_clear_apm_product_status',
function( Settings $settings = null ) use ( $c ): void {
// Clear DCC Product status.
$dcc_product_status = $c->get( 'wcgateway.helper.dcc-product-status' );
if ( $dcc_product_status instanceof DCCProductStatus ) {
$dcc_product_status->clear( $settings );
}
// Clear Pay Upon Invoice status.
$pui_product_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' );
if ( $pui_product_status instanceof PayUponInvoiceProductStatus ) {
$pui_product_status->clear( $settings );
}
// Clear Reference Transaction status.
delete_transient( 'ppcp_reference_transaction_enabled' );
}
);
2024-03-27 11:50:40 +00:00
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
add_filter(
'woocommerce_admin_billing_fields',
function ( $fields ) {
global $theorder;
if ( ! apply_filters( 'woocommerce_paypal_payments_order_details_show_paypal_email', true ) ) {
return $fields;
}
if ( ! is_array( $fields ) ) {
return $fields;
}
if ( ! $theorder instanceof WC_Order ) {
return $fields;
2024-03-27 11:50:40 +00:00
}
$email = $theorder->get_meta( PayPalGateway::ORDER_PAYER_EMAIL_META_KEY ) ?: '';
if ( ! $email ) {
return $fields;
2024-03-27 11:50:40 +00:00
}
// Is payment source is paypal exclude all non paypal funding sources.
$payment_source = $theorder->get_meta( PayPalGateway::ORDER_PAYMENT_SOURCE_META_KEY ) ?: '';
$is_paypal_funding_source = ( strpos( $theorder->get_payment_method_title(), '(via PayPal)' ) === false );
if ( $payment_source === 'paypal' && ! $is_paypal_funding_source ) {
return $fields;
}
$fields['paypal_email'] = array(
'label' => __( 'PayPal email address', 'woocommerce-paypal-payments' ),
'value' => $email,
'wrapper_class' => 'form-field-wide',
'custom_attributes' => array( 'disabled' => 'disabled' ),
);
return $fields;
2024-03-27 11:50:40 +00:00
}
);
2020-08-27 11:08:36 +03:00
}
2020-08-28 08:13:45 +03:00
/**
* Registers the payment gateways.
*
2021-10-13 16:28:40 +03:00
* @param ContainerInterface $container The container.
2020-08-28 08:13:45 +03:00
*/
2021-10-13 16:28:40 +03:00
private function register_payment_gateways( ContainerInterface $container ) {
2020-08-27 11:08:36 +03:00
add_filter(
'woocommerce_payment_gateways',
static function ( $methods ) use ( $container ): array {
$paypal_gateway = $container->get( 'wcgateway.paypal-gateway' );
assert( $paypal_gateway instanceof \WC_Payment_Gateway );
$paypal_gateway_enabled = wc_string_to_bool( $paypal_gateway->get_option( 'enabled' ) );
$methods[] = $paypal_gateway;
$onboarding_state = $container->get( 'onboarding.state' );
assert( $onboarding_state instanceof State );
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
$is_our_page = $container->get( 'wcgateway.is-ppcp-settings-page' );
$is_gateways_list_page = $container->get( 'wcgateway.is-wc-gateways-list-page' );
if ( $onboarding_state->current_state() !== State::STATE_ONBOARDED ) {
return $methods;
}
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
assert( $dcc_applies instanceof DccApplies );
$dcc_product_status = $container->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_product_status instanceof DCCProductStatus );
if ( $dcc_applies->for_country_currency() &&
// Show only if allowed in PayPal account, except when on our settings pages.
// Performing the full DCCProductStatus check only when on the gateway list page
// to avoid sending the API requests all the time.
( $is_our_page ||
( $is_gateways_list_page && $dcc_product_status->dcc_is_active() ) ||
( $settings->has( 'products_dcc_enabled' ) && $settings->get( 'products_dcc_enabled' ) )
)
) {
2020-08-27 11:08:36 +03:00
$methods[] = $container->get( 'wcgateway.credit-card-gateway' );
}
if ( $paypal_gateway_enabled && $container->get( 'wcgateway.settings.allow_card_button_gateway' ) ) {
$methods[] = $container->get( 'wcgateway.card-button-gateway' );
}
2022-07-19 09:20:26 +03:00
$pui_product_status = $container->get( 'wcgateway.pay-upon-invoice-product-status' );
assert( $pui_product_status instanceof PayUponInvoiceProductStatus );
$shop_country = $container->get( 'api.shop.country' );
if ( 'DE' === $shop_country &&
( $is_our_page ||
( $is_gateways_list_page && $pui_product_status->pui_is_active() ) ||
( $settings->has( 'products_pui_enabled' ) && $settings->get( 'products_pui_enabled' ) )
)
) {
$methods[] = $container->get( 'wcgateway.pay-upon-invoice-gateway' );
}
if ( 'MX' === $shop_country ) {
2022-08-02 12:11:18 +02:00
$methods[] = $container->get( 'wcgateway.oxxo-gateway' );
}
2022-07-05 11:30:20 +02:00
2020-08-27 11:08:36 +03:00
return (array) $methods;
}
);
add_action(
'woocommerce_settings_save_checkout',
static function () use ( $container ) {
$listener = $container->get( 'wcgateway.settings.listener' );
/**
* The settings listener.
*
* @var SettingsListener $listener
*/
2020-08-27 11:08:36 +03:00
$listener->listen();
}
);
add_action(
'admin_init',
static function () use ( $container ) {
$listener = $container->get( 'wcgateway.settings.listener' );
2023-03-06 17:07:31 +02:00
assert( $listener instanceof SettingsListener );
$listener->listen_for_merchant_id();
2023-03-06 17:07:31 +02:00
try {
$listener->listen_for_vaulting_enabled();
} catch ( RuntimeException $exception ) {
add_action(
'admin_notices',
function () use ( $exception ) {
printf(
'<div class="notice notice-error"><p>%1$s</p><p>%2$s</p></div>',
esc_html__( 'Authentication with PayPal failed: ', 'woocommerce-paypal-payments' ) . esc_attr( $exception->getMessage() ),
wp_kses_post(
__(
'Please verify your API Credentials and try again to connect your PayPal business account. Visit the <a href="https://docs.woocommerce.com/document/woocommerce-paypal-payments/" target="_blank">plugin documentation</a> for more information about the setup.',
'woocommerce-paypal-payments'
)
)
);
}
);
}
}
);
2020-08-27 11:08:36 +03:00
add_filter(
'woocommerce_form_field',
static function ( $field, $key, $args, $value ) use ( $container ) {
$renderer = $container->get( 'wcgateway.settings.render' );
/**
2020-08-28 08:13:45 +03:00
* The Settings Renderer object.
*
2020-08-27 11:08:36 +03:00
* @var SettingsRenderer $renderer
*/
2020-08-28 08:13:45 +03:00
$field = $renderer->render_multiselect( $field, $key, $args, $value );
$field = $renderer->render_password( $field, $key, $args, $value );
$field = $renderer->render_heading( $field, $key, $args, $value );
2021-09-16 12:41:08 +03:00
$field = $renderer->render_table( $field, $key, $args, $value );
$field = $renderer->render_html( $field, $key, $args, $value );
2020-08-27 11:08:36 +03:00
return $field;
},
10,
4
);
add_filter(
'woocommerce_available_payment_gateways',
static function ( $methods ) use ( $container ): array {
$disabler = $container->get( 'wcgateway.disabler' );
/**
2020-08-28 08:13:45 +03:00
* The Gateay disabler.
*
2020-08-27 11:08:36 +03:00
* @var DisableGateways $disabler
*/
return $disabler->handler( (array) $methods );
}
);
}
2020-08-28 08:13:45 +03:00
/**
* Registers the authorize order functionality.
*
* @param ContainerInterface $container The container.
*/
private function register_order_functionality( ContainerInterface $container ) {
2020-08-27 11:08:36 +03:00
add_filter(
'woocommerce_order_actions',
static function ( $order_actions ) use ( $container ): array {
global $theorder;
2022-02-14 13:53:37 +02:00
if ( ! is_a( $theorder, WC_Order::class ) ) {
return $order_actions;
}
2024-02-26 09:20:30 +00:00
$render_reauthorize = $container->get( 'wcgateway.admin.render-reauthorize-action' );
$render_authorize = $container->get( 'wcgateway.admin.render-authorize-action' );
/**
* Renders the authorize action in the select field.
*
* @var RenderAuthorizeAction $render
*/
2024-02-26 09:20:30 +00:00
return $render_reauthorize->render(
$render_authorize->render( $order_actions, $theorder ),
$theorder
);
2020-08-27 11:08:36 +03:00
}
);
add_action(
'woocommerce_order_action_ppcp_authorize_order',
2022-02-14 13:53:37 +02:00
static function ( WC_Order $wc_order ) use ( $container ) {
2020-08-27 11:08:36 +03:00
/**
* The authorized payments processor.
2020-08-28 08:13:45 +03:00
*
* @var AuthorizedPaymentsProcessor $authorized_payments_processor
2020-08-27 11:08:36 +03:00
*/
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$authorized_payments_processor->capture_authorized_payment( $wc_order );
2020-08-27 11:08:36 +03:00
}
);
2024-02-27 12:00:09 +00:00
add_action(
'woocommerce_order_action_ppcp_reauthorize_order',
static function ( WC_Order $wc_order ) use ( $container ) {
2024-02-28 11:14:59 +00:00
$admin_notices = $container->get( 'admin-notices.repository' );
assert( $admin_notices instanceof Repository );
2024-02-27 12:00:09 +00:00
/**
* The authorized payments processor.
*
* @var AuthorizedPaymentsProcessor $authorized_payments_processor
*/
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
2024-02-28 11:14:59 +00:00
if ( $authorized_payments_processor->reauthorize_payment( $wc_order ) !== AuthorizedPaymentsProcessor::SUCCESSFUL ) {
$message = sprintf(
'%1$s %2$s',
esc_html__( 'Reauthorization with PayPal failed: ', 'woocommerce-paypal-payments' ),
$authorized_payments_processor->reauthorization_failure_reason() ?: ''
);
$admin_notices->persist( new Message( $message, 'error' ) );
} else {
$admin_notices->persist( new Message( 'Payment reauthorized.', 'info' ) );
2024-03-04 15:20:35 +00:00
$wc_order->add_order_note(
__( 'Payment reauthorized.', 'woocommerce-paypal-payments' )
);
2024-02-28 11:14:59 +00:00
}
2024-02-27 12:00:09 +00:00
}
);
2020-08-27 11:08:36 +03:00
}
2020-08-28 08:13:45 +03:00
/**
* Registers the additional columns on the order list page.
*
* @param ContainerInterface $container The container.
*/
private function register_columns( ContainerInterface $container ) {
2020-08-27 11:08:36 +03:00
add_action(
'woocommerce_order_actions_start',
2020-08-28 08:13:45 +03:00
static function ( $wc_order_id ) use ( $container ) {
2020-08-27 11:08:36 +03:00
/**
2020-08-28 08:13:45 +03:00
* The Payment Status Order Detail.
*
2020-08-27 11:08:36 +03:00
* @var PaymentStatusOrderDetail $class
*/
$class = $container->get( 'wcgateway.admin.order-payment-status' );
2020-08-28 08:13:45 +03:00
$class->render( intval( $wc_order_id ) );
2020-08-27 11:08:36 +03:00
}
);
add_filter(
'manage_edit-shop_order_columns',
static function ( $columns ) use ( $container ) {
/**
2020-08-28 08:13:45 +03:00
* The Order Table Payment Status object.
*
* @var OrderTablePaymentStatusColumn $payment_status_column
2020-08-27 11:08:36 +03:00
*/
2020-08-28 08:13:45 +03:00
$payment_status_column = $container->get( 'wcgateway.admin.orders-payment-status-column' );
return $payment_status_column->register( $columns );
2020-08-27 11:08:36 +03:00
}
);
add_action(
'manage_shop_order_posts_custom_column',
2020-08-28 08:13:45 +03:00
static function ( $column, $wc_order_id ) use ( $container ) {
2020-08-27 11:08:36 +03:00
/**
2020-08-28 08:13:45 +03:00
* The column object.
*
* @var OrderTablePaymentStatusColumn $payment_status_column
2020-08-27 11:08:36 +03:00
*/
2020-08-28 08:13:45 +03:00
$payment_status_column = $container->get( 'wcgateway.admin.orders-payment-status-column' );
$payment_status_column->render( (string) $column, intval( $wc_order_id ) );
2020-08-27 11:08:36 +03:00
},
10,
2
);
}
2020-08-28 08:13:45 +03:00
/**
* Registers the PayPal Address preset to overwrite Shipping in checkout.
*
* @param ContainerInterface $container The container.
*/
2020-09-11 13:38:02 +03:00
private function register_checkout_paypal_address_preset( ContainerInterface $container ) {
2020-08-27 11:08:36 +03:00
add_filter(
'woocommerce_checkout_get_value',
static function ( ...$args ) use ( $container ) {
2020-08-28 08:13:45 +03:00
/**
* Its important to not instantiate the service too early as it
* depends on SessionHandler and WooCommerce Session.
*/
2020-08-27 11:08:36 +03:00
/**
2020-08-28 08:13:45 +03:00
* The CheckoutPayPalAddressPreset object.
*
2020-08-27 11:08:36 +03:00
* @var CheckoutPayPalAddressPreset $service
*/
$service = $container->get( 'wcgateway.checkout.address-preset' );
2020-08-28 08:13:45 +03:00
return $service->filter_checkout_field( ...$args );
2020-08-27 11:08:36 +03:00
},
10,
2
);
}
2020-09-16 10:18:45 +03:00
/**
* Returns the key for the module.
*
* @return string|void
*/
public function getKey() {
}
2020-04-06 11:16:18 +03:00
}