Showing posts with label Core Php. Show all posts
Showing posts with label Core Php. Show all posts

Monday 20 January 2020

Encrypt decrypt aes ecb openssl

This class provide a way of Encryption & decrypt GST PHP code helps illustrate how to encryption & decript, to protect sensitive data. Using openssl_encrypt and openssl_decrypt cryptographic functions.

you don't need to intantiate the class object because its funciton are static. you can directly access the methods.certificate_file_path is your cert file path it can be sandbox or production cert files ,you can either generate it or get it from providers.

You can also use below class to generate token for any web application , $filepath will be the path of security certificate (.cer) on your local machine or your server path where you kept the .cer file. If you don't have a .cer file you can just google for it to "How To Generate .Cer File". AES known as Advanced Encryption Standard is a symmetric-key encryption algorithm.

class GSTAPIENC {
    static function test(){
        return 1;
    }
    static function generateappKey ($appkey,$filepath){
        openssl_public_encrypt($appkey, $encrypted, file_get_contents($filepath));
        return base64_encode($encrypted);   
    }
    static function encryptOTP($otp_code,$appkey) {
       return base64_encode(openssl_encrypt($otp_code, "AES-256-ECB", $appkey, OPENSSL_RAW_DATA));
    }
    static function encryptData($data, $key) {
        return base64_encode(openssl_encrypt($data, "AES-256-ECB", $key, OPENSSL_RAW_DATA));
    }
    static function mac256($ent, $key) {
        $res = hash_hmac('sha256', $ent, $key, true); 
        return $res;
    }
    static function decryptData($data, $key) {
        return openssl_decrypt(base64_decode($data), "AES-256-ECB", $key, OPENSSL_RAW_DATA);
    }
    static function decodeJsonResponse($out, $rek, $ek) {
        $apiEK = GSTAPIENC::decryptData($rek, $ek);
        return base64_decode(GSTAPIENC::decryptData($out, $apiEK));
    }
    static function keygen($length = 10) {
        $key = '';
        list($usec, $sec) = explode(' ', microtime());
        mt_srand((float) $sec + ((float) $usec * 100000));
        $inputs = array_merge(range('z', 'a'), range(0, 9), range('A', 'Z'));
        for ($i = 0; $i < $length; $i++) {
            $key .= $inputs{mt_rand(0, 61)};
        }
        return base64_encode($key);
    } 
}

You can use it like below to generate API KEY

$certificate_file_path = "PATH OF YOUR SANDBOX OR PRODUCTION cert file.";
$generated_key = GSTAPIENC::keygen(32);
$appKey = GSTAPIENC::generateappKey(base64_decode($generated_key), $certificate_file_path);

You can also view download source

https://github.com/boy108zon/Encryption-Decription-GST

Wednesday 8 January 2020

Popover jquery content with ajax php

Let say if you have a user listing Or any kind of dynamic listing which is displaying information from database like mysql etc.. , so some time we want information of each users or product to display over a jQuery Popover with tab based. When popover will open it shows tab based information so that more information can be displayed over it.Below is the html link where user can click to get information.

Basically i had a loop of listing where i have to show up jquery popover with tab based so, when user click on button , viewSearchPayer function call and has one argument which can be userid or any thing. You can also take any unique ids or your data array element which is comming unique in Array. popoverId is important so that we can easily destroy or close the popover.

Response from your server side script like PHP etc will be html of tab ul li with user information or as per your requirements you can set accordingly.

<a class='btn btn-xs btn-warning' onclick='viewSearchPayer('EENPS9');'>gtInfo</a>

Below is the javascript function which is called when user click on button which have onclick event.

