PX : code

cyberlib by Timothy Whitfield
Download this code


<?
class payment
{
    var $name, $street, $city, $state, $zip, $country;
    var $type, $merchant_secret;
    var $card_no, $card_exp;

    /*Construction*/
    var $method="POST";
    var $host="localhost";
    var $port=8000;
    var $http_version="HTTP/1.0";
    var $type="mauthonly";
    /*End Construction*/
    
    function make_content($order_no, $amount)
    {
        $content="order-id="+(string)$order_no+"&";
        $content=$content+"amount=usd+"+$amount+"&";
        $content=$content+"card-number="+$this->card_no+"&";
        $content=$content+"card-name="+$this->name+"&";
        $content=$content+"card-address="+$this->street+"&";
        $content=$content+"card-city="+$this->city+"&";
        $content=$content+"card-state="+$this->state+"&";
        $content=$content+"card-zip="+$this->zip+"&";
        $content=$content+"card-exp="+$this->card_exp+"&";
        $content=$content+"card-country="+$this->country;

        return $content;
    }

    function make_request($order_no, $amount)
    {
        $content=$this->make_content($order_no, $amount);
    
        $content=ereg_replace(" ","\\+",$content);
        $content_length=(string)strlen($content);
        $req=$this->method;
        $req=$req+" /"+$this->merchant_secret+"/"+$this->type;
        $req=$req+" "+$this->http_version+"\\r\\n";

        $req=$req+"User-Agent: CyberCashMerchant-2.1.4\\r\\n";
        $req=$req+"Content-Type: application/x-www-form-urlencoded\\r\\n";
        $req=$req+"Content-Length: "+$content_length+"\\r\\n\\r\\n";
        $req=$req+$content+"\\r\\n\\r\\n";

        return $req;
    }    

    function send($order_no, $amount)
    {
        $req=$this->make_request($order_no, $amount);
        $fd=fsockopen($this->host,$this->port);

        if($fd<0)
            return "Error contacting cash register server.\\n";

        fputs($fd,$req);
    
        /* Retrieve header. */
        $i=0;
        $response="";
        while(!feof($fd) && $response != "\\n")
        {
            $response="";
            $more="";
            while(!feof($fd) && $more != "\\n")
            {
                $more=fgets($fd,2);
                if($more != "\\n" || $response=="")
                {
                    $response=$response+$more;
                }
            }
            $header[$i++]=$response;
        }

        /* Retrieve variables. */
        $response="";
        $more="";
        while(!feof($fd) && $more != "\\n")
        {
            $more="";
            $type="var";
            $varname="";
            $varval="";
            while(!feof($fd) && $more != "&" && $more != "\\n")
            {
                $more=fgets($fd,2);
                if($more != "&" && $more != "=" && $more != "\\n")
                {
                    $response=$response+$more;
                }
                if($more=="=")
                {
                    $varval=ereg_replace("\\+"," ",$response);
                    $response="";
                }
                if($more=="&" || $more=="\\n")
                {
                    $varval=$response;
                    $response="";
                }
            }
            if($varname != "")
            {
                $cybervar[$varname]=$varval;
            }
        }

        return $cybervar;
    }
};
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.