Validate birth date

This commit is contained in:
dinamiko 2022-05-10 11:59:24 +02:00
parent 5911e09310
commit c77cab9cbe
5 changed files with 81 additions and 35 deletions

View file

@ -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\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
@ -2192,6 +2193,9 @@ return array(
(string) $source_website_id()
);
},
'wcgateway.pay-upon-invoice-helper' => static function( ContainerInterface $container ): PayUponInvoiceHelper {
return new PayUponInvoiceHelper();
},
'wcgateway.pay-upon-invoice' => static function ( ContainerInterface $container ): PayUponInvoice {
return new PayUponInvoice(
$container->get( 'wcgateway.url' ),
@ -2200,7 +2204,8 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'onboarding.environment' ),
$container->get( 'ppcp.asset-version' )
$container->get( 'ppcp.asset-version' ),
$container->get( 'wcgateway.pay-upon-invoice-helper' )
);
},
'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool {

View file

@ -9,12 +9,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
use DateTime;
use Psr\Log\LoggerInterface;
use WC_Order;
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\WcGateway\Processor\TransactionIdHandlingTrait;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
@ -76,6 +76,13 @@ class PayUponInvoice {
*/
protected $asset_version;
/**
* The PUI helper.
*
* @var PayUponInvoiceHelper
*/
protected $pui_helper;
/**
* PayUponInvoice constructor.
*
@ -86,6 +93,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.
*/
public function __construct(
string $module_url,
@ -94,7 +102,8 @@ class PayUponInvoice {
LoggerInterface $logger,
Settings $settings,
Environment $environment,
string $asset_version
string $asset_version,
PayUponInvoiceHelper $pui_helper
) {
$this->module_url = $module_url;
$this->fraud_net = $fraud_net;
@ -103,6 +112,7 @@ class PayUponInvoice {
$this->settings = $settings;
$this->environment = $environment;
$this->asset_version = $asset_version;
$this->pui_helper = $pui_helper;
}
/**
@ -262,7 +272,7 @@ class PayUponInvoice {
}
$birth_date = filter_input( INPUT_POST, 'billing_birth_date', FILTER_SANITIZE_STRING );
if(! $this->validate_birth_date($birth_date)) {
if ( ! $this->pui_helper->validate_birth_date( $birth_date ) ) {
$errors->add( 'validation', __( 'Invalid birth date.', 'woocommerce-paypal-payments' ) );
}
},
@ -337,33 +347,4 @@ class PayUponInvoice {
return true;
}
/**
* Ensures date is valid, at least 18 years back and in the last 100 years timeframe.
*
* @param string $date
* @param string $format
* @return bool
*/
private function validate_birth_date(string $date, string $format = 'Y-m-d'): bool
{
$d = DateTime::createFromFormat($format, $date);
if($d === false) {
return false;
}
if($date !== $d->format($format)) {
return false;
}
if (time() < strtotime('+18 years', strtotime($date))) {
return false;
}
// if (time() > strtotime('-100 years', strtotime($date))) {
// return false;
// }
return true;
}
}

View file

@ -78,6 +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 LoggerInterface $logger The logger.
*/
public function __construct(

View file

@ -9,7 +9,34 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
class PayUponInvoiceHelper
{
use DateTime;
/**
* Class PayUponInvoiceHelper
*/
class PayUponInvoiceHelper {
/**
* Ensures date is valid and at least 18 years back.
*
* @param string $date The date.
* @param string $format The date format.
* @return bool
*/
public function validate_birth_date( string $date, string $format = 'Y-m-d' ): bool {
$d = DateTime::createFromFormat( $format, $date );
if ( false === $d ) {
return false;
}
if ( $date !== $d->format( $format ) ) {
return false;
}
if ( time() < strtotime( '+18 years', strtotime( $date ) ) ) {
return false;
}
return true;
}
}