Plugin Directory

Changeset 2123358


Ignore:
Timestamp:
07/15/2019 04:50:18 PM (7 years ago)
Author:
ibrahimhasanov
Message:

'v2'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uvibapay/trunk/UviPay/app/classes/UviPay.php

    r2110707 r2123358  
    1 <?php 
     1<?php
    22
     3namespace Uviba;
    34
    4 use \Curl\Curl;
     5class UviPay
     6{
     7  private static $privateKey;
     8  private static $apiDefError;
     9 
     10  public static function setPrivateKey($privateKey)
     11  {
     12    //if (strlen($privateKey) < 20)
     13    //throw new Exception('Correct private key is required to initialize UviPay client');
     14   
     15    self::$privateKey = $privateKey;
     16    self::$apiDefError = (object) array
     17    (
     18      'status'=>false,
     19      'error'=>array(
     20          'message' => "Sorry, some error happend",
     21          'code'    => "server_response",
     22          'type'    => "request"
     23      ),
     24    );
     25    $json = new ResponseObject;
     26    $json->set(self::$apiDefError);
     27    self::$apiDefError =  $json;
     28     
     29    /*
     30  self::$apiDefError = (object) array
     31    (
     32      'message' => "Sorry, some error happend",
     33      'code'    => "server_response",
     34      'type'    => "request"
     35    );
     36    */
     37  }
    538
     39  public static function setApiKey($privateKey)
     40  {
     41    return self::setPrivateKey($privateKey);
     42  }
    643
    7 class UviPay{
     44 
     45  /**
     46  * Send request to uviba api server
     47  * @param path Url path to send request
     48  * @param data Request parameters
     49  */
     50  public static function request($path, $data = array())
     51  {
     52    try
     53    {
     54      $ch = curl_init();
     55      curl_setopt($ch, CURLOPT_URL,"https://api.uviba.com/pay/v1/{$path}");
     56      curl_setopt($ch, CURLOPT_USERPWD, self::$privateKey); 
     57      curl_setopt($ch, CURLOPT_POST, 1);
     58      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
     59      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
     60      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     61      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     62      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     63      $response = curl_exec($ch);
     64      curl_close ($ch);
     65      //var_dump($response);
     66      $data = json_decode($response);
     67      $json = new ResponseObject;
     68      $json->set($data);
    869
    9 private static $private_key='';
    10 private static $api_version='v2';
    11 private static $api_subversion='1';
     70      return isset($json->status) ? $json : self::$apiDefError;
     71    }
     72    catch (Exception $e)
     73    {
     74      return self::$apiDefError;
     75    }
     76  }
     77 
     78  /**
     79  * @deprecated deprecated since v1.0. Use UviPay::request as alternative method
     80  */
     81  public static function APIRequest($url, $data = array())
     82  {
     83    return self::request($url, $data);
     84  }
     85 
     86  /**
     87  * Charge customer's credit card
     88  * @param data Request parameters
     89  */
     90  public static function charge($data = array())
     91  {
     92    if(!isset($data['token'])){
     93 
     94      if(isset($_GET['UvibaToken'])){
     95        $data['token']=$_GET['UvibaToken'];
     96      }else if(isset($_POST['UvibaToken'])){
     97        $data['token']=$_POST['UvibaToken'];
     98      }else if(isset($_GET['token'])){
     99        $data['token']=$_GET['token'];
     100      }else if(isset($_POST['token'])){
     101        $data['token']=$_POST['token'];
     102      }else{
     103        //not defined we will send error message
     104      }
     105    }
     106    return self::request('/charges', $data);
     107  }
     108 
     109  /**
     110  * Refund pervious charge
     111  * @param data Request parameters
     112  */
     113  public static function refund($charge_id,$data = array())
     114  {
     115    if(is_array($charge_id)){
     116      //so it is params
     117      $data=array_merge($charge_id,$data);
     118    }else{
     119      $data['charge_id']=$charge_id;
     120    }
     121    return self::request('/refunds', $data);
     122  }
     123 
     124  /**
     125  * Get your current balance
     126  * @param data Request parameters
     127  */
     128  public static function get_balance($data = array())
     129  {
     130    return self::request('/balance', $data);
     131  }
     132 
     133  /**
     134  * Cancel running subscribtion
     135  * @param data Request parameters
     136  */
     137  public static function cancel_subscription($sub_id, $data = array())
     138  {
     139    return self::request("/subscriptions/{$sub_id}?action=delete", $data);
     140  }
     141 
     142  /**
     143  * Send payment to specified email & account
     144  * @param data Request parameters
     145  */
     146  public static function send_payment($amount,$data = array())
     147  {
     148    if(is_array($amount)){
     149      //so it is params
     150      $data=array_merge($amount,$data);
     151    }else{
     152      $data['amount']=$amount;
     153    }
     154    return self::request('/transfers?action=send_payment', $data);
     155  }
    12156
    13     public static function bring_affiliates($method='cookie'){
    14         if($method=='cookie'){
    15             if(isset($_GET['uviba_params'])){
    16                 setcookie('uviba_params', json_encode($_GET['uviba_params']), time()+86400,'/',false,false);
    17             }
    18         }
    19     }
     157  /**
     158  * create link and put money in it.
     159  * @param data Request parameters
     160  */
     161  public static function create_paylink($amount,$data = array())
     162  {
     163    if(is_array($amount)){
     164      //so it is params
     165      $data=array_merge($amount,$data);
     166    }else{
     167      $data['amount']=$amount;
     168    }
     169    return self::request('/transfers?action=create_paylink', $data);
     170  }
     171 
     172  /**
     173  * Reverse sent payment
     174  * @param data Request parameters
     175  */
     176  public static function reverse_payment($data = array())
     177  {
     178    return self::request('/transfers?action=take_payment_back', $data);
     179  }
    20180
    21     public static function setApiPrivateKey($u_private_key=''){
    22         return self::$private_key=$u_private_key;
    23     }
    24     public static function setApiKey($u_private_key){
    25         return self::setApiPrivateKey($u_private_key);
    26     }
    27     public static function checkErrors(){
    28         //general errors, for example private key
    29         if(trim(self::$private_key)==''){
    30             // UviPay_CodeError means some error happened in code
    31             // UviPay_ResultError means errors that our server returned
    32             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    33                 'message'=>'Please define private key with UviPay::setApiPrivateKey function.',
    34             ),"Please define private key with UviPay::setApiPrivateKey function.", 0);
    35             exit;
    36         }
    37     }
    38 
    39 
    40     public static function refund($payment_info=array()){
    41         self::checkErrors();
    42         if(empty($payment_info)){
    43             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    44                 'message'=>'Payment Info is not defined in code. Please define it in UviPay::charge function.',
    45             ),"Payment Info is not defined in code. Please define it in UviPay::charge function.", 0);
    46             exit;
    47            
    48         }else if(!isset($payment_info['amount'])){
    49             $mes = 'Please provide how much money needed';
    50             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    51                 'message'=>$mes,
    52             ),$mes, 1);
    53             exit;
    54         }
    55 
    56         $key_identifier = substr( self::$private_key, 0, 8 );
    57         if($key_identifier=='sk_test_'){
    58             $isLive=false;
    59         }else if($key_identifier=='sk_live_'){
    60             $isLive=true;
    61         }else{
    62             $isLive=false;
    63         }
    64 
    65         $ch = new Curl();
    66         $ch->post('https://api.uviba.com/pay/refund',array(
    67             'sign'=>hash('sha256', trim($payment_info['charge_id']).'::'.trim(self::$private_key)),
    68             'isLive'=>$isLive,
    69             'amount'=>$payment_info['amount'],
    70             'charge_id'=>$payment_info['charge_id'],
    71             'api_version'=>self::$api_version,
    72             'api_subversion'=>self::$api_subversion,
    73             ));
    74  //var_dump($ch->response);
    75         try{
    76  
    77             $json_data = json_decode($ch->response);
    78  
    79         }catch(Exception $e){
    80             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    81                         'message'=>'Sorry some errors happened.',
    82                     ),"Sorry some errors happened.", 0);
    83                     exit;
    84         }
    85 
    86 
    87 
    88 if(is_null($json_data)){
    89     throw new UviPay_Exception_Base("UviPay_ResultError",array(
    90                         'message'=>'Sorry some errors happened.',
    91                     ),"Sorry some errors happened.", 0);
    92                     exit;
     181  /**
     182  * Verify sent webhook request
     183  * @param req_id Request id that sent to the webserver on webhook request
     184  * @param data Request parameters
     185  */ 
     186  public static function verify_webhook($req_id, $data = array())
     187  {
     188    return self::request("/webhooks/?action=verify&request_id={$req_id}", is_array($data) ? $data : array('verify_for' => $data));   
     189  }
    93190}
    94 if(isset($json_data->error_data,$json_data->error)){
    95 
    96 
    97     if($json_data->error===true){
    98         if(!empty($json_data->error_data)){
    99             throw new UviPay_Exception_Base("UviPay_ResultError",array(
    100                             'message'=>'Sorry some errors happened.',
    101                             'error'=>$json_data->error_data,
    102                         ),"Sorry some errors happened.", 0);
    103                         exit;
    104         }
    105     }
    106    
    107    
    108 }
    109 
    110 
    111 return $json_data->success_data;
    112 
    113 
    114 
    115     }
    116 
    117 
    118     public static function charge($payment_info=array()){
    119         self::checkErrors();
    120 
    121         if(empty($payment_info)){
    122             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    123                 'message'=>'Payment Info is not defined in code. Please define it in UviPay::charge function.',
    124             ),"Payment Info is not defined in code. Please define it in UviPay::charge function.", 0);
    125             exit;
    126            
    127         }else if(!isset($payment_info['amount'])){
    128             $mes = 'Please provide how much money needed';
    129             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    130                 'message'=>$mes,
    131             ),$mes, 1);
    132             exit;
    133         }
    134         if(!isset($payment_info['token'])){
    135  
    136             if(isset($_GET['UvibaToken'])){
    137                 $payment_info['token']=$_GET['UvibaToken'];
    138             }else if(isset($_POST['UvibaToken'])){
    139                 $payment_info['token']=$_POST['UvibaToken'];
    140             }else{
    141                 //not defined
    142                     throw new UviPay_Exception_Base("UviPay_CodeError",array(
    143                         'message'=>'Please provide token attribute.',
    144                     ),"Please provide token attribute.", 0);
    145                     exit;
    146             }
    147         }
    148 
    149         if(!isset($payment_info['uviba_params'])){
    150  
    151             if(isset($_GET['uviba_params'])){
    152                 $payment_info['uviba_params']=$_GET['uviba_params'];
    153             }else if(isset($_POST['uviba_params'])){
    154                 $payment_info['uviba_params']=$_POST['uviba_params'];
    155             }else{
    156                 //not defined
    157                 $payment_info['uviba_params']=array();
    158             }
    159             if(!empty($payment_info['uviba_params'])){
    160                 //if we gonna sent it, we will delete uviba_params things
    161                 setcookie('uviba_params', '', time()-1000,'/',false,false);
    162             }
    163         }
    164          $ch = new Curl();
    165 
    166         $key_identifier = substr( self::$private_key, 0, 8 );
    167         if($key_identifier=='sk_test_'){
    168             $isLive=false;
    169         }else if($key_identifier=='sk_live_'){
    170             $isLive=true;
    171         }else{
    172             $isLive=false;
    173         }
    174         $ch->post('https://api.uviba.com/pay/charge',array(
    175             'sign'=>hash('sha256', trim($payment_info['token']).'::'.trim(self::$private_key)),
    176             'isLive'=>$isLive,
    177             'amount'=>$payment_info['amount'],
    178             'UvibaToken'=>$payment_info['token'],
    179             'uviba_params'=>$payment_info['uviba_params'],
    180             'api_version'=>self::$api_version,
    181             'api_subversion'=>self::$api_subversion,
    182             ));
    183  //var_dump($ch->response);
    184         try{
    185  
    186             $json_data = json_decode($ch->response);
    187  
    188         }catch(Exception $e){
    189             throw new UviPay_Exception_Base("UviPay_CodeError",array(
    190                         'message'=>'Sorry some errors happened.',
    191                     ),"Sorry some errors happened.", 0);
    192                     exit;
    193         }
    194 
    195 
    196 
    197 if(is_null($json_data)){
    198     throw new UviPay_Exception_Base("UviPay_ResultError",array(
    199                         'message'=>'Sorry some errors happened.',
    200                     ),"Sorry some errors happened.", 0);
    201                     exit;
    202 }
    203 if(isset($json_data->error_data,$json_data->error)){
    204 
    205 
    206     if($json_data->error===true){
    207         if(!empty($json_data->error_data)){
    208             throw new UviPay_Exception_Base("UviPay_ResultError",array(
    209                             'message'=>'Sorry some errors happened.',
    210                             'error'=>$json_data->error_data,
    211                         ),"Sorry some errors happened.", 0);
    212                         exit;
    213         }
    214     }
    215    
    216    
    217 }
    218 
    219 
    220 return $json_data->success_data;
    221 
    222 
    223 //End of function
    224     }
    225 
    226    
    227 
    228 
    229 public static function update_lib($update_lib_info){
    230         foreach ($update_lib_info as $path => $code) {
    231             //if(file_exists(Uvi_UviPay_autoload_Page.$path)){
    232                 try{
    233                     //file_put_contents(Uvi_UviPay_autoload_Page.$path, $code);
    234                 }catch(Exception $e){}
    235             //}
    236         }
    237     }
    238 
    239 
    240     // End of Class
    241 }
    242  
    243 
    244 
    245 UviPay::bring_affiliates('cookie'); //should be called and included at every page
Note: See TracChangeset for help on using the changeset viewer.