function viewSearchPayer(id) {
var popoverId = 'clientinfo_' + id;
var elem='Information 
<button type="button" onclick="closePop('+id+')" id="close" class="close">×<button>';
    $.ajax({
        type: 'POST',
        url: 'your_url_here',
        data: {"method": "viewSearchPayer", "id": id},
        dataType: 'json',
        async: true,
        success: function (response) {
            var Result;
            if (response.RESULT == 'SUCCESS') {
                Result = response.data;
            } else {
                Result = response.data;
            }
            $("."+popoverId).popover({
                container: 'body',
                placement: 'top',
                html: true,
                title: elem,
                content: Result
            }).popover('show');
        }
    });
}
Below is the javascript function which is called when user click on close button over popover so that we can close it. popoverId should be unique or you can adjust it according to your need.
function closePop(popoverId) {
     var closepopoverId = 'clientinfo_' + popoverId;
     $("."+closepopoverId).popover('hide');
  }

Below is the simple tab based html markup which you can revert back from your server side language like PHP , In this tab based you can also get dynamic data and display accordingly from database to these tabs.

 <ul class="nav nav-tabs">
<li class="active"><a href="#tab1" data-toggle="tab">Info</a></li>
<li class="tab"><a href="#tab2" data-toggle="tab">Track</a></li>
</ul><div class="tab-content">
<div class="tab-pane active" id="tab1"> 
tab1</div>
<div class="tab-pane" id="tab2">
tab2</div></div>

Friday 7 July 2017

php mysql mysqli quries class

