2020-04-02 08:38:00 +03:00
< ? php
2020-08-31 11:12:46 +03:00
/**
* Registers and configures the necessary Javascript for the button , credit messaging and DCC fields .
*
2020-09-11 14:11:10 +03:00
* @ package WooCommerce\PayPalCommerce\Button\Assets
2020-08-31 11:12:46 +03:00
*/
2020-04-28 12:31:12 +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\Button\Assets ;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory ;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies ;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint ;
use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint ;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint ;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint ;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData ;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply ;
2020-10-16 13:32:26 +03:00
use WooCommerce\PayPalCommerce\Onboarding\Environment ;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Session\SessionHandler ;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper ;
2021-09-17 17:11:36 +02:00
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository ;
2021-05-19 15:39:33 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway ;
2021-10-08 10:23:19 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus ;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings ;
2020-04-02 08:38:00 +03:00
2020-08-31 11:12:46 +03:00
/**
* Class SmartButton
*/
2020-08-27 11:08:36 +03:00
class SmartButton implements SmartButtonInterface {
2021-06-17 11:51:43 +02:00
/**
* The Settings status helper .
*
* @ var SettingsStatus
*/
protected $settings_status ;
2020-08-31 11:12:46 +03:00
/**
* The URL to the module .
*
* @ var string
*/
private $module_url ;
2022-02-17 18:51:24 +02:00
/**
* The assets version .
*
* @ var string
*/
private $version ;
2020-08-31 11:12:46 +03:00
/**
* The Session Handler .
*
* @ var SessionHandler
*/
private $session_handler ;
/**
* The settings .
*
* @ var Settings
*/
2020-08-27 11:08:36 +03:00
private $settings ;
2020-08-31 11:12:46 +03:00
/**
* The Payer Factory .
*
* @ var PayerFactory
*/
private $payer_factory ;
/**
* The client ID .
*
* @ var string
*/
private $client_id ;
/**
* The Request Data .
*
* @ var RequestData
*/
private $request_data ;
/**
* The DCC Applies helper .
*
* @ var DccApplies
*/
private $dcc_applies ;
/**
* The Subscription Helper .
*
* @ var SubscriptionHelper
*/
private $subscription_helper ;
/**
* The Messages apply helper .
*
* @ var MessagesApply
*/
private $messages_apply ;
2020-10-16 13:32:26 +03:00
/**
* The environment object .
*
* @ var Environment
*/
private $environment ;
2021-03-25 16:57:42 +01:00
/**
* The payment token repository .
*
* @ var PaymentTokenRepository
*/
private $payment_token_repository ;
2021-03-25 16:11:45 +01:00
2021-11-30 10:40:38 +02:00
/**
* 3 - letter currency code of the shop .
*
* @ var string
*/
private $currency ;
2021-03-25 16:57:42 +01:00
/**
2020-08-31 11:12:46 +03:00
* SmartButton constructor .
*
2021-03-25 16:57:42 +01:00
* @ param string $module_url The URL to the module .
2022-02-17 18:51:24 +02:00
* @ param string $version The assets version .
2021-03-25 16:57:42 +01:00
* @ param SessionHandler $session_handler The Session Handler .
* @ param Settings $settings The Settings .
* @ param PayerFactory $payer_factory The Payer factory .
* @ param string $client_id The client ID .
* @ param RequestData $request_data The Request Data helper .
* @ param DccApplies $dcc_applies The DCC applies helper .
* @ param SubscriptionHelper $subscription_helper The subscription helper .
* @ param MessagesApply $messages_apply The Messages apply helper .
* @ param Environment $environment The environment object .
* @ param PaymentTokenRepository $payment_token_repository The payment token repository .
2021-06-17 11:51:43 +02:00
* @ param SettingsStatus $settings_status The Settings status helper .
2021-11-30 10:40:38 +02:00
* @ param string $currency 3 - letter currency code of the shop .
2020-08-31 11:12:46 +03:00
*/
2020-08-27 11:08:36 +03:00
public function __construct (
2020-08-31 11:12:46 +03:00
string $module_url ,
2022-02-17 18:51:24 +02:00
string $version ,
2020-08-31 11:12:46 +03:00
SessionHandler $session_handler ,
2020-08-27 11:08:36 +03:00
Settings $settings ,
2020-08-31 11:12:46 +03:00
PayerFactory $payer_factory ,
string $client_id ,
RequestData $request_data ,
DccApplies $dcc_applies ,
SubscriptionHelper $subscription_helper ,
2020-10-16 13:32:26 +03:00
MessagesApply $messages_apply ,
2021-03-25 16:11:45 +01:00
Environment $environment ,
2021-06-17 11:51:43 +02:00
PaymentTokenRepository $payment_token_repository ,
2021-11-30 10:40:38 +02:00
SettingsStatus $settings_status ,
string $currency
2020-08-27 11:08:36 +03:00
) {
2021-03-25 16:57:42 +01:00
$this -> module_url = $module_url ;
2022-02-17 18:51:24 +02:00
$this -> version = $version ;
2021-03-25 16:57:42 +01:00
$this -> session_handler = $session_handler ;
$this -> settings = $settings ;
$this -> payer_factory = $payer_factory ;
$this -> client_id = $client_id ;
$this -> request_data = $request_data ;
$this -> dcc_applies = $dcc_applies ;
$this -> subscription_helper = $subscription_helper ;
$this -> messages_apply = $messages_apply ;
$this -> environment = $environment ;
$this -> payment_token_repository = $payment_token_repository ;
2021-06-17 11:51:43 +02:00
$this -> settings_status = $settings_status ;
2021-11-30 10:40:38 +02:00
$this -> currency = $currency ;
2021-03-25 16:57:42 +01:00
}
2020-08-27 11:08:36 +03:00
/**
2020-08-31 11:12:46 +03:00
* Registers the necessary action hooks to render the HTML depending on the settings .
*
2020-08-27 11:08:36 +03:00
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
2020-08-27 11:08:36 +03:00
*/
2020-08-31 11:12:46 +03:00
public function render_wrapper () : bool {
2020-08-27 11:08:36 +03:00
2020-08-31 11:12:46 +03:00
if ( ! $this -> can_save_vault_token () && $this -> has_subscriptions () ) {
2020-08-27 11:08:36 +03:00
return false ;
}
if ( $this -> settings -> has ( 'enabled' ) && $this -> settings -> get ( 'enabled' ) ) {
2020-08-31 11:12:46 +03:00
$this -> render_button_wrapper_registrar ();
$this -> render_message_wrapper_registrar ();
2020-08-27 11:08:36 +03:00
}
if (
2020-09-02 13:52:40 +03:00
$this -> settings -> has ( 'dcc_enabled' )
&& $this -> settings -> get ( 'dcc_enabled' )
2020-08-31 11:12:46 +03:00
&& ! $this -> session_handler -> order ()
2020-08-27 11:08:36 +03:00
) {
add_action (
2021-07-26 10:27:34 +03:00
$this -> checkout_dcc_button_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'dcc_renderer' ,
2020-08-27 11:08:36 +03:00
),
11
);
2020-09-30 14:24:31 +03:00
add_action (
2021-07-26 18:32:16 +03:00
$this -> pay_order_renderer_hook (),
2020-09-30 14:24:31 +03:00
array (
$this ,
'dcc_renderer' ,
),
11
);
2021-02-16 17:06:50 +01:00
2021-11-19 11:06:16 +01:00
$subscription_helper = $this -> subscription_helper ;
2021-02-16 17:06:50 +01:00
add_filter (
'woocommerce_credit_card_form_fields' ,
2021-11-19 12:44:06 +02:00
function ( array $default_fields , $id ) use ( $subscription_helper ) : array {
2021-07-23 11:31:11 +02:00
if ( is_user_logged_in () && $this -> settings -> has ( 'vault_enabled' ) && $this -> settings -> get ( 'vault_enabled' ) && CreditCardGateway :: ID === $id ) {
2021-11-19 11:06:16 +01:00
2022-01-04 10:43:18 +01:00
$default_fields [ 'card-vault' ] = sprintf (
2022-01-11 10:31:47 +01:00
'<p class="form-row form-row-wide"><label for="ppcp-credit-card-vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>' ,
2022-01-04 10:43:18 +01:00
esc_html__ ( 'Save your Credit Card' , 'woocommerce-paypal-payments' )
);
if ( $subscription_helper -> cart_contains_subscription () || $subscription_helper -> order_pay_contains_subscription () ) {
$default_fields [ 'card-vault' ] = '' ;
2021-11-19 11:06:16 +01:00
}
2021-03-25 16:11:45 +01:00
2021-05-19 15:39:33 +02:00
$tokens = $this -> payment_token_repository -> all_for_user_id ( get_current_user_id () );
if ( $tokens && $this -> payment_token_repository -> tokens_contains_card ( $tokens ) ) {
2021-03-25 16:57:42 +01:00
$output = sprintf (
2021-03-31 10:54:44 +02:00
'<p class="form-row form-row-wide"><label>%1$s</label><select id="saved-credit-card" name="saved_credit_card"><option value="">%2$s</option>' ,
esc_html__ ( 'Or select a saved Credit Card payment' , 'woocommerce-paypal-payments' ),
2021-04-01 13:55:14 +03:00
esc_html__ ( 'Choose a saved payment' , 'woocommerce-paypal-payments' )
2021-03-25 16:57:42 +01:00
);
foreach ( $tokens as $token ) {
if ( isset ( $token -> source () -> card ) ) {
$output .= sprintf (
'<option value="%1$s">%2$s ...%3$s</option>' ,
$token -> id (),
$token -> source () -> card -> brand ,
$token -> source () -> card -> last_digits
);
}
}
2021-03-29 11:57:03 +02:00
$output .= '</select></p>' ;
2021-03-25 16:57:42 +01:00
$default_fields [ 'saved-credit-card' ] = $output ;
}
2021-02-16 17:06:50 +01:00
}
2021-03-25 16:11:45 +01:00
2021-02-16 17:06:50 +01:00
return $default_fields ;
},
10 ,
2
);
2020-08-27 11:08:36 +03:00
}
return true ;
}
2020-08-31 11:12:46 +03:00
/**
* Registers the hooks to render the credit messaging HTML depending on the settings .
*
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
2020-08-31 11:12:46 +03:00
*/
private function render_message_wrapper_registrar () : bool {
2020-08-27 11:08:36 +03:00
2020-08-31 11:12:46 +03:00
$not_enabled_on_cart = $this -> settings -> has ( 'message_cart_enabled' ) &&
2020-08-27 11:08:36 +03:00
! $this -> settings -> get ( 'message_cart_enabled' );
if (
is_cart ()
2020-08-31 11:12:46 +03:00
&& ! $not_enabled_on_cart
2020-08-27 11:08:36 +03:00
) {
add_action (
2021-07-26 18:23:25 +03:00
$this -> proceed_to_checkout_button_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'message_renderer' ,
2020-08-27 11:08:36 +03:00
),
19
);
}
2020-08-31 11:12:46 +03:00
$not_enabled_on_product_page = $this -> settings -> has ( 'message_product_enabled' ) &&
2020-08-27 11:08:36 +03:00
! $this -> settings -> get ( 'message_product_enabled' );
if (
( is_product () || wc_post_content_has_shortcode ( 'product_page' ) )
2020-08-31 11:12:46 +03:00
&& ! $not_enabled_on_product_page
2020-08-27 11:08:36 +03:00
) {
add_action (
2021-07-26 18:36:29 +03:00
$this -> single_product_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'message_renderer' ,
2020-08-27 11:08:36 +03:00
),
30
);
}
2020-08-31 11:12:46 +03:00
$not_enabled_on_checkout = $this -> settings -> has ( 'message_enabled' ) &&
2020-08-27 11:08:36 +03:00
! $this -> settings -> get ( 'message_enabled' );
2020-08-31 11:12:46 +03:00
if ( ! $not_enabled_on_checkout ) {
2020-08-27 11:08:36 +03:00
add_action (
2021-07-26 18:23:25 +03:00
$this -> checkout_dcc_button_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'message_renderer' ,
2020-08-27 11:08:36 +03:00
),
11
);
2020-09-30 14:24:31 +03:00
add_action (
2021-07-26 18:32:16 +03:00
$this -> pay_order_renderer_hook (),
2020-09-30 14:24:31 +03:00
array (
$this ,
'message_renderer' ,
),
11
);
2020-08-27 11:08:36 +03:00
}
return true ;
}
2020-08-31 11:12:46 +03:00
/**
* Registers the hooks where to render the button HTML according to the settings .
*
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
2020-08-31 11:12:46 +03:00
*/
private function render_button_wrapper_registrar () : bool {
2020-08-27 11:08:36 +03:00
2022-02-18 18:06:55 +04:00
$not_enabled_on_product_page = $this -> settings -> has ( 'button_single_product_enabled' ) &&
! $this -> settings -> get ( 'button_single_product_enabled' );
2020-08-27 11:08:36 +03:00
if (
2022-02-18 18:06:55 +04:00
( is_product () || wc_post_content_has_shortcode ( 'product_page' ) )
&& ! $not_enabled_on_product_page
2020-08-27 11:08:36 +03:00
) {
add_action (
2022-02-18 18:06:55 +04:00
$this -> single_product_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'button_renderer' ,
2020-08-27 11:08:36 +03:00
),
2022-02-18 18:06:55 +04:00
31
2020-08-27 11:08:36 +03:00
);
}
2022-03-28 16:33:45 +02:00
add_action ( $this -> pay_order_renderer_hook (), array ( $this , 'button_renderer' ), 10 );
2022-04-06 17:57:59 +04:00
$not_enabled_on_minicart = $this -> settings -> has ( 'button_mini_cart_enabled' ) &&
! $this -> settings -> get ( 'button_mini_cart_enabled' );
if (
! $not_enabled_on_minicart
) {
add_action (
$this -> mini_cart_button_renderer_hook (),
2022-04-06 18:18:05 +04:00
function () {
if ( $this -> is_cart_price_total_zero () ) {
2022-04-06 17:57:59 +04:00
return ;
}
2022-04-06 18:18:05 +04:00
2022-04-06 17:57:59 +04:00
echo ' < p
id = " ppc-button-minicart "
class = " woocommerce-mini-cart__buttons buttons "
></ p > ' ;
},
30
);
}
2022-02-18 18:06:55 +04:00
if ( $this -> is_cart_price_total_zero () ) {
return false ;
}
$not_enabled_on_cart = $this -> settings -> has ( 'button_cart_enabled' ) &&
! $this -> settings -> get ( 'button_cart_enabled' );
2020-08-27 11:08:36 +03:00
if (
2022-02-18 18:06:55 +04:00
is_cart ()
&& ! $not_enabled_on_cart
2020-08-27 11:08:36 +03:00
) {
add_action (
2022-02-18 18:06:55 +04:00
$this -> proceed_to_checkout_button_renderer_hook (),
2020-08-27 11:08:36 +03:00
array (
$this ,
2020-08-31 11:12:46 +03:00
'button_renderer' ,
2020-08-27 11:08:36 +03:00
),
2022-02-18 18:06:55 +04:00
20
2020-08-27 11:08:36 +03:00
);
}
2021-07-26 09:58:54 +03:00
add_action ( $this -> checkout_button_renderer_hook (), array ( $this , 'button_renderer' ), 10 );
2020-08-27 11:08:36 +03:00
return true ;
}
2020-08-31 11:12:46 +03:00
/**
* Enqueues the script .
*
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
2020-08-31 11:12:46 +03:00
*/
2020-08-27 11:08:36 +03:00
public function enqueue () : bool {
2020-08-31 11:12:46 +03:00
$buttons_enabled = $this -> settings -> has ( 'enabled' ) && $this -> settings -> get ( 'enabled' );
if ( ! is_checkout () && ! $buttons_enabled ) {
2020-08-27 11:08:36 +03:00
return false ;
}
2020-09-03 11:40:44 +03:00
2020-09-03 11:48:25 +03:00
$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 ;
}
2020-09-30 14:24:31 +03:00
if ( in_array ( $this -> context (), array ( 'pay-now' , 'checkout' ), true ) && $this -> can_render_dcc () ) {
2020-09-18 15:38:38 +03:00
wp_enqueue_style (
'ppcp-hosted-fields' ,
2022-01-14 14:34:54 +02:00
untrailingslashit ( $this -> module_url ) . '/assets/css/hosted-fields.css' ,
2020-09-18 15:38:38 +03:00
array (),
2022-02-17 18:51:24 +02:00
$this -> version
2020-09-18 15:38:38 +03:00
);
}
2020-09-03 11:48:25 +03:00
if ( $load_script ) {
wp_enqueue_script (
'ppcp-smart-button' ,
2022-01-14 14:34:54 +02:00
untrailingslashit ( $this -> module_url ) . '/assets/js/button.js' ,
2020-09-03 11:48:25 +03:00
array ( 'jquery' ),
2022-02-17 18:51:24 +02:00
$this -> version ,
2020-09-03 11:48:25 +03:00
true
);
wp_localize_script (
'ppcp-smart-button' ,
'PayPalCommerceGateway' ,
$this -> localize_script ()
);
}
2020-08-27 11:08:36 +03:00
return true ;
}
2020-08-31 11:12:46 +03:00
/**
* Renders the HTML for the buttons .
*/
public function button_renderer () {
2020-08-27 11:08:36 +03:00
$product = wc_get_product ();
if (
! is_checkout () && is_a ( $product , \WC_Product :: class )
&& (
$product -> is_type ( array ( 'external' , 'grouped' ) )
|| ! $product -> is_in_stock ()
)
) {
2022-02-17 20:01:47 +04:00
2020-08-27 11:08:36 +03:00
return ;
}
2022-02-22 16:07:24 +10:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
if ( ! isset ( $available_gateways [ 'ppcp-gateway' ] ) ) {
return ;
}
2022-03-17 15:59:19 +02:00
// The wrapper is needed for the loading spinner,
// otherwise jQuery block() prevents buttons rendering.
echo '<div class="ppc-button-wrapper"><div id="ppc-button"></div></div>' ;
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* Renders the HTML for the credit messaging .
*/
public function message_renderer () {
2020-08-27 11:08:36 +03:00
2021-06-23 11:59:52 +02:00
echo '<div id="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>' ;
2020-08-27 11:08:36 +03:00
}
2020-08-19 05:19:29 +03:00
2020-08-31 11:12:46 +03:00
/**
* The values for the credit messaging .
*
* @ return array
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
2020-08-31 11:12:46 +03:00
*/
private function message_values () : array {
2020-08-27 11:08:36 +03:00
if (
$this -> settings -> has ( 'disable_funding' )
&& in_array ( 'credit' , ( array ) $this -> settings -> get ( 'disable_funding' ), true )
) {
return array ();
}
2020-08-31 11:12:46 +03:00
$placement = 'product' ;
$product = wc_get_product ();
$amount = ( is_a ( $product , \WC_Product :: class ) ) ? wc_get_price_including_tax ( $product ) : 0 ;
$layout = $this -> settings -> has ( 'message_product_layout' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_layout' ) : 'text' ;
2020-08-31 11:12:46 +03:00
$logo_type = $this -> settings -> has ( 'message_product_logo' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_logo' ) : 'primary' ;
2020-08-31 11:12:46 +03:00
$logo_position = $this -> settings -> has ( 'message_product_position' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_position' ) : 'left' ;
2020-08-31 11:12:46 +03:00
$text_color = $this -> settings -> has ( 'message_product_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_color' ) : 'black' ;
2020-08-31 11:12:46 +03:00
$style_color = $this -> settings -> has ( 'message_product_flex_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_flex_color' ) : 'blue' ;
2020-08-31 11:12:46 +03:00
$ratio = $this -> settings -> has ( 'message_product_flex_ratio' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_product_flex_ratio' ) : '1x1' ;
2020-08-31 11:12:46 +03:00
$should_show = $this -> settings -> has ( 'message_product_enabled' )
2020-08-27 11:08:36 +03:00
&& $this -> settings -> get ( 'message_product_enabled' );
if ( is_checkout () ) {
2020-08-31 11:12:46 +03:00
$placement = 'payment' ;
$amount = WC () -> cart -> get_total ( 'raw' );
$layout = $this -> settings -> has ( 'message_layout' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_layout' ) : 'text' ;
2020-08-31 11:12:46 +03:00
$logo_type = $this -> settings -> has ( 'message_logo' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_logo' ) : 'primary' ;
2020-08-31 11:12:46 +03:00
$logo_position = $this -> settings -> has ( 'message_position' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_position' ) : 'left' ;
2020-08-31 11:12:46 +03:00
$text_color = $this -> settings -> has ( 'message_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_color' ) : 'black' ;
2020-08-31 11:12:46 +03:00
$style_color = $this -> settings -> has ( 'message_flex_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_flex_color' ) : 'blue' ;
2020-08-31 11:12:46 +03:00
$ratio = $this -> settings -> has ( 'message_flex_ratio' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_flex_ratio' ) : '1x1' ;
2020-08-31 11:12:46 +03:00
$should_show = $this -> settings -> has ( 'message_enabled' )
2020-08-27 11:08:36 +03:00
&& $this -> settings -> get ( 'message_enabled' );
}
if ( is_cart () ) {
2020-08-31 11:12:46 +03:00
$placement = 'cart' ;
$amount = WC () -> cart -> get_total ( 'raw' );
$layout = $this -> settings -> has ( 'message_cart_layout' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_layout' ) : 'text' ;
2020-08-31 11:12:46 +03:00
$logo_type = $this -> settings -> has ( 'message_cart_logo' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_logo' ) : 'primary' ;
2020-08-31 11:12:46 +03:00
$logo_position = $this -> settings -> has ( 'message_cart_position' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_position' ) : 'left' ;
2020-08-31 11:12:46 +03:00
$text_color = $this -> settings -> has ( 'message_cart_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_color' ) : 'black' ;
2020-08-31 11:12:46 +03:00
$style_color = $this -> settings -> has ( 'message_cart_flex_color' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_flex_color' ) : 'blue' ;
2020-08-31 11:12:46 +03:00
$ratio = $this -> settings -> has ( 'message_cart_flex_ratio' ) ?
2020-08-27 11:08:36 +03:00
$this -> settings -> get ( 'message_cart_flex_ratio' ) : '1x1' ;
2020-08-31 11:12:46 +03:00
$should_show = $this -> settings -> has ( 'message_cart_enabled' )
2020-08-27 11:08:36 +03:00
&& $this -> settings -> get ( 'message_cart_enabled' );
}
2020-08-31 11:12:46 +03:00
if ( ! $should_show ) {
2020-08-27 11:08:36 +03:00
return array ();
}
$values = array (
'wrapper' => '#ppcp-messages' ,
'amount' => $amount ,
'placement' => $placement ,
'style' => array (
'layout' => $layout ,
'logo' => array (
2020-08-31 11:12:46 +03:00
'type' => $logo_type ,
'position' => $logo_position ,
2020-08-27 11:08:36 +03:00
),
'text' => array (
2020-08-31 11:12:46 +03:00
'color' => $text_color ,
2020-08-27 11:08:36 +03:00
),
2020-08-31 11:12:46 +03:00
'color' => $style_color ,
2020-08-27 11:08:36 +03:00
'ratio' => $ratio ,
),
);
return $values ;
}
2020-08-19 05:19:29 +03:00
2020-08-31 11:12:46 +03:00
/**
2020-09-18 15:38:38 +03:00
* Whether DCC fields can be rendered .
2020-08-31 11:12:46 +03:00
*
2020-09-18 15:38:38 +03:00
* @ return bool
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found .
*/
private function can_render_dcc () : bool {
2020-09-30 14:24:31 +03:00
$can_render = $this -> settings -> has ( 'dcc_enabled' ) && $this -> settings -> get ( 'dcc_enabled' ) && $this -> settings -> has ( 'client_id' ) && $this -> settings -> get ( 'client_id' ) && $this -> dcc_applies -> for_country_currency ();
return $can_render ;
2020-09-18 15:38:38 +03:00
}
/**
* Renders the HTML for the DCC fields .
2020-08-31 11:12:46 +03:00
*/
public function dcc_renderer () {
2020-07-23 14:28:16 +03:00
2020-09-18 15:38:38 +03:00
$id = 'ppcp-hosted-fields' ;
if ( ! $this -> can_render_dcc () ) {
2020-08-27 11:08:36 +03:00
return ;
}
2020-07-23 14:28:16 +03:00
2022-02-17 20:01:47 +04:00
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
2021-12-21 17:51:28 +02:00
$label = 'checkout' === $this -> context () ? apply_filters ( 'woocommerce_order_button_text' , __ ( 'Place order' , 'woocommerce' ) ) : __ ( 'Pay for order' , 'woocommerce' );
2020-09-30 14:24:31 +03:00
2020-08-27 11:08:36 +03:00
printf (
2020-09-18 15:38:38 +03:00
' < div id = " %1 $s " style = " display:none; " >
2022-03-15 18:02:35 +04:00
< button type = " submit " class = " button alt ppcp-dcc-order-button " >% 2 $s </ button >
2020-09-14 14:17:39 +03:00
</ div >< div id = " payments-sdk__contingency-lightbox " ></ div >< style id = " ppcp-hide-dcc " >. payment_method_ppcp - credit - card - gateway { display : none ;} </ style > ' ,
2020-08-27 11:08:36 +03:00
esc_attr ( $id ),
2020-09-30 14:24:31 +03:00
esc_html ( $label )
2020-08-27 11:08:36 +03:00
);
}
2020-07-23 14:28:16 +03:00
2020-08-31 11:12:46 +03:00
/**
* Whether we can store vault tokens or not .
*
* @ return bool
2021-04-23 10:33:28 +02:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasn ' t been found .
2020-08-31 11:12:46 +03:00
*/
public function can_save_vault_token () : bool {
2020-08-27 11:08:36 +03:00
if ( ! $this -> settings -> has ( 'client_id' ) || ! $this -> settings -> get ( 'client_id' ) ) {
return false ;
}
2021-04-22 11:41:05 +02:00
2021-04-23 10:33:28 +02:00
if ( ! $this -> settings -> has ( 'vault_enabled' ) || ! $this -> settings -> get ( 'vault_enabled' ) ) {
2020-08-27 11:08:36 +03:00
return false ;
}
2021-04-22 11:41:05 +02:00
2021-12-24 12:37:06 +01:00
return true ;
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* Whether we need to initialize the script to enable tokenization for subscriptions or not .
*
* @ return bool
*/
private function has_subscriptions () : bool {
if ( ! $this -> subscription_helper -> accept_only_automatic_payment_gateways () ) {
2020-08-27 11:08:36 +03:00
return false ;
}
if ( is_product () ) {
2020-08-31 11:12:46 +03:00
return $this -> subscription_helper -> current_product_is_subscription ();
2020-08-27 11:08:36 +03:00
}
2022-01-04 11:01:34 +01:00
if ( is_wc_endpoint_url ( 'order-pay' ) ) {
return $this -> subscription_helper -> order_pay_contains_subscription ();
}
2020-08-31 11:12:46 +03:00
return $this -> subscription_helper -> cart_contains_subscription ();
2020-08-27 11:08:36 +03:00
}
2020-07-23 14:12:29 +03:00
2021-08-20 17:47:32 +03:00
/**
* Retrieves the 3 D Secure contingency settings .
*
* @ return string
*/
private function get_3ds_contingency () : string {
if ( $this -> settings -> has ( '3d_secure_contingency' ) ) {
2021-10-06 12:45:51 +03:00
$value = $this -> settings -> get ( '3d_secure_contingency' );
if ( $value ) {
return $value ;
}
2021-08-20 17:47:32 +03:00
}
return 'SCA_WHEN_REQUIRED' ;
}
2020-08-31 11:12:46 +03:00
/**
* The localized data for the smart button .
*
* @ return array
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasn ' t been found .
2020-08-31 11:12:46 +03:00
*/
private function localize_script () : array {
2020-09-30 14:24:31 +03:00
global $wp ;
2020-08-31 11:12:46 +03:00
$this -> request_data -> enqueue_nonce_fix ();
2020-08-27 11:08:36 +03:00
$localize = array (
2021-07-12 17:16:26 +02:00
'script_attributes' => $this -> attributes (),
'data_client_id' => array (
2022-01-21 15:25:17 +01:00
'set_attribute' => ( is_checkout () && $this -> dcc_is_enabled () ) || $this -> can_save_vault_token (),
2022-03-09 16:24:11 +02:00
'endpoint' => \WC_AJAX :: get_endpoint ( DataClientIdEndpoint :: ENDPOINT ),
2022-01-17 14:35:25 +01:00
'nonce' => wp_create_nonce ( DataClientIdEndpoint :: nonce () ),
'user' => get_current_user_id (),
'has_subscriptions' => $this -> has_subscriptions (),
2020-08-27 11:08:36 +03:00
),
2021-07-12 17:16:26 +02:00
'redirect' => wc_get_checkout_url (),
'context' => $this -> context (),
'ajax' => array (
2020-08-27 11:08:36 +03:00
'change_cart' => array (
2022-03-09 16:24:11 +02:00
'endpoint' => \WC_AJAX :: get_endpoint ( ChangeCartEndpoint :: ENDPOINT ),
2020-08-27 11:08:36 +03:00
'nonce' => wp_create_nonce ( ChangeCartEndpoint :: nonce () ),
),
'create_order' => array (
2022-03-09 16:24:11 +02:00
'endpoint' => \WC_AJAX :: get_endpoint ( CreateOrderEndpoint :: ENDPOINT ),
2020-08-27 11:08:36 +03:00
'nonce' => wp_create_nonce ( CreateOrderEndpoint :: nonce () ),
),
'approve_order' => array (
2022-03-09 16:24:11 +02:00
'endpoint' => \WC_AJAX :: get_endpoint ( ApproveOrderEndpoint :: ENDPOINT ),
2020-08-27 11:08:36 +03:00
'nonce' => wp_create_nonce ( ApproveOrderEndpoint :: nonce () ),
),
),
2021-07-12 17:16:26 +02:00
'enforce_vault' => $this -> has_subscriptions (),
2022-02-28 14:14:14 +01:00
'can_save_vault_token' => $this -> can_save_vault_token (),
2021-07-12 17:16:26 +02:00
'bn_codes' => $this -> bn_codes (),
'payer' => $this -> payerData (),
'button' => array (
2020-08-27 11:08:36 +03:00
'wrapper' => '#ppc-button' ,
'mini_cart_wrapper' => '#ppc-button-minicart' ,
'cancel_wrapper' => '#ppcp-cancel' ,
'url' => $this -> url (),
'mini_cart_style' => array (
2020-08-31 11:12:46 +03:00
'layout' => $this -> style_for_context ( 'layout' , 'mini-cart' ),
'color' => $this -> style_for_context ( 'color' , 'mini-cart' ),
'shape' => $this -> style_for_context ( 'shape' , 'mini-cart' ),
'label' => $this -> style_for_context ( 'label' , 'mini-cart' ),
'tagline' => $this -> style_for_context ( 'tagline' , 'mini-cart' ),
2021-06-17 17:04:02 +02:00
'height' => $this -> settings -> has ( 'button_mini-cart_height' ) && $this -> settings -> get ( 'button_mini-cart_height' ) ? $this -> normalize_height ( ( int ) $this -> settings -> get ( 'button_mini-cart_height' ) ) : 35 ,
2020-08-27 11:08:36 +03:00
),
'style' => array (
2020-08-31 11:12:46 +03:00
'layout' => $this -> style_for_context ( 'layout' , $this -> context () ),
'color' => $this -> style_for_context ( 'color' , $this -> context () ),
'shape' => $this -> style_for_context ( 'shape' , $this -> context () ),
'label' => $this -> style_for_context ( 'label' , $this -> context () ),
'tagline' => $this -> style_for_context ( 'tagline' , $this -> context () ),
2020-08-27 11:08:36 +03:00
),
),
2021-07-12 17:16:26 +02:00
'hosted_fields' => array (
2020-08-27 11:08:36 +03:00
'wrapper' => '#ppcp-hosted-fields' ,
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart' ,
'labels' => array (
2021-11-30 11:11:02 +01:00
'credit_card_number' => '' ,
'cvv' => '' ,
'mm_yy' => __ ( 'MM/YY' , 'woocommerce-paypal-payments' ),
'fields_not_valid' => __ (
2021-04-15 14:52:26 +02:00
'Unfortunately, your credit card details are not valid.' ,
2020-10-08 20:03:07 -03:00
'woocommerce-paypal-payments'
2020-08-27 11:08:36 +03:00
),
2021-11-30 11:11:02 +01:00
'card_not_supported' => __ (
2021-04-15 14:52:26 +02:00
'Unfortunately, we do not support your credit card.' ,
2020-10-08 20:03:07 -03:00
'woocommerce-paypal-payments'
2020-09-29 09:59:22 +03:00
),
2021-11-30 14:23:21 +01:00
'cardholder_name_required' => __ ( 'Cardholder\'s first and last name are required, please fill the checkout form required fields.' , 'woocommerce-paypal-payments' ),
2020-08-27 11:08:36 +03:00
),
2020-09-29 09:59:22 +03:00
'valid_cards' => $this -> dcc_applies -> valid_cards (),
2021-08-20 17:47:32 +03:00
'contingency' => $this -> get_3ds_contingency (),
2020-08-27 11:08:36 +03:00
),
2021-07-12 17:16:26 +02:00
'messages' => $this -> message_values (),
'labels' => array (
2020-08-27 11:08:36 +03:00
'error' => array (
'generic' => __ (
'Something went wrong. Please try again or choose another payment source.' ,
2020-10-08 20:03:07 -03:00
'woocommerce-paypal-payments'
2020-08-27 11:08:36 +03:00
),
),
),
2021-07-12 17:16:26 +02:00
'order_id' => 'pay-now' === $this -> context () ? absint ( $wp -> query_vars [ 'order-pay' ] ) : 0 ,
'single_product_buttons_enabled' => $this -> settings -> has ( 'button_product_enabled' ) && $this -> settings -> get ( 'button_product_enabled' ),
2021-07-13 09:43:30 +02:00
'mini_cart_buttons_enabled' => $this -> settings -> has ( 'button_mini-cart_enabled' ) && $this -> settings -> get ( 'button_mini-cart_enabled' ),
2020-08-27 11:08:36 +03:00
);
2020-08-31 11:12:46 +03:00
if ( $this -> style_for_context ( 'layout' , 'mini-cart' ) !== 'horizontal' ) {
2020-08-27 11:08:36 +03:00
unset ( $localize [ 'button' ][ 'mini_cart_style' ][ 'tagline' ] );
}
2020-08-31 11:12:46 +03:00
if ( $this -> style_for_context ( 'layout' , $this -> context () ) !== 'horizontal' ) {
2020-08-27 11:08:36 +03:00
unset ( $localize [ 'button' ][ 'style' ][ 'tagline' ] );
}
2020-08-31 11:12:46 +03:00
$this -> request_data -> dequeue_nonce_fix ();
2020-08-27 11:08:36 +03:00
return $localize ;
}
2020-04-13 09:07:20 +03:00
2020-08-31 11:12:46 +03:00
/**
* If we can find the payer data for a current customer , we will return it .
*
* @ return array | null
*/
2020-09-11 13:38:02 +03:00
private function payerData () {
2020-08-27 11:08:36 +03:00
$customer = WC () -> customer ;
2021-11-03 10:20:39 +02:00
if ( ! is_user_logged_in () || ! ( $customer instanceof \WC_Customer ) ) {
2020-08-27 11:08:36 +03:00
return null ;
}
2020-09-01 09:00:45 +03:00
return $this -> payer_factory -> from_customer ( $customer ) -> to_array ();
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* The JavaScript SDK url to load .
*
* @ return string
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found .
2020-08-31 11:12:46 +03:00
*/
2020-08-27 11:08:36 +03:00
private function url () : string {
2021-10-18 10:54:57 +02:00
$intent = ( $this -> settings -> has ( 'intent' ) ) ? $this -> settings -> get ( 'intent' ) : 'capture' ;
2021-10-05 12:12:19 +02:00
2020-08-27 11:08:36 +03:00
$params = array (
2020-08-31 11:12:46 +03:00
'client-id' => $this -> client_id ,
2021-11-30 10:40:38 +02:00
'currency' => $this -> currency ,
2020-10-08 07:28:43 +03:00
'integration-date' => PAYPAL_INTEGRATION_DATE ,
2020-08-27 11:08:36 +03:00
'components' => implode ( ',' , $this -> components () ),
2021-09-09 12:20:43 +02:00
'vault' => $this -> can_save_vault_token () ? 'true' : 'false' ,
2020-08-27 11:08:36 +03:00
'commit' => is_checkout () ? 'true' : 'false' ,
2021-10-05 12:12:19 +02:00
'intent' => ( $this -> subscription_helper -> cart_contains_subscription () || $this -> subscription_helper -> current_product_is_subscription () )
? 'authorize'
: $intent ,
2020-08-27 11:08:36 +03:00
);
if (
2020-10-16 13:32:26 +03:00
$this -> environment -> current_environment_is ( Environment :: SANDBOX )
&& defined ( 'WP_DEBUG' ) && \WP_DEBUG && is_user_logged_in ()
2021-11-03 10:20:39 +02:00
&& WC () -> customer instanceof \WC_Customer && WC () -> customer -> get_billing_country ()
2020-10-21 08:58:02 +03:00
&& 2 === strlen ( WC () -> customer -> get_billing_country () )
2020-08-27 11:08:36 +03:00
) {
$params [ 'buyer-country' ] = WC () -> customer -> get_billing_country ();
}
2021-09-09 12:20:43 +02:00
$disable_funding = $this -> settings -> has ( 'disable_funding' )
? $this -> settings -> get ( 'disable_funding' )
: array ();
2020-08-27 11:08:36 +03:00
if ( ! is_checkout () ) {
2020-08-31 11:12:46 +03:00
$disable_funding [] = 'card' ;
2020-08-27 11:08:36 +03:00
}
2021-09-09 12:20:43 +02:00
if ( is_checkout () && $this -> settings -> has ( 'dcc_enabled' ) && $this -> settings -> get ( 'dcc_enabled' ) ) {
$key = array_search ( 'card' , $disable_funding , true );
if ( false !== $key ) {
unset ( $disable_funding [ $key ] );
}
}
2020-09-10 10:58:41 +03:00
2021-03-17 09:43:58 +01:00
if ( count ( $disable_funding ) > 0 ) {
$params [ 'disable-funding' ] = implode ( ',' , array_unique ( $disable_funding ) );
2020-09-10 10:58:41 +03:00
}
2020-09-18 11:55:25 +03:00
2021-07-01 09:39:18 +02:00
$enable_funding = array ( 'venmo' );
2021-06-18 16:53:43 +02:00
if ( $this -> settings_status -> pay_later_messaging_is_enabled () || ! in_array ( 'credit' , $disable_funding , true ) ) {
2021-06-17 11:51:43 +02:00
$enable_funding [] = 'paylater' ;
}
if ( count ( $enable_funding ) > 0 ) {
$params [ 'enable-funding' ] = implode ( ',' , array_unique ( $enable_funding ) );
}
2020-09-18 12:02:27 +03:00
$smart_button_url = add_query_arg ( $params , 'https://www.paypal.com/sdk/js' );
2020-08-31 11:12:46 +03:00
return $smart_button_url ;
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* The attributes we need to load for the JS SDK .
*
* @ return array
*/
2020-08-27 11:08:36 +03:00
private function attributes () : array {
return array (
2020-08-31 11:12:46 +03:00
'data-partner-attribution-id' => $this -> bn_code_for_context ( $this -> context () ),
2020-08-27 11:08:36 +03:00
);
}
/**
2020-08-31 11:12:46 +03:00
* What BN Code to use in a given context .
*
* @ param string $context The context .
2020-08-27 11:08:36 +03:00
* @ return string
*/
2020-08-31 11:12:46 +03:00
private function bn_code_for_context ( string $context ) : string {
2020-08-27 11:08:36 +03:00
2020-08-31 11:12:46 +03:00
$codes = $this -> bn_codes ();
2020-08-27 11:08:36 +03:00
return ( isset ( $codes [ $context ] ) ) ? $codes [ $context ] : '' ;
}
/**
2020-08-31 11:12:46 +03:00
* BN Codes to use .
2020-08-27 11:08:36 +03:00
*
* @ return array
*/
2020-08-31 11:12:46 +03:00
private function bn_codes () : array {
2020-08-27 11:08:36 +03:00
return array (
'checkout' => 'Woo_PPCP' ,
'cart' => 'Woo_PPCP' ,
'mini-cart' => 'Woo_PPCP' ,
'product' => 'Woo_PPCP' ,
);
}
2020-08-31 11:12:46 +03:00
/**
* The JS SKD components we need to load .
*
* @ return array
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found .
2020-08-31 11:12:46 +03:00
*/
2020-08-27 11:08:36 +03:00
private function components () : array {
2020-09-03 11:40:44 +03:00
$components = array ();
2020-09-03 11:48:25 +03:00
if ( $this -> load_button_component () ) {
$components [] = 'buttons' ;
}
if ( $this -> messages_apply -> for_country () ) {
$components [] = 'messages' ;
}
if ( $this -> dcc_is_enabled () ) {
$components [] = 'hosted-fields' ;
}
return $components ;
}
/**
* Determines whether the button component should be loaded .
*
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found .
2020-09-03 11:48:25 +03:00
*/
private function load_button_component () : bool {
2020-09-03 11:40:44 +03:00
$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 (
2020-09-03 11:48:25 +03:00
$this -> settings -> has ( 'button_mini-cart_enabled' )
2020-09-03 11:40:44 +03:00
&& $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 ;
}
2021-01-05 11:35:47 +01:00
if ( $this -> context () === 'pay-now' ) {
$load_buttons = true ;
}
2020-09-03 11:48:25 +03:00
return $load_buttons ;
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* The current context .
*
* @ return string
*/
2020-08-27 11:08:36 +03:00
private function context () : string {
$context = 'mini-cart' ;
if ( is_product () || wc_post_content_has_shortcode ( 'product_page' ) ) {
$context = 'product' ;
}
if ( is_cart () ) {
$context = 'cart' ;
}
2020-08-31 11:12:46 +03:00
if ( is_checkout () && ! $this -> session_handler -> order () ) {
2020-08-27 11:08:36 +03:00
$context = 'checkout' ;
}
2020-09-30 14:24:31 +03:00
if ( is_checkout_pay_page () ) {
$context = 'pay-now' ;
}
2020-08-27 11:08:36 +03:00
return $context ;
}
2020-08-31 11:12:46 +03:00
/**
* Whether DCC is enabled or not .
*
* @ return bool
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found .
2020-08-31 11:12:46 +03:00
*/
private function dcc_is_enabled () : bool {
2020-10-01 11:37:36 +03:00
if ( ! is_checkout () ) {
return false ;
}
2020-09-01 09:00:45 +03:00
if ( ! $this -> dcc_applies -> for_country_currency () ) {
2020-08-27 11:08:36 +03:00
return false ;
}
$keys = array (
2020-10-01 11:37:36 +03:00
'client_id' ,
'client_secret' ,
'dcc_enabled' ,
2020-08-27 11:08:36 +03:00
);
2020-10-01 11:37:36 +03:00
foreach ( $keys as $key ) {
if ( ! $this -> settings -> has ( $key ) || ! $this -> settings -> get ( $key ) ) {
return false ;
2020-08-27 11:08:36 +03:00
}
}
2020-10-01 11:37:36 +03:00
return true ;
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* Determines the style for a given indicator in a given context .
*
* @ param string $style The style .
* @ param string $context The context .
*
* @ return string
2020-09-11 14:11:10 +03:00
* @ throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn ' t been found .
2020-08-31 11:12:46 +03:00
*/
private function style_for_context ( string $style , string $context ) : string {
2020-08-27 11:08:36 +03:00
$defaults = array (
'layout' => 'vertical' ,
'size' => 'responsive' ,
'color' => 'gold' ,
'shape' => 'pill' ,
'label' => 'paypal' ,
'tagline' => true ,
);
$value = isset ( $defaults [ $style ] ) ?
$defaults [ $style ] : '' ;
$value = $this -> settings -> has ( 'button_' . $style ) ?
$this -> settings -> get ( 'button_' . $style ) : $value ;
$value = $this -> settings -> has ( 'button_' . $context . '_' . $style ) ?
$this -> settings -> get ( 'button_' . $context . '_' . $style ) : $value ;
if ( is_bool ( $value ) ) {
$value = $value ? 'true' : 'false' ;
}
2020-09-02 12:47:53 +03:00
return ( string ) $value ;
2020-08-27 11:08:36 +03:00
}
2021-06-17 17:04:02 +02:00
/**
* Returns a value between 25 and 55.
*
* @ param int $height The input value .
* @ return int The normalized output value .
*/
private function normalize_height ( int $height ) : int {
if ( $height < 25 ) {
return 25 ;
}
if ( $height > 55 ) {
return 55 ;
}
return $height ;
}
2021-07-26 20:13:05 +03:00
2021-07-26 09:58:54 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns the action name that PayPal button will use for rendering on the checkout page .
2021-07-26 09:58:54 +03:00
*
* @ return string Action name .
*/
2021-07-26 20:13:05 +03:00
private function checkout_button_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal button will use for rendering on the checkout page .
*/
2021-07-26 20:13:05 +03:00
return ( string ) apply_filters ( 'woocommerce_paypal_payments_checkout_button_renderer_hook' , 'woocommerce_review_order_after_payment' );
2021-07-26 09:58:54 +03:00
}
2021-07-26 20:13:05 +03:00
2021-07-26 10:27:34 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns the action name that PayPal DCC button will use for rendering on the checkout page .
2021-07-26 10:27:34 +03:00
*
* @ return string
*/
2021-07-26 20:13:05 +03:00
private function checkout_dcc_button_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal DCC button will use for rendering on the checkout page .
*/
2021-07-26 20:13:05 +03:00
return ( string ) apply_filters ( 'woocommerce_paypal_payments_checkout_dcc_renderer_hook' , 'woocommerce_review_order_after_submit' );
2021-07-26 10:27:34 +03:00
}
2021-07-26 20:13:05 +03:00
2021-07-26 10:31:11 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns the action name that PayPal button and Pay Later message will use for rendering on the pay - order page .
2021-07-26 10:31:11 +03:00
*
* @ return string
*/
2021-07-26 20:13:05 +03:00
private function pay_order_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal button and Pay Later message will use for rendering on the pay - order page .
*/
2021-07-26 20:13:05 +03:00
return ( string ) apply_filters ( 'woocommerce_paypal_payments_pay_order_dcc_renderer_hook' , 'woocommerce_pay_order_after_submit' );
2021-07-26 10:31:11 +03:00
}
2021-07-26 20:13:05 +03:00
2021-07-26 10:18:43 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns action name that PayPal button will use for rendering next to Proceed to checkout button ( normally displayed in cart ) .
2021-07-26 10:18:43 +03:00
*
* @ return string
*/
2021-07-26 20:13:05 +03:00
private function proceed_to_checkout_button_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal button will use for rendering next to Proceed to checkout button ( normally displayed in cart ) .
*/
2021-07-26 10:18:43 +03:00
return ( string ) apply_filters (
2021-07-26 19:06:23 +03:00
'woocommerce_paypal_payments_proceed_to_checkout_button_renderer_hook' ,
2021-07-26 10:18:43 +03:00
'woocommerce_proceed_to_checkout'
);
}
2021-07-26 20:13:05 +03:00
2021-07-26 10:50:52 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns the action name that PayPal button will use for rendering in the WC mini cart .
2021-07-26 10:50:52 +03:00
*
* @ return string
*/
2021-07-26 20:13:05 +03:00
private function mini_cart_button_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal button will use for rendering in the WC mini cart .
*/
2021-07-26 10:50:52 +03:00
return ( string ) apply_filters (
'woocommerce_paypal_payments_mini_cart_button_renderer_hook' ,
'woocommerce_widget_shopping_cart_after_buttons'
);
}
2021-07-26 20:13:05 +03:00
2021-07-26 18:36:29 +03:00
/**
2022-02-17 18:19:55 +02:00
* Returns the action name that PayPal button and Pay Later message will use for rendering on the single product page .
2021-07-26 18:36:29 +03:00
*
* @ return string
*/
2021-07-26 20:13:05 +03:00
private function single_product_renderer_hook () : string {
2022-02-17 18:19:55 +02:00
/**
* The filter returning the action name that PayPal button and Pay Later message will use for rendering on the single product page .
*/
2021-07-26 20:13:05 +03:00
return ( string ) apply_filters ( 'woocommerce_paypal_payments_single_product_renderer_hook' , 'woocommerce_single_product_summary' );
2021-07-26 18:36:29 +03:00
}
2022-02-17 20:01:47 +04:00
/**
* Check if cart product price total is 0.
*
* @ return bool true if is 0 , otherwise false .
*/
protected function is_cart_price_total_zero () : bool {
2022-02-18 17:56:33 +04:00
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
return WC () -> cart -> get_cart_contents_total () == 0 ;
2022-02-17 20:01:47 +04:00
}
2020-04-06 11:16:18 +03:00
}