From 8b450764127591f9256e99148f783e6a8aa56b26 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Thu, 21 Jul 2022 14:15:25 +0200 Subject: [PATCH 1/6] Add phone field to pui when is removed from checkout --- .../Gateway/PayUponInvoice/PayUponInvoice.php | 20 +++++++++++++++++++ .../PayUponInvoice/PaymentSourceFactory.php | 6 +++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 87f89997a..6d8f5ebff 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -322,6 +322,26 @@ class PayUponInvoice { ) ); + $checkout_fields = WC()->checkout()->get_checkout_fields(); + if ( ! array_key_exists( 'billing_phone', $checkout_fields['billing'] ) ) { + woocommerce_form_field( + 'billing_phone', + array( + /** + * Use translation from WooCommerce here. + * phpcs:disable WordPress.WP.I18n.TextDomainMismatch + */ + 'label' => __( 'Phone', 'woocommerce' ), + // phpcs:enable WordPress.WP.I18n.TextDomainMismatch + 'type' => 'tel', + 'class' => array( 'form-row-wide' ), + 'validate' => array( 'phone' ), + 'autocomplete' => 'tel', + 'required' => true, + ) + ); + } + echo '
'; // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php index d4dc82562..a85251f15 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php @@ -24,8 +24,8 @@ class PaymentSourceFactory { * @return PaymentSource */ public function from_wc_order( WC_Order $order, string $birth_date ) { - $address = $order->get_address(); - + $address = $order->get_address(); + $phone = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? $address['phone'] ?? ''; $phone_country_code = WC()->countries->get_country_calling_code( $address['country'] ); $phone_country_code = is_array( $phone_country_code ) && ! empty( $phone_country_code ) ? $phone_country_code[0] : $phone_country_code; if ( is_string( $phone_country_code ) && '' !== $phone_country_code ) { @@ -44,7 +44,7 @@ class PaymentSourceFactory { $address['last_name'] ?? '', $address['email'] ?? '', $birth_date, - preg_replace( '/[^0-9]/', '', $address['phone'] ) ?? '', + preg_replace( '/[^0-9]/', '', $phone ) ?? '', $phone_country_code, $address['address_1'] ?? '', $address['city'] ?? '', From ca6f3dd088674e3a7efe5aac13804babb11dc790 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 2 Aug 2022 16:58:47 +0200 Subject: [PATCH 2/6] Fix psalm --- .../src/Gateway/PayUponInvoice/PaymentSourceFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php index c7faeeff2..5e13ba0a8 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PaymentSourceFactory.php @@ -25,7 +25,7 @@ class PaymentSourceFactory { */ public function from_wc_order( WC_Order $order, string $birth_date ) { $address = $order->get_address(); - $phone = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? $address['phone'] ?? ''; + $phone = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? $address['phone'] ?: ''; $phone_country_code = WC()->countries->get_country_calling_code( $address['country'] ); $phone_country_code = is_array( $phone_country_code ) && ! empty( $phone_country_code ) ? $phone_country_code[0] : $phone_country_code; if ( is_string( $phone_country_code ) && '' !== $phone_country_code ) { From b98400c6ce014830b17f859a3ff2446ddbed6d19 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 3 Aug 2022 11:25:08 +0200 Subject: [PATCH 3/6] Improve phpcs comment --- .../src/Gateway/PayUponInvoice/PayUponInvoice.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 3b2bd5ff4..3e83e01fe 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -338,12 +338,8 @@ class PayUponInvoice { woocommerce_form_field( 'billing_phone', array( - /** - * Use translation from WooCommerce here. - * phpcs:disable WordPress.WP.I18n.TextDomainMismatch - */ + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch 'label' => __( 'Phone', 'woocommerce' ), - // phpcs:enable WordPress.WP.I18n.TextDomainMismatch 'type' => 'tel', 'class' => array( 'form-row-wide' ), 'validate' => array( 'phone' ), From e65dab755aca40bf5de19b4f9bf4377af4676026 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 8 Aug 2022 15:45:54 +0200 Subject: [PATCH 4/6] Display phone field in pui gateway when is optional in checkout form --- .../src/Gateway/PayUponInvoice/PayUponInvoice.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 3e83e01fe..509b9cb27 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -333,8 +333,9 @@ class PayUponInvoice { ) ); - $checkout_fields = WC()->checkout()->get_checkout_fields(); - if ( ! array_key_exists( 'billing_phone', $checkout_fields['billing'] ) ) { + $checkout_fields = WC()->checkout()->get_checkout_fields(); + $checkout_phone_required = $checkout_fields['billing']['billing_phone']['required']; + if ( ! array_key_exists( 'billing_phone', $checkout_fields['billing'] ) || $checkout_phone_required === false ) { woocommerce_form_field( 'billing_phone', array( @@ -392,6 +393,9 @@ class PayUponInvoice { } $national_number = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ); + if ( ! $national_number ) { + $errors->add( 'validation', __( 'Phone field cannot be empty.', 'woocommerce-paypal-payments' ) ); + } if ( $national_number ) { $numeric_phone_number = preg_replace( '/[^0-9]/', '', $national_number ); if ( $numeric_phone_number && ! preg_match( '/^[0-9]{1,14}?$/', $numeric_phone_number ) ) { From b57567cf84723294426e66b63b1d61bbe10ed5c3 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 8 Aug 2022 15:48:36 +0200 Subject: [PATCH 5/6] Fix undefined index on billing phone --- .../src/Gateway/PayUponInvoice/PayUponInvoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 509b9cb27..2529afad4 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -334,7 +334,7 @@ class PayUponInvoice { ); $checkout_fields = WC()->checkout()->get_checkout_fields(); - $checkout_phone_required = $checkout_fields['billing']['billing_phone']['required']; + $checkout_phone_required = $checkout_fields['billing']['billing_phone']['required'] ?? false; if ( ! array_key_exists( 'billing_phone', $checkout_fields['billing'] ) || $checkout_phone_required === false ) { woocommerce_form_field( 'billing_phone', From 93b483f9e16fb466b86e6040ea78857bc22eb5b4 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 9 Aug 2022 12:23:24 +0200 Subject: [PATCH 6/6] Set order phone number if exists in request --- .../src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php index 4596bd321..68d4a19a7 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php @@ -215,6 +215,12 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway { } } + $phone_number = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? ''; + if ( $phone_number ) { + $wc_order->set_billing_phone( $phone_number ); + $wc_order->save(); + } + $wc_order->update_status( 'on-hold', __( 'Awaiting Pay upon Invoice payment.', 'woocommerce-paypal-payments' ) ); $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); $payment_source = $this->payment_source_factory->from_wc_order( $wc_order, $birth_date );