Below class file handle mysql and mysqli connection & quries. you can select all records as well as can place where clause also saving records , removing records , executing direct query with Mysql OR MySqli as defined in config.php file.
1. Your config.php file will be as below , you can change it and placed values accordingly as your mysql database. resource_type Mysql OR MySqli
$dbconfig['db_server'] = 'localhost';
$dbconfig['db_port'] = ':3306';
$dbconfig['db_username'] = 'root';
$dbconfig['db_password'] = '';
$dbconfig['db_name'] = 'test';
$resource_type='MySqli';
2. Below is MysqlDatabaseMysqli.php class which has functions , you can execute Queries with MySql or MySqli.
require_once 'config.php';
Class MysqlDatabaseMysqli {
    protected $connection = NULL;
    protected $connection_string = NULL;
    protected $associate_type = NULL;
    function __construct() {
        global $resource_type;
        $this->resource_type_string = strtolower($resource_type);
        switch ($resource_type) {
            case 'MySql':
                $this->associate_type = MYSQL_ASSOC;
                $this->msl_connect();
                break;
            case 'MySqli':
                $this->associate_type = MYSQLI_ASSOC;
                $this->msli_connect();
                break;
            default:
                echo "No resource type define";
                exit;
                break;
        }
    }
    /*
     *  msl->mysql
     */
    function msl_connect() {
        global $dbconfig;
        try {
            if ($link_identifier = @mysql_connect($dbconfig['db_server'], 
                $dbconfig['db_username'], $dbconfig['db_password'])) {
                $select_db = @mysql_select_db($dbconfig['db_name'], $link_identifier);
                if (!$select_db) {
                    throw new Exception(mysql_errno() . ' ' . mysql_error());
                }
                $this->connection = $link_identifier;
            } else {
                throw new Exception(mysql_errno() . ' ' . mysql_error());
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
    /*
     *  msli->mysqli
     */
    function msli_connect() {
        global $dbconfig;
        try {
            if ($link_identifier = @mysqli_connect($dbconfig['db_server']
                , $dbconfig['db_username'], $dbconfig['db_password'])) {
                $select_db = @mysqli_select_db($link_identifier, $dbconfig['db_name']);
                if (!$select_db) {
                    throw new Exception(mysqli_connect_errno() . ' Unable to select db');
                }
                $this->connection = $link_identifier;
            } else {
                throw new Exception(mysqli_connect_errno() . ' Unable to connect');
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
    /*
     *  Created By: Boy108zon
     *  @table_name: string
     *  @where: Array
     */
    function get_records($table_name, $where=NULL) {
        $connect = $this->resource_type_string . '_query';
        $num_rows = $this->resource_type_string . '_num_rows';
        $connect_close = $this->resource_type_string . '_close';
        /* if where included */
        $qwhere = '';
        if (is_array($where)) {
            $counter = 0;
            foreach ($where as $key => $value) {
                if ($counter > 0) {
                    $qwhere .= ' AND ';
                }
                $qwhere .= "$key = '$value'";
                $counter++;
            }
            $qwhere = 'WHERE ' . $qwhere;
        }
        $query = "SELECT * from $table_name $qwhere";
        if ($this->resource_type_string == 'mysqli') {
            $result_resource = $connect($this->connection, $query);
        } else {
            $result_resource = $connect($query, $this->connection);
        }
        $noOfrows = $num_rows($result_resource);
        if ($noOfrows > 0) {
            return $this->db_to_array($result_resource);
        } else {
            return NULL;
        }
    }
    /*
     *  Created By: Boy108zon
     *  @table_name: string
     *  @where: Array
     */
    function remove_records($table_name, $where=NULL) {
        $connect = $this->resource_type_string . '_query';
        $affected_rows = $this->resource_type_string . '_affected_rows';
        $connect_close = $this->resource_type_string . '_close';
        /* if where included */
        $qwhere = '';
        if (is_array($where)) {
            $counter = 0;
            foreach ($where as $key => $value) {
                if ($counter > 0) {
                    $qwhere .= ' AND ';
                }
                $qwhere .= "$key = '$value'";
                $counter++;
            }
            $qwhere = 'WHERE ' . $qwhere;
        }
        $query = "DELETE from $table_name $qwhere";
        if ($this->resource_type_string == 'mysqli') {
            $result_resource = $connect($this->connection, $query);
        } else {
            $result_resource = $connect($query, $this->connection);
        }
        $idds = $affected_rows($this->connection);
        //close connection
        @$connect_close($this->connection);
        if ($idds > 0) {
            return $idds;
        } else {
            return NULL;
        }
    }
    /*
     *  Created By: Boy108zon
     *  @result->mysql OR mysqli
     */
    function db_to_array($result) {
        $connection_string = $this->resource_type_string . '_fetch_array';
        $rows = array();
        while ($row = $connection_string($result, $this->associate_type)) {
            $rows[] = $row;
        }
        //close connection
        @$connect_close($this->connection);
        return $rows;
    }
    /*
     *  Created By: Boy108zon
     *  @table_name: String
     *  @post_array:Array
     *  @where:Array
     *  @mode:'save' OR edit
     *  Based on mode return type affected rows and latest insert id
     */
    function save_records($table_name, $post_array, $where=NULL, $mode='save') {
        $connect = $this->resource_type_string . '_query';
        $insert_id = $this->resource_type_string . '_insert_id';
        $affected_rows = $this->resource_type_string . '_affected_rows';
        $connect_close = $this->resource_type_string . '_close';
        $count = 0;
        $fields = '';
        foreach ($post_array as $col => $val) {
            if ($count > 0) {
                $fields .= ', ';
            }
            $fields .= "$col = '$val'";
            $count++;
        }
        $qwhere = '';
        if (is_array($where)) {
            $counter = 0;
            foreach ($where as $key => $value) {
                if ($counter > 0) {
                    $qwhere .= ' AND ';
                }
                $qwhere .= "$key = '$value'";
                $counter++;
            }
            $qwhere = 'WHERE ' . $qwhere;
        }
        if ($mode == 'edit') {
            $query = "UPDATE " . $table_name . " SET $fields $qwhere;";
        } else {
            $query = "INSERT INTO " . $table_name . " SET $fields;";
        }
        if ($this->resource_type_string == 'mysqli') {
            $result_resource = $connect($this->connection, $query);
        } else {
            $result_resource = $connect($query, $this->connection);
        }
        if ($mode == 'edit') {
            $idds = $affected_rows($this->connection);
        } else {
            $idds = $insert_id($this->connection);
        }
        //close connection
        @$connect_close($this->connection); 
        if ($idds > 0) {
            return $idds;
        } else {
            return NULL;
        }
    }
    /*
     *  Created By: Boy108zon
     *  @query: full query including inner , where
     *  if required we can also use it.
     */
    function get_direct_query_records($query) {
        $connect = $this->resource_type_string . '_query';
        $num_rows = $this->resource_type_string . '_num_rows';
        $connect_close = $this->resource_type_string . '_close';
        if ($this->resource_type_string == 'mysqli') {
            $result_resource = $connect($this->connection, $query);
        } else {
            $result_resource = $connect($query, $this->connection);
        }
        $noOfrows = $num_rows($result_resource);
        //close connection
        @$connect_close($this->connection); 
        if ($noOfrows > 0) {
            return $this->db_to_array($result_resource);
        } else {
            return NULL;
        }
    }
}
3. You can use it like below
require_once 'MysqlDatabaseMysqli.php'; 
$dbfun=new MysqlDatabaseMysqli(); 

$result=$dbfun->get_records('employee',array('name'=>'amit'));
//For Retrive records from employee table. 
$result=$dbfun->get_records('your_table_name');
//edit 
$result=$dbfun->save_records('your_table_name',$post_array,$where,$mode='Edit');
//save 
$result=$dbfun->save_records('your_table_name',$post_array);
//remove 
 $result=$dbfun->remove_records('your_table_name',array('id'=>6));
// direct query 
$result=$dbfun->get_direct_query_records($query='select * from employee');
4. You can download class from below
https://github.com/boy108zon/SingelMysqlMysqli

Thursday 14 July 2016

Skrill payment integration

1. For setting up both Merchant and Customer Test Account, please do the following:
Go to skrill.com and open two standard customer accounts without doing any transactions. Send the E-mail addresses of the accounts to integrationsupport@paysafe.com and inform them which one you`d like converted to Customer and which one to Merchant Test Accounts. Feel free to input any kind of data when opening up the as they will be converted. Be informed that these accounts will not be converted back to real ones and the funds added can only be used in test environment.
 
2. Your html markup will goes like below. This is just a sample or test html page. security and other things depends upon your code style.
<form action="https://pay.skrill.com?action=status_trn" method="post" name="payment_form" 
id="payment_form">
<input type="hidden" name="pay_to_email" value="demoqco@sun-fish.com">
<input type="hidden" name="transaction_id" value="">
<input type="hidden" name="return_url" value="">
<input type="hidden" name="cancel_url" value="">
<input type="hidden" name="status_url" value="">
<input type="hidden" name="language" value="EN">
<input type="hidden" name="pay_from_email" value="payer123@skrill.com">
<input type="hidden" name="amount" value="">
<input type="hidden" name="currency" value="">
<input type="hidden" name="firstname" value="">
<input type="hidden" name="lastname" value="">
<input type="hidden" name="postal_code" value="">
<input type="hidden" name="city" value="">
<input type="hidden" name="country" value="">
<input type="hidden" name="address" value="">
<input type="hidden" name="date_of_birth" value="">
<input type="hidden" name="confirmation_note" value="Thanks for depositing fund into your account">
<input type="submit" value="Pay!">
</form>
<script type='text/javascript'>
    window.onload = function () {
        document.payment_form.submit()
    }
</script>

Tuesday 10 November 2015

Neteller payment integration core Php , Curl.

You may also see the post here https://yourlearn.in/posts/view/6/neteller-payment-integration-core-php and know and read much more about it.


If you want to implement Neteller payment using Core / Simple Php below is code for you can get it. I have used below code for only testing purpose so i can check weather Neteller working OR not. Please never used below code on production if you want to use below code in production you have to modify it accordingly like.
 
You can create function for CURL request , passing merchant info in hidden field is not good way you can take that in your PHP SCRIPT.
 
To set up a Business Test Account, you need to provide the below information to merchantsupport@paysafe.com and they`ll set it up for you
Merchant descriptor name: Your descriptor name
Contact Person: Your Name
Phone number: Your Number
Country:      Your Country          
Contact E-mail: create an email on gmail , yahoo...etc and put here.
Currency Accounts: Your Currency USD..etc
 
You can find Member Test Accounts in the Integration Manual on page 153.
 
You can go to https://merchant.neteller.com/merchflux/public/index and grab your merchant info and replace in hidden field. You have to request there customer support for enabling API client key and secret key for testing purpose. During my code testing i found invalid client issue response from API so for issue i have to add IP address there. Another issue i have faced minimum amount , Amount should be passed as $_POST['amount']*100.
 
1. Your All Php , Html Code file or you can say your view file will goes like below.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>NETELLER</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>NETELLER</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" action=""><br>
Version: <input type="text" name="version" value="4.1"><br>
Amount : <input type="text" name="amount" value="1"><br>
Currency: <input type="text" name="currency" value="USD"><br>
net_account: <input type="text" name="net_account" value="12 digit number"><br>
secure_id: <input type="text" name="secure_id" value="326415"><br>
<div><hr></div>
MI: <input type="hidden" name="merchant_id" value="08762"><br>
MK: <input type="hidden" name="merch_key" value="client_id:client_key"><br>
MTID: <input type="hidden" name="merch_transid" value="<?php echo time(); ?>"><br>
MN: <input type="hidden" name="merch_name" value="Company Ltd"><br>
MA: <input type="hidden" name="merch_account" value="Comany Ltd"><br>
<input type="submit" name="button" value="Make transfer">
</form>
</body>
</html>
<?php
if (isset($_POST['button'])) {
$fields = array(
'version' => $_POST['version'],
'amount' => urlencode($_POST['amount']),
'currency' => $_POST['currency'],
'net_account' => urlencode($_POST['net_account']),
'secure_id' => urlencode($_POST['secure_id']),
'merchant_id' => urlencode($_POST['merchant_id']),
'merch_key' => urlencode($_POST['merch_key']),
'merch_transid' => urlencode($_POST['merch_transid']),
'button' => 'Make Transfer'
);

$merchantkey = $_POST['merch_key'];
$amount_value = $_POST['amount']*100;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://api.neteller.com/v1/oauth2/token?grant_type=client_credentials");
curl_setopt($ch, CURLOPT_USERPWD, $merchantkey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Cache-Control:no-cache"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$serverOutput = curl_exec($ch);
$serverOutput = json_decode($serverOutput);
if (isset($serverOutput->error) && $serverOutput->error != '') {
header("Location:your_page.php?status=" . $serverOutput->error->message . "&payment_type=neteller");
exit;
}
if (isset($serverOutput->accessToken) && $serverOutput->accessToken != "") {
$access_token = $serverOutput->accessToken;
$requestParams = array(
"paymentMethod" => array(
"type" => "neteller",
"value" => $_SESSION["email"],
),
"transaction" => array (
"merchantRefId" => (string) time(),
"amount" => $amount_value,
"currency" => "USD"
),
"verificationCode" => $_POST['secure_id']
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://api.neteller.com/v1/transferIn");
curl_setopt($curl, CURLOPT_HTTPHEADER, 
array("Content-Type:application/json", "Authorization: Bearer $access_token"));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($requestParams));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$serverOutput = curl_exec($curl);
$serverOutput = json_decode($serverOutput);
if (isset($serverOutput->error->code) && $serverOutput->error->message != '') {
header("Location:payment.php?status=" . urlencode($serverOutput->error->message) . "
&payment_type=neteller");
exit;
}
curl_close($ch);
//Success full return array;
echo "<pre>";
print_r($serverOutput);
}
}
?>