| px | top | add code | search | signup | login | help |
<?
##
# Net Solutions LLC - 9/21/99
#
# Flint Doungchak (flint@netsolutionsllc.com) & Patrick Shafer (pshafer@netsolutionsllc.com)
#
# This is our first go-around with a totally PHP cybercash processing script.
# This script was designed with an Win32 filestructure in mind so your might
# need to tweak it to get in to work with Linux, etc.
#
# Of course I invite improvements, and hope someone will post their UNIX
# solution for everyone to see. Please send comments and suggestions to
# flint@netsolutionsllc.com
#
# Warning: There isn't a lot of error checking yet so be carefull.
#
# I hope it serves a lot of people.
#
# You need cbsend.php3. It's the other half of this solution. This
# file only defines functions for cbsend.php3
##
//prevent multiple definitions of functions
if (!$cybercash_inc) {
$cybercash_inc = "1";
# All of your CyberCash definitions and file locations go here. You
# got mckencrypt.exe and mckdecrypt.exe from the MCK you downloaded
# from CC. Make sure your permissions are right.
$MCKversion = "3.2.0.4";
$CRYPTexec="c:\\PATH\\mck-shared\\mckencrypt.exe";
$CRYPTinfile="c:\\PATH\\msg-raw.txt";
$CRYPToutfile="c:\\PATH\\msg-encrypted.txt";
$DECRYPTexec="c:\\PATH\\mck-shared\\mckdecrypt.exe";
$DECRYPTinfile="c:\\PATH\\msg-encrypted.txt";
$DECRYPToutfile="c:\\PATH\\msg-decoded.txt";
# You merchant key and id go below.
$merchant_key = "-- Weird Long String goes here --";
$merchant_id = "-- ID goes here --";
//--------------------------------------------------------------------------------
//rpterror()
//
//Generate an error report
//
//expects:
// $msgstr - Message string to report
//
//returns:
// <null>
//--------------------------------------------------------------------------------
function rpterror($msgstr) {
# Insert the admin email here...
$to = "administrator@foo.com";
$subject = "CyberCash - PHP Error";
$from = "CYBERCASH-ERROR@foo.com";
$msg = "An error occured while trying to proccess a cybercash transaction\r\n";
$msg .= "The error occured in the Company Site\r\n";
$msg .= "The error message is:\r\n";
$msg .= "\t$msgstr\r\n";
mail($to,$subject,$msg,"From: $from");
header("Location: fatal-error.html");
die;
}
//--------------------------------------------------------------------------------
//gencpi()
//
//Generate CPI (Customer Payment Instrument) message
//
//expects:
// $ccnum - Credit card number
// $ccexp - Credit card experation
// $ccaddress - Credi card address (used for AVS verification)
// $cccity - Credit card city
// $ccstate - Credit card state
// $cczip - Credit card zip
// $cccountry - Credit card country
//
//returns:
// normal: URL encoded CPI message
// error: 0
//--------------------------------------------------------------------------------
function gencpi($ccnum,$ccexp,$ccname,$ccaddress,$cccity,$ccstate,$cczip,$cccountry) {
// do a sanity check
if (!$ccnum) {
rpterror("Error in: gencpi(): !ccnum");
return 0;
}
if (!$ccexp) return 0;
if (!$ccname) return 0;
if (!$ccaddress) {
rpterror("Error in: gencpi(): !ccaddress");
return 0;
}
if (!$cccity) return 0;
if (!$ccstate) return 0;
if (!$cczip) return 0;
if (!$cccountry) return 0;
// prepare string
$ret = "cpi.card-number=$ccnum";
$ret .= "&cpi.card-exp=$ccexp";
$ret .= "&cpi.card-name=$ccname";
$ret .= "&cpi.card-address=$ccaddress";
$ret .= "&cpi.card-city=$cccity";
$ret .= "&cpi.card-state=$ccstate";
$ret .= "&cpi.card-zip=$cczip";
$ret .= "&cpi.card-country=$cccountry";
// url encode it
$ret = rawurlencode($ret);
return($ret);
}
//--------------------------------------------------------------------------------
//genmo()
//
//Generate MO (Cash register data) message
//
//expects:
// $gen_oid - Order ID
// $gen_price - Purchase price
// $gen_desc - Description of the item
//
//returns:
// normal: a URL-encoded message
// error: 0
//--------------------------------------------------------------------------------
function genmo($gen_oid,$gen_price,$gen_desc){
global $merchant_id;
if (!$gen_oid) return 0;
if (!$gen_price) return 0;
if (!$gen_desc) $desc = "%OA";
//prepare string
$ret = "mo.cybercash-id=$merchant_id";
$ret .= "&mo.version=3.2.0.4";
$ret .= "&mo.signed-cpi=no";
$ret .= "&mo.order-id=$gen_oid";
$ret .= "&mo.price=$gen_price";
$ret .= "&mo.product-descr=%OA";
//url encode it
$ret = rawurlencode($ret);
return($ret);
}
//--------------------------------------------------------------------------------
//cryptmsg()
//
//Encrypt a message
//
//expects:
// $msg - message to be encrypted
//
//returns:
// normal: an encrypted message
// error: 0
//--------------------------------------------------------------------------------
function cryptmsg($msg) {
global $CRYPTexec;
global $CRYPTinfile;
global $CRYPToutfile;
global $DECRYPTexec;
global $DECRYPTinfile;
global $DECRYPToutfile;
global $merchant_key;
if (!$msg) return 0;
$CRYPTinFD = fopen($CRYPTinfile, "w");
fwrite($CRYPTinFD,$merchant_key,strlen($merchant_key));
fwrite($CRYPTinFD,"\r\n",strlen("\r\n"));
fwrite($CRYPTinFD,$msg,strlen($msg));
fwrite($CRYPTinFD,"\r\n",strlen("\r\n"));
fclose($CRYPTinFD);
$CRYPTcommand = "$CRYPTexec -f $CRYPTinfile > $CRYPToutfile";
exec($CRYPTcommand);
return(1);
}
//--------------------------------------------------------------------------------
//genmsg()
//
//Generate the HTTP msg to be sent to cybercash web servers
//
//expects:
// $mac - base 64 MD5 hash string
// $sessionkey - Time stamp of the encrypted message
// $encryptedmsg - The actual msg
//
//returns:
// normal: msg to be sent to web server
// error: 0
//--------------------------------------------------------------------------------
function genmsg($mac,$sessionkey,$encryptedmsg) {
global $merchant_id;
global $MCKversion;
if (!$mac) return(0);
if (!$sessionkey) return(0);
if (!$encryptedmsg) return(0);
//remove white space from $mac & encode it
$mac=chop($mac);
$mac=rawurlencode($mac);
//remove white space from $encrypted & encode it
$encryptedmessage=chop($encryptedmsg);
$encryptedmessage=rawurlencode($encryptedmsg);
//remove white space from $sessionkey & encode it
$sessionkey=chop($sessionkey);
$sessionkey=rawurlencode($sessionkey);
$sessionkey=ereg_replace("%20","+",$sessionkey);
//build msg
$args = "mac=$mac";
$args .= "&session-key=$sessionkey";
$args .= "&message=$encryptedmessage";
return($args);
}
function sndsocket($msg) {
global $merchant_id;
$rhost = "cr.cybercash.com";
$prgm = "/cgi-bin/directcardpayment.cgi";
$url = "$rhost";
$url .= "$prgm";
$url .= "/$merchant_id";
$cbnet = fsockopen($rhost,80);
if (!$cbnet) {
return(0);
}
else {
fputs($cbnet, "$msg");
for($i=1;$i<=9;$i++) {
if ($i == 8) {
$response = fgets($cbnet,1024);
}
else {
fgets($cbnet,1024);
}
}
fclose($cbnet);
}
return($response);
}
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.