endisha / intl-phone-number-format

This WordPress plugin allows to effortlessly format and validate international phone numbers within WooCommerce and WordPress fields.
1 stars 0 forks source link

Conflict with WooCommerce PayPal Payments plugin on international phone number validation #1

Open HLFH opened 6 months ago

HLFH commented 6 months ago

Describe the Bug

There is a conflict with International Phone Number Format plugin on international phone number validation and WooCommerce PayPal Payments. WooCommerce PayPal Payments may have to adapt its validation code in order to support E.164 international phone number format.

To Reproduce

  1. Install extension: https://fr.wordpress.org/plugins/international-phone-number-format/
  2. Go at the PayPal plugin in modules/ppcp-api-client/src/Factory/PayerFactory.php
  3. Required modification of the code to support international numbers that contain the "+" sign.

        public function from_wc_order( \WC_Order $wc_order ): Payer {
                $payer_id  = '';
                $birthdate = null;
    
                $phone = null;
                if ( $wc_order->get_billing_phone() ) {
                        // make sure the phone number contains only numbers and is max 14. chars long.
                        $national_number = $wc_order->get_billing_phone();
                        $national_number = preg_replace( '/[^0-9]/', '', $national_number );
                        $national_number = substr( $national_number, 0, 14 );
    
                        if ( $national_number ) {
                                $phone = new PhoneWithType(
                                        'HOME',
                                        new Phone( $national_number )
                                );
                        }
                }

PayPal orders may not go through because of this conflict. It is better to have support for E.164 standard to have reliable SMS notifications for shipping.

Expected Behavior

Support E.164 and PayPal orders would go through. For that, we may have to solve this plugin conflict on either side.

Actual Behavior

PayPal orders are not going through because of incomplete billing phone validation.

Environment

HLFH commented 6 months ago

Related: https://github.com/woocommerce/woocommerce-paypal-payments/issues/2190

endisha commented 6 months ago

@HLFH

Since the issue is not related to the WordPress core or the WooComerce plugin itself, I think there is nothing that can be done on our end.

I took a quick look at the WooCommerce PayPal plugin and found that it uses the WC_Order after processing orders to get the billing phone number and complete the payment.

Try to manipulate the billing phone number value, you can use this hook such as the code below:

add_filter('woocommerce_order_get_billing_phone', function ($value) {
    return ltrim( $value, '+' );
}, 10 );

This filter removes the leading "+" character from your billing phone number when fetched from an order.

keep in mind that applying this filter globally may lead to unintended consequences. It's advisable to implement specific conditions or rules to determine when this hook should be applied to avoid potential issues.