mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
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:
commit
956061b163
5 changed files with 121 additions and 25 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 ),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
43
modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php
Normal file
43
modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php
Normal 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;
|
||||
}
|
||||
}
|
32
tests/PHPUnit/WcGateway/Helper/PayUponInvoiceHelperTest.php
Normal file
32
tests/PHPUnit/WcGateway/Helper/PayUponInvoiceHelperTest.php
Normal 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],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue