1500,
'AUD' => 1200,
'GBP' => 720,
'USD' => 1200,
];
protected $compatibility_mode = false;
protected $assets = [];
protected $apiGateway;
protected static $instance;
const MODE_BASIC = 'basic';
const MODE_PLUS = 'plus';
protected $mode = self::MODE_BASIC;
public function __construct() {
$this->id = self::GATEWAY_NAME;
$this->icon = apply_filters( 'woocommerce_laybuy_gateway_icon', 'https://integration-assets.laybuy.com/woocommerce_laybuy_icons/laybuy_logo_small.svg' );
$this->has_fields = false;
$this->method_title = 'Laybuy';
$this->method_description = __( 'Use Laybuy as a credit card processor for WooCommerce.', 'woo_laybuy' );
$this->supported_currencies = array(
'AUD', 'NZD', 'GBP', 'USD'
);
if (defined('WC_LAYBUY_PLUS')) {
$this->mode = self::MODE_PLUS;
}
// Get setting values.
$this->title = $this->get_option( 'title' );
$this->description = 'Pay by Laybuy';
$this->enabled = $this->get_option( 'enabled' ) === 'yes';
$this->assets = include 'assets.php';
$currency = get_woocommerce_currency();
if (!empty($this->assets[strtolower($currency)])) {
$this->assets = $this->assets[strtolower($currency)];
} else {
$this->assets = $this->assets['aud'];
}
$this->supports = array('products', 'refunds');
$this->init_form_fields();
$this->init_settings();
if ($this->enabled === 'no') {
return;
}
$currency = get_woocommerce_currency();
if (in_array($currency, $this->supported_currencies)) {
if ($this->is_plus()) {
$this->pay_over_time_limit_min = floatval($this->settings["${currency}_pay_limit_min"]);
$this->pay_over_time_limit_max = floatval($this->settings["${currency}_pay_limit_max"]);
$this->check_pay_over_time_limits();
} else {
$this->pay_over_time_limit_max = $this->pay_limits_max[$currency];
}
}
if (!is_admin()) {
$this->compatibility_mode = $this->settings['laybuy_compatibility_mode'] == 'yes';
}
$this->apiGateway = new Laybuy_ApiGateway($this->settings);
if (array_key_exists('debug', $this->settings)) {
WC_Laybuy_Logger::$enabled = $this->settings['debug'] === 'yes';
}
}
/**
* Instantiate the class if no instance exists. Return the instance.
*
* @since 2.0.0
* @return WC_Payment_Gateway_Laybuy
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Initialise Gateway Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = require( dirname( __FILE__ ) . '/laybuy-settings.php' );
}
/**
* Is currency enabled for laybuy
* @return bool
*/
public function is_currency_enabled($currency) {
return in_array($currency, (array) @$this->settings['currency']);
}
public function payment_fields() {
if ($description = $this->get_description()) {
$description = apply_filters('checkout_modify_description', $description, $this->get_order_total());
echo wpautop(wptexturize($description));
}
}
/**
* Process a refund if supported.
*
* Note: This overrides the method defined in the parent class.
*
* @since 1.0.0
* @see WC_Payment_Gateway::process_refund() For the method that this overrides.
* @param int $order_id
* @param float $amount Optional. The amount to refund. This cannot exceed the total.
* @param string $reason Optional. The reason for the refund. Defaults to an empty string.
* @return bool
*/
public function process_refund($order_id, $amount = null, $reason = '') {
$order_id = (int) $order_id;
if (function_exists('wc_get_order')) {
$order = wc_get_order( $order_id );
} else {
$order = new WC_Order( $order_id );
}
if (method_exists($this, 'can_refund_order') && !$this->can_refund_order($order)) {
WC_Laybuy_Logger::error('Refund Failed - No Transaction ID.');
return false;
}
$processManager = Laybuy_ProcessManager::getInstance();
$processManager->setApiGateway($this->apiGateway)
->setWcGateway($this);
return $processManager->refund($order_id, $amount, $reason);
}
public function generate_wysiwyg_html($key, $data) {
$html = '';
$id = str_replace('-', '', $key);
$class = array_key_exists('class', $data) ? $data['class'] : '';
$css = array_key_exists('css', $data) ? ('') : '';
$name = "{$this->plugin_id}{$this->id}_{$key}";
$title = array_key_exists('title', $data) ? $data['title'] : '';
$value = array_key_exists($key, $this->settings) ? esc_attr( $this->settings[$key] ) : '';
$description = array_key_exists('description', $data) ? $data['description'] : '';
ob_start();
include dirname( __FILE__ ) . '/admin/wysiwyg.php';
$html = ob_get_clean();
return $html;
}
/**
* Process the HTML from one of the rich text editors and output the converted string.
*/
private function process_and_print_laybuy_paragraph($output_filter, $product = null) {
if (is_admin()) {
return;
}
if (is_null($product)) {
$product = $this->get_product_from_the_post();
}
if (!$this->is_product_supported($product, true)) {
# Don't display anything on the product page if the product is not supported when purchased on its own.
return;
}
if (!$this->is_currency_supported()) {
# Don't display anything on the product page if the website currency is not within supported currencies.
return;
}
if (!$product->is_in_stock() && $this->settings['laybuy_price_breakdown_out_of_stock'] == 'no') {
return;
}
$of_or_from = 'of';
$price = $this->getProductPrice($product);
$price = floatval($price);
if (!$price) {
return '';
}
if (!$this->is_product_within_limits($product, true) && $output_filter == 'laybuy_html_on_product_thumbnails') {
if ($this->is_plus() && $price < $this->pay_over_time_limit_min) {
$html = $this->get_plus_limits_html('product_thumbnails_');
} else {
$html = $this->assets['category_page_pay_over_limit'];
}
$html = str_replace(array(
'[PAY_TODAY]',
'[AMOUNT]',
'[MIN_PRICE]',
'[MAX_PRICE]',
), array(
$this->display_price_html( $price - $this->pay_over_time_limit_max ),
$this->display_price_html( $this->pay_over_time_limit_max / 5 ),
wc_price( $this->pay_over_time_limit_min, ['decimals' => 0] ),
wc_price( $this->pay_over_time_limit_max, ['decimals' => 0] ),
), $html);
} else {
$amount = $this->display_price_html( round($price / self::PAYMENTS_COUNT, 2) );
$html = str_replace(array(
'[OF_OR_FROM]',
'[AMOUNT]'
), array(
$of_or_from,
$amount
), $this->settings['category_pages_info_text']);
}
# Execute shortcodes on the string after running internal replacements,
# but before applying filters and rendering.
$html = do_shortcode( "
{$html}
" );
# Add the Modal Window to the page
# Website Admin have no access to the Modal Window codes for data integrity reasons
//$html = $this->apply_modal_window($html);
# Allow other plugins to maniplulate or replace the HTML echoed by this funtion.
echo apply_filters( $output_filter, $html, $product, $price );
}
/**
* Print a paragraph of Laybuy info onto each product item in the shop loop if enabled and the product is valid.
*
* Note: Hooked onto the "woocommerce_after_shop_loop_item_title" Action.
*/
public function print_info_for_listed_products($product = null) {
if (!$this->isAllowCurrencyForLaybuy()) {
return;
}
if( is_single()) {
if (!isset($this->settings['show_info_on_product_pages'])
|| $this->settings['show_info_on_product_pages'] != 'yes'
|| empty($this->settings['category_pages_info_text'])
|| !empty($product) && (!$product->is_in_stock() && $this->settings['laybuy_price_breakdown_out_of_stock'] == 'no') ) {
# Don't display anything on Single product page unless the Display on Individual Page is enabled
# The Variant selected is in stock
return;
}
}
else { #Category Pages
if (!isset($this->settings['show_info_on_category_pages'])
|| $this->settings['show_info_on_category_pages'] != 'yes'
|| empty($this->settings['category_pages_info_text'])) {
# Don't display anything on product items within the shop loop unless
# the "Payment info on product listing pages" box is ticked
# and there is a message to display.
return;
}
$this->process_and_print_laybuy_paragraph(
'laybuy_html_on_product_thumbnails',
$product
);
}
}
/**
* Render Laybuy elements (logo and payment schedule) on Cart page.
*
* This is dependant on all of the following criteria being met:
* - The Laybuy Payment Gateway is enabled.
* - The cart total is valid and within the merchant payment limits.
* - The "Payment Info on Cart Page" box is ticked and there is a message to display.
* - All of the items in the cart are considered eligible to be purchased with Laybuy.
*
* Note: Hooked onto the "woocommerce_cart_totals_after_order_total" Action.
*/
public function render_schedule_on_cart_page() {
$total = WC()->cart->total;
if (!array_key_exists('enabled', $this->settings) || $this->settings['enabled'] != 'yes') {
return;
} else {
if ($total <= 0 ) {
return;
}
}
if(!$this->isAllowCurrencyForLaybuy()) {
return;
}
if (
!isset($this->settings['show_info_on_cart_page']) ||
$this->settings['show_info_on_cart_page'] != 'yes' ||
empty($this->settings['cart_page_info_text'])
) {
return;
}
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
if (!$this->is_product_supported($product)) {
return;
}
}
if ($this->is_plus() && $total < $this->pay_over_time_limit_min) {
$html = '