Plugin Directory

Changeset 3270180


Ignore:
Timestamp:
04/10/2025 07:28:58 AM (9 months ago)
Author:
paygine
Message:

version 3.2.14

Location:
paygine
Files:
514 added
2 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • paygine/trunk/languages/paygine-payment-ru_RU.po

    r3213773 r3270180  
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: Method payment module 3.1.1\n"
     5"Project-Id-Version: Method payment module 3.2.14\n"
    66"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/paygine\n"
    77"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
     
    124124#: paygine.php:184
    125125msgid "Plait (one-stage payment)"
    126 msgstr "Только Халва Частями (одностадийная оплата)"
     126msgstr "Плайт (одностадийная оплата)"
    127127
    128128#: paygine.php:185
    129129msgid "Plait (two-stage payment)"
    130 msgstr "Только Халва Частями (двухстадийная оплата)"
     130msgstr "Плайт (двухстадийная оплата)"
    131131
    132132#: paygine.php:186
  • paygine/trunk/languages/paygine-payment.pot

    r3213773 r3270180  
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: Method payment module 3.1.1\n"
     5"Project-Id-Version: Method payment module 3.2.14\n"
    66"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/trunk\n"
    77"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  • paygine/trunk/paygine-payment.php

    r3213773 r3270180  
    11<?php
    2 declare(strict_types = 1);
    3 
    42/*
    53 * Plugin Name: Method payment module
    64 * Plugin URI: https://methodpay.ru/
    75 * Description: Receive payments via Visa / Mastercard / MIR easily with Method bank cards processing
    8  * Version: 3.1.1
     6 * Version: 3.2.14
    97 * Author: Method
    108 * Tested up to: 6.6.1
     
    1513 */
    1614
    17 if(!class_exists('B2P\Client'))
    18     require_once __DIR__ . '/sdk/sdk_autoload.php';
     15// Exit if accessed directly.
     16if ( ! defined( 'ABSPATH' ) ) {
     17    exit;
     18}
    1919
    20 use B2P\Client;
    21 use B2P\Responses\Error;
    22 use B2P\Models\Enums\CurrencyCode;
    23 use B2P\Models\Interfaces\CreditOrder;
    24 
    25 defined('ABSPATH') or die("No script kiddies please!");
    26 
    27 add_action('plugins_loaded', 'init_woocommerce_paygine', 0);
    28 
    29 function init_woocommerce_paygine() {
    30     if(!class_exists('WC_Payment_Gateway')) return;
    31 
    32     load_plugin_textdomain('paygine-payment', false, dirname(plugin_basename(__FILE__)) . '/languages');
    33 
    34     class woocommerce_paygine extends WC_Payment_Gateway {
    35         const PLUGIN_URL = 'https://methodpay.ru';
    36 
    37         protected string $api_operation;
    38         protected array $query_params = [];
    39         protected string $notify_url;
    40         protected string $sector;
    41         protected string $password;
    42         protected string $testmode;
    43         protected string $hash_algo;
    44         protected bool $notify_customer_enabled;
    45         protected string $registered_status;
    46         protected string $authorized_status;
    47         protected string $loan_status;
    48         protected string $completed_status;
    49         protected string $agreement_status;
    50         protected string $payment_expected_status;
    51         protected string $canceled_status;
    52         protected string $payment_method;
    53         protected string $tax;
    54         protected int $currency;
    55         protected array $fiscal_positions;
    56         protected array $shop_cart;
    57         protected string $logo_field;
    58         protected string $remove_logo_field;
    59 
    60         public function __construct() {
    61             require_once ABSPATH . 'wp-admin/includes/file.php';
    62 
    63             $this->id = 'paygine';
    64             $this->method_title = __('Method', 'paygine-payment');
    65             $this->method_description = sprintf(__('To accept payments through the plugin, you need to apply to connect to Method on the website <a href="%1$s" target="_blank">%1$s</a> and enter into an agreement with the company.<br/>Support email: <a href="mailto:%2$s">%2$s</a>', 'paygine-payment'), self::PLUGIN_URL, '[email protected]');
    66             $this->icon = plugins_url('assets/img/method.svg', __FILE__);
    67             $this->has_fields = true;
    68             $this->notify_url = add_query_arg('wc-api', 'paygine_notify', home_url('/'));
    69             $this->supports = ['refunds', 'products'];
    70 
    71             $this->init_form_fields();
    72             $this->init_settings();
    73 
    74             $this->title = $this->settings['title'] ? $this->settings['title'] : $this->method_title;
    75             $this->description = $this->settings['description'] ? $this->settings['description'] : sprintf(__('Payments with bank cards via the <a href="%s" target="_blank">Method</a> payment system.', 'paygine-payment'), self::PLUGIN_URL);
    76             $this->logo = $this->settings['logo'] ?? '';
    77             $this->sector = $this->settings['sector'];
    78             $this->password = $this->settings['password'];
    79             $this->testmode = $this->settings['testmode'];
    80             $this->hash_algo = ($this->settings['hash_algo'] === 'sha256') ? '1' : '0';
    81             $this->payment_method = $this->settings['payment_method'] ?? '';
    82             $this->tax = $this->settings['tax'] ?? '6';
    83             $this->notify_customer_enabled = $this->settings['notify_customer_enabled'] === 'yes';
    84             $this->currency = $this->get_currency(get_woocommerce_currency());
    85             $this->logo_field = $this->plugin_id . $this->id . '_logo';
    86             $this->remove_logo_field = $this->plugin_id . $this->id . '_remove_logo';
    87             $this->registered_status = $this->settings['registered_status'] ?? '';
    88             $this->authorized_status = $this->settings['authorized_status'] ?? '';
    89             $this->loan_status = $this->settings['loan_status'] ?? '';
    90             $this->completed_status = $this->settings['completed_status'] ?? '';
    91             $this->canceled_status = $this->settings['canceled_status'] ?? '';
    92             $this->agreement_status = $this->settings['agreement_status'] ?? '';
    93             $this->payment_expected_status = $this->settings['payment_expected_status'] ?? '';
    94 
    95             switch($this->payment_method) {
    96                 case 'purchaseWithInstallment':
    97                 case 'authorizeWithInstallment':
    98                     $this->title = __('Pay for your order in installments', 'paygine-payment');
    99                     $this->icon = plugins_url('assets/img/svkb.svg', __FILE__);
    100                     break;
    101                 default:
    102                     if($this->logo)
    103                         $this->icon = wp_get_attachment_url($this->logo);
    104                     break;
     20class WC_Paygine_Payments {
     21   
     22    /**
     23     * Plugin bootstrapping.
     24     */
     25    public static function init() {
     26       
     27        // Paygine Payment gateway class.
     28        add_action( 'plugins_loaded', array( __CLASS__, 'includes' ), 0 );
     29       
     30        // Make the Paygine Payment gateway available to WC.
     31        add_filter( 'woocommerce_payment_gateways', array( __CLASS__, 'add_gateway' ) );
     32       
     33        // Registers WooCommerce Blocks integration.
     34        add_action( 'woocommerce_blocks_loaded', array( __CLASS__, 'woocommerce_gateway_paygine_woocommerce_block_support' ) );
     35       
     36        // Loading languages
     37        load_plugin_textdomain('paygine-payment', false, dirname(plugin_basename(__FILE__)) . '/languages');
     38       
     39        add_filter('plugin_action_links_' . plugin_basename(__FILE__),
     40            function ($links) {
     41                if(!class_exists('woocommerce')){
     42                    return $links;
     43                }
     44               
     45                array_unshift($links, sprintf('<a href="%1$s">%2$s</a>', admin_url('admin.php?page=wc-settings&tab=checkout&section=paygine'), __('Settings', 'paygine-payment')));
     46               
     47                return $links;
    10548            }
    106 
    107             $this->getClient();
    108 
    109             add_action('woocommerce_api_paygine_notify', [$this, 'callback_notify']);
    110             add_action('woocommerce_api_paygine_complete_action', [$this, 'process_complete']);
    111             add_action("woocommerce_api_paygine_skvb_widget", [$this, 'skvb_widget']);
    112             add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
    113             add_action('woocommerce_order_item_add_action_buttons', [$this, 'wc_order_item_add_complete_button']);
    114             add_filter('woocommerce_generate_image_html', 'woocommerce_generate_image_html');
    115             add_action("woocommerce_order_status_changed", [$this, 'wc_paygine_order_payment_change'], 10, 4);
    116         }
    117 
    118         public function init_form_fields() {
    119             $wc_statuses = wc_get_order_statuses();
    120 
    121             $this->form_fields = [
    122                 'enabled' => [
    123                     'title' => __('Enable/Disable', 'paygine-payment'),
    124                     'type' => 'checkbox',
    125                     'label' => __('Enable Method checkout method', 'paygine-payment'),
    126                     'default' => 'yes'
    127                 ],
    128                 'title' => [
    129                     'title' => __('Title', 'paygine-payment'),
    130                     'type' => 'text',
    131                     'description' => __('Custom title for payment type', 'paygine-payment'),
    132                     'desc_tip' => true,
    133                     'placeholder' => $this->method_title,
    134                     'default' => ''
    135                 ],
    136                 'description' => [
    137                     'title' => __('Description', 'paygine-payment'),
    138                     'type' => 'textarea',
    139                     'description' => __('Custom description for payment type', 'paygine-payment'),
    140                     'desc_tip' => true,
    141                     'css' => 'width: 400px;',
    142                     'default' => ''
    143                 ],
    144                 'logo' => [
    145                     'title' => __('Logo', 'paygine-payment'),
    146                     'type' => 'image',
    147                     'description' => __('Custom logo for payment type', 'paygine-payment'),
    148                     'desc_tip' => true,
    149                     'default' => ''
    150                 ],
    151                 'sector' => [
    152                     'title' => __('Sector ID', 'paygine-payment'),
    153                     'type' => 'text',
    154                     'description' => __('Your shop identifier at Method', 'paygine-payment'),
    155                     'desc_tip' => true,
    156                     'placeholder' => '1234',
    157                     'default' => ''
    158                 ],
    159                 'password' => [
    160                     'title' => __('Password', 'paygine-payment'),
    161                     'type' => 'text',
    162                     'description' => __('Password to use for digital signature', 'paygine-payment'),
    163                     'desc_tip' => true,
    164                     'placeholder' => 'test',
    165                     'default' => ''
    166                 ],
    167                 'testmode' => [
    168                     'title' => __('Test Mode', 'paygine-payment'),
    169                     'type' => 'select',
    170                     'options' => [
    171                         '1' => __('Test mode - real payments will not be processed', 'paygine-payment'),
    172                         '0' => __('Production mode - payments will be processed', 'paygine-payment')
    173                     ],
    174                     'description' => __('Select test or live mode', 'paygine-payment'),
    175                     'desc_tip' => true,
    176                     'default' => '1'
    177                 ],
    178                 'payment_method' => [
    179                     'title' => __('Payment method', 'paygine-payment'),
    180                     'type' => 'select',
    181                     'options' => [
    182                         'purchase' => __('Standard acquiring (one-stage payment)', 'paygine-payment'),
    183                         'authorize' => __('Standard acquiring (two-stage payment)', 'paygine-payment') . ' *',
    184                         'purchaseWithInstallment' => __('Plait (one-stage payment)', 'paygine-payment'),
    185                         'authorizeWithInstallment' => __('Plait (two-stage payment)', 'paygine-payment') . ' *',
    186                         'purchaseSBP' => __('Fast Payment System', 'paygine-payment'),
    187                         'loan' => __('Loan', 'paygine-payment')
    188                     ],
    189                     'description' => '* ' . __('Payment occurs after confirmation by the manager in the personal account', 'paygine-payment'),
    190                     'desc_tip' => true,
    191                     'default' => 'purchase'
    192                 ],
    193                 'tax' => [
    194                     'title' => __('TAX', 'paygine-payment'),
    195                     'type' => 'select',
    196                     'options' => [
    197                         1 => __('VAT rate 20%', 'paygine-payment'),
    198                         2 => __('VAT rate 10%', 'paygine-payment'),
    199                         3 => __('VAT rate calc. 20/120', 'paygine-payment'),
    200                         4 => __('VAT rate calc. 10/110', 'paygine-payment'),
    201                         5 => __('VAT rate 0%', 'paygine-payment'),
    202                         7 => __('VAT rate 5%', 'paygine-payment'),
    203                         8 => __('VAT rate 7%', 'paygine-payment'),
    204                         9 => __('VAT rate calc. 5/105', 'paygine-payment'),
    205                         10 => __('VAT rate calc. 7/107', 'paygine-payment'),
    206                         6 => __('Not subject to VAT', 'paygine-payment'),
    207                     ],
    208                     'default' => '6'
    209                 ],
    210                 'notify_url' => [
    211                     'title' => __('Notify URL', 'paygine-payment'),
    212                     'type' => 'text',
    213                     'description' => __('Report this URL to Method technical support to receive payment notifications', 'paygine-payment'),
    214                     'desc_tip' => true,
    215                     'custom_attributes' => [
    216                         'readonly' => 'readonly',
    217                     ],
    218                     'default' => $this->notify_url
    219                 ],
    220                 'hash_algo' => [
    221                     'title' => __('Data encryption algorithm', 'paygine-payment'),
    222                     'type' => 'select',
    223                     'options' => [
    224                         'md5' => 'MD5',
    225                         'sha256' => 'SHA256',
    226                     ],
    227                     'description' => __('Must match your sector encryption settings in your personal account', 'paygine-payment'),
    228                     'desc_tip' => true,
    229                     'default' => 'md5'
    230                 ],
    231                 'custom_statuses_header' => [
    232                     'title' => __('Custom statuses for orders', 'paygine-payment'),
    233                     'type' => 'title'
    234                 ],
    235                 'registered_status' => [
    236                     'title' => __('Order registered', 'paygine-payment'),
    237                     'type' => 'select',
    238                     'options' => $wc_statuses,
    239                     'default' => 'wc-pending'
    240                 ],
    241                 'authorized_status' => [
    242                     'title' => __('Order authorized', 'paygine-payment'),
    243                     'type' => 'select',
    244                     'options' => $wc_statuses,
    245                     'default' => 'wc-on-hold'
    246                 ],
    247                 'loan_status' => [
    248                     'title' => __('Loan agreement approved but not signed', 'paygine-payment'),
    249                     'type' => 'select',
    250                     'options' => $wc_statuses,
    251                     'default' => 'wc-processing'
    252                 ],
    253                 'completed_status' => [
    254                     'title' => __('Order successfully paid', 'paygine-payment'),
    255                     'type' => 'select',
    256                     'options' => $wc_statuses,
    257                     'default' => 'wc-completed'
    258                 ],
    259                 'canceled_status' => [
    260                     'title' => __('Order canceled', 'paygine-payment'),
    261                     'type' => 'select',
    262                     'options' => $wc_statuses,
    263                     'default' => 'wc-refunded'
    264                 ],
    265                 'notify_customer_enabled' => [
    266                     'title' => __('Issuing an invoice for payment', 'paygine-payment'),
    267                     'type' => 'checkbox',
    268                     'label' => __('Enable issuing an invoice for payment by email of the payer', 'paygine-payment'),
    269                     'default' => 'no'
    270                 ],
    271                 'agreement_status' => [
    272                     'title' => __('Order agreement', 'paygine-payment'),
    273                     'type' => 'select',
    274                     'options' => $wc_statuses,
    275                     'default' => 'wc-on-hold'
    276                 ],
    277                 'payment_expected_status' => [
    278                     'title' => __('Payment expected', 'paygine-payment'),
    279                     'type' => 'select',
    280                     'options' => $wc_statuses,
    281                     'default' => 'wc-pending'
    282                 ]
    283             ];
    284 
    285             wp_enqueue_script('admin-paygine-script', plugins_url('assets/js/admin.js', __FILE__));
    286         }
    287 
    288         protected function getClient() {
    289             try {
    290                 if (isset($this->sector) && isset($this->password))
    291                     $this->client = new Client((int)$this->sector, $this->password, (bool)$this->testmode, (bool)$this->hash_algo);
    292             } catch (Exception $e) {
    293                 return new WP_Error('error', $e->getMessage());
    294             }
    295 
    296             return $this->client;
    297         }
    298 
    299         public function payment_fields() {
    300             if($this->payment_method === 'purchaseWithInstallment' || $this->payment_method === 'authorizeWithInstallment')
    301                 echo '<iframe style="width:100%;height:160px;border:none;min-width: 440px;" src="' . plugins_url('svkb_widget.php', __FILE__) . '?amount=' . print_r((isset(WC()->cart->cart_contents_total)) ? WC()->cart->cart_contents_total : '', true) . '"></iframe>';
    302         }
    303 
    304         public function admin_options() {?>
    305             <h3><?php echo esc_html(__('Method', 'paygine-payment'));?></h3>
    306             <p><?php echo sprintf(__('Payments with bank cards via the <a href="%s" target="_blank">Method</a> payment system.', 'paygine-payment'), self::PLUGIN_URL);?></p>
    307             <table class="form-table">
    308                 <?php $this->generate_settings_html();?>
    309             </table>
    310         <?php }
    311 
    312         public function process_admin_options() {
    313             if(!empty($_POST[$this->remove_logo_field]) && !empty($this->logo)){
    314                 if(wp_delete_attachment($this->logo))
    315                     $this->update_option('logo');
    316             } else if (!empty($_FILES[$this->logo_field]['name'])) {
    317                 require_once(ABSPATH.'wp-admin/includes/image.php');
    318                 require_once(ABSPATH.'wp-admin/includes/file.php');
    319                 require_once(ABSPATH.'wp-admin/includes/media.php');
    320 
    321                 if(!file_is_valid_image($_FILES[$this->logo_field]['tmp_name']))
    322                     return false;
    323 
    324                 $attachment_id = media_handle_upload($this->logo_field, 0);
    325                 if (is_wp_error($attachment_id))
    326                     return $attachment_id->get_error_message();
    327 
    328                 if(!empty($this->logo))
    329                     wp_delete_attachment($this->logo);
    330 
    331                 $this->update_option('logo', $attachment_id);
    332             }
    333 
    334             $this->init_settings();
    335 
    336             $post_data = $this->get_post_data();
    337 
    338             foreach ($this->get_form_fields() as $key => $field) {
    339                 if(in_array($this->get_field_type($field), ['title', 'image'])) continue;
    340 
    341                 try {
    342                     $this->settings[$key] = $this->get_field_value($key, $field, $post_data);
    343                 } catch (Exception $e) {
    344                     $this->add_error($e->getMessage());
    345                 }
    346             }
    347 
    348             return update_option($this->get_option_key(), apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings));
    349         }
    350 
    351         public function generate_image_html($key, $data) {
    352             $field_key = $this->get_field_key($key);
    353             $defaults = [
    354                 'title' => '',
    355                 'disabled' => false,
    356                 'class' => '',
    357                 'css' => '',
    358                 'placeholder' => '',
    359                 'type' => 'file',
    360                 'desc_tip' => false,
    361                 'description' => '',
    362                 'custom_attributes' => [],
    363             ];
    364 
    365             $data = wp_parse_args($data, $defaults);
    366 
    367             ob_start();?>
    368             <tr valign="top">
    369                 <th scope="row" class="titledesc">
    370                     <label for="<?php echo esc_attr($field_key);?>"><?php echo wp_kses_post($data['title']);?> <?php echo $this->get_tooltip_html($data);?></label>
    371                 </th>
    372                 <td class="forminp">
    373                     <fieldset>
    374                         <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']);?></span></legend>
    375                         <input class="input-text regular-input" type="file" name="<?php echo esc_attr($field_key);?>" id="<?php echo esc_attr($field_key);?>" accept="image/*">
    376                     </fieldset>
    377                     <?php if(!empty($this->logo)){
    378                         $logo_meta = wp_get_attachment_metadata($this->logo);?>
    379                         <img src="<?php echo wp_get_attachment_url($this->logo)?>" alt="" width="100" style="margin: 10px">
    380                         <div>(<?php echo esc_html($logo_meta['width'] . 'x' . $logo_meta['height']);?>)</div>
    381                         <br>
    382                         <label for="<?php echo esc_attr($this->remove_logo_field);?>">
    383                             <input class="" type="checkbox" name="<?php echo esc_attr($this->remove_logo_field);?>" id="<?php echo esc_attr($this->remove_logo_field);?>" style="" value="1"> <?php echo wp_kses_post( __('Remove logo', 'paygine-payment'));?>
    384                         </label>
    385                     <?php } else { ?>
    386                         <img src="<?php echo plugins_url('assets/img/method.svg', plugin_basename(__FILE__));?>" alt="" width="100" style="margin: 10px">
    387                     <?php } ?>
    388                 </td>
    389             </tr>
    390             <?php return ob_get_clean();
    391         }
    392 
    393         function wc_order_item_add_complete_button($order) {
    394             if($order->get_payment_method() === $this->id && $order->get_meta('paygine_order_state', true) === 'AUTHORIZED') {
    395                 wp_register_script('scripts-js', plugins_url('assets/js/scripts.js', plugin_basename(__FILE__)) , '', '', true);
    396                 wp_enqueue_script('scripts-js');
    397             ?>
    398                 <input type="hidden" id="nonce_paygine_complete" value="<?php echo wp_create_nonce('paygine_complete_action' . $order->get_id());?>">
    399                 <button type="button" id="button_paygine_complete" class="button custom-items"><?php _e('Complete payment', 'paygine-payment');?></button>
    400             <?php }
    401         }
    402 
    403         public function process_payment($order_id) {
    404             $wc_order = wc_get_order($order_id);
    405 
    406             if($this->notify_customer_enabled && $wc_order->get_status() !== $this->agreement_status && !get_post_meta($order_id, 'paygine_order_moderated', true)) {
    407                 $wc_order->add_order_note(__('Order created successfully', 'paygine-payment'));
    408                 $wc_order->update_status($this->agreement_status);
    409 
    410                 update_post_meta($wc_order->get_id(), 'paygine_order_moderated', 'yes');
    411 
    412                 WC()->cart->empty_cart();
    413 
    414                 return [
    415                     'result' => 'success',
    416                     'redirect' => $this->get_return_url($wc_order),
    417                 ];
    418             }
    419 
    420             $register_order_id = get_post_meta($order_id, 'paygine_order_id', true);
    421 
    422             if($register_order_id) {
    423                 $payment_url = $this->process_payment_payform((int)$register_order_id, $order_id);
    424             } else {
    425                 $pay_order_id = $this->process_payment_registration($wc_order);
    426                 $payment_url = $this->process_payment_payform($pay_order_id, $order_id);
    427             }
    428 
    429             return [
    430                 'result' => 'success',
    431                 'redirect' => $payment_url
    432             ];
    433         }
    434 
    435         public function wc_paygine_order_payment_change($order_id, $status_from, $status_to, $order) {
    436             if($this->notify_customer_enabled && $order->payment_method === 'paygine' && ($status_from === 'on-hold' && $status_to === 'pending'))
    437                 $this->process_payment_registration($order);
    438         }
    439 
    440         public function process_payment_registration($order): int {
    441             $this->calc_fiscal_position_shop_cart($order, $this->client->centifyAmount($order->get_total()));
    442 
    443             $register_data = [
    444                 'reference' => $order->get_id(),
    445                 'amount' => $this->client->centifyAmount($order->get_total()),
    446                 'currency' => $this->currency,
    447                 'email' => $order->get_billing_email(),
    448                 'description' => sprintf(__('Order #%s', 'paygine-payment'), ltrim($order->get_order_number(), '#')),
    449                 'url' => $this->notify_url,
    450                 'mode' => 0,
    451                 'fiscal_positions' => $this->fiscal_positions
    452             ];
    453 
    454             if ($this->notify_customer_enabled)
    455                 $register_data['notify_customer'] = 1;
    456 
    457             try {
    458                 $response = $this->client->register($register_data);
    459                 if($response instanceof Error)
    460                     throw new Exception($response->description->getValue());
    461 
    462                 $b2p_order_id = (int)$response->id;
    463                 if (!$b2p_order_id)
    464                     throw new Exception(__('Failed to get Method order ID', 'paygine-payment'));
    465             } catch (Exception $e) {
    466                 wc_add_notice($e->getMessage(), 'error');
    467             }
    468 
    469             $order->update_status($this->{strtolower($response->getState()) . '_status'});
    470             update_post_meta($order->get_id(), 'paygine_order_id', $b2p_order_id);
    471             update_post_meta($order->get_id(), 'paygine_order_state', $response->getState());
    472             $order->add_order_note(__('Order registered successfully', 'paygine-payment') . " (ID: $b2p_order_id)");
    473 
    474             return $b2p_order_id;
    475         }
    476 
    477         public function process_payment_payform($pay_order_id, $reference): string {
    478             $operation_params = ['id' => $pay_order_id];
    479             if (str_contains($this->payment_method, 'WithInstallment') && $this->shop_cart)
    480                 $operation_params['shop_cart'] = base64_encode(wp_json_encode($this->shop_cart));
    481             if (str_contains($this->payment_method, 'loan'))
    482                 $operation_params['reference'] = $reference;
    483 
    484             $url = call_user_func([$this->client, $this->payment_method], $operation_params);
    485 
    486             $parsed_url = parse_url($url);
    487             $this->api_operation = $parsed_url['path'];
    488             parse_str($parsed_url['query'], $this->query_params);
    489 
    490             return $url;
    491         }
    492 
    493         public function callback_notify() {
    494             $wc_order = [];
    495             $checkout_url = apply_filters('woocommerce_get_checkout_url', wc_get_checkout_url());
    496 
    497             try {
    498                 if ($isNotifyRequest = $this->isNotifyRequest()) {
    499                     $input = file_get_contents("php://input");
    500                     $ct_order_id = $this->client->handleResponse($input)->order_id->getValue();
    501                 } else {
    502                     if(isset($_REQUEST['error'])) {
    503                         $message = __('Failed to pay for the order', 'paygine-payment') . ":\n" . sanitize_text_field($_REQUEST['error']);
    504 
    505                         wc_add_notice(nl2br($message), 'error');
    506                         wp_redirect($checkout_url);
    507                     }
    508 
    509                     $ct_order_id = (int) sanitize_text_field($_REQUEST['id']);
    510                     if(!$ct_order_id)
    511                         throw new Exception(__('Failed to get Method order ID', 'paygine-payment'));
    512 
    513                     $pc_ref_id = (int) sanitize_text_field($_REQUEST['reference']);
    514                     if(!$pc_ref_id)
    515                         throw new Exception(__('Undefined order ID', 'paygine-payment'));
    516 
    517                     $pc_order_id = (int)get_post_meta($pc_ref_id, 'paygine_order_id', true);
    518                     if($ct_order_id !== $pc_order_id)
    519                         throw new Exception(__('Request data is not valid', 'paygine-payment'));
    520                 }
    521 
    522                 $ct_order = $this->client->order(['id' => $ct_order_id]);
    523                 if($ct_order instanceof Error)
    524                     throw new Exception($ct_order->description->getValue());
    525 
    526                 $wc_order = wc_get_order((int)$ct_order->reference);
    527                 if(!$wc_order)
    528                     throw new Exception(__('Failed to get order information', 'paygine-payment'));
    529 
    530                 $wc_order->update_meta_data('paygine_order_state', $ct_order->getState());
    531 
    532                 $paid = false;
    533 
    534                 if($ct_order->getState() !== get_post_meta($wc_order->get_id(), 'paygine_order_state', true)) {
    535                     if($ct_order instanceof CreditOrder) {
    536                         $wc_order->update_status($ct_order->isPaid() ? $this->completed_status : $this->loan_status);
    537                         $wc_order->add_order_note($ct_order->isPaid() ? __('The loan agreement was successfully completed and signed', 'paygine-payment') : __('The loan agreement was successfully completed, but not signed', 'paygine-payment'));
    538 
    539                         $paid = true;
    540                     } else {
    541                         $wc_order->update_status($this->{strtolower($ct_order->getState()) . '_status'});
    542                         $wc_order->add_order_note(__('Payment successful', 'paygine-payment') . ' (' .strtolower($ct_order->getState()).')');
    543                         $paid = $ct_order->isPaid();
    544                     }
    545 
    546                     $wc_order->save();
    547                 }
    548 
    549                 if ($isNotifyRequest) {
    550                     echo 'ok';
    551                 } else {
    552                     if($paid) {
    553                         WC()->cart->empty_cart();
    554                         wp_redirect($this->get_return_url($wc_order));
    555                     } else {
    556                         wc_add_notice(nl2br(__('Failed to pay incorrect card', 'paygine-payment')), 'error');
    557                         wp_redirect($checkout_url);
    558                     }
    559                 }
    560             } catch (\throwable $e) {
    561                 if ($isNotifyRequest) {
    562                     echo 'ok';
    563                 } else {
    564                     $message = __('Failed to pay for the order', 'paygine-payment') . ":\n" . $e->getMessage();
    565 
    566                     if($wc_order)
    567                         $wc_order->add_order_note(nl2br($message));
    568 
    569                     wc_add_notice(nl2br($message), 'error');
    570                     wp_redirect($checkout_url);
    571                 }
    572             }
    573 
    574             exit;
     49        );
     50    }
     51   
     52    /**
     53     * Add the Paygine Payment gateway to the list of available gateways.
     54     *
     55     * @param array
     56     */
     57    public static function add_gateway( $gateways ) {
     58       
     59        $options = get_option( 'woocommerce_paygine_settings', array() );
     60       
     61        if ( isset( $options['hide_for_non_admin_users'] ) ) {
     62            $hide_for_non_admin_users = $options['hide_for_non_admin_users'];
     63        } else {
     64            $hide_for_non_admin_users = 'no';
    57565        }
    57666       
    577         public function skvb_widget() {
    578             include_once __DIR__ . '/svkb_widget.php';
    579             exit;
     67        if ( ( 'yes' === $hide_for_non_admin_users && current_user_can( 'manage_options' ) ) || 'no' === $hide_for_non_admin_users ) {
     68            $gateways[] = 'WC_Paygine_Gateway';
    58069        }
     70        return $gateways;
     71    }
     72   
     73    /**
     74     * Plugin includes.
     75     */
     76    public static function includes() {
    58177       
    582         public function process_complete() {
    583             try {
    584                 $order_id = (int) sanitize_text_field($_REQUEST['order_id']);
    585                 if(!$order_id)
    586                     throw new Exception(__('Undefined order ID', 'paygine-payment'));
    587 
    588                 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['paygine_nonce_value'])), 'paygine_complete_action' . $order_id))
    589                     throw new Exception(__('Operation failed. Please refresh the page', 'paygine-payment'));
    590 
    591                 $wc_order = wc_get_order($order_id);
    592                 if(!$wc_order)
    593                     throw new Exception(__('Failed to get order information', 'paygine-payment'));
    594 
    595                 $pc_order_id = get_post_meta($wc_order->get_id(), 'paygine_order_id', true);
    596                 if(!$pc_order_id)
    597                     throw new Exception(__('Failed to get Method order ID', 'paygine-payment'));
    598 
    599                 $ct_order = $this->client->order(['id' => $pc_order_id]);
    600                 if($ct_order instanceof Error)
    601                     throw new Exception($ct_order->description->getValue());
    602 
    603                 $complete_result = $ct_order->complete();
    604                 if(!$complete_result)
    605                     throw new Exception(__('Unable to debit funds', 'paygine-payment'));
    606 
    607                 if($ct_order instanceof CreditOrder) {
    608                     $success_message = __('The loan agreement was successfully completed and signed', 'paygine-payment');
    609                 } else {
    610                     $wc_order->update_status($this->completed_status);
    611                     $success_message = __('Funds for the order have been successfully debited', 'paygine-payment');
    612                 }
    613 
    614                 $wc_order->update_meta_data('paygine_order_state', 'COMPLETED');
    615                 $wc_order->add_order_note($success_message);
    616 
    617                 $wc_order->save();
    618 
    619                 echo wp_json_encode([
    620                     'success' => true,
    621                     'message' => nl2br($success_message)
    622                 ]);
    623             } catch (Exception $e) {
    624                 echo wp_json_encode([
    625                     'success' => false,
    626                     'message' => nl2br($e->getMessage())
    627                 ]);
    628 
    629                 return new WP_Error('error', $e->getMessage());
    630             }
    631 
    632             exit;
    633         }
    634 
    635         public function process_refund($order_id, $amount = null, $reason = '') {
    636             try {
    637                 $wc_order = wc_get_order($order_id);
    638                 if(!$wc_order)
    639                     throw new Exception(__('Failed to get order information', 'paygine-payment'));
    640 
    641                 if (!$_REQUEST['security'])
    642                     throw new Exception(__('Operation failed. Please refresh the page', 'paygine-payment'));
    643 
    644                 if($amount !== $wc_order->get_total())
    645                     return new WP_Error('error', __('Error, please enter the refund amount. Refunds must be made for the full amount of the order', 'paygine-payment'));
    646 
    647                 $pc_order_id = get_post_meta($wc_order->get_id(), 'paygine_order_id', true);
    648                 if(!$pc_order_id)
    649                     throw new Exception(__('Failed to get Method order ID', 'paygine-payment'));
    650 
    651                 $ct_order = $this->client->order(['id' => $pc_order_id]);
    652                 if($ct_order instanceof Error)
    653                     throw new Exception($ct_order->description->getValue());
    654 
    655                 $reverse_result = $ct_order->reverse();
    656                 if($reverse_result instanceof Error)
    657                     return new WP_Error('error', __('Unable to issue a refund on a credit order', 'paygine-payment'));
    658             } catch (Exception $e) {
    659                 echo wp_json_encode([
    660                     'success' => false,
    661                     'message' => nl2br($e->getMessage())
    662                 ]);
    663 
    664                 return new WP_Error('error', $e->getMessage());
    665             }
    666 
    667             $wc_order->update_meta_data('paygine_order_state', 'CANCELED');
    668             $comment = __('Payment canceled successfully', 'paygine-payment');
    669 
    670             if($reason){
    671                 $comment .= PHP_EOL . esc_html($reason);
    672             }
    673 
    674             $wc_order->add_order_note($comment);
    675 
    676             $wc_order->save();
    677 
    678             return wp_json_encode([
    679                 'success' => true,
    680                 'message' => nl2br($comment)
    681             ]);
    682         }
    683 
    684         public function isNotifyRequest(): bool {
    685             try {
    686                 $input = file_get_contents("php://input");
    687                 if ($input && $this->client->handleResponse($input)) return true;
    688                 return false;
    689             } catch (\throwable $e) {
    690                 return false;
    691             }
    692         }
    693 
    694         private function calc_fiscal_position_shop_cart($order, $order_amount) {
    695             $fiscal_positions = [];
    696             $fiscal_amount = 0;
    697             $shop_cart = [];
    698 
    699             $basket_items = $order->get_items();
    700             $shipping_amount = $order->get_shipping_total();
    701 
    702             foreach ($basket_items as $b_key => $basket_item) {
    703                 $basket_item_data = $basket_item->get_data();
    704 
    705                 $fiscal_positions[$b_key]['quantity'] = $basket_item_data['quantity'];
    706                 $fiscal_amount += $basket_item_data['quantity'] * ($fiscal_positions[$b_key]['amount'] = $this->client->centifyAmount($basket_item->get_product()->get_price()));
    707                 $fiscal_positions[$b_key]['tax'] = (int)$this->tax;
    708                 $fiscal_positions[$b_key]['name'] = str_ireplace([';', '|'], '', $basket_item_data['name']);
    709 
    710                 $shop_cart[] = [
    711                     'name' => $basket_item_data['name'],
    712                     'goodCost' => (int)$basket_item->get_product()->get_price(),
    713                     'quantityGoods' => $basket_item_data['quantity']
    714                 ];
    715             }
    716 
    717             if ($shipping_amount) {
    718                 $fiscal_positions[] = [
    719                     'quantity' => 1,
    720                     'amount' => $this->client->centifyAmount($shipping_amount),
    721                     'tax' => (int)$this->tax,
    722                     'name' => 'Доставка'
    723                 ];
    724                 $fiscal_amount += $this->client->centifyAmount($shipping_amount);
    725                 $shop_cart[] = [
    726                     'name' => 'Доставка',
    727                     'goodCost' => (int)$shipping_amount,
    728                     'quantityGoods' => 1
    729                 ];
    730             }
    731 
    732             if ($fiscal_diff = abs($fiscal_amount - $order_amount)) {
    733                 $fiscal_positions[] = [1, $fiscal_diff, (int)$this->tax, 'Скидка', 14];
    734                 $shop_cart = [];
    735             }
    736 
    737             $this->fiscal_positions = $fiscal_positions;
    738             $this->shop_cart = $shop_cart;
    739         }
    740 
    741         public function get_currency($wc_currency): int {
    742             if (isset(CurrencyCode::cases()[$wc_currency])) {
    743                 return CurrencyCode::cases()[$wc_currency];
    744             } else throw new Exception('wrong currency');
     78        // Make the WC_Paygine_Gateway class available.
     79        if ( class_exists( 'WC_Payment_Gateway' ) ) {
     80            require_once 'includes/class-wc-gateway-paygine.php';
    74581        }
    74682    }
     83   
     84    /**
     85     * Plugin url.
     86     *
     87     * @return string
     88     */
     89    public static function plugin_url() {
     90        return untrailingslashit( plugins_url( '/', __FILE__ ) );
     91    }
     92   
     93    /**
     94     * Plugin url.
     95     *
     96     * @return string
     97     */
     98    public static function plugin_abspath() {
     99        return trailingslashit( plugin_dir_path( __FILE__ ) );
     100    }
     101   
     102    /**
     103     * Registers WooCommerce Blocks integration.
     104     *
     105     */
     106    public static function woocommerce_gateway_paygine_woocommerce_block_support() {
     107        if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
     108            require_once 'includes/blocks/class-wc-paygine-payments-blocks.php';
     109            add_action(
     110                'woocommerce_blocks_payment_method_type_registration',
     111                function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
     112                    $payment_method_registry->register( new WC_Gateway_Paygine_Blocks_Support() );
     113                }
     114            );
     115        }
     116    }
     117}
    747118
    748     function add_paygine_gateway($methods) {
    749         $methods[] = 'woocommerce_paygine';
    750 
    751         return $methods;
    752     }
    753 
    754     add_filter('woocommerce_payment_gateways', 'add_paygine_gateway');
    755 
    756     add_filter('plugin_action_links_' . plugin_basename( __FILE__ ),
    757         function($links) {
    758             if (!class_exists('woocommerce'))
    759                 return $links;
    760 
    761             array_unshift($links, sprintf('<a href="%1$s">%2$s</a>', admin_url('admin.php?page=wc-settings&tab=checkout&section=paygine'), __('Settings', 'paygine-payment')));
    762 
    763             return $links;
    764         }
    765     );
    766 }
     119WC_Paygine_Payments::init();
  • paygine/trunk/readme.txt

    r3213773 r3270180  
    55Tested up to: 6.6.1
    66Requires PHP: 8.1
    7 Stable tag: 3.1.1
     7Stable tag: 3.2.14
    88WC requires at least: 6.0
    99WC tested up to: 9.1.2
Note: See TracChangeset for help on using the changeset viewer.