2020-07-02 09:37:07 +03:00
< ? php
2020-08-28 08:13:45 +03:00
/**
* Renders the settings of the Gateways .
*
2020-09-11 14:11:10 +03:00
* @ package WooCommerce\PayPalCommerce\WcGateway\Settings
2020-08-28 08:13:45 +03:00
*/
2020-07-02 12:48:40 +03:00
2020-07-02 09:37:07 +03:00
declare ( strict_types = 1 );
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\WcGateway\Settings ;
2020-07-02 09:37:07 +03:00
2020-10-02 12:09:23 +03:00
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message ;
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository ;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies ;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply ;
use WooCommerce\PayPalCommerce\Onboarding\State ;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway ;
2020-07-02 09:37:07 +03:00
use Psr\Container\ContainerInterface ;
2020-09-16 10:00:28 +03:00
use Woocommerce\PayPalCommerce\WcGateway\Helper\DccProductStatus ;
2020-07-02 09:37:07 +03:00
2020-08-28 08:13:45 +03:00
/**
* Class SettingsRenderer
*/
2020-08-27 11:08:36 +03:00
class SettingsRenderer {
2020-08-28 08:13:45 +03:00
/**
* The settings .
*
* @ var ContainerInterface
*/
2020-08-27 11:08:36 +03:00
private $settings ;
2020-08-28 08:13:45 +03:00
/**
* The current onboarding state .
*
* @ var State
*/
2020-08-27 11:08:36 +03:00
private $state ;
2020-08-28 08:13:45 +03:00
/**
* The setting fields .
*
* @ var array
*/
2020-08-27 11:08:36 +03:00
private $fields ;
2020-08-28 08:13:45 +03:00
/**
* Helper to see if DCC gateway can be shown .
*
* @ var DccApplies
*/
private $dcc_applies ;
/**
* Helper to see if messages are supposed to show up .
*
* @ var MessagesApply
*/
private $messages_apply ;
2020-09-16 10:00:28 +03:00
/**
* The DCC Product Status .
*
* @ var DccProductStatus
*/
private $dcc_product_status ;
2020-08-28 08:13:45 +03:00
/**
* SettingsRenderer constructor .
*
* @ param ContainerInterface $settings The Settings .
2020-09-16 10:00:28 +03:00
* @ param State $state The current state .
* @ param array $fields The setting fields .
* @ param DccApplies $dcc_applies Whether DCC gateway can be shown .
2020-08-28 08:13:45 +03:00
* @ param MessagesApply $messages_apply Whether messages can be shown .
2020-09-16 10:00:28 +03:00
* @ param DccProductStatus $dcc_product_status The product status .
2020-08-28 08:13:45 +03:00
*/
2020-08-27 11:08:36 +03:00
public function __construct (
ContainerInterface $settings ,
State $state ,
array $fields ,
2020-08-28 08:13:45 +03:00
DccApplies $dcc_applies ,
2020-09-16 10:00:28 +03:00
MessagesApply $messages_apply ,
DccProductStatus $dcc_product_status
2020-08-27 11:08:36 +03:00
) {
2020-09-16 10:00:28 +03:00
$this -> settings = $settings ;
$this -> state = $state ;
$this -> fields = $fields ;
$this -> dcc_applies = $dcc_applies ;
$this -> messages_apply = $messages_apply ;
$this -> dcc_product_status = $dcc_product_status ;
2020-08-27 11:08:36 +03:00
}
2020-07-02 09:37:07 +03:00
2020-10-02 12:09:23 +03:00
/**
* Returns the notice , when onboarding failed .
*
* @ return array
*/
public function messages () : array {
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset ( $_GET [ 'ppcp-onboarding-error' ] ) ) {
return array ();
}
$messages = array (
new Message (
__ (
'We could not complete the onboarding process. Some features, such as card processing, will not be available. To fix this, please try again.' ,
'paypal-payments-for-woocommerce'
),
'error' ,
false
),
);
return $messages ;
}
2020-08-28 08:13:45 +03:00
/**
* Renders the multiselect field .
*
* @ param string $field The current field HTML .
* @ param string $key The current key .
* @ param array $config The configuration array .
* @ param string $value The current value .
*
* @ return string
*/
public function render_multiselect ( $field , $key , $config , $value ) : string {
2020-07-02 12:48:40 +03:00
2020-08-28 08:13:45 +03:00
if ( 'ppcp-multiselect' !== $config [ 'type' ] ) {
2020-08-27 11:08:36 +03:00
return $field ;
}
2020-07-02 09:37:07 +03:00
2020-08-27 11:08:36 +03:00
$options = array ();
2020-08-28 08:13:45 +03:00
foreach ( $config [ 'options' ] as $option_key => $option_value ) {
$selected = ( in_array ( $option_key , $value , true ) ) ? 'selected="selected"' : '' ;
2020-07-02 09:37:07 +03:00
2020-08-28 08:13:45 +03:00
$options [] = '<option value="' . esc_attr ( $option_key ) . '" ' . $selected . '>' .
esc_html ( $option_value ) .
2020-08-27 11:08:36 +03:00
'</option>' ;
}
2020-07-02 09:37:07 +03:00
2020-08-27 11:08:36 +03:00
$html = sprintf (
' < select
2020-07-02 09:37:07 +03:00
multiple
class = " %s "
name = " %s "
>% s </ select > ' ,
2020-08-27 11:08:36 +03:00
esc_attr ( implode ( ' ' , $config [ 'class' ] ) ),
esc_attr ( $key ) . '[]' ,
implode ( '' , $options )
);
2020-07-02 09:37:07 +03:00
2020-08-27 11:08:36 +03:00
return $html ;
}
2020-07-28 12:18:57 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the password input field .
*
* @ param string $field The current field HTML .
* @ param string $key The current key .
* @ param array $config The configuration array .
* @ param string $value The current value .
*
* @ return string
*/
public function render_password ( $field , $key , $config , $value ) : string {
2020-07-28 12:18:57 +03:00
2020-08-28 08:13:45 +03:00
if ( 'ppcp-password' !== $config [ 'type' ] ) {
2020-08-27 11:08:36 +03:00
return $field ;
}
2020-07-28 12:18:57 +03:00
2020-08-27 11:08:36 +03:00
$html = sprintf (
' < input
2020-07-28 12:18:57 +03:00
type = " password "
autocomplete = " new-password "
class = " %s "
name = " %s "
value = " %s "
> ' ,
2020-08-27 11:08:36 +03:00
esc_attr ( implode ( ' ' , $config [ 'class' ] ) ),
esc_attr ( $key ),
esc_attr ( $value )
);
2020-07-28 12:18:57 +03:00
2020-08-27 11:08:36 +03:00
return $html ;
}
2020-07-28 12:23:50 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the text input field .
*
* @ param string $field The current field HTML .
* @ param string $key The current key .
* @ param array $config The configuration array .
* @ param string $value The current value .
*
* @ return string
*/
public function render_text_input ( $field , $key , $config , $value ) : string {
if ( 'ppcp-text-input' !== $config [ 'type' ] ) {
2020-08-27 11:08:36 +03:00
return $field ;
}
2020-07-28 12:23:50 +03:00
2020-08-27 11:08:36 +03:00
$html = sprintf (
' < input
2020-07-28 12:23:50 +03:00
type = " text "
autocomplete = " off "
class = " %s "
name = " %s "
value = " %s "
> ' ,
2020-08-27 11:08:36 +03:00
esc_attr ( implode ( ' ' , $config [ 'class' ] ) ),
esc_attr ( $key ),
esc_attr ( $value )
);
2020-07-28 12:23:50 +03:00
2020-08-27 11:08:36 +03:00
return $html ;
}
2020-08-14 10:09:11 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the heading field .
*
* @ param string $field The current field HTML .
* @ param string $key The current key .
* @ param array $config The configuration array .
* @ param string $value The current value .
*
* @ return string
*/
public function render_heading ( $field , $key , $config , $value ) : string {
2020-08-14 10:09:11 +03:00
2020-08-28 08:13:45 +03:00
if ( 'ppcp-heading' !== $config [ 'type' ] ) {
2020-08-27 11:08:36 +03:00
return $field ;
}
2020-08-14 10:09:11 +03:00
2020-08-27 11:08:36 +03:00
$html = sprintf (
'<h3 class="%s">%s</h3>' ,
esc_attr ( implode ( ' ' , $config [ 'class' ] ) ),
esc_html ( $config [ 'heading' ] )
);
2020-08-14 10:09:11 +03:00
2020-08-27 11:08:36 +03:00
return $html ;
}
2020-07-02 09:37:07 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the settings .
*/
2020-09-02 09:56:04 +03:00
public function render () {
2020-08-27 11:08:36 +03:00
2020-09-02 10:56:26 +03:00
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
2020-09-02 12:21:37 +03:00
$is_dcc = isset ( $_GET [ SectionsRenderer :: KEY ] ) && CreditCardGateway :: ID === sanitize_text_field ( wp_unslash ( $_GET [ SectionsRenderer :: KEY ] ) );
2020-09-02 10:56:26 +03:00
$nonce = wp_create_nonce ( SettingsListener :: NONCE );
2020-08-27 11:08:36 +03:00
?>
< input type = " hidden " name = " ppcp-nonce " value = " <?php echo esc_attr( $nonce ); ?> " >
< ? php
foreach ( $this -> fields as $field => $config ) :
2020-08-31 09:42:39 +03:00
if ( ! in_array ( $this -> state -> current_state (), $config [ 'screens' ], true ) ) {
2020-08-27 11:08:36 +03:00
continue ;
}
2020-08-28 08:13:45 +03:00
if ( $is_dcc && ! in_array ( $config [ 'gateway' ], array ( 'all' , 'dcc' ), true ) ) {
2020-08-27 11:08:36 +03:00
continue ;
}
2020-08-28 08:13:45 +03:00
if ( ! $is_dcc && ! in_array ( $config [ 'gateway' ], array ( 'all' , 'paypal' ), true ) ) {
2020-08-27 11:08:36 +03:00
continue ;
}
if (
in_array ( 'dcc' , $config [ 'requirements' ], true )
2020-09-01 09:00:45 +03:00
&& ! $this -> dcc_applies -> for_country_currency ()
2020-08-27 11:08:36 +03:00
) {
continue ;
}
2020-09-16 10:00:28 +03:00
if (
in_array ( 'dcc' , $config [ 'requirements' ], true )
&& ! $this -> dcc_product_status -> dcc_is_active ()
) {
continue ;
}
2020-08-27 11:08:36 +03:00
if (
in_array ( 'messages' , $config [ 'requirements' ], true )
2020-08-31 11:12:46 +03:00
&& ! $this -> messages_apply -> for_country ()
2020-08-27 11:08:36 +03:00
) {
continue ;
}
$value = $this -> settings -> has ( $field ) ? $this -> settings -> get ( $field ) : null ;
$key = 'ppcp[' . $field . ']' ;
$id = 'ppcp-' . $field ;
$config [ 'id' ] = $id ;
2020-08-28 08:13:45 +03:00
$th_td = 'ppcp-heading' !== $config [ 'type' ] ? 'td' : 'th' ;
$colspan = 'ppcp-heading' !== $config [ 'type' ] ? 1 : 2 ;
2020-09-25 11:58:30 +03:00
$classes = isset ( $config [ 'classes' ] ) ? $config [ 'classes' ] : array ();
2020-08-27 11:08:36 +03:00
?>
2020-09-25 11:58:30 +03:00
< tr valign = " top " id = " <?php echo esc_attr( 'field-' . $field ); ?> " class = " <?php echo esc_attr( implode( ' ', $classes ) ); ?> " >
2020-08-28 08:13:45 +03:00
< ? php if ( 'ppcp-heading' !== $config [ 'type' ] ) : ?>
2020-08-27 11:08:36 +03:00
< th >
< label
for = " <?php echo esc_attr( $id ); ?> "
>< ? php echo esc_html ( $config [ 'title' ] ); ?> </label>
< ? php if ( isset ( $config [ 'desc_tip' ] ) && $config [ 'desc_tip' ] ) : ?>
< span
class = " woocommerce-help-tip "
data - tip = " <?php echo esc_attr( $config['description'] ); ?> "
></ span >
< ? php
unset ( $config [ 'description' ] );
endif ;
?>
</ th >
< ? php endif ; ?>
2020-08-28 08:13:45 +03:00
<< ? php echo esc_attr ( $th_td ); ?> colspan="<?php echo (int) $colspan; ?>">
2020-08-27 11:08:36 +03:00
< ? php
2020-08-28 08:13:45 +03:00
'ppcp-text' === $config [ 'type' ] ?
$this -> render_text ( $config )
2020-08-27 11:08:36 +03:00
: woocommerce_form_field ( $key , $config , $value );
?>
2020-08-28 08:13:45 +03:00
</< ? php echo esc_attr ( $th_td ); ?> >
2020-08-27 11:08:36 +03:00
</ tr >
< ? php
endforeach ;
2020-09-03 08:43:25 +03:00
if ( $is_dcc ) {
if ( $this -> dcc_applies -> for_country_currency () ) {
if ( State :: STATE_ONBOARDED > $this -> state -> current_state () ) {
$this -> render_dcc_onboarding_info ();
2020-09-16 10:00:28 +03:00
} elseif ( State :: STATE_ONBOARDED === $this -> state -> current_state () && $this -> dcc_product_status -> dcc_is_active () ) {
2020-09-03 08:43:25 +03:00
$this -> render_3d_secure_info ();
2020-09-16 10:00:28 +03:00
} elseif ( ! $this -> dcc_product_status -> dcc_is_active () ) {
$this -> render_dcc_not_active_yet ();
2020-09-03 08:43:25 +03:00
}
} else {
$this -> render_dcc_does_not_apply_info ();
}
}
2020-08-27 11:08:36 +03:00
}
2020-07-02 12:48:40 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the ppcp - text field given a configuration .
*
* @ param array $config The configuration array .
*/
private function render_text ( array $config ) {
2020-08-27 11:08:36 +03:00
echo wp_kses_post ( $config [ 'text' ] );
if ( isset ( $config [ 'hidden' ] ) ) {
$value = $this -> settings -> has ( $config [ 'hidden' ] ) ?
( string ) $this -> settings -> get ( $config [ 'hidden' ] )
: '' ;
echo ' < input
2020-08-21 09:41:12 +03:00
type = " hidden "
2020-08-27 11:08:36 +03:00
name = " ppcp[' . esc_attr( $config['hidden'] ) . '] "
value = " ' . esc_attr( $value ) . ' "
2020-08-21 09:41:12 +03:00
> ' ;
2020-08-27 11:08:36 +03:00
}
}
2020-09-03 08:43:25 +03:00
2020-09-16 10:00:28 +03:00
/**
* Renders the information that the PayPal account can not yet process DCC .
*/
private function render_dcc_not_active_yet () {
?>
< tr >
< th >< ? php esc_html_e ( 'Onboarding' , 'paypal-payments-for-woocommerce' ); ?> </th>
< td class = " notice notice-error " >
< p >
< ? php
esc_html_e (
'Credit Card processing for your account has not yet been activated by PayPal. If your account is new, this can take some days. Otherwise, please get in contact with PayPal.' ,
'paypal-payments-for-woocommerce'
);
?>
</ p >
</ td >
</ tr >
< ? php
}
2020-09-03 08:43:25 +03:00
/**
* Renders the 3 d secure info text .
*/
private function render_3d_secure_info () {
?>
< tr >
2020-09-11 10:20:12 +03:00
< th >< ? php esc_html_e ( '3D Secure' , 'paypal-payments-for-woocommerce' ); ?> </th>
2020-09-03 08:43:25 +03:00
< 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.
__ (
' 3 D 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 3 D Secure .% 2 $s ' ,
2020-09-11 10:20:12 +03:00
'paypal-payments-for-woocommerce'
2020-09-03 08:43:25 +03:00
),
'<a href = "#">' ,
'</a>'
)
);
?>
</ p >
</ td >
</ tr >
< ? php
}
/**
* Renders the DCC onboarding info .
*/
private function render_dcc_onboarding_info () {
?>
< tr >
2020-09-11 10:20:12 +03:00
< th >< ? php esc_html_e ( 'Onboarding' , 'paypal-payments-for-woocommerce' ); ?> </th>
2020-09-03 08:43:25 +03:00
< 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.' ,
2020-09-11 10:20:12 +03:00
'paypal-payments-for-woocommerce'
2020-09-03 08:43:25 +03:00
);
?>
< a
href = " <?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway' ) ); ?> "
>
2020-09-11 10:20:12 +03:00
< ? php esc_html_e ( 'Click here to complete your onboarding.' , 'paypal-payments-for-woocommerce' ); ?>
2020-09-03 08:43:25 +03:00
</ 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 >
2020-09-11 10:20:12 +03:00
< th >< ? php esc_html_e ( 'Card Processing not available' , 'paypal-payments-for-woocommerce' ); ?> </th>
2020-09-03 08:43:25 +03:00
< td class = " notice notice-error " >
< p >
< ? php
esc_html_e (
'Unfortunatly, the card processing option is not yet available in your country.' ,
2020-09-11 10:20:12 +03:00
'paypal-payments-for-woocommerce'
2020-09-03 08:43:25 +03:00
);
?>
< 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.' ,
2020-09-11 10:20:12 +03:00
'paypal-payments-for-woocommerce'
2020-09-03 08:43:25 +03:00
);
?>
</ a >
</ p >
</ td >
</ tr >
< ? php
}
2020-08-27 11:08:36 +03:00
}