Plugin Directory

Changeset 3844098


Ignore:
Timestamp:
12/04/2026 09:40:26 AM (3 hours ago)
Author:
tiptoppay
Message:

v.1.0.6

Location:
tiptoppay-gateway-for-woocommerce
Files:
27 added
3 edited

Legend:

Unmodified
Added
Removed
  • tiptoppay-gateway-for-woocommerce/trunk/inc/class-tiptoppay-api.php

    r3303671 r3844098  
    55  class TipTopPay_Api
    66  {
     7    private $enabledDMS;
     8    private $status_chancel;
     9    private $status_pay;
     10    private $api_pass;
     11    private $DMS_AU_status;
     12    private $public_id;
    713   
    814    public function __construct($enabledDMS, $statusChancel, $statusPay, $apiPass, $DMS_AU_status, $publicID)
    … …  
    1824    public function processRequest()
    1925    {
    20       $action = sanitize_text_field($_GET['action']);
     26      if (!$this->hasValidSignature()) {
     27        exit('{"error":"access denied"}');
     28      }
     29
     30      if (empty($_POST)) {
     31        exit('{"error":"empty request"}');
     32      }
     33
     34      $action = sanitize_text_field($_GET['action'] ?? '');
    2135      $request = [
    22         'Data' => sanitize_text_field($_POST['Data']),
    23         'TransactionId' => sanitize_text_field($_POST['TransactionId']),
    24         'AccountId' => sanitize_text_field($_POST['AccountId']),
    25         'CardExpDate' => sanitize_text_field($_POST['CardExpDate']),
    26         'CardLastFour' => sanitize_text_field($_POST['CardLastFour']),
    27         'CardType' => sanitize_text_field($_POST['CardType']),
    28         'Token' => sanitize_text_field($_POST['Token']),
    29         'InvoiceId' => sanitize_text_field($_POST['InvoiceId']),
    30         'Type' => sanitize_text_field($_POST['Type']),
    31         'Url' => sanitize_text_field($_POST['Url']),
    32         'Amount' => sanitize_text_field($_POST['Amount'])
     36        'Data' => sanitize_text_field($_POST['Data'] ?? ''),
     37        'TransactionId' => sanitize_text_field($_POST['TransactionId'] ?? ''),
     38        'AccountId' => sanitize_text_field($_POST['AccountId'] ?? ''),
     39        'CardExpDate' => sanitize_text_field($_POST['CardExpDate'] ?? ''),
     40        'CardLastFour' => sanitize_text_field($_POST['CardLastFour'] ?? ''),
     41        'CardType' => sanitize_text_field($_POST['CardType'] ?? ''),
     42        'Token' => sanitize_text_field($_POST['Token'] ?? ''),
     43        'InvoiceId' => sanitize_text_field($_POST['InvoiceId'] ?? ''),
     44        'Type' => sanitize_text_field($_POST['Type'] ?? ''),
     45        'Url' => sanitize_text_field($_POST['Url'] ?? ''),
     46        'Amount' => $_POST['Amount'] ?? ''
    3347      ];
    34      
    35       if (empty($request))
    36         exit('{"error":"empty request"}');
    37      
     48
    3849      switch ($action) {
    3950        case 'check':
    … …  
    6374      die();
    6475    }
     76
     77    private function hasValidSignature()
     78    {
     79      $signature = $_SERVER['HTTP_CONTENT_HMAC'] ?? '';
     80      if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' || !is_string($signature) || $signature === '' || !is_string($this->api_pass) || $this->api_pass === '') {
     81        return false;
     82      }
     83
     84      $body = file_get_contents('php://input');
     85      if ($body === false || $body === '') {
     86        return false;
     87      }
     88
     89      $expected = base64_encode(hash_hmac('sha256', $body, $this->api_pass, true));
     90
     91      return hash_equals($expected, $signature);
     92    }
    6593   
    6694    public function processCheckAction($request)
    6795    {
    68      
    69       if (!empty($request['Data'])) {
    70         $request_data = json_decode(stripslashes($request['Data']), true);
    71        
    72         if (isset($request_data['add_payment_method']) && $request_data['add_payment_method'] == 1) {
    73           echo json_encode(array('code' => 0));
    74          
    75           return;
    76         }
     96      if (($request['InvoiceId'] ?? '') === '' && $this->isCardBinding($request)) {
     97        echo json_encode(array('code' => 0));
     98        return;
    7799      }
    78100     
    … …  
    80102     
    81103      if (!$order) {
    82         die();
     104        echo json_encode(array('code' => 10));
     105        return;
    83106      }
    84107     
    … …  
    118141    private function processSuccessAction($request)
    119142    {
    120      
    121       $order = $this->getOrder($request);
    122       $DMS_TYPE = $this->enabledDMS;
    123      
    124       if ($DMS_TYPE == 'yes') {
    125         if ($order) {
     143      $isCardBinding = $request['InvoiceId'] === '' && !empty($request['AccountId']) && $this->isCardBinding($request);
     144
     145      if (!$isCardBinding) {
     146        $order = $this->getOrder($request);
     147        if (!$order) {
     148          echo json_encode(array('code' => 10));
     149          return;
     150        }
     151
     152        if (!$this->isCorrectSum($request, $order)) {
     153          echo json_encode(array('code' => 12));
     154          return;
     155        }
     156
     157        if ($this->enabledDMS == 'yes') {
    126158          $order->update_status($this->DMS_AU_status);
    127         }
    128       } else {
    129         $order->update_status($this->status_pay);
    130         $order->payment_complete();
    131         $order->add_order_note(sprintf('Payment approved (TransactionID: %s)', json_encode($request['TransactionId'])));
     159        } else {
     160          $order->update_status($this->status_pay);
     161          $order->payment_complete();
     162          $order->add_order_note(sprintf('Payment approved (TransactionID: %s)', json_encode($request['TransactionId'])));
     163        }
    132164      }
    133165     
    … …  
    174206        }
    175207       
    176         $request_data = json_decode(stripslashes($request['Data']), true);
    177        
    178         if (isset($request_data['add_payment_method']) && $request_data['add_payment_method'] == 1) {
    179           if ($request['Data']) {
    180             $auth = base64_encode($this->public_id . ":" . $this->api_pass);
    181             wp_remote_post('https://api.tiptoppay.kz/payments/void', array(
    182               'timeout' => 30,
    183               'redirection' => 5,
    184               'httpversion' => '1.0',
    185               'blocking' => true,
    186               'headers' => array('Authorization' => 'Basic ' . $auth, 'Content-Type' => 'application/json'),
    187               'body' => json_encode(array('TransactionId' => $request['TransactionId'])),
    188               'cookies' => array()
    189             ));
    190           }
    191         }
    192        
    193        
     208        if ($isCardBinding) {
     209          $auth = base64_encode($this->public_id . ":" . $this->api_pass);
     210          wp_remote_post('https://api.tiptoppay.kz/payments/void', array(
     211            'timeout' => 30,
     212            'redirection' => 5,
     213            'httpversion' => '1.0',
     214            'blocking' => true,
     215            'headers' => array('Authorization' => 'Basic ' . $auth, 'Content-Type' => 'application/json'),
     216            'body' => json_encode(array('TransactionId' => $request['TransactionId'])),
     217            'cookies' => array()
     218          ));
     219        }
     220
    194221      }
    195222     
    … …  
    212239    {
    213240      $order = self::getOrder($request);
    214       if ($order) {
    215         $order->update_status($this->status_pay);
    216       }
    217       $data['code'] = 0;
    218       echo json_encode($data);
     241      if (!$order) {
     242        echo json_encode(array('code' => 10));
     243        return;
     244      }
     245
     246      if (!$this->isCorrectSum($request, $order)) {
     247        echo json_encode(array('code' => 12));
     248        return;
     249      }
     250
     251      $order->update_status($this->status_pay);
     252      echo json_encode(array('code' => 0));
    219253    }
    220254   
    221255    public static function getOrder($request)
    222256    {
    223       global $woocommerce;
    224       $order = new WC_Order($request['InvoiceId']);
    225      
    226       return $order;
     257      $invoiceId = $request['InvoiceId'] ?? '';
     258      if ((!is_string($invoiceId) && !is_int($invoiceId)) || !ctype_digit((string) $invoiceId) || (int) $invoiceId <= 0) {
     259        return false;
     260      }
     261
     262      $order = wc_get_order($invoiceId);
     263
     264      return $order instanceof WC_Order ? $order : false;
    227265    }
    228266   
    … …  
    238276      $note = 'Ссылка на чек ' . $Type . ': ' . esc_url($url);
    239277      $order = self::getOrder($request);
     278      if (!$order) {
     279        echo json_encode(array('code' => 10));
     280        return;
     281      }
    240282      $var = $order->add_order_note($note, 1);
    241283      $order->save();
    … …  
    247289    private function isCorrectSum($request, $order)
    248290    {
    249       $sum = $request['Amount'];
    250       $paymentSum = $order->get_total();
    251      
    252       return round($paymentSum, 2) == round($sum, 2);
     291      $sum = $request['Amount'] ?? '';
     292      if (!is_string($sum) || !preg_match('/\A[0-9]+(?:\.[0-9]{1,2})?\z/', $sum)) {
     293        return false;
     294      }
     295
     296      return round((float) $order->get_total(), 2) === (float) $sum;
     297    }
     298
     299    private function isCardBinding($request)
     300    {
     301      $data = json_decode(stripslashes($request['Data'] ?? ''), true);
     302
     303      $marker = $data['Metadata']['add_payment_method'] ?? $data['add_payment_method'] ?? null;
     304
     305      return $marker === 1 || $marker === '1';
    253306    }
    254307   
  • tiptoppay-gateway-for-woocommerce/trunk/readme.txt

    r3464118 r3844098  
    55Requires at least: 4.9.7
    66Tested up to: 6.7.1
    7 Stable tag: 1.0.5
     7Stable tag: 1.0.6
    88Requires PHP: 7.4
    99License: GPLv2 or later
  • tiptoppay-gateway-for-woocommerce/trunk/tiptoppay_gateway_for_wc.php

    r3464118 r3844098  
    44   * Plugin URI: https://tiptoppay.kz/
    55   * Description: Extends WooCommerce with TipTopPay Gateway.
    6    * Version: 1.0.4
     6   * Version: 1.0.6
    77   * Author: TipTop Pay
    88   * Author URI: https://tiptoppay.kz/
Note: See TracChangeset for help on using the changeset viewer.