Add birth date validation for pay for order

This commit is contained in:
dinamiko 2022-05-16 16:51:29 +02:00
parent de12327f6c
commit be22a60b93
5 changed files with 127 additions and 96 deletions

View file

@ -2177,7 +2177,8 @@ return array(
$container->get( 'wcgateway.pay-upon-invoice-payment-source-factory' ),
$container->get( 'onboarding.environment' ),
$container->get( 'wcgateway.transaction-url-provider' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'wcgateway.pay-upon-invoice-helper' )
);
},
'wcgateway.pay-upon-invoice-fraudnet-session-id' => static function ( ContainerInterface $container ): FraudNetSessionId {

View file

@ -332,8 +332,11 @@ class PayUponInvoice {
add_filter(
'woocommerce_available_payment_gateways',
function( array $methods ): array {
if ( ! $this->is_checkout_ready_for_pui() ) {
function ( array $methods ): array {
if (
! $this->pui_product_status->pui_is_active()
|| ! $this->pui_helper->is_checkout_ready_for_pui()
) {
unset( $methods[ PayUponInvoiceGateway::ID ] );
}
@ -425,90 +428,4 @@ class PayUponInvoice {
)
);
}
/**
* Checks whether checkout is ready for PUI.
*
* @return bool
*/
private function is_checkout_ready_for_pui(): bool {
if ( ! $this->pui_product_status->pui_is_active() ) {
return false;
}
$billing_country = filter_input( INPUT_POST, 'country', FILTER_SANITIZE_STRING ) ?? null;
if ( $billing_country && 'DE' !== $billing_country ) {
return false;
}
if ( 'EUR' !== get_woocommerce_currency() ) {
return false;
}
$cart = WC()->cart ?? null;
if ( $cart && ! is_checkout_pay_page() ) {
$cart_total = (float) $cart->get_total( 'numeric' );
if ( $cart_total < 5 || $cart_total > 2500 ) {
return false;
}
$items = $cart->get_cart_contents();
foreach ( $items as $item ) {
$product = wc_get_product( $item['product_id'] );
if ( is_a( $product, WC_Product::class ) && ! $this->product_ready_for_pui( $product ) ) {
return false;
}
}
}
if ( is_wc_endpoint_url( 'order-pay' ) ) {
/**
* Needed for WordPress `query_vars`.
*
* @psalm-suppress InvalidGlobal
*/
global $wp;
if ( isset( $wp->query_vars['order-pay'] ) && absint( $wp->query_vars['order-pay'] ) > 0 ) {
$order_id = absint( $wp->query_vars['order-pay'] );
$order = wc_get_order( $order_id );
if ( is_a( $order, WC_Order::class ) ) {
foreach ( $order->get_items() as $item_id => $item ) {
if ( is_a( $item, WC_Order_Item_Product::class ) ) {
$product = wc_get_product( $item->get_product_id() );
if ( is_a( $product, WC_Product::class ) && ! $this->product_ready_for_pui( $product ) ) {
return false;
}
}
}
}
}
}
return true;
}
/**
* Ensures product is ready for PUI.
*
* @param WC_Product $product WC product.
* @return bool
*/
private function product_ready_for_pui( WC_Product $product ):bool {
if ( $product->is_downloadable() || $product->is_virtual() ) {
return false;
}
if ( is_a( $product, WC_Product_Variable::class ) ) {
foreach ( $product->get_available_variations( 'object' ) as $variation ) {
if ( is_a( $variation, WC_Product_Variation::class ) ) {
if ( true === $variation->is_downloadable() || true === $variation->is_virtual() ) {
return false;
}
}
}
}
return true;
}
}

View file

@ -12,12 +12,15 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WC_Order;
use WC_Order_Item_Product;
use WC_Payment_Gateway;
use WC_Product;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
/**
@ -71,6 +74,13 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
*/
protected $logger;
/**
* The PUI helper.
*
* @var PayUponInvoiceHelper
*/
protected $pui_helper;
/**
* PayUponInvoiceGateway constructor.
*
@ -80,6 +90,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
* @param Environment $environment The environment.
* @param TransactionUrlProvider $transaction_url_provider The transaction URL provider.
* @param LoggerInterface $logger The logger.
* @param PayUponInvoiceHelper $pui_helper The PUI helper.
*/
public function __construct(
PayUponInvoiceOrderEndpoint $order_endpoint,
@ -87,7 +98,8 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
PaymentSourceFactory $payment_source_factory,
Environment $environment,
TransactionUrlProvider $transaction_url_provider,
LoggerInterface $logger
LoggerInterface $logger,
PayUponInvoiceHelper $pui_helper
) {
$this->id = self::ID;
@ -115,6 +127,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
$this->logger = $logger;
$this->environment = $environment;
$this->transaction_url_provider = $transaction_url_provider;
$this->pui_helper = $pui_helper;
}
/**
@ -180,12 +193,21 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
* @return array
*/
public function process_payment( $order_id ) {
$wc_order = wc_get_order( $order_id );
$wc_order = wc_get_order( $order_id );
$birth_date = filter_input( INPUT_POST, 'billing_birth_date', FILTER_SANITIZE_STRING ) ?? '';
$pay_for_order = filter_input( INPUT_GET, 'pay_for_order', FILTER_SANITIZE_STRING );
if ( 'true' === $pay_for_order ) {
if ( ! $this->pui_helper->validate_birth_date( $birth_date ) ) {
wc_add_notice( 'Invalid birth date.', 'error' );
return array(
'result' => 'failure',
);
}
}
$wc_order->update_status( 'on-hold', __( 'Awaiting Pay Upon Invoice payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$birth_date = filter_input( INPUT_POST, 'billing_birth_date', FILTER_SANITIZE_STRING ) ?? '';
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$payment_source = $this->payment_source_factory->from_wc_order( $wc_order, $birth_date );
try {

View file

@ -10,6 +10,11 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use DateTime;
use WC_Order;
use WC_Order_Item_Product;
use WC_Product;
use WC_Product_Variable;
use WC_Product_Variation;
/**
* Class PayUponInvoiceHelper
@ -40,4 +45,86 @@ class PayUponInvoiceHelper {
return true;
}
/**
* Ensures product is ready for PUI.
*
* @param WC_Product $product WC product.
* @return bool
*/
public function product_ready_for_pui( WC_Product $product ):bool {
if ( $product->is_downloadable() || $product->is_virtual() ) {
return false;
}
if ( is_a( $product, WC_Product_Variable::class ) ) {
foreach ( $product->get_available_variations( 'object' ) as $variation ) {
if ( is_a( $variation, WC_Product_Variation::class ) ) {
if ( true === $variation->is_downloadable() || true === $variation->is_virtual() ) {
return false;
}
}
}
}
return true;
}
/**
* Checks whether checkout is ready for PUI.
*
* @return bool
*/
public function is_checkout_ready_for_pui(): bool {
$billing_country = filter_input( INPUT_POST, 'country', FILTER_SANITIZE_STRING ) ?? null;
if ( $billing_country && 'DE' !== $billing_country ) {
return false;
}
if ( 'EUR' !== get_woocommerce_currency() ) {
return false;
}
$cart = WC()->cart ?? null;
if ( $cart && ! is_checkout_pay_page() ) {
$cart_total = (float) $cart->get_total( 'numeric' );
if ( $cart_total < 5 || $cart_total > 2500 ) {
return false;
}
$items = $cart->get_cart_contents();
foreach ( $items as $item ) {
$product = wc_get_product( $item['product_id'] );
if ( is_a( $product, WC_Product::class ) && ! $this->product_ready_for_pui( $product ) ) {
return false;
}
}
}
if ( is_wc_endpoint_url( 'order-pay' ) ) {
/**
* Needed for WordPress `query_vars`.
*
* @psalm-suppress InvalidGlobal
*/
global $wp;
if ( isset( $wp->query_vars['order-pay'] ) && absint( $wp->query_vars['order-pay'] ) > 0 ) {
$order_id = absint( $wp->query_vars['order-pay'] );
$order = wc_get_order( $order_id );
if ( is_a( $order, WC_Order::class ) ) {
foreach ( $order->get_items() as $item_id => $item ) {
if ( is_a( $item, WC_Order_Item_Product::class ) ) {
$product = wc_get_product( $item->get_product_id() );
if ( is_a( $product, WC_Product::class ) && ! $this->product_ready_for_pui( $product ) ) {
return false;
}
}
}
}
}
}
return true;
}
}