نویسنده موضوع: کلاس پرداخت بانک سامان  (دفعات بازدید: 3408 بار)

0 کاربر و 3 مهمان درحال دیدن موضوع.

آفلاین zarif

  • Administrator
  • *****
  • ارسال: 275
    • خدمات میزبانی مشهد هاست
کلاس پرداخت بانک سامان
« در: نوامبر 19, 2008, 07:46:34 »
براي شروع يه جدول داريم با فيلدهاي?

id:
res_num :کد رهگيري سمت فروشنده
ref_num : رسيد بانکي
total_amont : مبلغ کل سفارش که بايد پرداخت بشه
payment : مبلغي که خريدار به حساب فروشنده واريز کرده .
date_start : زمان شروع تراکنش از لحظه اي که خريدار به بانک هدايت مي شود. ثانيه اي

اگر payment برابر با صفر باشه پولي پرداخت نشده ويا جواب تاييد بانک بدستمون نرسيده به هر حال refNum ذخيره ميشه در هر حالتي چون ممکن بعدا کاربر درخواست بده وبايد بررسي بشه.
حالا اگر اين دو تا فيلد يعني totalamont و payment با هم برابر باشن پول پرداخت شده .
اگر payment کمتر از totalamont باشه مقدار پولي که کاربر واريز کرده تاييد شده اما نتونستيم کل مبلغ رو برگشت بزنيم که در اينده بايد اين کار رو انجام بديم.
اگر payment بيشتر از totalamont باشه پولي که کاربر واريز کرده بيشتر از کل مبلغ سفارش بوده و تاييد شده اما نتونستيم مابقي مبلغ رو برگشت بزنيم که در اينده بايد اين کار رو انجام بديم .

حالا کلاس .

متغيرها
action$ : ادرس ارسال مقادير اوليه به بانک.
webMethodURL$ : ادرس اجراي وب متدها
redirectURL$ : ادرس ارسال از بانک به سايت فروشنده
totalAmont$ : کل مبلغ
refNum$ : رسيد بانکي
resNum$ : کد رهگيري
merchantID$ : کد فرشنده
password$ : رمز فروشنده
msg$ : ذخيره خطا ها
errorState$ : خطاهاي وضعيت
errorVerify$ : خطاهاي تاييد و برگشت مبلغ
state$ : تنظيم style

متدها.

سازنده : password ,merchantid رو تنظيم ميکنه .

createResNum : يه کد رهگيري سمت فروشنده ميسازه .

searchResNum( $ResNum ) : دنبال يه کد رهگيري مي گرده .

searchRefNum( $RefNum ) : دنبال يه رسيد بانکي ميگرده .

saveStoreInfo( $TotalAmont ) : با گرفتن کل مبلغ سفارش کد رهگيري و مبلغ سفارش را ذخيره ميکنه .

saveBankInfo($payment ) : کل مبلغي که به حساب فروشنده واريز شده رو مي گيره و با رسيد بانکي ذخيره ميکنه .

recieverParams( $ResNum,$RefNum,$State ): پارامترهاي بانک رو ميگيره.


lastCheck(): وضعيت پرداخت رو مشخص مي کنه تاييد و يا برگشت

VerifyTrans() : تاييد پرداخت .

ReverseTrans( $revNumber ) : با گرفتن يک مبلغ اون رو به حساب کاربر برگشت ميزنه.

SendParams() : کاربر رو به بانک هدايت ميکنه .

setMsg($type,$index) : خطاها رو تنظيم ميکنه .

getMsg($dis) : بدست اوردن خطا و يا نمايش خطا .

مشتریان عزیز لطف کنند  سرویسی که از ما خریداری نموده اند به همراه مشخصات خود به من پیغام خصوصی بدهند تا دسترسی ویژه در انجمن بهشان داده شود .

آفلاین zarif

  • Administrator
  • *****
  • ارسال: 275
    • خدمات میزبانی مشهد هاست
پاسخ : کلاس پرداخت بانک سامان
« پاسخ #1 در: نوامبر 19, 2008, 07:48:04 »
ایجاد جدول و تعریف کلاسها و توابع :

    <?php
    
/*******************************************************************************
    *                                                                             *
    * @version  sbpayment.php version 0.1                                         *
    * @copyright Copyright (c) 2008.                                              *
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License. *
    * @author said shamspour  saidshp@yahoo.com.                                  *
    *                                                                             *
    *******************************************************************************/

    /*
      CREATE TABLE SBPayment (id INT(10) NOT NULL AUTO_INCREMENT,
                              res_num CHAR(20) NOT NULL,
                              ref_num CHAR(20) NOT NULL,
                              total_amont INT NOT NULL,
                              payment INT NOT NULL DEFAULT 0,
                              date_start INT(12) NOT NULL,
                              primary key(id),
                              unique(res_num),
                              index(ref_num));
    */

    
