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

114 lines
2.5 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;
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 selected shop country.
*
* @var string
*/
protected $shop_country;
/**
* The PUI seller product status.
*
* @var PayUponInvoiceProductStatus
*/
protected $pui_product_status;
/**
* PayUponInvoiceHelper constructor.
*
* @param CheckoutHelper $checkout_helper The checkout helper.
* @param string $shop_country The selected shop country.
* @param PayUponInvoiceProductStatus $pui_product_status The PUI seller product status.
*/
public function __construct( CheckoutHelper $checkout_helper, string $shop_country, PayUponInvoiceProductStatus $pui_product_status ) {
$this->checkout_helper = $checkout_helper;
$this->shop_country = $shop_country;
$this->pui_product_status = $pui_product_status;
}
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 ready in admin screen.
*
* @return bool
*/
public function is_pui_ready_in_admin(): bool {
if ( $this->shop_country === 'DE' && $this->pui_product_status->pui_is_active() ) {
return true;
}
return false;
}
2022-05-10 10:25:06 +02:00
}