piątek, 5 marca 2010

Zend based class for polish payment gateway Płatności.pl - MVC - Zend Framework

A two-day task while applying for a job.
It applies to specification but not tested in real world app.
Sorry for formatting. I didn't find a gadget that would format the code for blogger.
/**

 * Class PlatnosciPL


 *

 * Contains functionality that is used with Platnosci.pl service

 *

 * PHP version 5

 *

 * LICENSE:


 *

 * @category   CategoryName

 * @package    PackageName

 * @author     soofit.blogspot.com

 * @copyright

 * @version    SVN: $Id$


 * @link   

 */


include('QueryServer.php');

include(
'QueryString.php');



class 
PlatnosciPL
{



    
/**


     * Config variables

     *

     */

    
public $server='www.platnosci.pl';

    public 
$appName='paygw';

    public 
$appUrl='https://www.platnosci.pl/paygw/';


    
/**

     * Encoding of the client's side.

     *

     */

    
public $encoding=array('utf'=>'UTF','win'=>'WIN','iso'=>'ISO');


    
/**

     * Procedure name indicating the operation to perform

     *

     */

    
public $procedure=array('new'=>'NewPayment','get'=>'Payment/get','confirm'=>'Payment/confirm','cancel'=>'Payment/cancel');


    
/**

     * Format of the service response

     *

     */

    
public $format=array('txt'=>'txt','xml'=>'xml');




    public 
$pos_id='12345';

    public 
$session_id='1234567';

    public 
$ts;//timestamp

    
public $sig='';


    public 
$key1;

    public 
$key2;

    public 
$client_ip='1.1.1.1';

    public 
$pos_auth_key='2ref';

    public 
$desc='opis';


    public 
$js='0';

    public 
$amount=12;



    function 
__construct() {

        
$this->ts=time();


    }



    
/**

     * + composes the url part for interacting with the service

     * @param string $enc encoding


     * @param string $proc procedure

     * @return string e.g /paygw/ISO/Payment/get

     * @access public

     */

    
function getServerScript($enc='iso',$proc='get'){


        return 
'/'.$this->appName.'/'.$this->encoding[$enc].'/'.$this->procedure[$proc];


    }

    
/**

     * + sends the form data to the service

     * @param string $params  key=value pairs eg key=value&key=value&


     * @return string result

     * @access public

     */

    
function newPayment(&$params)

    {

        
$this->sig=md5($this->pos_id+$this->session_id+$this->ts+$this->key1);


        
$qs=new QueryServer();

        
$str=$qs->go($this->server,$this->getServerScript('iso','get'),$params);


        return 
$str;



    }

    
/**

     * + receives the transaction status info from the service

     * + checks the hash string


     * + updates the database

     * @param string $params  key=value pairs eg key=value&key=value&

     * @access public


     */

    
function receiveTransactionStatus(&$params)

    {

        
$sig=md5($params['pos_id']+

            
$params['session_id']+


            
$params['ts']+

            
$params['key2']);

        if(
$sig==$params['sig']){

            
//update trans status in db if not


            //updated yet (platnosci.pl may call this not once)

            
echo "OK";

            exit;

        }else{

            echo 
"Not OK";


        }

    }

    
/**

     * + gets the transaction status info from the service

     * + checks the hash string


     * @return string result

     * @access public

     */

    
function getTransactionStatus()

    {

        
$qs=new QueryString();

        
$qs->a['sig']=md5(


            
$this->pos_id+

            
$this->session_id+

            
$this->ts+

            
$this->key1);

        
$qserver=new QueryServer();


        
$str=$qserver->go($this->server,$this->getServerScript('iso','get'),$qs);


        
$msg='Error occured while getting the status.';

        if (
eregi("<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>"


            
,$str

            
,$parts)){

            
//verify received

            
$sig=md5($parts[1].$parts[2].$parts[3].$parts[4].$parts[5].$parts[6].$parts[7].$this->key2);


            if(
$sig==$parts[8]){

              switch (
$parts[5]) {

                  case 
1$msg=$parts[5].' nowa'; break;


                  case 
2$msg=$parts[5].' anulowana'; break;

                  case 
3$msg=$parts[5].' odrzucona'; break;


                  case 
4$msg=$parts[5].' rozpoczta'; break;

                  case 
5$msg=$parts[5].' oczekuje na odbir'; break;


                  case 
6$msg=$parts[5].' autoryzacja odmowna'; break;

                  case 
7$msg=$parts[5].' patno odrzucona'; break;


                  case 
99$msg=$parts[5].' patno odebrana - zakoczona'; break;

                  case 
888$msg=$parts[5].' błędny status'; break;


                  default: 
$msg=$parts[5].' brak statusu'; break;

                }

            

            }

        }

        return 
$msg;


    }

    
/**

     * + cancels the transaction

     * + checks the hash of a string

     * @return string result


     * @access public

     */

    
function cancelPayment()

    {

        
$qs=new QueryString();

        
$qs->a['sig']=md5(


            
$this->pos_id+

            
$this->session_id+

            
$this->ts+

            
$this->key1);

        
$qserver=new QueryServer();


        
$str=$qserver->go($this->server,$this->getServerScript('iso','get'),$qs);


        
$msg='Error occured while cancelling the transaction.';

        if (
eregi("<trans>.*<status>(.*)</status><pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>"


            
,$str

            
,$parts)){

            
//verify received

            
$sig=md5($parts[2].$parts[3].$parts[4].$this->key2);


            if(
$sig==$parts[5]){

              switch (
$parts[1]) {

                  case 
'OK'$msg='OK'; break;


                  case 
'ERROR'$msg='Error'; break;

                  default: 
$msg=' brak statusu'; break;

                }


            

            }

        }

        return 
$msg;

    }



}
?>

Brak komentarzy:

Prześlij komentarz