class SBPayment
    
{

        public 
$action  'https://acquirer.sb24.com/CardServices/controller';

        public 
$webMethodURL 'https://Acquirer.sb24.com/ref-payment/ws/ReferencePayment?WSDL';

        public 
$redirectURL 'http//www.yourdomain.com/sbpayment.php';

        public 
$totalAmont;

        public 
$refNum;

        public 
$resNum;

        protected 
$payment;

        protected 
$merchantID;

        protected 
$password;

        protected 
$msg = array();

        protected 
$errorState = array(
        
'Canceled By User'     => 'تراکنش بوسيله خريدار کنسل شده',
        
'Invalid Amount'       => 'مبلغ سند برگشتي  از مبلغ تراکنش اصلي بيشتر است',
        
'Invalid Transaction'  => 'درخواست برگشت تراکنش رسيده است در حالي که تراکنش اصلي پيدا نمي شود',
        
'Invalid Card Number'  => 'شماره کارت اشتباه است',
        
'No Such Issuer'       => 'چنين صادر کننده کارتي وجود ندارد',
        
'Expired Card Pick Up' => 'از تاريخ انقضاي کارت گذشته است',
        
'Incorrect PIN'        => 'رمز کارت اشتباه است pin',
        
'No Sufficient Funds'  => 'موجودي به اندازه کافي در حساب شما نيست',
        
'Issuer Down Slm'      => 'سيستم کارت بنک صادر کننده فعال نيست',
        
'TME Error'            => 'خطا در شبکه بانکي',
        
'Exceeds Withdrawal Amount Limit'      => 'مبلغ بيش از سقف برداشت است',
        
'Transaction Cannot Be Completed'      => 'امکان سند خوردن وجود ندارد',
        
'Allowable PIN Tries Exceeded Pick Up' => 'رمز کارت 3 مرتبه اشتباه وارد شده کارت شما غير فعال اخواهد شد',
        
'Response Received Too Late'           => 'تراکنش در شبکه بانکي تايم اوت خورده',
        
'Suspected Fraud Pick Up'              => 'اشتباه وارد شده cvv2 ويا ExpDate فيلدهاي'
        
);

        protected 
$errorVerify = array(
        
'-1'  => 'خطاي داخلي شبکه',
        
'-2'  => 'سپرده ها برابر نيستند',
        
'-3'  => 'ورودي ها حاوي کاراکترهاي غير مجاز ميباشد',
        
'-4'  => 'کلمه عبور يا کد فروشنده اشتباه است',
        
'-5'  => 'خطاي بانک اطلاعاتي',
        
'-6'  => 'سند قبلا برگشت کامل خورده',
        
'-7'  => 'رسيد ديجيتالي تهي است',
        
'-8'  => 'طول ورودي ها بيشتر از حد مجاز است',
        
'-9'  => 'وجود کارکترهاي غير مجاز در مبلغ برگشتي',
        
'-10' => 'رسيد ديجيتالي حاوي کارکترهاي غير مجاز است',
        
'-11' => 'طول ورودي ها کمتر از حد مجاز است',
        
'-12' => 'مبلغ برگشتي منفي است',
        
'-13' => 'مبلغ برگشتي براي برگشت جزيي بيش از مبلغ برگشت نخورده رسيد ديجيتالي است',
        
'-14' => 'چنين تراکنشي تعريف نشده است',
        
'-15' => 'مبلغ برگشتي به صورت اعشاري داده شده',
        
'-16' => 'خطاي داخلي سيستم',
        
'-17' => 'برگشت زدن تراکنشي که با کارت بانکي غير از بانک سامان انجام شده',
        
'-18' => 'فروشنده نامعتبر است ip address'
        
);

        public 
$style = array('TableBorderColor' => '',
                              
'TableBGColor'     => '',
                              
'PageBGColor'      => '',
                              
'PageBorderColor'  => '',
                              
'TitleFont'        => '',
                              
'TitleColor'       => '',
                              
'TitleSize'        => '',
                              
'TextFont'         => '',
                              
'TextColor'        => '',
                              
'TextSize'         => '',
                              
'TypeTextColor'    => '',
                              
'TypeTextColor'    => '',
                              
'TypeTextSize'     => '',
                              
'LogoURI'          => ''
                              
);

        function 
__construct($mID '',$pass '')
        {
            
$this->merchantID $mID;
            
$this->password   $pass;

        }

        protected function 
createResNum()
        {
            do{
                
$m md5(microtime());
                
$resNum substr($m,0,20);
                
$search mysql_query("SELECT res_num FROM sbpayment WHERE res_num = '$resNum'");
                if( 
mysql_num_rows($search) < ) {
                    break;
                }
            }while( 
true );
            
$this->resNum $resNum;
        }

