File: /www/sites/timexsaleall.com/index/wp-content/plugins/npcard/includes/main.php
<?php
namespace NPCard;
use NPCard\Ajax\Order;
use NPCard\GateWays\Card;
use NPCard\GateWays\Webhook;
class Main
{
private static $instance;
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function init()
{
$this->registerEvents();
}
private function registerEvents()
{
add_filter('woocommerce_payment_gateways', [$this, 'addPaymentGateways']);
add_action('woocommerce_api_' . Card::ROUTE_SLUG_ASYNC_INTENT, [new Card(), 'checkout']);
add_action('woocommerce_api_' . Card::ROUTE_SLUG_WEBHOOK, [new Webhook(), 'init']);
add_action('woocommerce_api_' . Card::ROUTE_SLUG_AJAX, [new Order(), 'init']);
add_action('woocommerce_plugins_version_' . Card::PLUGIN_VERSION_ID, [new Card(), 'getVersion']);
add_action('woocommerce_sign_url_' . Card::GATEWAY_ID . '_webhook', [new Card(), 'getWebhookSignUrl']);
add_action('wp_enqueue_scripts', [$this, 'addPluginStyle']);
add_filter('plugin_action_links_' . plugin_basename(NPCARD_PLUGIN_PATH . NPCARD_PLUGIN_NAME . '.php'), [$this, 'addPluginSettingsLink']);
}
public function addPaymentGateways($gateways)
{
$gateways[] = Card::class;
//$gateways[] = NextPay::class;
return $gateways;
}
public function addPluginSettingsLink($links)
{
$actionLinks = array(
'settings' => '<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=npcard') . '" aria-label="' . esc_attr__('View NPCard settings', 'woocommerce') . '">' . esc_html__('Settings', 'woocommerce') . '</a>',
);
return array_merge($actionLinks, $links);
}
public function addPluginStyle()
{
wp_enqueue_style('npcard-styles', NPCARD_PLUGIN_URL . '/assets/css/card.css', [], NPCARD_PLUGIN_VERSION);
wp_enqueue_script('npcard-js', NPCARD_PLUGIN_URL . '/assets/js/card.js', [], NPCARD_PLUGIN_VERSION, true);
}
public function addGlobalSettings($settings, $currentSection)
{
if ($currentSection === 'npcard_general') {
$settings = [
'title' => [
'type' => 'title',
'desc' => '<h2>API settings</h2>
<br>Please enter your npcard credentials
<br><br>
<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout') . '">Back to payment overview</a>
<br>
<br>',
],
'app_id' => [
'title' => __('Unique App ID', NPCARD_PLUGIN_NAME),
'type' => 'text',
'desc' => '',
'id' => 'npcard_app_id',
'value' => get_option('npcard_app_id'),
],
'api_key' => [
'title' => __('API Key', NPCARD_PLUGIN_NAME),
'type' => 'password',
'desc' => '',
'id' => 'npcard_api_key',
'value' => get_option('npcard_api_key'),
],
'enable_sandbox' => [
'title' => __('Enable sandbox', NPCARD_PLUGIN_NAME),
'desc' => __('Yes', NPCARD_PLUGIN_NAME),
'type' => 'checkbox',
'default' => 'yes',
'id' => 'npcard_enable_sandbox',
'value' => get_option('npcard_enable_sandbox'),
],
'sectionend' => [
'type' => 'sectionend',
],
];
}
return $settings;
}
public function addPluginJs()
{
$async = Card::get_async_intent_url();
$paymentMessage = Card::PAYMENT_MESSAGE;
$inlineScript = <<<NPCARD
<script>
jQuery(function ($) {
$('#billing_phone').on('input', function() {
var value = $(this).val();
$(this).val(value.replace(/\D/g, ''));
});
$('#order_review').on('submit', function (e) {
let cardPaymentOption = jQuery('#payment_method_npcard');
if (cardPaymentOption.length && cardPaymentOption.is(':checked')) {
e.preventDefault();
npcardClient.getAjax('$async')
}
})
$(document.body).on('checkout_error', function (e, msg) {
if(msg && msg.indexOf('$paymentMessage') !== -1){
npcardClient.getAjax('$async')
}
});
})
</script>
NPCARD;
wp_add_inline_script('npcard-js', $inlineScript);
}
}