Merge branch 'pcp-50-settings-in-one-gateway'

This commit is contained in:
David Remer 2020-09-03 12:10:52 +03:00
commit 840b9cd9f0
15 changed files with 442 additions and 139 deletions

View file

@ -11,7 +11,7 @@ class Renderer {
} }
renderButtons(wrapper, contextConfig) { renderButtons(wrapper, contextConfig) {
if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper)) { if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper) || 'undefined' === typeof paypal.Buttons ) {
return; return;
} }

View file

@ -60,8 +60,7 @@ return array(
} }
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$paypal_disabled = ! $settings->has( 'enabled' ) || ! $settings->get( 'enabled' ); $paypal_disabled = ! $settings->has( 'enabled' ) || ! $settings->get( 'enabled' );
$credit_card_disabled = ! $settings->has( 'dcc_gateway_enabled' ) || ! $settings->get( 'dcc_gateway_enabled' ); if ( $paypal_disabled ) {
if ( $paypal_disabled && $credit_card_disabled ) {
return new DisabledSmartButton(); return new DisabledSmartButton();
} }
$payee_repository = $container->get( 'api.repository.payee' ); $payee_repository = $container->get( 'api.repository.payee' );

View file

@ -165,8 +165,8 @@ class SmartButton implements SmartButtonInterface {
} }
if ( if (
$this->settings->has( 'dcc_gateway_enabled' ) $this->settings->has( 'dcc_enabled' )
&& $this->settings->get( 'dcc_gateway_enabled' ) && $this->settings->get( 'dcc_enabled' )
&& ! $this->session_handler->order() && ! $this->session_handler->order()
) { ) {
add_action( add_action(
@ -312,25 +312,39 @@ class SmartButton implements SmartButtonInterface {
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) { if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
return false; return false;
} }
wp_enqueue_style(
'ppcp-hosted-fields',
$this->module_url . '/assets/css/hosted-fields.css',
array(),
1
);
wp_enqueue_script(
'ppcp-smart-button',
$this->module_url . '/assets/js/button.js',
array( 'jquery' ),
1,
true
);
wp_localize_script( if ( $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
'ppcp-smart-button', wp_enqueue_style(
'PayPalCommerceGateway', 'ppcp-hosted-fields',
$this->localize_script() $this->module_url . '/assets/css/hosted-fields.css',
); array(),
1
);
}
$load_script = false;
if ( is_checkout() && $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
$load_script = true;
}
if ( $this->load_button_component() ) {
$load_script = true;
}
if ( $load_script ) {
wp_enqueue_script(
'ppcp-smart-button',
$this->module_url . '/assets/js/button.js',
array( 'jquery' ),
1,
true
);
wp_localize_script(
'ppcp-smart-button',
'PayPalCommerceGateway',
$this->localize_script()
);
}
return true; return true;
} }
@ -732,7 +746,11 @@ class SmartButton implements SmartButtonInterface {
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found. * @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found.
*/ */
private function components(): array { private function components(): array {
$components = array( 'buttons' ); $components = array();
if ( $this->load_button_component() ) {
$components[] = 'buttons';
}
if ( $this->messages_apply->for_country() ) { if ( $this->messages_apply->for_country() ) {
$components[] = 'messages'; $components[] = 'messages';
} }
@ -742,6 +760,45 @@ class SmartButton implements SmartButtonInterface {
return $components; return $components;
} }
/**
* Determines whether the button component should be loaded.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found.
*/
private function load_button_component() : bool {
$load_buttons = false;
if (
$this->context() === 'checkout'
&& $this->settings->has( 'button_enabled' )
&& $this->settings->get( 'button_enabled' )
) {
$load_buttons = true;
}
if (
$this->context() === 'product'
&& $this->settings->has( 'button_product_enabled' )
&& $this->settings->get( 'button_product_enabled' )
) {
$load_buttons = true;
}
if (
$this->settings->has( 'button_mini-cart_enabled' )
&& $this->settings->get( 'button_mini-cart_enabled' )
) {
$load_buttons = true;
}
if (
$this->context() === 'cart'
&& $this->settings->has( 'button_cart_enabled' )
&& $this->settings->get( 'button_cart_enabled' )
) {
$load_buttons = true;
}
return $load_buttons;
}
/** /**
* The current context. * The current context.
* *
@ -772,7 +829,7 @@ class SmartButton implements SmartButtonInterface {
return false; return false;
} }
$keys = array( $keys = array(
'dcc_gateway_enabled' => 'is_checkout', 'dcc_enabled' => 'is_checkout',
); );
foreach ( $keys as $key => $callback ) { foreach ( $keys as $key => $callback ) {
if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) { if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) {
@ -811,6 +868,6 @@ class SmartButton implements SmartButtonInterface {
if ( is_bool( $value ) ) { if ( is_bool( $value ) ) {
$value = $value ? 'true' : 'false'; $value = $value ? 'true' : 'false';
} }
return $value; return (string) $value;
} }
} }

View file

@ -12,7 +12,6 @@ const groupToggle = (selector, group) => {
'change', 'change',
(event) => { (event) => {
if (! event.target.checked) { if (! event.target.checked) {
group.forEach( (elementToHide) => { group.forEach( (elementToHide) => {
document.querySelector(elementToHide).style.display = 'none'; document.querySelector(elementToHide).style.display = 'none';
@ -20,8 +19,8 @@ const groupToggle = (selector, group) => {
return; return;
} }
group.forEach( (elementToHide) => { group.forEach( (elementToShow) => {
document.querySelector(elementToHide).style.display = 'table-row'; document.querySelector(elementToShow).style.display = 'table-row';
}) })
} }
); );
@ -34,23 +33,27 @@ const groupToggleSelect = (selector, group) => {
return; return;
} }
const value = toggleElement.value; const value = toggleElement.value;
group.forEach( (elementToHide) => { group.forEach( (elementToToggle) => {
if (value === elementToHide.value) { const domElement = document.querySelector(elementToToggle.selector);
document.querySelector(elementToHide.selector).style.display = 'table-row'; if (! domElement) {
return; return;
} }
document.querySelector(elementToHide.selector).style.display = 'none'; if (value === elementToToggle.value && domElement.style.display !== 'none') {
domElement.style.display = 'table-row';
return;
}
domElement.style.display = 'none';
}) })
toggleElement.addEventListener( toggleElement.addEventListener(
'change', 'change',
(event) => { (event) => {
const value = event.target.value; const value = event.target.value;
group.forEach( (elementToHide) => { group.forEach( (elementToToggle) => {
if (value === elementToHide.value) { if (value === elementToToggle.value) {
document.querySelector(elementToHide.selector).style.display = 'table-row'; document.querySelector(elementToToggle.selector).style.display = 'table-row';
return; return;
} }
document.querySelector(elementToHide.selector).style.display = 'none'; document.querySelector(elementToToggle.selector).style.display = 'none';
}) })
} }
); );

View file

@ -23,6 +23,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsListener; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsListener;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
@ -81,6 +82,9 @@ return array(
static function ( ContainerInterface $container ): AuthorizeOrderActionNotice { static function ( ContainerInterface $container ): AuthorizeOrderActionNotice {
return new AuthorizeOrderActionNotice(); return new AuthorizeOrderActionNotice();
}, },
'wcgateway.settings.sections-renderer' => static function ( ContainerInterface $container ): SectionsRenderer {
return new SectionsRenderer();
},
'wcgateway.settings.render' => static function ( ContainerInterface $container ): SettingsRenderer { 'wcgateway.settings.render' => static function ( ContainerInterface $container ): SettingsRenderer {
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
@ -178,7 +182,7 @@ return array(
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'sandbox_on' => array( 'sandbox_on' => array(
'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ),
@ -189,7 +193,7 @@ return array(
State::STATE_START, State::STATE_START,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'sandbox_on_info' => array( 'sandbox_on_info' => array(
'title' => __( 'Sandbox', 'paypal-for-woocommerce' ), 'title' => __( 'Sandbox', 'paypal-for-woocommerce' ),
@ -201,7 +205,7 @@ return array(
), ),
'hidden' => 'sandbox_on', 'hidden' => 'sandbox_on',
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'merchant_email' => array( 'merchant_email' => array(
'title' => __( 'Email address', 'paypal-for-woocommerce' ), 'title' => __( 'Email address', 'paypal-for-woocommerce' ),
@ -214,7 +218,7 @@ return array(
State::STATE_START, State::STATE_START,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'merchant_email_info' => array( 'merchant_email_info' => array(
'title' => __( 'Email address', 'paypal-for-woocommerce' ), 'title' => __( 'Email address', 'paypal-for-woocommerce' ),
@ -226,7 +230,7 @@ return array(
), ),
'hidden' => 'merchant_email', 'hidden' => 'merchant_email',
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'toggle_manual_input' => array( 'toggle_manual_input' => array(
'type' => 'ppcp-text', 'type' => 'ppcp-text',
@ -238,7 +242,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'client_id' => array( 'client_id' => array(
'title' => __( 'Client Id', 'paypal-for-woocommerce' ), 'title' => __( 'Client Id', 'paypal-for-woocommerce' ),
@ -252,7 +256,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'client_secret' => array( 'client_secret' => array(
'title' => __( 'Secret Key', 'paypal-for-woocommerce' ), 'title' => __( 'Secret Key', 'paypal-for-woocommerce' ),
@ -266,7 +270,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'title' => array( 'title' => array(
'title' => __( 'Title', 'paypal-for-woocommerce' ), 'title' => __( 'Title', 'paypal-for-woocommerce' ),
@ -284,6 +288,21 @@ return array(
'requirements' => array(), 'requirements' => array(),
'gateway' => 'paypal', 'gateway' => 'paypal',
), ),
'dcc_enabled' => array(
'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ),
'desc_tip' => true,
'description' => __( 'Once enabled, the Credit Card option will show up in the checkout.', 'paypal-for-woocommerce' ),
'label' => __( 'Enable PayPal Card Processing', 'paypal-for-woocommerce' ),
'type' => 'checkbox',
'default' => false,
'gateway' => 'dcc',
'requirements' => array(
'dcc',
),
'screens' => array(
State::STATE_ONBOARDED,
),
),
'dcc_gateway_title' => array( 'dcc_gateway_title' => array(
'title' => __( 'Title', 'paypal-for-woocommerce' ), 'title' => __( 'Title', 'paypal-for-woocommerce' ),
'type' => 'text', 'type' => 'text',
@ -294,10 +313,11 @@ return array(
'default' => __( 'Credit Cards', 'paypal-for-woocommerce' ), 'default' => __( 'Credit Cards', 'paypal-for-woocommerce' ),
'desc_tip' => true, 'desc_tip' => true,
'screens' => array( 'screens' => array(
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(
'dcc',
),
'gateway' => 'dcc', 'gateway' => 'dcc',
), ),
'description' => array( 'description' => array(
@ -332,10 +352,11 @@ return array(
'paypal-for-woocommerce' 'paypal-for-woocommerce'
), ),
'screens' => array( 'screens' => array(
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(
'dcc',
),
'gateway' => 'dcc', 'gateway' => 'dcc',
), ),
'intent' => array( 'intent' => array(
@ -356,7 +377,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'capture_for_virtual_only' => array( 'capture_for_virtual_only' => array(
'title' => __( 'Capture Virtual-Only Orders ', 'paypal-for-woocommerce' ), 'title' => __( 'Capture Virtual-Only Orders ', 'paypal-for-woocommerce' ),
@ -372,7 +393,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'payee_preferred' => array( 'payee_preferred' => array(
'title' => __( 'Instant Payments ', 'paypal-for-woocommerce' ), 'title' => __( 'Instant Payments ', 'paypal-for-woocommerce' ),
@ -484,7 +505,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
'prefix' => array( 'prefix' => array(
'title' => __( 'Installation prefix', 'paypal-for-woocommerce' ), 'title' => __( 'Installation prefix', 'paypal-for-woocommerce' ),
@ -498,7 +519,7 @@ return array(
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => 'all', 'gateway' => 'paypal',
), ),
// General button styles. // General button styles.
@ -512,6 +533,18 @@ return array(
'requirements' => array(), 'requirements' => array(),
'gateway' => 'paypal', 'gateway' => 'paypal',
), ),
'button_enabled' => array(
'title' => __( 'Enable buttons on Checkout', 'paypal-for-woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable on Checkout', 'paypal-for-woocommerce' ),
'default' => true,
'screens' => array(
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'paypal',
),
'button_layout' => array( 'button_layout' => array(
'title' => __( 'Button Layout', 'paypal-for-woocommerce' ), 'title' => __( 'Button Layout', 'paypal-for-woocommerce' ),
'type' => 'select', 'type' => 'select',
@ -1337,9 +1370,9 @@ return array(
'gateway' => 'paypal', 'gateway' => 'paypal',
), ),
'message_cart_enabled' => array( 'message_cart_enabled' => array(
'title' => __( 'Enable message on Single Product', 'paypal-for-woocommerce' ), 'title' => __( 'Enable message on Cart', 'paypal-for-woocommerce' ),
'type' => 'checkbox', 'type' => 'checkbox',
'label' => __( 'Enable on Single Product', 'paypal-for-woocommerce' ), 'label' => __( 'Enable on Cart', 'paypal-for-woocommerce' ),
'default' => true, 'default' => true,
'screens' => array( 'screens' => array(
State::STATE_PROGRESSIVE, State::STATE_PROGRESSIVE,

View file

@ -73,6 +73,10 @@ class DisableGateways {
unset( $methods[ CreditCardGateway::ID ] ); unset( $methods[ CreditCardGateway::ID ] );
} }
if ( $this->settings->has('button_enabled') && ! $this->settings->get('button_enabled') && ! $this->session_handler->order()) {
unset( $methods[ PayPalGateway::ID] );
}
if ( ! $this->needs_to_disable_gateways() ) { if ( ! $this->needs_to_disable_gateways() ) {
return $methods; return $methods;
} }

View file

@ -80,11 +80,11 @@ class CreditCardGateway extends PayPalGateway {
} }
$this->method_title = __( $this->method_title = __(
'PayPal Credit Card Processing', 'PayPal Card Processing',
'paypal-for-woocommerce' 'paypal-for-woocommerce'
); );
$this->method_description = __( $this->method_description = __(
'Provide your customers with the option to pay with credit card.', 'Accept debit and credit cards, and local payment methods with PayPals latest solution.',
'paypal-for-woocommerce' 'paypal-for-woocommerce'
); );
$this->title = $this->config->has( 'dcc_gateway_title' ) ? $this->title = $this->config->has( 'dcc_gateway_title' ) ?
@ -123,20 +123,6 @@ class CreditCardGateway extends PayPalGateway {
); );
} }
/**
* Renders the settings.
*
* @return string
*/
public function generate_ppcp_html(): string {
ob_start();
$this->settings_renderer->render( true );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/** /**
* Returns the title of the gateway. * Returns the title of the gateway.
* *
@ -211,4 +197,13 @@ class CreditCardGateway extends PayPalGateway {
), ),
); );
} }
/**
* Whether the gateway is available or not.
*
* @return bool
*/
public function is_available() : bool {
return $this->config->has( 'dcc_enabled' ) && $this->config->get( 'dcc_enabled' );
}
} }

View file

@ -14,6 +14,7 @@ use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor; use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
@ -116,11 +117,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
); );
} }
$this->method_title = __( 'PayPal Checkout', 'paypal-for-woocommerce' ); $this->method_title = $this->define_method_title();
$this->method_description = __( $this->method_description = $this->define_method_description();
'Accept PayPal, PayPal Credit and alternative payment types with PayPals latest solution.',
'paypal-for-woocommerce'
);
$this->title = $this->config->has( 'title' ) ? $this->title = $this->config->has( 'title' ) ?
$this->config->get( 'title' ) : $this->method_title; $this->config->get( 'title' ) : $this->method_title;
$this->description = $this->config->has( 'description' ) ? $this->description = $this->config->has( 'description' ) ?
@ -154,15 +152,20 @@ class PayPalGateway extends \WC_Payment_Gateway {
public function init_form_fields() { public function init_form_fields() {
$this->form_fields = array( $this->form_fields = array(
'enabled' => array( 'enabled' => array(
'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ), 'title' => __( 'Enable/Disable', 'paypal-for-woocommerce' ),
'type' => 'checkbox', 'type' => 'checkbox',
'label' => __( 'Enable PayPal Payments', 'paypal-for-woocommerce' ), 'desc_tip' => true,
'default' => 'no', 'description' => __( 'In order to use PayPal or PayPal Card Processing, you need to enable the Gateway.', 'paypal-for-woocommerce' ),
'label' => __( 'Enable the PayPal Gateway', 'paypal-for-woocommerce' ),
'default' => 'no',
), ),
'ppcp' => array( 'ppcp' => array(
'type' => 'ppcp', 'type' => 'ppcp',
), ),
); );
if ( $this->is_credit_card_tab() ) {
unset( $this->form_fields['enabled'] );
}
} }
/** /**
@ -299,4 +302,65 @@ class PayPalGateway extends \WC_Payment_Gateway {
ob_end_clean(); ob_end_clean();
return $content; return $content;
} }
/**
* Defines the method title. If we are on the credit card tab in the settings, we want to change this.
*
* @return string
*/
private function define_method_title(): string {
if ( $this->is_credit_card_tab() ) {
return __( 'PayPal Card Processing', 'paypal-for-woocommerce' );
}
if ( $this->is_paypal_tab() ) {
return __( 'PayPal Checkout', 'paypal-for-woocommerce' );
}
return __( 'PayPal', 'paypal-for-woocommerce' );
}
/**
* Defines the method description. If we are on the credit card tab in the settings, we want to change this.
*
* @return string
*/
private function define_method_description(): string {
if ( $this->is_credit_card_tab() ) {
return __(
'Accept debit and credit cards, and local payment methods with PayPals latest solution.',
'paypal-for-woocommerce'
);
}
return __(
'Accept PayPal, PayPal Credit and alternative payment types with PayPals latest solution.',
'paypal-for-woocommerce'
);
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
/**
* Determines, whether the current session is on the credit card tab in the admin settings.
*
* @return bool
*/
private function is_credit_card_tab() : bool {
return is_admin()
&& isset( $_GET[ SectionsRenderer::KEY ] )
&& CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) );
}
/**
* Whether we are on the PayPal settings tab.
*
* @return bool
*/
private function is_paypal_tab() : bool {
return ! $this->is_credit_card_tab()
&& is_admin()
&& isset( $_GET['section'] )
&& self::ID === sanitize_text_field( wp_unslash( $_GET['section'] ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
} }

View file

@ -57,7 +57,7 @@ class ConnectAdminNotice {
$message = sprintf( $message = sprintf(
/* translators: %1$s the gateway name. */ /* translators: %1$s the gateway name. */
__( __(
'PayPal Payments is almost ready. To get started, <a href="%1$s">connect your account</a>.', 'PayPal Checkout is almost ready. To get started, <a href="%1$s">connect your account</a>.',
'paypal-for-woocommerce' 'paypal-for-woocommerce'
), ),
admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ) admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' )

View file

@ -0,0 +1,59 @@
<?php
/**
* Renders the Sections Tab.
*
* @package Inpsyde\PayPalCommerce\WcGateway\Settings
*/
declare( strict_types=1 );
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class SectionsRenderer
*/
class SectionsRenderer {
public const KEY = 'ppcp-tab';
/**
* Whether the sections tab should be rendered.
*
* @return bool
*/
public function should_render() : bool {
global $current_section;
return PayPalGateway::ID === $current_section;
}
/**
* Renders the Sections tab.
*/
public function render() {
if ( ! $this->should_render() ) {
return;
}
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current = ! isset( $_GET[ self::KEY ] ) ? PayPalGateway::ID : sanitize_text_field( wp_unslash( $_GET[ self::KEY ] ) );
$sections = array(
PayPalGateway::ID => __( 'PayPal Checkout', 'paypal-for-woocommerce' ),
CreditCardGateway::ID => __( 'PayPal Card Processing', 'paypal-for-woocommerce' ),
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
$url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway&' . self::KEY . '=' . $id );
echo '<li><a href="' . esc_url( $url ) . '" class="' . ( $current === $id ? 'current' : '' ) . '">' . esc_html( $label ) . '</a> ' . ( end( $array_keys ) === $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
}

View file

@ -79,17 +79,7 @@ class Settings implements ContainerInterface {
*/ */
public function reset(): bool { public function reset(): bool {
$this->load(); $this->load();
$fields_to_reset = array( $this->settings = array();
'enabled',
'dcc_gateway_enabled',
'intent',
'client_id',
'client_secret',
'merchant_email',
);
foreach ( $fields_to_reset as $id ) {
$this->settings[ $id ] = null;
}
return true; return true;
} }

View file

@ -119,16 +119,11 @@ class SettingsListener {
$raw_data = ( isset( $_POST['ppcp'] ) ) ? (array) wp_unslash( $_POST['ppcp'] ) : array(); $raw_data = ( isset( $_POST['ppcp'] ) ) ? (array) wp_unslash( $_POST['ppcp'] ) : array();
// phpcs:enable phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // phpcs:enable phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$settings = $this->retrieve_settings_from_raw_data( $raw_data ); $settings = $this->retrieve_settings_from_raw_data( $raw_data );
if ( isset( $_GET['section'] ) && PayPalGateway::ID === $_GET['section'] ) { if ( ! isset( $_GET[ SectionsRenderer::KEY ] ) || PayPalGateway::ID === $_GET[ SectionsRenderer::KEY ] ) {
$settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] ) $settings['enabled'] = isset( $_POST['woocommerce_ppcp-gateway_enabled'] )
&& 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] ); && 1 === absint( $_POST['woocommerce_ppcp-gateway_enabled'] );
$this->maybe_register_webhooks( $settings );
} }
if ( isset( $_GET['section'] ) && CreditCardGateway::ID === $_GET['section'] ) {
$dcc_enabled_post_key = 'woocommerce_ppcp-credit-card-gateway_enabled';
$settings['dcc_gateway_enabled'] = isset( $_POST[ $dcc_enabled_post_key ] )
&& 1 === absint( $_POST[ $dcc_enabled_post_key ] );
}
$this->maybe_register_webhooks( $settings );
foreach ( $settings as $id => $value ) { foreach ( $settings as $id => $value ) {
$this->settings->set( $id, $value ); $this->settings->set( $id, $value );
@ -189,13 +184,17 @@ class SettingsListener {
} }
if ( if (
'dcc' === $config['gateway'] 'dcc' === $config['gateway']
&& sanitize_text_field( wp_unslash( $_GET['section'] ) ) !== 'ppcp-credit-card-gateway' && (
! isset( $_GET[ SectionsRenderer::KEY ] )
|| sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) !== CreditCardGateway::ID
)
) { ) {
continue; continue;
} }
if ( if (
'paypal' === $config['gateway'] 'paypal' === $config['gateway']
&& sanitize_text_field( wp_unslash( $_GET['section'] ) ) !== 'ppcp-gateway' && isset( $_GET[ SectionsRenderer::KEY ] )
&& sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) ) !== PayPalGateway::ID
) { ) {
continue; continue;
} }

View file

@ -12,6 +12,7 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies; use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply; use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
use Inpsyde\PayPalCommerce\Onboarding\State; use Inpsyde\PayPalCommerce\Onboarding\State;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
/** /**
@ -209,12 +210,12 @@ class SettingsRenderer {
/** /**
* Renders the settings. * Renders the settings.
*
* @param bool $is_dcc Whether it is the DCC gateway or not.
*/ */
public function render( bool $is_dcc ) { public function render() {
$nonce = wp_create_nonce( SettingsListener::NONCE ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
$is_dcc = isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) );
$nonce = wp_create_nonce( SettingsListener::NONCE );
?> ?>
<input type="hidden" name="ppcp-nonce" value="<?php echo esc_attr( $nonce ); ?>"> <input type="hidden" name="ppcp-nonce" value="<?php echo esc_attr( $nonce ); ?>">
<?php <?php
@ -276,38 +277,18 @@ class SettingsRenderer {
<?php <?php
endforeach; endforeach;
if ( $is_dcc ) : if ( $is_dcc ) {
?> if ( $this->dcc_applies->for_country_currency() ) {
<tr> if ( State::STATE_ONBOARDED > $this->state->current_state() ) {
<th><?php esc_html_e( '3D Secure', 'paypal-for-woocommerce' ); ?></th> $this->render_dcc_onboarding_info();
<td> }
<p> if ( State::STATE_ONBOARDED === $this->state->current_state() ) {
<?php $this->render_3d_secure_info();
/** }
* We still need to provide a docs link. } else {
* $this->render_dcc_does_not_apply_info();
* @todo: Provide link to documentation. }
*/ }
echo wp_kses_post(
sprintf(
// translators: %1$s and %2$s is a link tag.
__(
'3D Secure benefits cardholders and merchants by providing
an additional layer of verification using Verified by Visa,
MasterCard SecureCode and American Express SafeKey.
%1$sLearn more about 3D Secure.%2$s',
'paypal-for-woocommerce'
),
'<a href = "#">',
'</a>'
)
);
?>
</p>
</td>
</tr>
<?php
endif;
} }
/** /**
@ -328,4 +309,101 @@ class SettingsRenderer {
> '; > ';
} }
} }
/**
* Renders the 3d secure info text.
*/
private function render_3d_secure_info() {
?>
<tr>
<th><?php esc_html_e( '3D Secure', 'paypal-for-woocommerce' ); ?></th>
<td>
<p>
<?php
/**
* We still need to provide a docs link.
*
* @todo: Provide link to documentation.
*/
echo wp_kses_post(
sprintf(
// translators: %1$s and %2$s is a link tag.
__(
'3D Secure benefits cardholders and merchants by providing
an additional layer of verification using Verified by Visa,
MasterCard SecureCode and American Express SafeKey.
%1$sLearn more about 3D Secure.%2$s',
'paypal-for-woocommerce'
),
'<a href = "#">',
'</a>'
)
);
?>
</p>
</td>
</tr>
<?php
}
/**
* Renders the DCC onboarding info.
*/
private function render_dcc_onboarding_info() {
?>
<tr>
<th><?php esc_html_e( 'Onboarding', 'paypal-for-woocommerce' ); ?></th>
<td class="notice notice-error">
<p>
<?php
esc_html_e(
'You need to complete your onboarding, before you can use the PayPal Card Processing option.',
'paypal-for-woocommerce'
);
?>
<a
href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ) ); ?>"
>
<?php esc_html_e( 'Click here to complete your onboarding.', 'paypal-for-woocommerce' ); ?>
</a>
</p>
</td>
</tr>
<?php
}
/**
* Renders the info, that DCC is not available in the merchant's country.
*/
private function render_dcc_does_not_apply_info() {
?>
<tr>
<th><?php esc_html_e( 'Card Processing not available', 'paypal-for-woocommerce' ); ?></th>
<td class="notice notice-error">
<p>
<?php
esc_html_e(
'Unfortunatly, the card processing option is not yet available in your country.',
'paypal-for-woocommerce'
);
?>
<a
href="https://developer.paypal.com/docs/platforms/checkout/reference/country-availability-advanced-cards/"
target="_blank"
rel="noreferrer noopener"
>
<?php
esc_html_e(
'Click here to see, in which countries this option is currently available.',
'paypal-for-woocommerce'
);
?>
</a>
</p>
</td>
</tr>
<?php
}
} }

