Merge pull request #640 from woocommerce/PCP-657-1.9.0-test1-can-checkout-with-an

[PUI] can checkout with any date as birth date (657)
This commit is contained in:
Emili Castells 2022-05-11 16:50:21 +02:00 committed by GitHub
commit 956061b163
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 25 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\PayUponInvoice\PayUponInvoiceGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
@ -2193,6 +2194,9 @@ return array(
(string) $source_website_id() (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 { 'wcgateway.pay-upon-invoice-product-status' => static function( ContainerInterface $container ): PayUponInvoiceProductStatus {
return new PayUponInvoiceProductStatus( return new PayUponInvoiceProductStatus(
$container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.settings' ),
@ -2211,7 +2215,8 @@ return array(
$container->get( 'onboarding.state' ), $container->get( 'onboarding.state' ),
$container->get( 'wcgateway.is-ppcp-settings-page' ), $container->get( 'wcgateway.is-ppcp-settings-page' ),
$container->get( 'wcgateway.current-ppcp-settings-page-id' ), $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 { 'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool {

View file

@ -16,6 +16,7 @@ use WC_Product_Variation;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
@ -79,6 +80,13 @@ class PayUponInvoice {
*/ */
protected $asset_version; protected $asset_version;
/**
* The PUI helper.
*
* @var PayUponInvoiceHelper
*/
protected $pui_helper;
/** /**
* The onboarding state. * The onboarding state.
* *
@ -117,6 +125,7 @@ class PayUponInvoice {
* @param Settings $settings The settings. * @param Settings $settings The settings.
* @param Environment $environment The environment. * @param Environment $environment The environment.
* @param string $asset_version The asset version. * @param string $asset_version The asset version.
* @param PayUponInvoiceHelper $pui_helper The PUI helper.
* @param State $state The onboarding state. * @param State $state The onboarding state.
* @param bool $is_ppcp_settings_page The is ppcp settings page. * @param bool $is_ppcp_settings_page The is ppcp settings page.
* @param string $current_ppcp_settings_page_id Current PayPal settings page id. * @param string $current_ppcp_settings_page_id Current PayPal settings page id.
@ -133,7 +142,8 @@ class PayUponInvoice {
State $state, State $state,
bool $is_ppcp_settings_page, bool $is_ppcp_settings_page,
string $current_ppcp_settings_page_id, string $current_ppcp_settings_page_id,
PayUponInvoiceProductStatus $pui_product_status PayUponInvoiceProductStatus $pui_product_status,
PayUponInvoiceHelper $pui_helper
) { ) {
$this->module_url = $module_url; $this->module_url = $module_url;
$this->fraud_net = $fraud_net; $this->fraud_net = $fraud_net;
@ -146,6 +156,7 @@ class PayUponInvoice {
$this->is_ppcp_settings_page = $is_ppcp_settings_page; $this->is_ppcp_settings_page = $is_ppcp_settings_page;
$this->current_ppcp_settings_page_id = $current_ppcp_settings_page_id; $this->current_ppcp_settings_page_id = $current_ppcp_settings_page_id;
$this->pui_product_status = $pui_product_status; $this->pui_product_status = $pui_product_status;
$this->pui_helper = $pui_helper;
} }
/** /**
@ -304,6 +315,11 @@ class PayUponInvoice {
if ( 'DE' !== $fields['billing_country'] ) { if ( 'DE' !== $fields['billing_country'] ) {
$errors->add( 'validation', __( 'Billing country not available.', 'woocommerce-paypal-payments' ) ); $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, 10,
2 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. * Checks whether checkout is ready for PUI.
* *
@ -393,26 +431,4 @@ class PayUponInvoice {
return true; 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 ),
)
);
}
} }

View file

@ -78,7 +78,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param PaymentSourceFactory $payment_source_factory The payment source factory. * @param PaymentSourceFactory $payment_source_factory The payment source factory.
* @param Environment $environment The environment. * @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. * @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(

View file

@ -0,0 +1,43 @@
<?php
/**
* Helper methods for PUI.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Helper
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
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;
}
$date_time = strtotime( $date );
if ( $date_time && time() < strtotime( '+18 years', $date_time ) ) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use DateTime;
use WooCommerce\PayPalCommerce\TestCase;
class PayUponInvoiceHelperTest extends TestCase
{
/**
* @dataProvider datesProvider
*/
public function testValidateBirthDate($input, $output)
{
$this->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],
];
}
}