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;
|
|
|
|
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
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
class WcGatewayModule implements ModuleInterface {
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* Setup the module.
|
|
|
|
*
|
|
|
|
* @return ServiceProviderInterface
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Runs the module.
|
|
|
|
*
|
2020-09-16 10:18:45 +03:00
|
|
|
* @param ContainerInterface|null $container The container.
|
2020-08-28 08:13:45 +03:00
|
|
|
*/
|
2021-07-14 14:58:23 +02:00
|
|
|
public function run( ContainerInterface $container ): void {
|
2020-08-28 08:13:45 +03:00
|
|
|
$this->register_payment_gateways( $container );
|
|
|
|
$this->register_order_functionality( $container );
|
|
|
|
$this->register_columns( $container );
|
|
|
|
$this->register_checkout_paypal_address_preset( $container );
|
2020-08-27 11:08:36 +03:00
|
|
|
|
2020-09-02 09:56:04 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_sections_checkout',
|
2020-09-02 10:56:26 +03:00
|
|
|
function() use ( $container ) {
|
|
|
|
$section_renderer = $container->get( 'wcgateway.settings.sections-renderer' );
|
2020-09-02 09:56:04 +03:00
|
|
|
/**
|
|
|
|
* The Section Renderer.
|
|
|
|
*
|
|
|
|
* @var SectionsRenderer $section_renderer
|
|
|
|
*/
|
|
|
|
$section_renderer->render();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-03-25 13:45:57 +02:00
|
|
|
if ( $container->has( 'wcgateway.url' ) ) {
|
2021-03-25 14:29:56 +02:00
|
|
|
$assets = new SettingsPageAssets(
|
2021-03-25 14:39:13 +02:00
|
|
|
$container->get( 'wcgateway.url' ),
|
2021-04-29 17:40:41 +02:00
|
|
|
$container->get( 'wcgateway.absolute-path' ),
|
|
|
|
$container->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,
|
|
|
|
static function ( $notices ) use ( $container ): array {
|
|
|
|
$notice = $container->get( 'wcgateway.notice.connect' );
|
|
|
|
/**
|
2020-08-28 08:13:45 +03:00
|
|
|
* The Connect Admin Notice object.
|
|
|
|
*
|
2020-08-27 11:08:36 +03:00
|
|
|
* @var ConnectAdminNotice $notice
|
|
|
|
*/
|
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
|
|
|
}
|
2020-08-28 08:13:45 +03:00
|
|
|
$authorize_order_action = $container->get( 'wcgateway.notice.authorize-order-action' );
|
|
|
|
$authorized_message = $authorize_order_action->message();
|
|
|
|
if ( $authorized_message ) {
|
|
|
|
$notices[] = $authorized_message;
|
2020-08-27 11:08:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:09:23 +03:00
|
|
|
$settings_renderer = $container->get( 'wcgateway.settings.render' );
|
|
|
|
/**
|
|
|
|
* The settings renderer.
|
|
|
|
*
|
|
|
|
* @var SettingsRenderer $settings_renderer
|
|
|
|
*/
|
|
|
|
$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',
|
2020-08-27 11:08:36 +03:00
|
|
|
static function () use ( $container ) {
|
|
|
|
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,
|
|
|
|
static function () use ( $container ) {
|
|
|
|
$endpoint = $container->get( 'wcgateway.endpoint.return-url' );
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
);
|
2021-06-10 11:05:29 +02:00
|
|
|
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_email_recipient_customer_on_hold_order',
|
|
|
|
function( $recipient, $order ) {
|
|
|
|
if ( $order->get_payment_method() === PayPalGateway::ID || $order->get_payment_method() === CreditCardGateway::ID ) {
|
|
|
|
$recipient = '';
|
|
|
|
}
|
|
|
|
return $recipient;
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2020-08-27 11:08:36 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* Registers the payment gateways.
|
|
|
|
*
|
2020-09-16 10:18:45 +03:00
|
|
|
* @param ContainerInterface|null $container The container.
|
2020-08-28 08:13:45 +03:00
|
|
|
*/
|
2020-09-16 10:18:45 +03:00
|
|
|
private function register_payment_gateways( ContainerInterface $container = null ) {
|
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 );
|
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 ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
/**
|
2020-08-28 08:13:45 +03:00
|
|
|
* The PayPal Gateway.
|
|
|
|
*
|
2020-08-27 11:08:36 +03:00
|
|
|
* @var PayPalGateway $gateway
|
|
|
|
*/
|
|
|
|
$gateway = $container->get( 'wcgateway.paypal-gateway' );
|
2020-08-28 08:13:45 +03:00
|
|
|
$gateway->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
|
|
|
}
|