View file

@ -22,6 +22,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Interop\Container\ServiceProviderInterface; use Interop\Container\ServiceProviderInterface;
@ -56,6 +57,19 @@ class WcGatewayModule implements ModuleInterface {
$this->register_checkout_paypal_address_preset( $container ); $this->register_checkout_paypal_address_preset( $container );
$this->ajax_gateway_enabler( $container ); $this->ajax_gateway_enabler( $container );
add_action(
'woocommerce_sections_checkout',
function() use ( $container ) {
$section_renderer = $container->get( 'wcgateway.settings.sections-renderer' );
/**
* The Section Renderer.
*
* @var SectionsRenderer $section_renderer
*/
$section_renderer->render();
}
);
add_filter( add_filter(
Repository::NOTICES_FILTER, Repository::NOTICES_FILTER,
static function ( $notices ) use ( $container ): array { static function ( $notices ) use ( $container ): array {
@ -130,7 +144,7 @@ class WcGatewayModule implements ModuleInterface {
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$key = PayPalGateway::ID === $_POST['gateway_id'] ? 'enabled' : ''; $key = PayPalGateway::ID === $_POST['gateway_id'] ? 'enabled' : '';
if ( CreditCardGateway::ID === $_POST['gateway_id'] ) { if ( CreditCardGateway::ID === $_POST['gateway_id'] ) {
$key = 'dcc_gateway_enabled'; $key = 'dcc_enabled';
} }
if ( ! $key ) { if ( ! $key ) {
return; return;
@ -158,12 +172,14 @@ class WcGatewayModule implements ModuleInterface {
static function ( $methods ) use ( $container ): array { static function ( $methods ) use ( $container ): array {
$methods[] = $container->get( 'wcgateway.paypal-gateway' ); $methods[] = $container->get( 'wcgateway.paypal-gateway' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' ); $dcc_applies = $container->get( 'api.helpers.dccapplies' );
$screen = ! function_exists( 'get_current_screen' ) ? (object) array( 'id' => 'front' ) : get_current_screen();
/** /**
* The DCC Applies object. * The DCC Applies object.
* *
* @var DccApplies $dcc_applies * @var DccApplies $dcc_applies
*/ */
if ( $dcc_applies->for_country_currency() ) { if ( 'woocommerce_page_wc-settings' !== $screen->id && $dcc_applies->for_country_currency() ) {
$methods[] = $container->get( 'wcgateway.credit-card-gateway' ); $methods[] = $container->get( 'wcgateway.credit-card-gateway' );
} }
return (array) $methods; return (array) $methods;

View file

@ -21,6 +21,7 @@ class WcGatewayTest extends TestCase
public function testProcessPaymentSuccess() { public function testProcessPaymentSuccess() {
expect('is_admin')->andReturn(false);
$orderId = 1; $orderId = 1;
$wcOrder = Mockery::mock(\WC_Order::class); $wcOrder = Mockery::mock(\WC_Order::class);
@ -64,6 +65,7 @@ class WcGatewayTest extends TestCase
} }
public function testProcessPaymentOrderNotFound() { public function testProcessPaymentOrderNotFound() {
expect('is_admin')->andReturn(false);
$orderId = 1; $orderId = 1;
$settingsRenderer = Mockery::mock(SettingsRenderer::class); $settingsRenderer = Mockery::mock(SettingsRenderer::class);
@ -95,6 +97,7 @@ class WcGatewayTest extends TestCase
public function testProcessPaymentFails() { public function testProcessPaymentFails() {
expect('is_admin')->andReturn(false);
$orderId = 1; $orderId = 1;
$wcOrder = Mockery::mock(\WC_Order::class); $wcOrder = Mockery::mock(\WC_Order::class);
@ -136,6 +139,7 @@ class WcGatewayTest extends TestCase
} }
public function testCaptureAuthorizedPayment() { public function testCaptureAuthorizedPayment() {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class); $wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder $wcOrder
@ -181,6 +185,7 @@ class WcGatewayTest extends TestCase
public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() { public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class); $wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder $wcOrder
->expects('get_status') ->expects('get_status')
@ -233,6 +238,7 @@ class WcGatewayTest extends TestCase
*/ */
public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) { public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class); $wcOrder = Mockery::mock(\WC_Order::class);
$settingsRenderer = Mockery::mock(SettingsRenderer::class); $settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class); $orderProcessor = Mockery::mock(OrderProcessor::class);