Merge branch 'trunk' into PCP-860-apm

This commit is contained in:
Alex P 2023-06-27 15:54:06 +03:00
commit 2b566fcf44
No known key found for this signature in database
GPG key ID: 54487A734A204D71
14 changed files with 292 additions and 43 deletions

View file

@ -112,7 +112,7 @@ class PurchaseUnitFactory {
$items = array_filter(
$this->item_factory->from_wc_order( $order ),
function ( Item $item ): bool {
return $item->unit_amount()->value() > 0;
return $item->unit_amount()->value() >= 0;
}
);
$shipping = $this->shipping_factory->from_wc_order( $order );
@ -168,7 +168,7 @@ class PurchaseUnitFactory {
$items = array_filter(
$this->item_factory->from_wc_cart( $cart ),
function ( Item $item ): bool {
return $item->unit_amount()->value() > 0;
return $item->unit_amount()->value() >= 0;
}
);

View file

@ -102,6 +102,9 @@ class CheckoutActionHandler {
} else {
errorHandler.message(data.data.message);
}
// fire WC event for other plugins
jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] );
}
throw {type: 'create-order-error', data: data.data};

View file

@ -40,6 +40,10 @@ class FreeTrialHandler {
if (errors.length > 0) {
this.spinner.unblock();
this.errorHandler.messages(errors);
// fire WC event for other plugins
jQuery( document.body ).trigger( 'checkout_error' , [ this.errorHandler.currentHtml() ] );
return;
}
} catch (error) {

View file

@ -40,6 +40,15 @@ class ErrorHandler {
this._scrollToMessages();
}
/**
* @returns {String}
*/
currentHtml()
{
const messageContainer = this._getMessageContainer();
return messageContainer.outerHTML;
}
/**
* @private
* @param {String} text

View file

@ -10,6 +10,7 @@ class CreditCardRenderer {
this.spinner = spinner;
this.cardValid = false;
this.formValid = false;
this.emptyFields = new Set(['number', 'cvv', 'expirationDate']);
this.currentHostedFieldsInstance = null;
}
@ -130,7 +131,7 @@ class CreditCardRenderer {
return event.fields[key].isValid;
});
const className = this._cardNumberFiledCLassNameByCardType(event.cards[0].type);
const className = event.cards.length ? this._cardNumberFiledCLassNameByCardType(event.cards[0].type) : '';
event.fields.number.isValid
? cardNumber.classList.add(className)
: this._recreateElementClassAttribute(cardNumber, cardNumberField.className);
@ -138,6 +139,12 @@ class CreditCardRenderer {
this.formValid = formValid;
});
hostedFields.on('empty', (event) => {
this.emptyFields.add(event.emittedBy);
});
hostedFields.on('notEmpty', (event) => {
this.emptyFields.delete(event.emittedBy);
});
show(buttonSelector);
@ -249,7 +256,16 @@ class CreditCardRenderer {
});
} else {
this.spinner.unblock();
const message = ! this.cardValid ? this.defaultConfig.hosted_fields.labels.card_not_supported : this.defaultConfig.hosted_fields.labels.fields_not_valid;
let message = this.defaultConfig.labels.error.generic;
if (this.emptyFields.size > 0) {
message = this.defaultConfig.hosted_fields.labels.fields_empty;
} else if (!this.cardValid) {
message = this.defaultConfig.hosted_fields.labels.card_not_supported;
} else if (!this.formValid) {
message = this.defaultConfig.hosted_fields.labels.fields_not_valid;
}
this.errorHandler.message(message);
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Assets;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Product;
use WC_Product_Variation;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
@ -806,8 +807,6 @@ class SmartButton implements SmartButtonInterface {
* @return array
*/
public function script_data(): array {
global $wp;
$is_free_trial_cart = $this->is_free_trial_cart();
$url_params = $this->url_params();
@ -903,6 +902,10 @@ class SmartButton implements SmartButtonInterface {
'credit_card_number' => '',
'cvv' => '',
'mm_yy' => __( 'MM/YY', 'woocommerce-paypal-payments' ),
'fields_empty' => __(
'Card payment details are missing. Please fill in all required fields.',
'woocommerce-paypal-payments'
),
'fields_not_valid' => __(
'Unfortunately, your credit card details are not valid.',
'woocommerce-paypal-payments'
@ -944,7 +947,7 @@ class SmartButton implements SmartButtonInterface {
// phpcs:ignore WordPress.WP.I18n
'shipping_field' => _x( 'Shipping %s', 'checkout-validation', 'woocommerce' ),
),
'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0,
'order_id' => 'pay-now' === $this->context() ? $this->get_order_pay_id() : 0,
'single_product_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'product' ),
'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ),
'basic_checkout_validation_enabled' => $this->basic_checkout_validation_enabled,
@ -1018,6 +1021,19 @@ class SmartButton implements SmartButtonInterface {
$params['buyer-country'] = WC()->customer->get_billing_country();
}
if ( 'pay-now' === $this->context() ) {
$wc_order_id = $this->get_order_pay_id();
if ( $wc_order_id ) {
$wc_order = wc_get_order( $wc_order_id );
if ( $wc_order instanceof WC_Order ) {
$currency = $wc_order->get_currency();
if ( $currency ) {
$params['currency'] = $currency;
}
}
}
}
$disable_funding = $this->settings->has( 'disable_funding' )
? $this->settings->get( 'disable_funding' )
: array();
@ -1444,4 +1460,19 @@ class SmartButton implements SmartButtonInterface {
return $this->context() === 'product' ? $product_intent : $other_context_intent;
}
/**
* Returns the ID of WC order on the order-pay page, or 0.
*
* @return int
*/
protected function get_order_pay_id(): int {
global $wp;
if ( ! isset( $wp->query_vars['order-pay'] ) ) {
return 0;
}
return absint( $wp->query_vars['order-pay'] );
}
}

View file

@ -57,6 +57,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\UnsupportedCurrencyAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
@ -208,6 +209,12 @@ return array(
$settings = $container->get( 'wcgateway.settings' );
return new ConnectAdminNotice( $state, $settings );
},
'wcgateway.notice.currency-unsupported' => static function ( ContainerInterface $container ): UnsupportedCurrencyAdminNotice {
$state = $container->get( 'onboarding.state' );
$shop_currency = $container->get( 'api.shop.currency' );
$supported_currencies = $container->get( 'api.supported-currencies' );
return new UnsupportedCurrencyAdminNotice( $state, $shop_currency, $supported_currencies );
},
'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice {
return new GatewayWithoutPayPalAdminNotice(
CreditCardGateway::ID,
@ -223,7 +230,8 @@ return array(
$container->get( 'onboarding.state' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.is-wc-payments-page' ),
$container->get( 'wcgateway.is-ppcp-settings-page' )
$container->get( 'wcgateway.is-ppcp-settings-page' ),
$container->get( 'wcgateway.settings.status' )
);
},
'wcgateway.notice.authorize-order-action' =>

View file

@ -84,8 +84,11 @@ class DisableGateways {
unset( $methods[ CreditCardGateway::ID ] );
}
if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) && ! $this->session_handler->order() && is_checkout() ) {
unset( $methods[ PayPalGateway::ID ] );
if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
unset( $methods[ CardButtonGateway::ID ] );
if ( ! $this->session_handler->order() && is_checkout() ) {
unset( $methods[ PayPalGateway::ID ] );
}
}
if ( ! $this->needs_to_disable_gateways() ) {

View file

@ -277,6 +277,27 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->order_endpoint = $order_endpoint;
}
/**
* Return the gateway's title.
*
* @return string
*/
public function get_title() {
if ( is_admin() ) {
// $theorder and other things for retrieving the order or post info are not available
// in the constructor, so must do it here.
global $theorder;
if ( $theorder instanceof WC_Order ) {
$payment_method_title = $theorder->get_payment_method_title();
if ( $payment_method_title ) {
$this->title = $payment_method_title;
}
}
}
return parent::get_title();
}
/**
* Whether the Gateway needs to be setup.
*

View file

@ -13,11 +13,16 @@ use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
/**
* Creates the admin message about the gateway being enabled without the PayPal gateway.
*/
class GatewayWithoutPayPalAdminNotice {
private const NOTICE_OK = '';
private const NOTICE_DISABLED_GATEWAY = 'disabled_gateway';
private const NOTICE_DISABLED_LOCATION = 'disabled_location';
/**
* The gateway ID.
*
@ -53,27 +58,37 @@ class GatewayWithoutPayPalAdminNotice {
*/
private $is_ppcp_settings_page;
/**
* The Settings status helper.
*
* @var SettingsStatus|null
*/
protected $settings_status;
/**
* ConnectAdminNotice constructor.
*
* @param string $id The gateway ID.
* @param State $state The state.
* @param ContainerInterface $settings The settings.
* @param bool $is_payments_page Whether the current page is the WC payment page.
* @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page.
* @param string $id The gateway ID.
* @param State $state The state.
* @param ContainerInterface $settings The settings.
* @param bool $is_payments_page Whether the current page is the WC payment page.
* @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page.
* @param SettingsStatus|null $settings_status The Settings status helper.
*/
public function __construct(
string $id,
State $state,
ContainerInterface $settings,
bool $is_payments_page,
bool $is_ppcp_settings_page
bool $is_ppcp_settings_page,
?SettingsStatus $settings_status = null
) {
$this->id = $id;
$this->state = $state;
$this->settings = $settings;
$this->is_payments_page = $is_payments_page;
$this->is_ppcp_settings_page = $is_ppcp_settings_page;
$this->settings_status = $settings_status;
}
/**
@ -82,8 +97,25 @@ class GatewayWithoutPayPalAdminNotice {
* @return Message|null
*/
public function message(): ?Message {
if ( ! $this->should_display() ) {
return null;
$notice_type = $this->check();
switch ( $notice_type ) {
case self::NOTICE_DISABLED_GATEWAY:
/* translators: %1$s the gateway name, %2$s URL. */
$text = __(
'%1$s cannot be used without the PayPal gateway. <a href="%2$s">Enable the PayPal gateway</a>.',
'woocommerce-paypal-payments'
);
break;
case self::NOTICE_DISABLED_LOCATION:
/* translators: %1$s the gateway name, %2$s URL. */
$text = __(
'%1$s cannot be used without enabling the Checkout location for the PayPal gateway. <a href="%2$s">Enable the Checkout location</a>.',
'woocommerce-paypal-payments'
);
break;
default:
return null;
}
$gateway = $this->get_gateway();
@ -94,11 +126,7 @@ class GatewayWithoutPayPalAdminNotice {
$name = $gateway->get_method_title();
$message = sprintf(
/* translators: %1$s the gateway name, %2$s URL. */
__(
'%1$s cannot be used without the PayPal gateway. <a href="%2$s">Enable the PayPal gateway</a>.',
'woocommerce-paypal-payments'
),
$text,
$name,
admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' )
);
@ -106,22 +134,33 @@ class GatewayWithoutPayPalAdminNotice {
}
/**
* Whether the message should be displayed.
* Checks whether one of the messages should be displayed.
*
* @return bool
* @return string One of the NOTICE_* constants.
*/
protected function should_display(): bool {
protected function check(): string {
if ( State::STATE_ONBOARDED !== $this->state->current_state() ||
( ! $this->is_payments_page && ! $this->is_ppcp_settings_page ) ) {
return false;
}
if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) {
return false;
return self::NOTICE_OK;
}
$gateway = $this->get_gateway();
$gateway = $this->get_gateway();
$gateway_enabled = $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) );
return $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) );
if ( ! $gateway_enabled ) {
return self::NOTICE_OK;
}
$paypal_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' );
if ( ! $paypal_enabled ) {
return self::NOTICE_DISABLED_GATEWAY;
}
if ( $this->settings_status && ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
return self::NOTICE_DISABLED_LOCATION;
}
return self::NOTICE_OK;
}
/**

View file

@ -0,0 +1,97 @@
<?php
/**
* Registers the admin message about unsupported currency set in WC shop settings.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Notice
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Notice;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
/**
* Class UnsupportedCurrencyAdminNotice
*/
class UnsupportedCurrencyAdminNotice {
/**
* The state.
*
* @var State
*/
private $state;
/**
* The supported currencies.
*
* @var array
*/
private $supported_currencies;
/**
* The shop currency.
*
* @var string
*/
private $shop_currency;
/**
* UnsupportedCurrencyAdminNotice constructor.
*
* @param State $state The state.
* @param string $shop_currency The shop currency.
* @param array $supported_currencies The supported currencies.
*/
public function __construct( State $state, string $shop_currency, array $supported_currencies ) {
$this->state = $state;
$this->shop_currency = $shop_currency;
$this->supported_currencies = $supported_currencies;
}
/**
* Returns the message.
*
* @return Message|null
*/
public function unsupported_currency_message() {
if ( ! $this->should_display() ) {
return null;
}
$message = sprintf(
/* translators: %1$s the shop currency, 2$s the gateway name. */
__(
'Attention: Your current WooCommerce store currency (%1$s) is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the <a href="%2$s">PayPal currency support page</a> for more information on supported currencies.',
'woocommerce-paypal-payments'
),
$this->shop_currency,
'https://developer.paypal.com/api/rest/reference/currency-codes/'
);
return new Message( $message, 'warning' );
}
/**
* Whether the message should display.
*
* @return bool
*/
protected function should_display(): bool {
return $this->state->current_state() === State::STATE_ONBOARDED && ! $this->currency_supported();
}
/**
* Whether the currency is supported by PayPal.
*
* @return bool
*/
private function currency_supported(): bool {
$currency = $this->shop_currency;
$supported_currencies = $this->supported_currencies;
return in_array( $currency, $supported_currencies, true );
}
}

View file

@ -25,7 +25,7 @@ return function ( ContainerInterface $container, array $fields ): array {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$vault_enabled = $settings->has( 'vault_enabled' ) && $settings->get( 'vault_enabled' );
$vault_enabled = false;//$settings->has( 'vault_enabled' ) && $settings->get( 'vault_enabled' );
$pay_later_messaging_enabled_label = $vault_enabled
? __( "You have PayPal vaulting enabled, that's why Pay Later options are unavailable now. You cannot use both features at the same time.", 'woocommerce-paypal-payments' )
@ -33,11 +33,12 @@ return function ( ContainerInterface $container, array $fields ): array {
$selected_country = $container->get( 'api.shop.country' );
$default_messaging_flex_color = $selected_country === 'US' ? 'white-no-border' : 'white';
$render_preview_element = function ( string $id, string $type ): string {
$button_message = __( 'Pay Later Button Preview', 'woocommerce-paypal-payments' );
$messaging_message = __( 'Pay Later Messaging Preview', 'woocommerce-paypal-payments' );
$render_preview_element = function ( string $id, string $type, string $message ): string {
return '
<div class="ppcp-preview ppcp-' . $type . '-preview pay-later">
<h4>' . __( 'Preview', 'woocommerce-paypal-payments' ) . '</h4>
<h4>' . $message . '</h4>
<div id="' . $id . '" class="ppcp-' . $type . '-preview-inner"></div>
</div>';
};
@ -82,7 +83,7 @@ return function ( ContainerInterface $container, array $fields ): array {
),
'pay_later_button_preview' => array(
'type' => 'ppcp-text',
'text' => $render_preview_element( 'ppcpPayLaterButtonPreview', 'button' ),
'text' => $render_preview_element( 'ppcpPayLaterButtonPreview', 'button', $button_message ),
'screens' => array( State::STATE_ONBOARDED ),
'requirements' => array( 'messages' ),
'gateway' => Settings::PAY_LATER_TAB_ID,
@ -245,7 +246,7 @@ return function ( ContainerInterface $container, array $fields ): array {
),
'pay_later_general_message_preview' => array(
'type' => 'ppcp-text',
'text' => $render_preview_element( 'ppcpGeneralMessagePreview', 'message' ),
'text' => $render_preview_element( 'ppcpGeneralMessagePreview', 'message', $messaging_message ),
'screens' => array( State::STATE_ONBOARDED ),
'requirements' => array( 'messages' ),
'gateway' => Settings::PAY_LATER_TAB_ID,
@ -369,7 +370,7 @@ return function ( ContainerInterface $container, array $fields ): array {
),
'pay_later_product_message_preview' => array(
'type' => 'ppcp-text',
'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message' ),
'text' => $render_preview_element( 'ppcpProductMessagePreview', 'message', $messaging_message ),
'screens' => array( State::STATE_ONBOARDED ),
'requirements' => array( 'messages' ),
'gateway' => Settings::PAY_LATER_TAB_ID,
@ -493,7 +494,7 @@ return function ( ContainerInterface $container, array $fields ): array {
),
'pay_later_cart_message_preview' => array(
'type' => 'ppcp-text',
'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message' ),
'text' => $render_preview_element( 'ppcpCartMessagePreview', 'message', $messaging_message ),
'screens' => array( State::STATE_ONBOARDED ),
'requirements' => array( 'messages' ),
'gateway' => Settings::PAY_LATER_TAB_ID,
@ -617,7 +618,7 @@ return function ( ContainerInterface $container, array $fields ): array {
),
'pay_later_checkout_message_preview' => array(
'type' => 'ppcp-text',
'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message' ),
'text' => $render_preview_element( 'ppcpCheckoutMessagePreview', 'message', $messaging_message ),
'screens' => array( State::STATE_ONBOARDED ),
'requirements' => array( 'messages' ),
'gateway' => Settings::PAY_LATER_TAB_ID,

View file

@ -31,7 +31,7 @@ return function ( ContainerInterface $container, array $fields ): array {
$render_preview_element = function ( string $id ): string {
return '
<div class="ppcp-preview ppcp-button-preview">
<h4>' . __( 'Preview', 'woocommerce-paypal-payments' ) . '</h4>
<h4>' . __( 'Button Styling Preview', 'woocommerce-paypal-payments' ) . '</h4>
<div id="' . $id . '" class="ppcp-button-preview-inner"></div>
</div>';
};

View file

@ -39,6 +39,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\GatewayWithoutPayPalAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\UnsupportedCurrencyAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\HeaderRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
@ -197,6 +198,13 @@ class WCGatewayModule implements ModuleInterface {
$notices[] = $connect_message;
}
$notice = $c->get( 'wcgateway.notice.currency-unsupported' );
assert( $notice instanceof UnsupportedCurrencyAdminNotice );
$unsupported_currency_message = $notice->unsupported_currency_message();
if ( $unsupported_currency_message ) {
$notices[] = $unsupported_currency_message;
}
foreach ( array(
$c->get( 'wcgateway.notice.dcc-without-paypal' ),
$c->get( 'wcgateway.notice.card-button-without-paypal' ),
@ -278,6 +286,15 @@ class WCGatewayModule implements ModuleInterface {
$settings->set( 'products_dcc_enabled', false );
$settings->set( 'products_pui_enabled', false );
$settings->persist();
// Update caches.
$dcc_status = $c->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_status instanceof DCCProductStatus );
$dcc_status->dcc_is_active();
$pui_status = $c->get( 'wcgateway.pay-upon-invoice-product-status' );
assert( $pui_status instanceof PayUponInvoiceProductStatus );
$pui_status->pui_is_active();
}
);
@ -615,7 +632,7 @@ class WCGatewayModule implements ModuleInterface {
* @var OrderTablePaymentStatusColumn $payment_status_column
*/
$payment_status_column = $container->get( 'wcgateway.admin.orders-payment-status-column' );
$payment_status_column->render( $column, intval( $wc_order_id ) );
$payment_status_column->render( (string) $column, intval( $wc_order_id ) );
},
10,
2