Plugin Directory

Changeset 3092050


Ignore:
Timestamp:
05/24/2024 10:41:33 AM (20 months ago)
Author:
cartasi
Message:

v 7.3.3

Location:
nexi-xpay-build
Files:
61 added
10 edited

Legend:

Unmodified
Added
Removed
  • nexi-xpay-build/trunk/README.md

    r3087872 r3092050  
    88WC Requires at least: 2.7.0
    99WC Tested up to: 8.8.3
    10 Stable tag: 7.3.2
     10Stable tag: 7.3.3
    1111License: GNU General Public License v3.0
    1212License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    275275 * Added - Installment payments for the Greek market
    276276 * Fixed - Minor issues
     277 
     278= 7.3.3 =
     279 * Fixed - Minor issues
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_3DS20_Data_Provider.php

    r3017117 r3092050  
    309309    }
    310310
    311     private static function getUserOrders()
    312     {
    313         return get_posts(array(
    314             'numberposts' => -1,
    315             'meta_key' => '_customer_user',
    316             'orderby' => 'date',
    317             'order' => 'DESC',
    318             'meta_value' => get_current_user_id(),
    319             'post_type' => wc_get_order_types(),
    320             'post_status' => array_keys(wc_get_order_statuses()),
    321         ));
    322     }
    323 
    324311    public static function getParamsFromWC($wc)
    325312    {
     
    349336        $userParams = array();
    350337
    351 
    352338        if ($wc->customer->get_date_created()) {
    353339            $userParams['chAccDate'] = $wc->customer->get_date_created()->format("Y-m-d");
     
    365351        return $params;
    366352    }
     353
    367354}
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Gateway_NPG_API.php

    r3087872 r3092050  
    121121            $orderId = $this->generate_npg_order_id(18, $order->get_order_number());
    122122
    123             $customerId = get_post_meta($order->get_id(), '_customer_user', true);
     123            $customerId = $order->get_customer_id();
    124124
    125125            $payload = array(
     
    130130                    "description" => "WC Order " . $order->get_id(),
    131131                    "customField" => "Woocommerce " . WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_WOOCOMMERCE_VERSION . " - nexi" . WC_GATEWAY_NEXI_PLUGIN_VARIANT . " " . WC_GATEWAY_XPAY_VERSION,
    132                     "customerId" => $customerId
    133132                ),
    134133                "paymentSession" => array(
     
    147146            );
    148147
     148            if (is_user_logged_in() && $customerId) {
     149                $payload["order"]["customerId"] = $customerId;
     150            }
     151
    149152            if ($installmentsNumber && $installmentsNumber >= 2) {
    150153                $payload["order"]["plan"] = array(
     
    508511    public function recurring_payment($order, $contractId, $amount)
    509512    {
    510         $customerId = get_post_meta($order->get_id(), '_customer_user', true);
     513        $customerId = $order->get_customer_id();
    511514
    512515        $orderId = $this->generate_npg_order_id(18, $order->get_id());
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Gateway_NPG_Generic_Method.php

    r3087872 r3092050  
    8585    {
    8686        try {
    87             $contractId = get_post_meta($order->get_id(), '_npg_' . 'recurringContractId', true);
     87            $subscriptionId = $order->get_meta('_subscription_renewal');
    8888
    89             if ($contractId === null) {
     89            if ($subscriptionId) {
     90                $idToUse = $subscriptionId;
     91            } else {
     92                $idToUse = $order->get_id();
     93            }
     94
     95            Log::actionInfo(__METHOD__ . "::" . __LINE__ . ' $idToUse ' . json_encode($idToUse));
     96
     97            $contractId = get_post_meta($idToUse, '_npg_' . 'recurringContractId', true);
     98
     99            if ($contractId === null || $contractId === '') {
    90100                throw new \Exception('Invalid contract id');
    91101            }
    92102
    93             $currency = get_post_meta($order->get_id(), '_npg_' . 'operationCurrency', true);
    94 
    95             if ($currency === null || $currency === '') {
    96                 $currency = get_post_meta($order->get_id(), '_order_currency', true);
    97             }
     103            $currency = $order->get_currency();
    98104
    99105            $newOrderId = \Nexi\WC_Gateway_NPG_API::getInstance()->recurring_payment($order, $contractId, \Nexi\WC_Gateway_NPG_Currency::calculate_amount_to_min_unit($amount_to_charge, $currency));
    100106
    101             //must be uptaed otherwise refferrs to the the first payment
     107            // must be updated otherwise refferrs to the the first payment
    102108            update_post_meta($order->get_id(), '_npg_' . "orderId", $newOrderId);
    103109
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Gateway_Nexi_Register_Available.php

    r3087872 r3092050  
    122122                    }
    123123                    break;
     124
    124125                case 'xpay_build':
    125                     if ($currentConfig['nexi_gateway'] == GATEWAY_NPG) {
     126                    if (WC_Nexi_Helper::nexi_array_key_exists_and_equals($currentConfig, 'nexi_gateway', GATEWAY_NPG)) {
    126127                        $mainGateway = new \Nexi\WC_Gateway_NPG_Cards_Build();
    127128                    } else {
     
    129130                    }
    130131                    break;
     132
    131133                default:
    132134                    Log::actionWarning(__('Invalid plugin variant value', 'woocommerce-gateway-nexi-xpay'));
     
    155157                    $this->evaluate_one_apm_xpay($am);
    156158                }
    157             } else if ($currentConfig['nexi_gateway'] == GATEWAY_NPG) {
     159            } else if (WC_Nexi_Helper::nexi_array_key_exists_and_equals($currentConfig, 'nexi_gateway', GATEWAY_NPG)) {
    158160                if (is_admin() || static::is_currency_valid_for_apm($this->currency, 'CARDS')) {
    159161                    $this->paymentGateways[] = $mainGateway;
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Gateway_XPay_API.php

    r3087872 r3092050  
    174174        );
    175175
    176         $costumer_id = get_post_meta($order->get_id(), '_customer_user', true);
     176        $costumer_id = $order->get_customer_id();
    177177
    178178        if ($recurringPaymentRequired) {
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Gateway_XPay_Cards.php

    r3017117 r3092050  
    6262    }
    6363
    64     public static function addNexiPendingPaymentOrderStatus()
    65     {
    66         register_post_status('wc-pd-pending-status', array(
    67             'label' => __('To be authorized', 'woocommerce-gateway-nexi-xpay'),
    68             'public' => true,
    69             'exclude_from_search' => false,
    70             'show_in_admin_all_list' => true,
    71             'show_in_admin_status_list' => true,
    72             'label_count' => _n_noop('To be authorized (%s)', 'To be authorized (%s)', 'woocommerce-gateway-nexi-xpay')
    73         ));
    74     }
    75 
    76     public static function wcOrderStatusesFilter($orderStatuses)
    77     {
    78         $orderStatuses['wc-pd-pending-status'] = __('To be authorized', 'woocommerce-gateway-nexi-xpay');
    79 
    80         return $orderStatuses;
    81     }
    82 
    83     public static function validOrderStatusesForPaymentCompleteFilter($orderStatuses)
    84     {
    85         $orderStatuses[] = 'pd-pending-status';
    86 
    87         return $orderStatuses;
    88     }
    89 
    9064    function init_form_fields()
    9165    {
    9266        parent::init_form_fields();
    9367    }
     68
    9469}
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Pending_Status.php

    r3017117 r3092050  
    11<?php
     2
    23/**
    34 * Copyright (c) 2019 Nexi Payments S.p.A.
     
    1314namespace Nexi;
    1415
    15 class WC_Pending_Status extends \WC_Payment_Gateway
     16class WC_Pending_Status
    1617{
    1718
  • nexi-xpay-build/trunk/src/classes/Nexi/WC_Save_Order_Meta.php

    r3017117 r3092050  
    5757        // doing so, when a new recurring payment is made for the same order, this data is copied automatically to the new order and can be used in the payment process
    5858        if (
    59             function_exists("wcs_is_subscription") &&
    60             wcs_is_subscription($order_id) ||
    61             (function_exists("wcs_order_contains_subscription") &&
    62                 (wcs_order_contains_subscription($order_id) || wcs_order_contains_renewal($order_id))
    63             )
     59            (function_exists("wcs_is_subscription") && wcs_is_subscription($order_id)) ||
     60            (function_exists("wcs_order_contains_subscription") && (wcs_order_contains_subscription($order_id) || wcs_order_contains_renewal($order_id)))
    6461        ) {
    6562            if (get_option("woocommerce_subscriptions_turn_off_automatic_payments") !== "yes") {
  • nexi-xpay-build/trunk/woocommerce-gateway-nexi-xpay.php

    r3087872 r3092050  
    55 * Plugin URI:
    66 * Description: Payment plugin for payment cards and alternative methods. Powered by Nexi.
    7  * Version: 7.3.2
     7 * Version: 7.3.3
    88 * Author: Nexi SpA
    99 * Author URI: https://www.nexi.it
     
    2222}
    2323
    24 if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) || is_plugin_active_for_network('woocommerce/woocommerce.php')) {
    25     define("WC_GATEWAY_XPAY_VERSION", "7.3.2");
    26 
    27     define("GATEWAY_XPAY", "xpay");
    28     define("GATEWAY_NPG", "npg");
    29 
    30     // The script build-plugin-variants.sh replaces xpay_build with 'xpay' or 'xpay_build'
    31     define('WC_GATEWAY_NEXI_PLUGIN_VARIANT', 'xpay_build');
    32     define('WC_GATEWAY_XPAY_PLUGIN_COLL', false);
    33 
    34     define('WC_SETTINGS_KEY', 'woocommerce_' . WC_GATEWAY_NEXI_PLUGIN_VARIANT . '_settings');
    35 
    36     define('NPG_OR_AUTHORIZED', 'AUTHORIZED');
    37     define('NPG_OR_EXECUTED', 'EXECUTED');
    38     define('NPG_OR_DECLINED', 'DECLINED');
    39     define('NPG_OR_DENIED_BY_RISK', 'DENIED_BY_RISK');
    40     define('NPG_OR_THREEDS_VALIDATED', 'THREEDS_VALIDATED');
    41     define('NPG_OR_THREEDS_FAILED', 'THREEDS_FAILED');
    42     define('NPG_OR_3DS_FAILED', '3DS_FAILED');
    43     define('NPG_OR_PENDING', 'PENDING');
    44     define('NPG_OR_CANCELED', 'CANCELED');
    45     define('NPG_OR_CANCELLED', 'CANCELLED');
    46     define('NPG_OR_VOIDED', 'VOIDED');
    47     define('NPG_OR_REFUNDED', 'REFUNDED');
    48     define('NPG_OR_FAILED', 'FAILED');
    49     define('NPG_OR_EXPIRED', 'EXPIRED');
    50 
    51     define('NPG_PAYMENT_SUCCESSFUL', [
    52         NPG_OR_AUTHORIZED,
    53         NPG_OR_EXECUTED,
    54     ]);
    55 
    56     define('NPG_PAYMENT_FAILURE', [
    57         NPG_OR_DECLINED,
    58         NPG_OR_DENIED_BY_RISK,
    59         NPG_OR_FAILED,
    60         NPG_OR_THREEDS_FAILED,
    61         NPG_OR_3DS_FAILED,
    62     ]);
    63 
    64     define('NPG_CONTRACT_CIT', 'CIT');
    65 
    66     define('NPG_OT_AUTHORIZATION', 'AUTHORIZATION');
    67     define('NPG_OT_CAPTURE', 'CAPTURE');
    68     define('NPG_OT_VOID', 'VOID');
    69     define('NPG_OT_REFUND', 'REFUND');
    70     define('NPG_OT_CANCEL', 'CANCEL');
    71 
    72     define('NPG_NO_RECURRING', 'NO_RECURRING');
    73     define('NPG_SUBSEQUENT_PAYMENT', 'SUBSEQUENT_PAYMENT');
    74     define('NPG_CONTRACT_CREATION', 'CONTRACT_CREATION');
    75     define('NPG_CARD_SUBSTITUTION', 'CARD_SUBSTITUTION');
    76 
    77     define('NPG_RT_MIT_SCHEDULED', 'MIT_SCHEDULED');
    78 
    79     load_plugin_textdomain('woocommerce-gateway-nexi-xpay', false, dirname(plugin_basename(__FILE__)) . '/lang');
    80 
    81     include_once realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . "autoload.php";
    82 
    83     add_filter('woocommerce_payment_gateways', "\Nexi\WC_Gateway_Nexi_Register_Available::register");
    84 
    85     // Register endpoint in the rest api for s2s notification API, for post payment redirect url and for cancel url
    86     add_action('rest_api_init', '\Nexi\WC_Gateway_XPay_Process_Completion::rest_api_init');
    87     add_action('rest_api_init', '\Nexi\WC_Gateway_NPG_Process_Completion::rest_api_init');
    88 
    89     \Nexi\WC_Gateway_XPay_Process_Completion::register();
    90     \Nexi\WC_Gateway_NPG_Process_Completion::register();
    91 
    92     \Nexi\WC_Pagodil_Widget::register();
    93 
    94     add_action('wp_ajax_get_build_fields', '\Nexi\WC_Gateway_NPG_Cards_Build::get_build_fields');
    95     add_action('wp_ajax_nopriv_get_build_fields', '\Nexi\WC_Gateway_NPG_Cards_Build::get_build_fields');
    96 
    97     define('WC_ECOMMERCE_GATEWAY_NEXI_MAIN_FILE', __FILE__);
    98 
    99     function xpay_gw_wp_enqueue_scripts()
    100     {
    101         wp_enqueue_script('xpay-checkout', plugins_url('assets/js/xpay.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
    102         wp_enqueue_style('xpay-checkout', plugins_url('assets/css/xpay.css', __FILE__), [], WC_GATEWAY_XPAY_VERSION);
    103 
    104         if (WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay_build') {
    105             if (\Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
    106                 wp_enqueue_script('xpay-build-npg-checkout', plugins_url('assets/js/xpay-build-npg.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
    107             } else {
    108                 wp_enqueue_script('xpay-build-checkout', plugins_url('assets/js/xpay-build.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
    109             }
    110         }
    111     }
    112 
    113     add_action('admin_init', '\Nexi\WC_Admin_Page::init');
    114 
    115     add_action('wp_enqueue_scripts', 'xpay_gw_wp_enqueue_scripts');
    116 
    117     if (!defined("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_WOOCOMMERCE_VERSION")) {
    118         if (!function_exists('get_plugins')) {
    119             require_once ABSPATH . 'wp-admin/includes/plugin.php';
    120         }
    121 
    122         $plugins = get_plugins();
    123 
    124         define("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_WOOCOMMERCE_VERSION", $plugins["woocommerce/woocommerce.php"]["Version"]);
    125     }
    126 
    127     if (!defined("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_DEFAULT_LOGO_URL")) {
    128         $default_logo_url = plugins_url('assets/images/logo.jpg', plugin_basename(__FILE__));
    129 
    130         define("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_DEFAULT_LOGO_URL", $default_logo_url);
    131     }
    132 
    133     // custom hook called by the scheduled cron
    134     add_action('wp_nexi_polling', 'wp_nexi_polling_executor');
    135 
    136     function wp_nexi_polling_executor()
    137     {
    138         $args = array(
    139             'payment_method' => WC_GATEWAY_NEXI_PLUGIN_VARIANT,
    140             'status' => ['wc-pending'],
    141             'orderby' => 'date',
    142             'order' => 'ASC',
    143         );
    144 
    145         $orders = wc_get_orders($args);
    146 
    147         foreach ($orders as $order) {
    148             $authorizationRecord = \Nexi\WC_Gateway_NPG_API::getInstance()->get_order_status($order->get_id());
    149 
    150             if ($authorizationRecord === null) {
    151                 \Nexi\Log::actionWarning(__FUNCTION__ . ': authorization operation not found for order: ' . $order->get_id());
    152                 continue;
    153             }
    154 
    155             $orderObj = new \WC_Order($order->get_id());
    156 
    157             switch ($authorizationRecord['operationResult']) {
    158                 case NPG_OR_AUTHORIZED:
    159                 case NPG_OR_EXECUTED:
    160                     $completed = $orderObj->payment_complete(get_post_meta($order->get_id(), "_npg_" . "orderId", true));
    161 
    162                     if ($completed) {
    163                         \Nexi\WC_Save_Order_Meta::saveSuccessNpg(
    164                             $order->get_id(),
    165                             $authorizationRecord
    166                         );
    167                     } else {
    168                         \Nexi\Log::actionWarning(__FUNCTION__ . ': unable to change order status: ' . $orderObj->get_status());
    169                     }
    170                     break;
    171 
    172                 case NPG_OR_PENDING:
    173                     \Nexi\Log::actionWarning(__FUNCTION__ . ': operation not in a final status yet');
    174                     break;
    175 
    176                 case NPG_OR_CANCELED:
    177                 case NPG_OR_CANCELLED:
    178                     \Nexi\Log::actionWarning(__FUNCTION__ . ': payment canceled');
    179 
    180                     if ($order->get_status() != 'cancelled') {
    181                         $order->update_status('cancelled');
    182                     }
    183                     break;
    184 
    185                 case NPG_OR_DECLINED:
    186                 case NPG_OR_DENIED_BY_RISK:
    187                 case NPG_OR_THREEDS_FAILED:
    188                 case NPG_OR_3DS_FAILED:
    189                 case NPG_OR_FAILED:
    190                     \Nexi\Log::actionWarning(__FUNCTION__ . ': payment error - operation: ' . json_encode($authorizationRecord));
    191 
    192                     if ($order->get_status() != 'cancelled') {
    193                         $orderObj->update_status('failed');
    194                     }
    195 
    196                     $orderObj->add_order_note(__('Payment error', 'woocommerce-gateway-nexi-xpay'));
    197                     break;
    198 
    199                 default:
    200                     \Nexi\Log::actionWarning(__FUNCTION__ . ': payment error - not managed operation status: ' . json_encode($authorizationRecord));
    201                     break;
    202             }
    203         }
    204     }
    205 
    206     add_action('wp_nexi_update_npg_payment_methods', 'wp_nexi_update_npg_payment_methods_executor');
    207 
    208     function wp_nexi_update_npg_payment_methods_executor()
    209     {
    210         try {
    211             \Nexi\WC_Gateway_NPG_API::getInstance()->get_profile_info();
    212         } catch (\Exception $exc) {
    213             \Nexi\Log::actionWarning(__FUNCTION__ . $exc->getMessage());
    214         }
    215     }
    216 
    217     // to add a new custom interval for cron execution
    218     function my_add_nexi_schedules_for_polling($schedules)
    219     {
    220         // add a 'nexi_polling_schedule' schedule to the existing set
    221         $schedules['nexi_polling_schedule'] = array(
    222             'interval' => 300,
    223             'display' => __('5 minutes')
    224         );
    225 
    226         $schedules['nexi_polling_schedule_2h'] = array(
    227             'interval' => 7200,
    228             'display' => __('2 hours'),
    229         );
    230 
    231         return $schedules;
    232     }
    233 
    234     add_filter('cron_schedules', 'my_add_nexi_schedules_for_polling');
    235 
    236     //chcks if the task is not already scheduled
    237     if (!wp_next_scheduled('wp_nexi_polling') && WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay' && \Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
    238         //schedules the task by giving the first execution time, the interval and the hook to call
    239         wp_schedule_event(time(), 'nexi_polling_schedule', 'wp_nexi_polling');
    240     }
    241 
    242     if (!wp_next_scheduled('wp_nexi_update_npg_payment_methods') && WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay' && \Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
    243         //schedules the task by giving the first execution time, the interval and the hook to call
    244         wp_schedule_event(time(), 'nexi_polling_schedule_2h', 'wp_nexi_update_npg_payment_methods');
    245     }
    246 
    247     function xpay_plugin_activation()
    248     {
    249         $nexi_unique = get_option("nexi_unique");
    250 
    251         if ($nexi_unique == "") {
    252             update_option('nexi_unique', uniqid());
    253         }
    254     }
    255 
    256     register_activation_hook(__FILE__, 'xpay_plugin_activation');
    257 
    258     function xpay_plugin_deactivation()
    259     {
    260         $timestamp = wp_next_scheduled('wp_nexi_polling');
    261 
    262         if ($timestamp) {
    263             wp_unschedule_event($timestamp, 'wp_nexi_polling');
    264         }
    265 
    266         $timestamp = wp_next_scheduled('wp_nexi_update_npg_payment_methods');
    267 
    268         if ($timestamp) {
    269             wp_unschedule_event($timestamp, 'wp_nexi_update_npg_payment_methods');
    270         }
    271     }
    272 
    273     register_deactivation_hook(__FILE__, 'xpay_plugin_deactivation');
    274 
    275     function xpay_plugin_action_links($links)
    276     {
    277         $plugin_links = array(
    278             '<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . WC_GATEWAY_NEXI_PLUGIN_VARIANT)) . '">' . __('Settings') . '</a>',
    279         );
    280 
    281         return array_merge($plugin_links, $links);
    282     }
    283 
    284     add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'xpay_plugin_action_links');
    285 
    286     function nexi_xpay_plugin_init()
    287     {
    288         \Nexi\WC_Pending_Status::addNexiPendingPaymentOrderStatus();
    289 
    290         \Nexi\WC_Nexi_Db::run_updates();
    291     }
    292 
    293     add_action('init', 'nexi_xpay_plugin_init');
    294 
    295     add_filter('wc_order_statuses', '\Nexi\WC_Pending_Status::wcOrderStatusesFilter');
    296 
    297     add_filter('woocommerce_valid_order_statuses_for_payment_complete', '\Nexi\WC_Pending_Status::validOrderStatusesForPaymentCompleteFilter');
    298 
    299     add_action('woocommerce_payment_token_deleted', '\Nexi\WC_Gateway_NPG_Cards::woocommerce_payment_token_deleted', 10, 2);
    300 
    301     function nexixpay_admin_warning()
    302     {
    303         if (!extension_loaded('bcmath')) {
    304             $notice = '
     24add_action('plugins_loaded', 'nexi_xpay_plugins_loaded');
     25
     26function nexi_xpay_plugins_loaded()
     27{
     28    if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) || is_plugin_active_for_network('woocommerce/woocommerce.php')) {
     29        define("WC_GATEWAY_XPAY_VERSION", "7.3.3");
     30
     31        define("GATEWAY_XPAY", "xpay");
     32        define("GATEWAY_NPG", "npg");
     33
     34        // The script build-plugin-variants.sh replaces xpay_build with 'xpay' or 'xpay_build'
     35        define('WC_GATEWAY_NEXI_PLUGIN_VARIANT', 'xpay_build');
     36        define('WC_GATEWAY_XPAY_PLUGIN_COLL', false);
     37
     38        define('WC_SETTINGS_KEY', 'woocommerce_' . WC_GATEWAY_NEXI_PLUGIN_VARIANT . '_settings');
     39
     40        define('NPG_OR_AUTHORIZED', 'AUTHORIZED');
     41        define('NPG_OR_EXECUTED', 'EXECUTED');
     42        define('NPG_OR_DECLINED', 'DECLINED');
     43        define('NPG_OR_DENIED_BY_RISK', 'DENIED_BY_RISK');
     44        define('NPG_OR_THREEDS_VALIDATED', 'THREEDS_VALIDATED');
     45        define('NPG_OR_THREEDS_FAILED', 'THREEDS_FAILED');
     46        define('NPG_OR_3DS_FAILED', '3DS_FAILED');
     47        define('NPG_OR_PENDING', 'PENDING');
     48        define('NPG_OR_CANCELED', 'CANCELED');
     49        define('NPG_OR_CANCELLED', 'CANCELLED');
     50        define('NPG_OR_VOIDED', 'VOIDED');
     51        define('NPG_OR_REFUNDED', 'REFUNDED');
     52        define('NPG_OR_FAILED', 'FAILED');
     53        define('NPG_OR_EXPIRED', 'EXPIRED');
     54
     55        define('NPG_PAYMENT_SUCCESSFUL', [
     56            NPG_OR_AUTHORIZED,
     57            NPG_OR_EXECUTED,
     58        ]);
     59
     60        define('NPG_PAYMENT_FAILURE', [
     61            NPG_OR_DECLINED,
     62            NPG_OR_DENIED_BY_RISK,
     63            NPG_OR_FAILED,
     64            NPG_OR_THREEDS_FAILED,
     65            NPG_OR_3DS_FAILED,
     66        ]);
     67
     68        define('NPG_CONTRACT_CIT', 'CIT');
     69
     70        define('NPG_OT_AUTHORIZATION', 'AUTHORIZATION');
     71        define('NPG_OT_CAPTURE', 'CAPTURE');
     72        define('NPG_OT_VOID', 'VOID');
     73        define('NPG_OT_REFUND', 'REFUND');
     74        define('NPG_OT_CANCEL', 'CANCEL');
     75
     76        define('NPG_NO_RECURRING', 'NO_RECURRING');
     77        define('NPG_SUBSEQUENT_PAYMENT', 'SUBSEQUENT_PAYMENT');
     78        define('NPG_CONTRACT_CREATION', 'CONTRACT_CREATION');
     79        define('NPG_CARD_SUBSTITUTION', 'CARD_SUBSTITUTION');
     80
     81        define('NPG_RT_MIT_SCHEDULED', 'MIT_SCHEDULED');
     82
     83        load_plugin_textdomain('woocommerce-gateway-nexi-xpay', false, dirname(plugin_basename(__FILE__)) . '/lang');
     84
     85        include_once realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . "autoload.php";
     86
     87        add_filter('woocommerce_payment_gateways', "\Nexi\WC_Gateway_Nexi_Register_Available::register");
     88
     89        // Register endpoint in the rest api for s2s notification API, for post payment redirect url and for cancel url
     90        add_action('rest_api_init', '\Nexi\WC_Gateway_XPay_Process_Completion::rest_api_init');
     91        add_action('rest_api_init', '\Nexi\WC_Gateway_NPG_Process_Completion::rest_api_init');
     92
     93        \Nexi\WC_Gateway_XPay_Process_Completion::register();
     94        \Nexi\WC_Gateway_NPG_Process_Completion::register();
     95
     96        \Nexi\WC_Pagodil_Widget::register();
     97
     98        add_action('wp_ajax_get_build_fields', '\Nexi\WC_Gateway_NPG_Cards_Build::get_build_fields');
     99        add_action('wp_ajax_nopriv_get_build_fields', '\Nexi\WC_Gateway_NPG_Cards_Build::get_build_fields');
     100
     101        define('WC_ECOMMERCE_GATEWAY_NEXI_MAIN_FILE', __FILE__);
     102
     103        function xpay_gw_wp_enqueue_scripts()
     104        {
     105            wp_enqueue_script('xpay-checkout', plugins_url('assets/js/xpay.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
     106            wp_enqueue_style('xpay-checkout', plugins_url('assets/css/xpay.css', __FILE__), [], WC_GATEWAY_XPAY_VERSION);
     107
     108            if (WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay_build') {
     109                if (\Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
     110                    wp_enqueue_script('xpay-build-npg-checkout', plugins_url('assets/js/xpay-build-npg.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
     111                } else {
     112                    wp_enqueue_script('xpay-build-checkout', plugins_url('assets/js/xpay-build.js', __FILE__), array('jquery'), WC_GATEWAY_XPAY_VERSION);
     113                }
     114            }
     115        }
     116
     117        add_action('admin_init', '\Nexi\WC_Admin_Page::init');
     118
     119        add_action('wp_enqueue_scripts', 'xpay_gw_wp_enqueue_scripts');
     120
     121        if (!defined("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_WOOCOMMERCE_VERSION")) {
     122            if (!function_exists('get_plugins')) {
     123                require_once ABSPATH . 'wp-admin/includes/plugin.php';
     124            }
     125
     126            $plugins = get_plugins();
     127
     128            define("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_WOOCOMMERCE_VERSION", $plugins["woocommerce/woocommerce.php"]["Version"]);
     129        }
     130
     131        if (!defined("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_DEFAULT_LOGO_URL")) {
     132            $default_logo_url = plugins_url('assets/images/logo.jpg', plugin_basename(__FILE__));
     133
     134            define("WC_WOOCOMMERCE_GATEWAY_NEXI_XPAY_DEFAULT_LOGO_URL", $default_logo_url);
     135        }
     136
     137        // custom hook called by the scheduled cron
     138        add_action('wp_nexi_polling', 'wp_nexi_polling_executor');
     139
     140        function wp_nexi_polling_executor()
     141        {
     142            $args = array(
     143                'payment_method' => WC_GATEWAY_NEXI_PLUGIN_VARIANT,
     144                'status' => ['wc-pending'],
     145                'orderby' => 'date',
     146                'order' => 'ASC',
     147            );
     148
     149            $orders = wc_get_orders($args);
     150
     151            foreach ($orders as $order) {
     152                $authorizationRecord = \Nexi\WC_Gateway_NPG_API::getInstance()->get_order_status($order->get_id());
     153
     154                if ($authorizationRecord === null) {
     155                    \Nexi\Log::actionWarning(__FUNCTION__ . ': authorization operation not found for order: ' . $order->get_id());
     156                    continue;
     157                }
     158
     159                $orderObj = new \WC_Order($order->get_id());
     160
     161                switch ($authorizationRecord['operationResult']) {
     162                    case NPG_OR_AUTHORIZED:
     163                    case NPG_OR_EXECUTED:
     164                        $completed = $orderObj->payment_complete(get_post_meta($order->get_id(), "_npg_" . "orderId", true));
     165
     166                        if ($completed) {
     167                            \Nexi\WC_Save_Order_Meta::saveSuccessNpg(
     168                                $order->get_id(),
     169                                $authorizationRecord
     170                            );
     171                        } else {
     172                            \Nexi\Log::actionWarning(__FUNCTION__ . ': unable to change order status: ' . $orderObj->get_status());
     173                        }
     174                        break;
     175
     176                    case NPG_OR_PENDING:
     177                        \Nexi\Log::actionWarning(__FUNCTION__ . ': operation not in a final status yet');
     178                        break;
     179
     180                    case NPG_OR_CANCELED:
     181                    case NPG_OR_CANCELLED:
     182                        \Nexi\Log::actionWarning(__FUNCTION__ . ': payment canceled');
     183
     184                        if ($order->get_status() != 'cancelled') {
     185                            $order->update_status('cancelled');
     186                        }
     187                        break;
     188
     189                    case NPG_OR_DECLINED:
     190                    case NPG_OR_DENIED_BY_RISK:
     191                    case NPG_OR_THREEDS_FAILED:
     192                    case NPG_OR_3DS_FAILED:
     193                    case NPG_OR_FAILED:
     194                        \Nexi\Log::actionWarning(__FUNCTION__ . ': payment error - operation: ' . json_encode($authorizationRecord));
     195
     196                        if ($order->get_status() != 'cancelled') {
     197                            $orderObj->update_status('failed');
     198                        }
     199
     200                        $orderObj->add_order_note(__('Payment error', 'woocommerce-gateway-nexi-xpay'));
     201                        break;
     202
     203                    default:
     204                        \Nexi\Log::actionWarning(__FUNCTION__ . ': payment error - not managed operation status: ' . json_encode($authorizationRecord));
     205                        break;
     206                }
     207            }
     208        }
     209
     210        add_action('wp_nexi_update_npg_payment_methods', 'wp_nexi_update_npg_payment_methods_executor');
     211
     212        function wp_nexi_update_npg_payment_methods_executor()
     213        {
     214            try {
     215                \Nexi\WC_Gateway_NPG_API::getInstance()->get_profile_info();
     216            } catch (\Exception $exc) {
     217                \Nexi\Log::actionWarning(__FUNCTION__ . $exc->getMessage());
     218            }
     219        }
     220
     221        // to add a new custom interval for cron execution
     222        function my_add_nexi_schedules_for_polling($schedules)
     223        {
     224            // add a 'nexi_polling_schedule' schedule to the existing set
     225            $schedules['nexi_polling_schedule'] = array(
     226                'interval' => 300,
     227                'display' => __('5 minutes')
     228            );
     229
     230            $schedules['nexi_polling_schedule_2h'] = array(
     231                'interval' => 7200,
     232                'display' => __('2 hours'),
     233            );
     234
     235            return $schedules;
     236        }
     237
     238        add_filter('cron_schedules', 'my_add_nexi_schedules_for_polling');
     239
     240        //chcks if the task is not already scheduled
     241        if (!wp_next_scheduled('wp_nexi_polling') && WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay' && \Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
     242            //schedules the task by giving the first execution time, the interval and the hook to call
     243            wp_schedule_event(time(), 'nexi_polling_schedule', 'wp_nexi_polling');
     244        }
     245
     246        if (!wp_next_scheduled('wp_nexi_update_npg_payment_methods') && WC_GATEWAY_NEXI_PLUGIN_VARIANT == 'xpay' && \Nexi\WC_Nexi_Helper::nexi_is_gateway_NPG()) {
     247            //schedules the task by giving the first execution time, the interval and the hook to call
     248            wp_schedule_event(time(), 'nexi_polling_schedule_2h', 'wp_nexi_update_npg_payment_methods');
     249        }
     250
     251        function xpay_plugin_activation()
     252        {
     253            $nexi_unique = get_option("nexi_unique");
     254
     255            if ($nexi_unique == "") {
     256                update_option('nexi_unique', uniqid());
     257            }
     258        }
     259
     260        register_activation_hook(__FILE__, 'xpay_plugin_activation');
     261
     262        function xpay_plugin_deactivation()
     263        {
     264            $timestamp = wp_next_scheduled('wp_nexi_polling');
     265
     266            if ($timestamp) {
     267                wp_unschedule_event($timestamp, 'wp_nexi_polling');
     268            }
     269
     270            $timestamp = wp_next_scheduled('wp_nexi_update_npg_payment_methods');
     271
     272            if ($timestamp) {
     273                wp_unschedule_event($timestamp, 'wp_nexi_update_npg_payment_methods');
     274            }
     275        }
     276
     277        register_deactivation_hook(__FILE__, 'xpay_plugin_deactivation');
     278
     279        function xpay_plugin_action_links($links)
     280        {
     281            $plugin_links = array(
     282                '<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . WC_GATEWAY_NEXI_PLUGIN_VARIANT)) . '">' . __('Settings') . '</a>',
     283            );
     284
     285            return array_merge($plugin_links, $links);
     286        }
     287
     288        add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'xpay_plugin_action_links');
     289
     290        function nexi_xpay_plugin_init()
     291        {
     292            \Nexi\WC_Pending_Status::addNexiPendingPaymentOrderStatus();
     293
     294            \Nexi\WC_Nexi_Db::run_updates();
     295        }
     296
     297        add_action('init', 'nexi_xpay_plugin_init');
     298
     299        add_filter('wc_order_statuses', '\Nexi\WC_Pending_Status::wcOrderStatusesFilter');
     300
     301        add_filter('woocommerce_valid_order_statuses_for_payment_complete', '\Nexi\WC_Pending_Status::validOrderStatusesForPaymentCompleteFilter');
     302
     303        add_action('woocommerce_payment_token_deleted', '\Nexi\WC_Gateway_NPG_Cards::woocommerce_payment_token_deleted', 10, 2);
     304
     305        function nexixpay_admin_warning()
     306        {
     307            if (!extension_loaded('bcmath')) {
     308                $notice = '
    305309                <div class="notice notice-warning">
    306310                    <p><b>Nexi XPay</b>: ' . __('Warning, the PHP extension bcmath is not enabled. The amounts calculated by the plugin may be incorrect; please enable it to ensure correct calculations.', 'woocommerce-gateway-nexi-xpay') . '</p>
     
    308312            ';
    309313
    310             echo $notice;
    311         }
     314                echo $notice;
     315            }
     316        }
     317
     318        add_action('admin_notices', 'nexixpay_admin_warning');
    312319    }
    313 
    314     add_action('admin_notices', 'nexixpay_admin_warning');
    315320}
Note: See TracChangeset for help on using the changeset viewer.