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;
|
|
|
|
|
2022-05-16 16:51:29 +02:00
|
|
|
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;
|
2022-05-16 16:51:29 +02:00
|
|
|
|
2022-08-12 16:50:05 +04:00
|
|
|
/**
|
|
|
|
* The selected shop country.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $shop_country;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The PUI seller product status.
|
|
|
|
*
|
|
|
|
* @var PayUponInvoiceProductStatus
|
|
|
|
*/
|
|
|
|
protected $pui_product_status;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PayUponInvoiceHelper constructor.
|
|
|
|
*
|
2022-08-17 17:11:20 +04:00
|
|
|
* @param CheckoutHelper $checkout_helper The checkout helper.
|
2022-08-12 16:50:05 +04:00
|
|
|
* @param string $shop_country The selected shop country.
|
|
|
|
* @param PayUponInvoiceProductStatus $pui_product_status The PUI seller product status.
|
|
|
|
*/
|
2022-08-17 17:11:20 +04:00
|
|
|
public function __construct( CheckoutHelper $checkout_helper, string $shop_country, PayUponInvoiceProductStatus $pui_product_status ) {
|
2022-08-12 16:50:05 +04:00
|
|
|
|
2022-08-17 17:11:20 +04:00
|
|
|
$this->checkout_helper = $checkout_helper;
|
2022-08-12 16:50:05 +04:00
|
|
|
$this->shop_country = $shop_country;
|
|
|
|
$this->pui_product_status = $pui_product_status;
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:59:24 +02:00
|
|
|
/**
|
2022-05-16 16:51:29 +02:00
|
|
|
* Checks whether checkout is ready for PUI.
|
2022-05-10 11:59:24 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-05-16 16:51:29 +02:00
|
|
|
public function is_checkout_ready_for_pui(): bool {
|
2022-05-18 15:14:04 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:51:29 +02:00
|
|
|
$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 ) ) {
|
2022-05-16 16:51:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-22 11:24:10 +02:00
|
|
|
* Checks if currency is allowed for PUI.
|
2022-05-16 16:51:29 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-07-22 11:24:10 +02:00
|
|
|
private function is_valid_currency(): bool {
|
|
|
|
global $wp;
|
2022-07-22 12:18:17 +02:00
|
|
|
$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-05-16 16:51:29 +02:00
|
|
|
}
|
|
|
|
|
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-05-16 16:51:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 11:24:10 +02:00
|
|
|
return false;
|
2022-05-16 16:51:29 +02:00
|
|
|
}
|
2022-08-12 16:50:05 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether PUI is ready in admin screen.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_pui_ready_in_admin(): bool {
|
2022-08-17 19:08:18 +04:00
|
|
|
if ( $this->shop_country === 'DE' && $this->pui_product_status->pui_is_active() ) {
|
2022-08-12 16:50:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-10 10:25:06 +02:00
|
|
|
}
|