2020-04-02 08:38:00 +03:00
|
|
|
<?php
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* The Gateway module.
|
|
|
|
*
|
|
|
|
* @package Inpsyde\PayPalCommerce\WcGateway
|
|
|
|
*/
|
2020-04-13 22:30:57 +03:00
|
|
|
|
2020-04-02 08:38:00 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Inpsyde\PayPalCommerce\WcGateway;
|
|
|
|
|
|
|
|
use Dhii\Container\ServiceProvider;
|
|
|
|
use Dhii\Modular\Module\ModuleInterface;
|
2020-04-28 11:42:23 +03:00
|
|
|
use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
|
2020-08-18 11:48:56 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
|
2020-08-20 07:22:02 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
2020-04-23 11:19:09 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
|
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
|
2020-08-14 17:53:39 +02:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset;
|
2020-04-02 08:38:00 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
|
2020-08-27 10:58:08 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
|
2020-08-18 12:53:51 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
2020-08-18 08:46:18 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
2020-04-10 12:36:42 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
|
2020-07-10 08:39:58 +03:00
|
|
|
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
2020-07-02 09:37:07 +03:00
|
|
|
use Inpsyde\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.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container The container.
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
public function run( ContainerInterface $container ) {
|
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 );
|
|
|
|
$this->ajax_gateway_enabler( $container );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return $notices;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
add_action(
|
|
|
|
'woocommerce-paypal-commerce-gateway.deactivate',
|
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* Adds the functionality to listen to the ajax enable gateway switch.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container The container.
|
|
|
|
*/
|
|
|
|
private function ajax_gateway_enabler( ContainerInterface $container ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
add_action(
|
|
|
|
'wp_ajax_woocommerce_toggle_gateway_enabled',
|
|
|
|
static function () use ( $container ) {
|
|
|
|
if (
|
|
|
|
! current_user_can( 'manage_woocommerce' )
|
|
|
|
|| ! check_ajax_referer(
|
|
|
|
'woocommerce-toggle-payment-gateway-enabled',
|
|
|
|
'security'
|
|
|
|
)
|
|
|
|
|| ! isset( $_POST['gateway_id'] )
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-28 08:13:45 +03:00
|
|
|
* The settings.
|
|
|
|
*
|
2020-08-27 11:08:36 +03:00
|
|
|
* @var Settings $settings
|
|
|
|
*/
|
|
|
|
$settings = $container->get( 'wcgateway.settings' );
|
2020-08-28 08:13:45 +03:00
|
|
|
$key = PayPalGateway::ID === $_POST['gateway_id'] ? 'enabled' : '';
|
|
|
|
if ( CreditCardGateway::ID === $_POST['gateway_id'] ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
$key = 'dcc_gateway_enabled';
|
|
|
|
}
|
|
|
|
if ( ! $key ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$enabled = $settings->has( $key ) ? $settings->get( $key ) : false;
|
|
|
|
if ( ! $enabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$settings->set( $key, false );
|
|
|
|
$settings->persist();
|
|
|
|
},
|
|
|
|
9
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* Registers the payment gateways.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container The container.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
$screen = get_current_screen();
|
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
|
|
|
*/
|
2020-09-02 09:55:20 +03:00
|
|
|
if ( $screen->id !== 'woocommerce_page_wc-settings' && $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' );
|
|
|
|
$listener->listen();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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-08-28 08:13:45 +03:00
|
|
|
static function ( $order_actions ): array {
|
|
|
|
$order_actions['ppcp_authorize_order'] = __(
|
2020-08-27 11:08:36 +03:00
|
|
|
'Capture authorized PayPal payment',
|
2020-09-01 11:40:13 +03:00
|
|
|
'paypal-for-woocommerce'
|
2020-08-27 11:08:36 +03:00
|
|
|
);
|
2020-08-28 08:13:45 +03:00
|
|
|
return $order_actions;
|
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.
|
|
|
|
*/
|
|
|
|
private function register_checkout_paypal_address_preset( ContainerInterface $container ): void {
|
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-04-06 11:16:18 +03:00
|
|
|
}
|