PHP encode/encrypt array to certain character length -
i making payment process via php possible, need pass unique id alphanumeric character string of maximum length of 19 characters.
<?php $amount = $amt; // amount of payment (ex- 20 20$) $uniqnum = $uniqueid; //a string of maximum 19 charatcers //now rest of payment happens ?>
and while payment gets processed, user returned website parameters, of which, parameter "uniqid" same sent(as mentioned above).
<?php //this receive $pfs_voucher_id = $_get["p96"]; //transaction id (identifies invoices etc) $order_number = $_get["p120"]; //sent me unique id //rest of logic ipn if($ipn_response == 'y') { echo "payment of uniqueid ".$order_number." has been succeeded"; //i got order number still couldn't tell whom order meant } ?>
since receive not information payment other amount , status of payment, compelled me use unique id identify other information regarding payment such user made payment , package had chosen.
one way of doing this(recognizing user) store necessary information in database when user decides make payment, generating own unique id of characters, example-
<?php //function generate unique "human-readable" string of length function randomly($length=6) { $conso=array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z"); $vocal=array("a","e","i","o","u"); $unique=""; do{ srand ((double)microtime()*1000000); $max = $length/2; for($i=1; $i<=$max; $i++) { $unique.=$conso[rand(0,19)]; $unique.=$vocal[rand(0,4)]; } }while(exists_in_database($unique) == true); return $unique; } //check if string found in database function exists_in_database($str) { //since using `payments` table save unique ids, must check if exists //@ using codeigniter $this->db->select('*')->from('payments')->where('uniqid',$str); $query = $this->db->get(); if($query->num_rows() > 0) return true; else return false; } //generate unique id of 6 characters $uniqid = randomly(); //get other info payments $userid = get_loggedin_user(); //get userid of loggedin user $packageid = $this->input->post('packageid'); //or $_post['packageid'] $packagename = $this->package->getpackage($packageid)->name; //fetch package name database //now save record in payments table payment status pending $data = array("uniqid"=>$uniqid,"userid"-> $userid, "pkg_id"=>$packageid,"pkg_name"=>$packagename,"pay_status"=>'pending'); $this->db->insert('payments',$data); ?>
and when user returns site after payment, check if uniqueid received exists on database or not. if so, make status received.
<?php //this receive $pfs_voucher_id = $_get["p96"]; //transaction id (identifies invoices etc) $order_number = $_get["p120"]; //sent me unique id //rest of logic ipn if($ipn_response == 'y') { $query = $this->db->where('uniqid',$order_number)->get('payments'); $payment = isset($query->userid)?$query:null; if(isset($query->userid)) { echo "payment of uniqueid ".$order_number." has been succeeded"; //i got order number , can tell user corresponds $data = array("uniqid"=>$order_number,"userid"-> $query->userid,"pay_status"=>'completed'); $this->db->update('payments',$data); //update payment status complete } } ?>
but problem of going way unlimited number of entries still exist if user not pay or cancels payment. cancel url not receive unique id , unable delete pending status of payment regarding him. so, better idea insert payment when "ok" response received payment gateway.
so, want know how encrypt/encode required details (userid , package id atleast) unique id of 19 characters or less, can validate information. glad know if can suggest better way of doing so.
<?php function encode_arr($data) { return base64_encode(serialize($data)); } function decode_arr($data) { return unserialize(base64_decode($data)); } $uniqid = encode_arr(array('userid'=>$userid,'pkgid'=>$packageid,'timestamp'=>now())); ?>
i tried making array of userid , package , serializing , base64 encoding make unique string out of it.but make more unique, thinking adding timestamp helpful transaction time. exceed 19 characters limit. should go hashing/encryption technique?
ps: using codeigniter if help.
Comments
Post a Comment