        protected function 
searchResNum$resNum )
        {
            
$search mysql_query"select * FROM SBPayment WHERE res_num='$resNum'");
            if ( 
mysql_num_rows$search ) < ) {
                return 
false;
            }
            return 
mysql_fetch_assoc$search );
        }

        protected function 
searchRefNum$refNum )
        {
            
$search mysql_query"select * FROM SBPayment WHERE ref_num = '$refNum'" );
            if ( 
mysql_num_rows$search ) < ) {
                return 
false;
            }
            return 
mysql_fetch_assoc$search );
        }

        protected function 
saveBankInfo$payment )
        {
            
$this->payment $payment;
            return 
mysql_query"UPDATE sbpayment SET ref_num = '$this->refNum' ,payment = '$payment' WHERE res_num = '$this->resNum'" ) or $this->setMsg(mysql_error());
        }

        public function 
saveStoreInfo$totalAmont )
        {
            if( 
$totalAmont == '' ) {
                
$this->setMsg"Error: TotalAmont" );
                return 
false;
            }
            
$time time();
            
$this->totalAmont $totalAmont;
            
$this->createResNum();
            return 
mysql_query"INSERT INTO sbpayment SET res_num = '$this->resNum', total_amont = '$this->totalAmont', date_start = $time" ) or $this->setMsg(mysql_error());
        }

         public function 
receiverParams$resNum '' $refNum '' ,$state '' )
        {
            if( ( empty(
$state) or empty($resNum) or strlen($refNum) != 20 ) or $state != 'OK' ) {
                if(isset(
$this->errorState[$state])) {
                    
$this->setMsg'state',$state );
                } else {
                    
$this->setMsg("error state");
                }
                return 
false;
            }

            
$searchResNum $this->searchResNum$resNum );

            if( 
is_array$searchResNum ) ) {
                if( 
$searchResNum['payment'] > 0) {
                    
$this->setMsg"لطفا به قسمت رهگيري سفازش مراجعه کنيد" );
                    return 
false;
                }
            } else {
                
$this->setMsg("همچين تراکنشي در سمت فروشنده تعريف نشده");
                return 
false;
            }

            
$this->refNum     $refNum;
            
$this->resNum     $resNum;
            
$this->totalAmont $searchResNum['total_amont'];

            return 
$this->lastCheck();
        }


        protected function 
lastCheck()
        {
            if( empty(
$this->resNum) or strlen($this->refNum) != 20 ) {
                
$this->setMsg"Error: resNum or refNum is empty" );
                return 
false;
            }
            
//web method verify transaction
            
$verify     $this->verifyTrans();

            if( 
$verify ) {
                if( 
$verify == $this->totalAmont ) {

                    
$this->saveBankInfo$verify );
                    
$this->setMsg("پرداخت با موفقيت انجام شد  لطفا کد رهگيري را يادداشت کنيد");
                    
$this->setMsg"$this->resNum"." : کد رهگيري " );
                    return 
true;


                } elseif( 
$verify $this->totalAmont ) {

                    
//web method partial reverse transaction
                    
$revAmont $verify $this->totalAmont;
                    
$reverse  $this->reverseTrans$revAmont );

                    
$this->setMsg("کاربر گرامي  مبلغ پرداختي بيش از مبلغ درخواستي است");
                    if( 
$reverse == ) {
                        
$this->setMsg("مابقي مبلغ پرداخت شده به حساب شما برگشت خورده");
                        
$this->saveBankInfo$this->totalAmont );
                    } else {
                        
$this->setMsg'verify',$reverse );
                        
$this->setMsg"ما بقي مبلغ پرداختي شما در اينده اي نزديک به حساب شما برگشت خواهد خورد " );
                        
$this->saveBankInfo$verify );
                    }
                    
$this->setMsg("پرداخت با موفقيت انجام شد  لطفا کد رهگيري را يادداشت کنيد");
                    
$this->setMsg"$this->resNum"." : کد رهگيري " );
                    return 
true;


                } elseif( 
$verify $this->totalAmont ) {

                    
//web method full reverse transaction
                    
$rev     $this->reverseTrans$verify );
                    
$this->setMsg("مبلغ پرداختي شما کمتر از مباغ سفارش است ");
                    if( 
$rev == ) {
                        
$this->setMsg("کل مبلغ پرداختي به حساب شما برگشت خورده");
                        
$this->saveBankInfo);
                    } else {
                        
$this->setMsg("در اينده اي نزديک کل مبلغ پرداختي به حساب شما برگشت خواهد خورد لطفا براي پي گيري کد رهگيري را يادداشت کنيد");
                        
$this->setMsg"$this->resNum"." : کد رهگيري " );
                        
$this->setMsg'verify',$rev );
                        
$this->saveBankInfo$verify );
                    }
                    return 
false;
                }
                
