From 1f94ce26542fe370fa011f370938ed93bc9e540c Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 4 Nov 2022 16:20:27 +0100 Subject: [PATCH 1/4] Ensure pay for order is ready for pui --- .../Gateway/PayUponInvoice/PayUponInvoice.php | 1 + .../src/Helper/PayUponInvoiceHelper.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 89693271a..8419b3e3f 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -459,6 +459,7 @@ class PayUponInvoice { if ( ! $this->pui_product_status->pui_is_active() || ! $this->pui_helper->is_checkout_ready_for_pui() + || ! $this->pui_helper->is_pay_for_order_ready_for_pui() ) { unset( $methods[ PayUponInvoiceGateway::ID ] ); } diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php index bc364df72..63a721e51 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php @@ -77,6 +77,39 @@ class PayUponInvoiceHelper { return true; } + /** + * Checks whether pay for order is ready for PUI. + * + * @return bool + */ + public function is_pay_for_order_ready_for_pui(): bool { + /** + * Needed for WordPress `query_vars`. + * + * @psalm-suppress InvalidGlobal + */ + global $wp; + + $order_id = isset( $wp->query_vars['order-pay'] ) ? (int) $wp->query_vars['order-pay'] : 0; + if ( $order_id === 0 ) { + return false; + } + + $order = wc_get_order( $order_id ); + if ( ! is_a( $order, WC_Order::class ) ) { + return false; + } + + $address = $order->get_address(); + foreach ( $address as $key => $value ) { + if ( $value === '' ) { + return false; + } + } + + return true; + } + /** * Checks if currency is allowed for PUI. * From 1e6aa372168840a261c59c7f869505171e72f810 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 7 Nov 2022 12:15:42 +0100 Subject: [PATCH 2/4] Use null coalescing operator instead of ternary --- modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php index b5a011c15..739ac2152 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php @@ -145,7 +145,7 @@ class PayUponInvoiceHelper { */ global $wp; - $order_id = isset( $wp->query_vars['order-pay'] ) ? (int) $wp->query_vars['order-pay'] : 0; + $order_id = (int) ( $wp->query_vars['order-pay'] ?? 0 ); if ( $order_id === 0 ) { return false; } From 5bb46c0637d443d3d43c378d5486611ddf415da1 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 8 Nov 2022 11:20:26 +0100 Subject: [PATCH 3/4] Validate required fields only --- .../src/Helper/PayUponInvoiceHelper.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php index 739ac2152..7ec0c0889 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php @@ -155,9 +155,20 @@ class PayUponInvoiceHelper { return false; } - $address = $order->get_address(); + $address = $order->get_address(); + $required_fields = array( + 'first_name', + 'last_name', + 'email', + 'phone', + 'address_1', + 'city', + 'postcode', + 'country', + ); + foreach ( $address as $key => $value ) { - if ( $value === '' ) { + if ( in_array( $key, $required_fields, true ) && $value === '' ) { return false; } } From ee982a3fed3acd096f174f04514b33045c90ea4c Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 9 Nov 2022 11:39:39 +0100 Subject: [PATCH 4/4] Refactor --- modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php index 7ec0c0889..8ba589789 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php @@ -167,8 +167,9 @@ class PayUponInvoiceHelper { 'country', ); - foreach ( $address as $key => $value ) { - if ( in_array( $key, $required_fields, true ) && $value === '' ) { + foreach ( $required_fields as $key ) { + $value = $address[ $key ] ?? ''; + if ( $value === '' ) { return false; } }