shojibflamon

Walletmix Payment Gateway

May 7th, 2016
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.64 KB | None | 0 0
  1. <?PHP
  2. /*
  3.     Author: Walletmix Ltd.
  4.     Version: 2.5.4.17
  5.     Title: Walletmix Payment Gateway Integration (Class)
  6.     Email: support@walletmix.com
  7.  
  8. */
  9. CLASS WALLETMIX {
  10.  
  11.     PRIVATE $service_access_username;
  12.     PRIVATE $service_access_password;
  13.     PRIVATE $merchant_id;
  14.     PRIVATE $access_app_key;
  15.     PRIVATE $shipping_charge;
  16.     PRIVATE $discount;
  17.     PRIVATE $merchant_order_id;
  18.     PRIVATE $app_name;
  19.     PRIVATE $callback_url;
  20.     PRIVATE $currency;
  21.     PRIVATE $transaction_related_params = array();
  22.     PRIVATE $extra_data;
  23.     PRIVATE $product_description;
  24.     PRIVATE $amount;
  25.     PRIVATE $server_details_url;
  26.     PRIVATE $check_payment_url;
  27.     PRIVATE $database_driver;
  28.  
  29.     PUBLIC FUNCTION walletmix($service_access_username, $service_access_password, $merchant_id, $access_app_key) {
  30.         $this->service_access_username = $service_access_username;
  31.         $this->service_access_password = $service_access_password;
  32.         $this->merchant_id = $merchant_id;
  33.         $this->access_app_key = $access_app_key;
  34.         $this->amount = 0;
  35.         $this->product_description = '';
  36.         $this->server_details_url = 'http://sandbox.walletmix.com/check-server';
  37.         $this->check_payment_url = 'http://sandbox.walletmix.com/check-payment';
  38.     }
  39.     PUBLIC FUNCTION set_shipping_charge($shipping_charge) {
  40.         $this->shipping_charge = $shipping_charge;
  41.     }
  42.     PUBLIC FUNCTION set_discount($discount) {
  43.         $this->discount = $discount;
  44.     }
  45.     PUBLIC FUNCTION set_product_description($products) {
  46.         $quantity = 0;
  47.         foreach($products as $product) {
  48.             $price = $product['price'];
  49.             $total_price = $product['quantity']*$price;
  50.             $this->product_description .= '{'.$product['quantity'] . 'x' . $product['name'] . '['.$price.']=['.$total_price.']}+';
  51.             $quantity += $product['quantity'];
  52.             $this->amount += $total_price;
  53.         }
  54.         $this->amount = $this->amount+$this->shipping_charge-$this->discount;
  55.         $this->product_description.='{shipping rate:'.$this->shipping_charge.'}-{discount amount:'.$this->discount.'}='.number_format( $this->amount, 2, '.', '' );
  56.  
  57.         return $this->product_description;
  58.     }
  59.     PUBLIC FUNCTION set_merchant_order_id($merchant_order_id) {
  60.         $this->merchant_order_id = $merchant_order_id;
  61.     }
  62.     PUBLIC FUNCTION set_app_name($app_name) {
  63.         $this->app_name = $app_name;
  64.     }
  65.     PUBLIC FUNCTION set_currency($currency) {
  66.         $this->currency = $currency;
  67.     }
  68.     PUBLIC FUNCTION set_callback_url($url) {
  69.         $this->callback_url = $url;
  70.     }
  71.     PUBLIC FUNCTION set_extra_json($extra_data) {
  72.         $this->extra_data = json_encode($extra_data);
  73.     }
  74.     PUBLIC FUNCTION get_auth() {
  75.         $encodeValue = base64_encode($this->service_access_username.':'.$this->service_access_password);
  76.         $auth = 'Basic '.$encodeValue;
  77.         return $auth;
  78.     }
  79.     PUBLIC FUNCTION get_options_value() {
  80.         $options  = base64_encode('s='.$_SERVER['HTTP_HOST'].',i='.$_SERVER['SERVER_ADDR']);
  81.         return $options;
  82.     }
  83.     PUBLIC FUNCTION get_cart_info() {
  84.         $cart_info = $this->merchant_id.','.$_SERVER['HTTP_HOST'].','.$this->app_name;
  85.         return $cart_info;
  86.     }
  87.     PUBLIC FUNCTION get_product_description() {
  88.         return $this->product_description;
  89.     }
  90.     PUBLIC FUNCTION get_extra_json() {
  91.         return $this->extra_data;
  92.     }
  93.     PUBLIC FUNCTION set_transaction_related_params($data) {
  94.         $this->transaction_related_params['wmx_id'] = $this->merchant_id;
  95.         $this->transaction_related_params['app_name'] = $this->app_name;
  96.         $this->transaction_related_params['access_app_key'] = $this->access_app_key;
  97.         $this->transaction_related_params['authorization'] = self::get_auth();
  98.         $this->transaction_related_params['options'] = self::get_options_value();
  99.         $this->transaction_related_params['callback_url'] = $this->callback_url;
  100.         $this->transaction_related_params['currency'] = $this->currency;
  101.         $this->transaction_related_params['merchant_order_id'] = $this->merchant_order_id;
  102.         $this->transaction_related_params['merchant_ref_id'] = uniqid();
  103.         $this->transaction_related_params['amount'] = $this->amount;
  104.         $this->transaction_related_params['cart_info'] = self::get_cart_info();
  105.         $this->transaction_related_params['product_desc'] = self::get_product_description();
  106.         $this->transaction_related_params['extra_json'] = self::get_extra_json();
  107.         foreach($data as $key=>$value){
  108.             $this->transaction_related_params[$key] = $value;
  109.         }
  110.         return $this->transaction_related_params;
  111.     }
  112.     PUBLIC FUNCTION send_data_to_walletmix() {
  113.         $getServerDetails = json_decode(self::curl_request_get($this->server_details_url));
  114.        
  115.         if($getServerDetails->selectedServer){
  116.             $wmx_response = self::curl_request_post($getServerDetails->url,$this->transaction_related_params);
  117.             $wmx_response_d = json_decode($wmx_response);
  118.  
  119.             if($wmx_response_d->statusCode === '1000'){
  120.  
  121.                 $data = array('wmx_id'=>$this->merchant_id, 'authorization'=>self::get_auth(),'access_app_key'=>$this->access_app_key,'token'=>$wmx_response_d->token,'amount'=>$this->amount);
  122.  
  123.                 if($this->database_driver == 'txt'){
  124.                     self::write_file($data);
  125.                 }elseif($this->database_driver == 'session'){
  126.                     self::reset_ression('wmx_token');
  127.                     self::reset_ression('amount');
  128.  
  129.                     self::put_session('wmx_token',$wmx_response_d->token);
  130.                     self::put_session('amount',$this->amount);
  131.                 }
  132.                 $wmx_url = $getServerDetails->bank_payment_url."/".$wmx_response_d->token;
  133.                 header("Location:".$wmx_url);
  134.                 exit;
  135.             }else{
  136.                 echo $wmx_response;
  137.             }
  138.         }else{
  139.             echo 'Server Two Temporary Down. Please Try After Sometime.';
  140.         }
  141.     }
  142.    
  143.     PUBLIC FUNCTION curl_request_get($url) {
  144.         $ch = curl_init();
  145.         curl_setopt($ch,CURLOPT_URL,$url);
  146.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  147.         curl_setopt($ch,CURLOPT_HEADER, false);
  148.         curl_setopt($ch,CURLOPT_CUSTOMREQUEST, 'GET');
  149.         $output = curl_exec($ch);
  150.         curl_close($ch);
  151.         return $output;
  152.     }
  153.    
  154.     PUBLIC FUNCTION curl_request_post($url,$params) {
  155.         $postData = http_build_query($params);
  156.         $ch = curl_init();
  157.         curl_setopt($ch,CURLOPT_URL,$url);
  158.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  159.         curl_setopt($ch,CURLOPT_HEADER, false);
  160.         curl_setopt($ch,CURLOPT_POST, count($postData));
  161.         curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);
  162.         $output = curl_exec($ch);
  163.         curl_close($ch);
  164.         return $output;
  165.     }
  166.     PUBLIC FUNCTION write_file($data) {
  167.         $file_name = fopen("data.txt", "w") or die("Unable to open file!");
  168.         $data = json_encode($data);
  169.         fwrite($file_name, $data);
  170.         fclose($file_name);
  171.     }
  172.     PUBLIC FUNCTION check_payment($params) {
  173.         $wmx_response = self::curl_request_post($this->check_payment_url,$params);
  174.         return $wmx_response;
  175.     }
  176.     PUBLIC FUNCTION read_file() {
  177.         $file_name = fopen("data.txt", "r") or die("Unable to open file!");
  178.         $data =  fread($file_name,filesize("data.txt"));
  179.         fclose($file_name);
  180.         return $data;
  181.     }
  182.     PUBLIC FUNCTION read_data() {
  183.         $data = array(
  184.             'wmx_id'        =>  $this->merchant_id,
  185.             'authorization' =>  self::get_auth(),
  186.             'access_app_key'=>  $this->access_app_key,
  187.             'token'         =>  self::get_session('wmx_token'),
  188.             'amount'        =>  self::get_session('amount')
  189.         );
  190.         return json_encode($data);
  191.     }
  192.  
  193.     PUBLIC FUNCTION put_session($key,$value){
  194.         $_SESSION[$key] = $value;
  195.     }
  196.     PUBLIC FUNCTION set_database_driver($option){
  197.         if($option == 'session'){
  198.             self::start_session();
  199.         }
  200.         $this->database_driver = $option;
  201.     }
  202.     PUBLIC FUNCTION get_database_driver(){
  203.         return $this->database_driver;
  204.     }
  205.     PUBLIC FUNCTION get_session($key){
  206.         return $_SESSION[$key];
  207.     }
  208.     PUBLIC FUNCTION start_session(){
  209.         if(session_status()!=PHP_SESSION_ACTIVE) session_start();
  210.     }
  211.     PUBLIC FUNCTION reset_ression($key){
  212.         if(isset($_SESSION[$key])){
  213.             unset($_SESSION[$key]);
  214.         }
  215.     }
  216.     PUBLIC FUNCTION debug($data,$die = false) {
  217.         echo '<pre>';
  218.         print_r($data);
  219.         echo '</pre>';
  220.         if($die){die();}
  221.     }
  222.  
  223.  
  224. }
  225. ?>
Add Comment
Please, Sign In to add comment