//Error transaction
            
} elseif ( $verify or $verify == false ) {
                
$this->setMsg"کاربر گرامي مشکلي در تاييد  پرداخت پيش امده" );
                
$this->setMsg'verify',$verify );
                
$this->saveBankInfo);
                return 
false;
            }
        }
       protected function 
verifyTrans()
        {
            if(empty(
$this->refNum) or empty($this->merchantID) ) {
                return 
false;
            }
            
$soapClient = new soapclient$this->webMethodURL,'wsdl' );
            
$soapProxy  $soapClient->getProxy();
            
$result     false;

            for( 
$a=1;$a<6;++$a ) {
                
$result  $soapProxy->verifyTransaction$this->refNum,$this->merchantID );
                if( 
$result != false ) {
                    break;
                }
            }
            return 
$result;
        }

        protected function 
reverseTrans$revNumber )
        {
            if( 
$revNumber <= or empty($this->refNum) or empty($this->merchantID) or empty($this->password) ) {
                return 
false;
            }
            
$soapClient = new soapclient$this->webMethodURL,'wsdl' );
            
$soapProxy  $soapClient->getProxy();
            
$result     false;

            for( 
$a=1;$a<6;++$a ) {
                
$result     $soapProxy->reverseTransaction$this->refNum,$this->merchantID,$this->password,$revNumber );
                if( 
$result != false )
                    break;
            }
            return 
$result;
        }

        public function 
sendParams()
        {

            if ( 
$this->totalAmont <= or empty($this->action) or empty($this->redirectURL) or empty($this->resNum) or empty($this->merchantID) ) {
                
$this->setMsg"Error: function sendParams()" );
                return 
false;
            }
            
$form  "<html>";
            
$form .= "<body onLoad=\"document.forms['sendparams'].submit();\" >";
            
$form .= "<form name=\"sendparams\" method=\"POST\" action=\"$this->action\" enctype=\"application/x-www-form-urlencoded\" >\n";
            foreach ( 
$this->style as $key=>$val ) {
                if( 
$val != '' ) {
                    
$form .= "<input type=\"hidden\" name=\"$key\" value=\"$val\" />\n";
                }
            }
            
$form .= "<input type=\"hidden\" name=\"Amont\" value=\"$this->totalAmont\" />\n";
            
$form .= "<input type=\"hidden\" name=\"ResNum\" value=\"$this->resNum\" />\n";
            
$form .= "<input type=\"hidden\" name=\"MID\" value=\"$this->merchantID\" />\n";
            
$form .= "<input type=\"hidden\" name=\"RedirectURL\" value=\"$this->redirectURL\" />\n";
            
$form .= "</form>";
            
$form .= "</body>";
            
$form .= "</html>";

            print 
$form;
        }

        protected function 
setMsg($type='',$index='')
        {
            if ( 
$type == 'state' and isset( $this->errorState[$index] ) ) {
                
$this->msg[] = $this->errorState[$index];

            } elseif( 
$type == 'verify' and isset($this->errorVerify[$index]) ) {
                
$this->msg[] = $this->errorVerify[$index];

            } elseif( 
$type != 'verify' and $type != 'state') {
                
$this->msg[] = "$type";
            }
        }

        public function 
getMsg($dis='')
        {
            if( 
count($this->msg) == ) return array();
            if( 
$dis == 'display' ) {
                
$msg  "<ul>\n";
                foreach ( 
$this->msg as $v ) { $msg .= "<li> $v </li>\n"; }
                
$msg .= "</ul>\n";
                return print 
$msg;
            }
            return 
$this->msg;
        }
    }


    
?>

مثال ساده :

    <?php
    ob_start
();
    
header("content-type: text/html; charset=utf-8");

    include 
'./nusoap/lib/nusoap.php';
    include 
'./sbpayment.php';


    
$conn mysql_connect('localhost','root','');
    
mysql_select_db('store',$conn);

    
$sb = new SBPayment("merchantID","password");

    if( isset(
$_POST['State']) ){
        
$State  $_POST['State'];
        
$RefNum $_POST['RefNum'];
        
$ResNum $_POST['ResNum'];
       
        
$sb->receiverParams($ResNum,$RefNum,$State);
        
$sb->getMsg('display');

    } elseif( isset( 
$_POST['submit'] ) ) {

        if( 
$sb->saveStoreInfo$_POST['totalAmont'] ) ) {
            
$sb->sendParams();
        } else {
            
$sb->getMsg('display');
        }
    } else { 
?>


        <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="post" />
        مبلغ سفارش<input type="text" name="totalAmont" />
        <input type="submit" name="submit" value="payment" />
        </form>
    <?php ?>

مشتریان عزیز لطف کنند  سرویسی که از ما خریداری نموده اند به همراه مشخصات خود به من پیغام خصوصی بدهند تا دسترسی ویژه در انجمن بهشان داده شود .