This commit is contained in:
David Remer 2020-08-19 05:39:45 +03:00
parent 715b748b87
commit 54652951bb
7 changed files with 121 additions and 65 deletions

View file

@ -169,7 +169,8 @@ class SmartButton implements SmartButtonInterface
return true;
}
public function buttonRenderer() {
public function buttonRenderer()
{
$product = wc_get_product();
if (
! is_checkout() && is_a($product, \WC_Product::class)
@ -185,7 +186,9 @@ class SmartButton implements SmartButtonInterface
$this->renderMessage();
}
private function renderMessage() {
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
private function renderMessage()
{
$markup = '<div
data-pp-message
data-pp-placement="%s"
@ -194,38 +197,58 @@ class SmartButton implements SmartButtonInterface
></div>';
$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') ? $this->settings->get('message_product_layout') : 'text';
$logoType = $this->settings->has('message_product_logo') ? $this->settings->get('message_product_logo') : 'primary';
$logoPosition = $this->settings->has('message_product_position') ? $this->settings->get('message_product_position') : 'left';
$textColor = $this->settings->has('message_product_color') ? $this->settings->get('message_product_color') : 'black';
$styleColor = $this->settings->has('message_product_flex_color') ? $this->settings->get('message_product_flex_color') : 'blue';
$ratio = $this->settings->has('message_product_flex_ratio') ? $this->settings->get('message_product_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_product_enabled') && $this->settings->get('message_product_enabled');
$layout = $this->settings->has('message_product_layout') ?
$this->settings->get('message_product_layout') : 'text';
$logoType = $this->settings->has('message_product_logo') ?
$this->settings->get('message_product_logo') : 'primary';
$logoPosition = $this->settings->has('message_product_position') ?
$this->settings->get('message_product_position') : 'left';
$textColor = $this->settings->has('message_product_color') ?
$this->settings->get('message_product_color') : 'black';
$styleColor = $this->settings->has('message_product_flex_color') ?
$this->settings->get('message_product_flex_color') : 'blue';
$ratio = $this->settings->has('message_product_flex_ratio') ?
$this->settings->get('message_product_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_product_enabled')
&& $this->settings->get('message_product_enabled');
if (is_checkout()) {
$placement = 'payment';
$amount = WC()->cart->get_total('raw');
$layout = $this->settings->has('message_layout') ? $this->settings->get('message_layout') : 'text';
$logoType = $this->settings->has('message_logo') ? $this->settings->get('message_logo') : 'primary';
$logoPosition = $this->settings->has('message_position') ? $this->settings->get('message_position') : 'left';
$textColor = $this->settings->has('message_color') ? $this->settings->get('message_color') : 'black';
$styleColor = $this->settings->has('message_flex_color') ? $this->settings->get('message_flex_color') : 'blue';
$ratio = $this->settings->has('message_flex_ratio') ? $this->settings->get('message_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_enabled') && $this->settings->get('message_enabled');
$layout = $this->settings->has('message_layout') ?
$this->settings->get('message_layout') : 'text';
$logoType = $this->settings->has('message_logo') ?
$this->settings->get('message_logo') : 'primary';
$logoPosition = $this->settings->has('message_position') ?
$this->settings->get('message_position') : 'left';
$textColor = $this->settings->has('message_color') ?
$this->settings->get('message_color') : 'black';
$styleColor = $this->settings->has('message_flex_color') ?
$this->settings->get('message_flex_color') : 'blue';
$ratio = $this->settings->has('message_flex_ratio') ?
$this->settings->get('message_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_enabled')
&& $this->settings->get('message_enabled');
}
if (is_cart()) {
$placement = 'cart';
$amount = WC()->cart->get_total('raw');
$layout = $this->settings->has('message_cart_layout') ? $this->settings->get('message_cart_layout') : 'text';
$logoType = $this->settings->has('message_cart_logo') ? $this->settings->get('message_cart_logo') : 'primary';
$logoPosition = $this->settings->has('message_cart_position') ? $this->settings->get('message_cart_position') : 'left';
$textColor = $this->settings->has('message_cart_color') ? $this->settings->get('message_cart_color') : 'black';
$styleColor = $this->settings->has('message_cart_flex_color') ? $this->settings->get('message_cart_flex_color') : 'blue';
$ratio = $this->settings->has('message_cart_flex_ratio') ? $this->settings->get('message_cart_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_cart_enabled') && $this->settings->get('message_cart_enabled');
$layout = $this->settings->has('message_cart_layout') ?
$this->settings->get('message_cart_layout') : 'text';
$logoType = $this->settings->has('message_cart_logo') ?
$this->settings->get('message_cart_logo') : 'primary';
$logoPosition = $this->settings->has('message_cart_position') ?
$this->settings->get('message_cart_position') : 'left';
$textColor = $this->settings->has('message_cart_color') ?
$this->settings->get('message_cart_color') : 'black';
$styleColor = $this->settings->has('message_cart_flex_color') ?
$this->settings->get('message_cart_flex_color') : 'blue';
$ratio = $this->settings->has('message_cart_flex_ratio') ?
$this->settings->get('message_cart_flex_ratio') : '1x1';
$shouldShow = $this->settings->has('message_cart_enabled')
&& $this->settings->get('message_cart_enabled');
}
if (! $shouldShow) {
@ -235,7 +258,7 @@ class SmartButton implements SmartButtonInterface
'data-pp-message' => '',
'data-pp-placement' => $placement,
'data-pp-amount' => $amount,
'data-pp-style-layout' => $layout
'data-pp-style-layout' => $layout,
];
if ($layout === 'text') {
$attributes['data-pp-style-logo-type'] = $logoType;
@ -253,7 +276,6 @@ class SmartButton implements SmartButtonInterface
echo '></div>';
}
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
public function dccRenderer()
{
@ -433,7 +455,8 @@ class SmartButton implements SmartButtonInterface
//ToDo: Update date on releases.
'integration-date' => date('Y-m-d'),
'components' => implode(',', $this->components()),
'vault' => (is_checkout() && $this->dccIsEnabled()) || $this->canSaveVaultToken() ? 'true' : 'false',
'vault' => (is_checkout() && $this->dccIsEnabled()) || $this->canSaveVaultToken() ?
'true' : 'false',
'commit' => is_checkout() ? 'true' : 'false',
'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture',
];

View file

@ -38,11 +38,11 @@ class CheckoutPayPalAddressPreset
*/
public function filterCheckoutFiled($defaultValue, $fieldId): ?string
{
if(! is_string($defaultValue)) {
if (! is_string($defaultValue)) {
$defaultValue = null;
}
if(!is_string($fieldId)) {
if (!is_string($fieldId)) {
return $defaultValue;
}
@ -51,7 +51,8 @@ class CheckoutPayPalAddressPreset
private function readPresetForField(string $fieldId): ?string
{
if(!$order = $this->sessionHandler->order()) {
$order = $this->sessionHandler->order();
if (! $order) {
return null;
}
@ -64,7 +65,7 @@ class CheckoutPayPalAddressPreset
'billing_postcode' => 'postalCode',
'billing_country' => 'countryCode',
'billing_city' => 'adminArea2',
'billing_state' => 'adminArea1'
'billing_state' => 'adminArea1',
];
$payerNameMap = [
'billing_last_name' => 'surname',
@ -77,19 +78,24 @@ class CheckoutPayPalAddressPreset
'billing_phone' => 'nationalNumber',
];
if(array_key_exists($fieldId, $addressMap) && $shipping) {
if (array_key_exists($fieldId, $addressMap) && $shipping) {
return $shipping->address()->{$addressMap[$fieldId]}() ?: null;
}
if(array_key_exists($fieldId, $payerNameMap) && $payer) {
if (array_key_exists($fieldId, $payerNameMap) && $payer) {
return $payer->name()->{$payerNameMap[$fieldId]}() ?: null;
}
if(array_key_exists($fieldId, $payerMap) && $payer) {
if (array_key_exists($fieldId, $payerMap) && $payer) {
return $payer->{$payerMap[$fieldId]}() ?: null;
}
if(array_key_exists($fieldId, $payerPhoneMap) && $payer && $payer->phone() && $payer->phone()->phone()) {
if (
array_key_exists($fieldId, $payerPhoneMap)
&& $payer
&& $payer->phone()
&& $payer->phone()->phone()
) {
return $payer->phone()->phone()->{$payerPhoneMap[$fieldId]}() ?: null;
}
@ -98,17 +104,19 @@ class CheckoutPayPalAddressPreset
private function readShippingFromOrder(): ?Shipping
{
if(!$order = $this->sessionHandler->order()) {
$order = $this->sessionHandler->order();
if (! $order) {
return null;
}
if(array_key_exists($order->id(), $this->shippingCache)) {
if (array_key_exists($order->id(), $this->shippingCache)) {
return $this->shippingCache[$order->id()];
}
$shipping = null;
foreach($this->sessionHandler->order()->purchaseUnits() as $unit) {
if($shipping = $unit->shipping()) {
foreach ($this->sessionHandler->order()->purchaseUnits() as $unit) {
$shipping = $unit->shipping();
if ($shipping) {
break;
}
}

View file

@ -41,7 +41,6 @@ class DisableGateways
unset($methods[CreditCardGateway::ID]);
}
if (! $this->needsToDisableGateways()) {
return $methods;
}
@ -57,13 +56,13 @@ class DisableGateways
return $this->sessionHandler->order() !== null;
}
private function isCreditCard() : bool
private function isCreditCard(): bool
{
$order =$this->sessionHandler->order();
$order = $this->sessionHandler->order();
if (! $order) {
return false;
}
if ( ! $order->paymentSource() || ! $order->paymentSource()->card()) {
if (! $order->paymentSource() || ! $order->paymentSource()->card()) {
return false;
}
return true;

View file

@ -1,20 +1,21 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Psr\Container\ContainerInterface;
//phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
class CreditCardGateway extends PayPalGateway
{
public const ID = 'ppcp-credit-card-gateway';
public function __construct(
SettingsRenderer $settingsRenderer,
OrderProcessor $orderProcessor,
@ -82,7 +83,6 @@ class CreditCardGateway extends PayPalGateway
];
}
public function generate_ppcp_html(): string
{
@ -92,5 +92,4 @@ class CreditCardGateway extends PayPalGateway
ob_end_clean();
return $content;
}
}
}

View file

@ -46,6 +46,7 @@ class SettingsListener
* Nonce verification is done in self::isValidUpdateRequest
*/
//phpcs:disable WordPress.Security.NonceVerification.Missing
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if (isset($_POST['save']) && sanitize_text_field(wp_unslash($_POST['save'])) === 'reset') {
$this->settings->reset();
$this->settings->persist();
@ -57,6 +58,7 @@ class SettingsListener
}
//phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
//phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
/**
* Sanitization is done at a later stage.
*/
@ -77,21 +79,34 @@ class SettingsListener
if ($this->cache->has(PayPalBearer::CACHE_KEY)) {
$this->cache->delete(PayPalBearer::CACHE_KEY);
}
//phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
//phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
//phpcs:enable WordPress.Security.NonceVerification.Missing
}
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
//phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
private function retrieveSettingsFromRawData(array $rawData): array
{
if (! isset($_GET['section'])) {
return [];
}
$settings = [];
foreach ($this->settingFields as $key => $config) {
if (! in_array($this->state->currentState(), $config['screens'], true)) {
continue;
}
if ($config['gateway'] === 'dcc' && wp_unslash(sanitize_text_field($_GET['section'])) !== 'ppcp-credit-card-gateway') {
if (
$config['gateway'] === 'dcc'
&& sanitize_text_field(wp_unslash($_GET['section'])) !== 'ppcp-credit-card-gateway'
) {
continue;
}
if ($config['gateway'] === 'paypal' && wp_unslash(sanitize_text_field($_GET['section'])) !== 'ppcp-gateway') {
if (
$config['gateway'] === 'paypal'
&& sanitize_text_field(wp_unslash($_GET['section'])) !== 'ppcp-gateway'
) {
continue;
}
switch ($config['type']) {
@ -141,7 +156,11 @@ class SettingsListener
if (
! isset($_REQUEST['section'])
|| ! in_array(sanitize_text_field(wp_unslash($_REQUEST['section'])), ['ppcp-gateway', 'ppcp-credit-card-gateway'],true)
|| ! in_array(
sanitize_text_field(wp_unslash($_REQUEST['section'])),
['ppcp-gateway', 'ppcp-credit-card-gateway'],
true
)
) {
return false;
}

View file

@ -134,10 +134,10 @@ class SettingsRenderer
if (! in_array($this->state->currentState(), $config['screens'], true)) {
continue;
}
if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'],true)) {
if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'], true)) {
continue;
}
if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'],true)) {
if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'], true)) {
continue;
}
if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) {
@ -164,10 +164,10 @@ class SettingsRenderer
endif; ?>
</th>
<?php endif; ?>
<<?php echo $thTd; ?> colspan="<?php echo (int) $colspan; ?>"><?php
<<?php echo esc_attr($thTd); ?> colspan="<?php echo (int) $colspan; ?>"><?php
$config['type'] === 'ppcp-text' ?
$this->renderText($config)
: woocommerce_form_field($id, $config, $value); ?></<?php echo $thTd; ?>>
: woocommerce_form_field($id, $config, $value); ?></<?php echo esc_attr($thTd); ?>>
</tr>
<?php endforeach;
}

View file

@ -37,6 +37,7 @@ class WcGatewayModule implements ModuleInterface
$this->registerOrderFunctionality($container);
$this->registerColumns($container);
$this->registerCheckoutAddressPreset($container);
$this->ajaxGatewayEnabler($container);
add_filter(
Repository::NOTICES_FILTER,
@ -58,7 +59,16 @@ class WcGatewayModule implements ModuleInterface
return $notices;
}
);
add_action(
'woocommerce-paypal-commerce-gateway.deactivate',
static function () use ($container) {
delete_option(Settings::KEY);
}
);
}
private function ajaxGatewayEnabler(ContainerInterface $container)
{
add_action(
'wp_ajax_woocommerce_toggle_gateway_enabled',
static function () use ($container) {
@ -78,7 +88,7 @@ class WcGatewayModule implements ModuleInterface
*/
$settings = $container->get('wcgateway.settings');
$key = $_POST['gateway_id'] === PayPalGateway::ID ? 'enabled' : '';
if ($_POST['gateway_id'] === CreditCardGateway::ID ) {
if ($_POST['gateway_id'] === CreditCardGateway::ID) {
$key = 'dcc_gateway_enabled';
}
if (! $key) {
@ -93,12 +103,6 @@ class WcGatewayModule implements ModuleInterface
},
9
);
add_action(
'woocommerce-paypal-commerce-gateway.deactivate',
static function () use ($container) {
delete_option(Settings::KEY);
}
);
}
private function registerPaymentGateWay(ContainerInterface $container)
@ -224,9 +228,13 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'woocommerce_checkout_get_value',
static function (...$args) use ($container) {
// It's important to not instantiate the service to early
// as it depends on SessionHandler and WooCommerce Session
/* @var CheckoutPayPalAddressPreset $service */
// Its important to not instantiate the service too early as it
// depends on SessionHandler and WooCommerce Session
/**
* @var CheckoutPayPalAddressPreset $service
*/
$service = $container->get('wcgateway.checkout.address-preset');
return $service->filterCheckoutFiled(...$args);