Merge branch 'trunk' into pcp-328-mismatch

This commit is contained in:
Alex P 2022-06-20 16:35:37 +03:00
commit 84a5d9cd0f
22 changed files with 215 additions and 58 deletions

View file

@ -31,6 +31,19 @@ const bootstrap = () => {
const onSmartButtonClick = (data, actions) => {
window.ppcpFundingSource = data.fundingSource;
// TODO: quick fix to get the error about empty form before attempting PayPal order
// it should solve #513 for most of the users, but proper solution should be implemented later.
const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
requiredFields.each((i, input) => {
jQuery(input).trigger('validate');
});
if (jQuery('form.woocommerce-checkout .woocommerce-invalid').length) {
errorHandler.clear();
errorHandler.message(PayPalCommerceGateway.labels.error.js_validation);
return actions.reject();
}
const form = document.querySelector('form.woocommerce-checkout');
if (form) {
jQuery('#ppcp-funding-source-form-input').remove();

View file

@ -20,7 +20,9 @@ class CheckoutActionHandler {
const errorHandler = this.errorHandler;
const formSelector = this.config.context === 'checkout' ? 'form.checkout' : 'form#order_review';
const formValues = jQuery(formSelector).serialize();
const formData = new FormData(document.querySelector(formSelector));
// will not handle fields with multiple values (checkboxes, <select multiple>), but we do not care about this here
const formJsonObj = Object.fromEntries(formData);
const createaccount = jQuery('#createaccount').is(":checked") ? true : false;
@ -34,7 +36,7 @@ class CheckoutActionHandler {
order_id:this.config.order_id,
payment_method: getCurrentPaymentMethod(),
funding_source: window.ppcpFundingSource,
form:formValues,
form: formJsonObj,
createaccount: createaccount
})
}).then(function (res) {
@ -59,7 +61,7 @@ class CheckoutActionHandler {
}
}
return;
throw new Error(data.data.message);
}
const input = document.createElement('input');
input.setAttribute('type', 'hidden');

View file

@ -14,6 +14,7 @@ class SingleProductBootstap {
if (!this.shouldRender()) {
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
this.renderer.hideButtons(this.gateway.button.wrapper);
this.messages.hideMessages();
return;
}
@ -26,6 +27,7 @@ class SingleProductBootstap {
if (!this.shouldRender()) {
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
this.messages.hideMessages();
return;
}
@ -51,6 +53,8 @@ class SingleProductBootstap {
else if (document.querySelector('.product .woocommerce-Price-amount')) {
priceText = document.querySelector('.product .woocommerce-Price-amount').innerText;
}
priceText = priceText.replace(/,/g, '.');
const amount = parseFloat(priceText.replace(/([^\d,\.\s]*)/g, ''));
return amount === 0;

View file

@ -53,5 +53,14 @@ class MessageRenderer {
}
return true;
}
hideMessages() {
const domElement = document.querySelector(this.config.wrapper);
if (! domElement ) {
return false;
}
domElement.style.display = 'none';
return true;
}
}
export default MessageRenderer;

View file

@ -151,6 +151,7 @@ return array(
$three_d_secure = $container->get( 'button.helper.three-d-secure' );
$settings = $container->get( 'wcgateway.settings' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
$order_helper = $container->get( 'api.order-helper' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new ApproveOrderEndpoint(
$request_data,
@ -159,6 +160,7 @@ return array(
$three_d_secure,
$settings,
$dcc_applies,
$order_helper,
$logger
);
},

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Button\Assets;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Product;
use WC_Product_Variation;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
@ -847,6 +848,10 @@ class SmartButton implements SmartButtonInterface {
'Something went wrong. Please try again or choose another payment source.',
'woocommerce-paypal-payments'
),
'js_validation' => __(
'Required form fields are not filled or invalid.',
'woocommerce-paypal-payments'
),
),
),
'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0,
@ -926,9 +931,9 @@ class SmartButton implements SmartButtonInterface {
}
if ( $this->is_free_trial_cart() ) {
$all_sources = $this->all_funding_sources;
$all_sources = array_keys( $this->all_funding_sources );
if ( $is_dcc_enabled ) {
$all_sources = array_keys( array_diff_key( $all_sources, array( 'card' => '' ) ) );
$all_sources = array_diff( $all_sources, array( 'card' ) );
}
$disable_funding = $all_sources;
}
@ -1256,9 +1261,25 @@ class SmartButton implements SmartButtonInterface {
/**
* The filter returning true if PayPal buttons/messages can be rendered for this product, or false otherwise.
*/
$in_stock = $product->is_in_stock();
if ( $product->is_type( 'variable' ) ) {
/**
* The method is defined in WC_Product_Variable class.
*
* @psalm-suppress UndefinedMethod
*/
$variations = $product->get_available_variations( 'objects' );
$in_stock = $this->has_in_stock_variation( $variations );
}
/**
* Allows to filter if PayPal buttons/messages can be rendered for the given product.
*/
return apply_filters(
'woocommerce_paypal_payments_product_supports_payment_request_button',
! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(),
! $product->is_type( array( 'external', 'grouped' ) ) && $in_stock,
$product
);
}
@ -1295,4 +1316,20 @@ class SmartButton implements SmartButtonInterface {
}
return '';
}
/**
* Checks if variations contain any in stock variation.
*
* @param WC_Product_Variation[] $variations The list of variations.
* @return bool True if any in stock variation, false otherwise.
*/
protected function has_in_stock_variation( array $variations ): bool {
foreach ( $variations as $variation ) {
if ( $variation->is_in_stock() ) {
return true;
}
}
return false;
}
}

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
@ -26,7 +27,6 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/
class ApproveOrderEndpoint implements EndpointInterface {
const ENDPOINT = 'ppc-approve-order';
/**
@ -71,6 +71,13 @@ class ApproveOrderEndpoint implements EndpointInterface {
*/
private $dcc_applies;
/**
* The order helper.
*
* @var OrderHelper
*/
protected $order_helper;
/**
* The logger.
*
@ -87,6 +94,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
* @param ThreeDSecure $three_d_secure The 3d secure helper object.
* @param Settings $settings The settings.
* @param DccApplies $dcc_applies The DCC applies object.
* @param OrderHelper $order_helper The order helper.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -96,6 +104,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
ThreeDSecure $three_d_secure,
Settings $settings,
DccApplies $dcc_applies,
OrderHelper $order_helper,
LoggerInterface $logger
) {
@ -105,6 +114,7 @@ class ApproveOrderEndpoint implements EndpointInterface {
$this->threed_secure = $three_d_secure;
$this->settings = $settings;
$this->dcc_applies = $dcc_applies;
$this->order_helper = $order_helper;
$this->logger = $logger;
}
@ -173,10 +183,10 @@ class ApproveOrderEndpoint implements EndpointInterface {
wp_send_json_success( $order );
}
if ( ! $order->status()->is( OrderStatus::APPROVED ) ) {
if ( $this->order_helper->contains_physical_goods( $order ) && ! $order->status()->is( OrderStatus::APPROVED ) && ! $order->status()->is( OrderStatus::CREATED ) ) {
$message = sprintf(
// translators: %s is the id of the order.
__( 'Order %s is not approved yet.', 'woocommerce-paypal-payments' ),
__( 'Order %s is not ready for processing yet.', 'woocommerce-paypal-payments' ),
$data['order_id']
);

View file

@ -403,9 +403,9 @@ class CreateOrderEndpoint implements EndpointInterface {
}
if ( ! $payer && isset( $data['form'] ) ) {
parse_str( $data['form'], $form_fields );
$form_fields = $data['form'];
if ( isset( $form_fields['billing_email'] ) && '' !== $form_fields['billing_email'] ) {
if ( is_array( $form_fields ) && isset( $form_fields['billing_email'] ) && '' !== $form_fields['billing_email'] ) {
return $this->payer_factory->from_checkout_form( $form_fields );
}
}

View file

@ -81,15 +81,9 @@ class RequestData {
$data = array();
foreach ( (array) $assoc_array as $raw_key => $raw_value ) {
if ( ! is_array( $raw_value ) ) {
/**
* The 'form' key is preserved for url encoded data and needs different
* sanitization.
*/
if ( 'form' !== $raw_key ) {
$data[ sanitize_text_field( (string) $raw_key ) ] = sanitize_text_field( (string) $raw_value );
} else {
$data[ sanitize_text_field( (string) $raw_key ) ] = sanitize_text_field( urldecode( (string) $raw_value ) );
}
// Not sure if it is a good idea to sanitize everything at this level,
// but should be fine for now since we do not send any HTML or multi-line texts via ajax.
$data[ sanitize_text_field( (string) $raw_key ) ] = sanitize_text_field( (string) $raw_value );
continue;
}
$data[ sanitize_text_field( (string) $raw_key ) ] = $this->sanitize( $raw_value );