diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 9aae83b14..a04dc1580 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -38,6 +38,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoice; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus; +use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; @@ -2193,6 +2194,9 @@ return array( (string) $source_website_id() ); }, + 'wcgateway.pay-upon-invoice-helper' => static function( ContainerInterface $container ): PayUponInvoiceHelper { + return new PayUponInvoiceHelper(); + }, 'wcgateway.pay-upon-invoice-product-status' => static function( ContainerInterface $container ): PayUponInvoiceProductStatus { return new PayUponInvoiceProductStatus( $container->get( 'wcgateway.settings' ), @@ -2211,7 +2215,8 @@ return array( $container->get( 'onboarding.state' ), $container->get( 'wcgateway.is-ppcp-settings-page' ), $container->get( 'wcgateway.current-ppcp-settings-page-id' ), - $container->get( 'wcgateway.pay-upon-invoice-product-status' ) + $container->get( 'wcgateway.pay-upon-invoice-product-status' ), + $container->get( 'wcgateway.pay-upon-invoice-helper' ) ); }, 'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool { diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 1ed219c68..cfde5387e 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -16,6 +16,7 @@ use WC_Product_Variation; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint; use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Onboarding\Environment; +use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; @@ -79,6 +80,13 @@ class PayUponInvoice { */ protected $asset_version; + /** + * The PUI helper. + * + * @var PayUponInvoiceHelper + */ + protected $pui_helper; + /** * The onboarding state. * @@ -117,6 +125,7 @@ class PayUponInvoice { * @param Settings $settings The settings. * @param Environment $environment The environment. * @param string $asset_version The asset version. + * @param PayUponInvoiceHelper $pui_helper The PUI helper. * @param State $state The onboarding state. * @param bool $is_ppcp_settings_page The is ppcp settings page. * @param string $current_ppcp_settings_page_id Current PayPal settings page id. @@ -133,7 +142,8 @@ class PayUponInvoice { State $state, bool $is_ppcp_settings_page, string $current_ppcp_settings_page_id, - PayUponInvoiceProductStatus $pui_product_status + PayUponInvoiceProductStatus $pui_product_status, + PayUponInvoiceHelper $pui_helper ) { $this->module_url = $module_url; $this->fraud_net = $fraud_net; @@ -146,6 +156,7 @@ class PayUponInvoice { $this->is_ppcp_settings_page = $is_ppcp_settings_page; $this->current_ppcp_settings_page_id = $current_ppcp_settings_page_id; $this->pui_product_status = $pui_product_status; + $this->pui_helper = $pui_helper; } /** @@ -304,6 +315,11 @@ class PayUponInvoice { if ( 'DE' !== $fields['billing_country'] ) { $errors->add( 'validation', __( 'Billing country not available.', 'woocommerce-paypal-payments' ) ); } + + $birth_date = filter_input( INPUT_POST, 'billing_birth_date', FILTER_SANITIZE_STRING ); + if ( $birth_date && ! $this->pui_helper->validate_birth_date( $birth_date ) ) { + $errors->add( 'validation', __( 'Invalid birth date.', 'woocommerce-paypal-payments' ) ); + } }, 10, 2 @@ -346,6 +362,28 @@ class PayUponInvoice { ); } + /** + * Registers PUI assets. + */ + public function register_assets(): void { + wp_enqueue_script( + 'ppcp-pay-upon-invoice', + trailingslashit( $this->module_url ) . 'assets/js/pay-upon-invoice.js', + array(), + $this->asset_version + ); + + wp_localize_script( + 'ppcp-pay-upon-invoice', + 'FraudNetConfig', + array( + 'f' => $this->fraud_net->session_id(), + 's' => $this->fraud_net->source_website_id(), + 'sandbox' => $this->environment->current_environment_is( Environment::SANDBOX ), + ) + ); + } + /** * Checks whether checkout is ready for PUI. * @@ -393,26 +431,4 @@ class PayUponInvoice { return true; } - - /** - * Registers PUI assets. - */ - public function register_assets(): void { - wp_enqueue_script( - 'ppcp-pay-upon-invoice', - trailingslashit( $this->module_url ) . 'assets/js/pay-upon-invoice.js', - array(), - $this->asset_version - ); - - wp_localize_script( - 'ppcp-pay-upon-invoice', - 'FraudNetConfig', - array( - 'f' => $this->fraud_net->session_id(), - 's' => $this->fraud_net->source_website_id(), - 'sandbox' => $this->environment->current_environment_is( Environment::SANDBOX ), - ) - ); - } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php index 7bff8b02a..8afea94a1 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php @@ -78,7 +78,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway { * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. * @param PaymentSourceFactory $payment_source_factory The payment source factory. * @param Environment $environment The environment. - * @param TransactionUrlProvider $transaction_url_provider The transaction url provider. + * @param TransactionUrlProvider $transaction_url_provider The transaction URL provider. * @param LoggerInterface $logger The logger. */ public function __construct( diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php new file mode 100644 index 000000000..ce2dc1b8c --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php @@ -0,0 +1,43 @@ +format( $format ) ) { + return false; + } + + $date_time = strtotime( $date ); + if ( $date_time && time() < strtotime( '+18 years', $date_time ) ) { + return false; + } + + return true; + } +} diff --git a/tests/PHPUnit/WcGateway/Helper/PayUponInvoiceHelperTest.php b/tests/PHPUnit/WcGateway/Helper/PayUponInvoiceHelperTest.php new file mode 100644 index 000000000..3a1d9bdc0 --- /dev/null +++ b/tests/PHPUnit/WcGateway/Helper/PayUponInvoiceHelperTest.php @@ -0,0 +1,32 @@ +assertSame((new PayUponInvoiceHelper())->validate_birth_date($input), $output); + } + + public function datesProvider(): array{ + $format = 'Y-m-d'; + + return [ + ['', false], + [(new DateTime())->format($format), false], + [(new DateTime('-17 years'))->format($format), false], + ['1942-02-31', false], + ['01-01-1942', false], + ['1942-01-01', true], + ]; + } + +}