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 Dhii\Container\ServiceProvider;
|
|
|
|
use Dhii\Modular\Module\ModuleInterface;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
|
2020-09-30 08:18:17 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction;
|
2021-03-25 10:21:28 +02:00
|
|
|
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\Gateway\CreditCardGateway;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
|
2021-08-31 10:20:11 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Notice\DccWithoutPayPalAdminNotice;
|
2021-10-14 15:45:57 +02:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
2020-09-16 10:00:28 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
|
2020-04-02 08:38:00 +03:00
|
|
|
use Interop\Container\ServiceProviderInterface;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* Class WcGatewayModule
|
|
|
|
*/
|
2021-09-13 16:33:41 +02:00
|
|
|
class WCGatewayModule implements ModuleInterface {
|
2020-08-27 11:08:36 +03:00
|
|
|
|
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 ) {
|
|
|
|
$section_renderer = $c->get( 'wcgateway.settings.sections-renderer' );
|
2020-09-02 09:56:04 +03:00
|
|
|
/**
|
|
|
|
* The Section Renderer.
|
|
|
|
*
|
|
|
|
* @var SectionsRenderer $section_renderer
|
|
|
|
*/
|
|
|
|
$section_renderer->render();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-08-30 08:10:43 +02:00
|
|
|
if ( $c->has( 'wcgateway.url' ) ) {
|
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( 'wcgateway.absolute-path' ),
|
|
|
|
$c->get( 'api.bearer' )
|
2021-03-25 14:29:56 +02:00
|
|
|
);
|
2021-03-25 10:21:28 +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
|
|
|
}
|
2021-08-31 10:20:11 +03:00
|
|
|
|
2021-09-30 12:52:15 +02:00
|
|
|
$dcc_without_paypal_notice = $c->get( 'wcgateway.notice.dcc-without-paypal' );
|
2021-08-31 10:20:11 +03:00
|
|
|
assert( $dcc_without_paypal_notice instanceof DccWithoutPayPalAdminNotice );
|
|
|
|
$dcc_without_paypal_message = $dcc_without_paypal_notice->message();
|
|
|
|
if ( $dcc_without_paypal_message ) {
|
|
|
|
$notices[] = $dcc_without_paypal_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 );
|
2020-10-02 12:09:23 +03:00
|
|
|
$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( PayPalRequestIdRepository::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
|
|
|
}
|
|
|
|
);
|
2022-02-03 16:23:23 +01:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_paypal_payments_gateway_migrate',
|
|
|
|
static function () use ( $c ) {
|
|
|
|
$settings = $c->get( 'wcgateway.settings' );
|
|
|
|
if ( $settings->get( '3d_secure_contingency' ) === '3D_SECURE' ) {
|
|
|
|
$settings->set( '3d_secure_contingency', 'SCA_ALWAYS' );
|
|
|
|
$settings->persist();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
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 {
|
2020-08-28 08:13:45 +03:00
|
|
|
$methods[] = $container->get( 'wcgateway.paypal-gateway' );
|
|
|
|
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
|
2020-09-02 09:55:20 +03:00
|
|
|
|
2020-08-27 11:08:36 +03:00
|
|
|
/**
|
2020-08-28 08:13:45 +03:00
|
|
|
* The DCC Applies object.
|
|
|
|
*
|
|
|
|
* @var DccApplies $dcc_applies
|
2020-08-27 11:08:36 +03:00
|
|
|
*/
|
2021-08-27 13:50:31 +03:00
|
|
|
if ( $dcc_applies->for_country_currency() ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
$methods[] = $container->get( 'wcgateway.credit-card-gateway' );
|
|
|
|
}
|
|
|
|
return (array) $methods;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_settings_save_checkout',
|
|
|
|
static function () use ( $container ) {
|
|
|
|
$listener = $container->get( 'wcgateway.settings.listener' );
|
2020-09-16 10:00:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The settings listener.
|
|
|
|
*
|
|
|
|
* @var SettingsListener $listener
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
$listener->listen();
|
|
|
|
}
|
|
|
|
);
|
2020-09-16 10:00:28 +03:00
|
|
|
add_action(
|
|
|
|
'admin_init',
|
|
|
|
static function () use ( $container ) {
|
|
|
|
$listener = $container->get( 'wcgateway.settings.listener' );
|
|
|
|
/**
|
|
|
|
* The settings listener.
|
|
|
|
*
|
|
|
|
* @var SettingsListener $listener
|
|
|
|
*/
|
|
|
|
$listener->listen_for_merchant_id();
|
2021-03-19 17:37:01 +01:00
|
|
|
$listener->listen_for_vaulting_enabled();
|
2020-09-16 10:00:28 +03:00
|
|
|
}
|
|
|
|
);
|
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_text_input( $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 );
|
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',
|
2020-09-30 08:18:17 +03:00
|
|
|
static function ( $order_actions ) use ( $container ): array {
|
|
|
|
global $theorder;
|
|
|
|
|
|
|
|
if ( ! is_a( $theorder, \WC_Order::class ) ) {
|
|
|
|
return $order_actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
$render = $container->get( 'wcgateway.admin.render-authorize-action' );
|
|
|
|
/**
|
|
|
|
* Renders the authorize action in the select field.
|
|
|
|
*
|
|
|
|
* @var RenderAuthorizeAction $render
|
|
|
|
*/
|
|
|
|
return $render->render( $order_actions, $theorder );
|
2020-08-27 11:08:36 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_order_action_ppcp_authorize_order',
|
2020-08-28 08:13:45 +03:00
|
|
|
static function ( \WC_Order $wc_order ) use ( $container ) {
|
2021-10-14 15:45:57 +02:00
|
|
|
|
2020-08-27 11:08:36 +03:00
|
|
|
/**
|
2021-10-14 15:45:57 +02:00
|
|
|
* The authorized payments processor.
|
2020-08-28 08:13:45 +03:00
|
|
|
*
|
2021-10-14 15:45:57 +02:00
|
|
|
* @var AuthorizedPaymentsProcessor $authorized_payments_processor
|
2020-08-27 11:08:36 +03:00
|
|
|
*/
|
2021-10-14 15:45:57 +02: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
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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( $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
|
|
|
}
|