woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceHelper.php

104 lines
2.3 KiB
PHP
Raw Normal View History

2022-05-10 10:25:06 +02:00
<?php
/**
* Helper methods for PUI.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Helper
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use WC_Order;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2022-05-10 10:25:06 +02:00
2022-05-10 11:59:24 +02:00
/**
* Class PayUponInvoiceHelper
*/
class PayUponInvoiceHelper {
/**
2022-07-05 11:30:20 +02:00
* The checkout helper.
2022-05-10 11:59:24 +02:00
*
2022-07-05 11:30:20 +02:00
* @var CheckoutHelper
2022-05-10 11:59:24 +02:00
*/
2022-07-05 11:30:20 +02:00
protected $checkout_helper;
/**
* The settings.
*
* @var Settings
*/
protected $settings;
/**
* PayUponInvoiceHelper constructor.
*
* @param CheckoutHelper $checkout_helper The checkout helper.
* @param Settings $settings The Settings.
*/
2022-08-19 16:40:59 +04:00
public function __construct( CheckoutHelper $checkout_helper, Settings $settings ) {
$this->checkout_helper = $checkout_helper;
$this->settings = $settings;
}
2022-05-10 11:59:24 +02:00
/**
* Checks whether checkout is ready for PUI.
2022-05-10 11:59:24 +02:00
*
* @return bool
*/
public function is_checkout_ready_for_pui(): bool {
$gateway_settings = get_option( 'woocommerce_ppcp-pay-upon-invoice-gateway_settings' );
if ( $gateway_settings && '' === $gateway_settings['customer_service_instructions'] ) {
2022-05-10 11:59:24 +02:00
return false;
}
$billing_country = filter_input( INPUT_POST, 'country', FILTER_SANITIZE_STRING ) ?? null;
if ( $billing_country && 'DE' !== $billing_country ) {
2022-05-10 11:59:24 +02:00
return false;
}
2022-07-22 11:24:10 +02:00
if ( ! $this->is_valid_currency() ) {
2022-05-10 11:59:24 +02:00
return false;
}
2022-07-05 11:30:20 +02:00
if ( ! $this->checkout_helper->is_checkout_amount_allowed( 5, 2500 ) ) {
return false;
}
return true;
}
/**
2022-07-22 11:24:10 +02:00
* Checks if currency is allowed for PUI.
*
* @return bool
*/
2022-07-22 11:24:10 +02:00
private function is_valid_currency(): bool {
global $wp;
$order_id = isset( $wp->query_vars['order-pay'] ) ? (int) $wp->query_vars['order-pay'] : 0;
2022-07-22 11:24:10 +02:00
if ( 0 === $order_id ) {
return 'EUR' === get_woocommerce_currency();
}
2022-07-22 11:24:10 +02:00
$order = wc_get_order( $order_id );
if ( is_a( $order, WC_Order::class ) ) {
return 'EUR' === $order->get_currency();
}
2022-07-22 11:24:10 +02:00
return false;
}
/**
* Checks whether PUI is enabled.
*
* @return bool True if PUI is active, otherwise false.
* @throws NotFoundException If problem when checking the settings.
*/
public function is_pui_enabled(): bool {
2022-08-19 16:25:31 +04:00
return $this->settings->has( 'products_pui_enabled' ) && $this->settings->get( 'products_pui_enabled' );
}
2022-05-10 10:25:06 +02:00
}