Page 1 of 1

Reference values from a text box

Posted: Thu Jun 17, 2010 3:29 am
by dark_uk
Hi guys,

I'm in the process of building a PHP payment page which works somewhat like a donation page. The user inputs the amount they wish to pay, hit send and are taken to the HSBC CPI. I HAVE MANAGED TO GET THIS WORKING! although only with an amount stated in the PHP, not with it referencing values from a text box.

Here's my PHP code to make it clearer:

Code: Select all

<?php
session_start();
if (!extension_loaded("libCcCpiTools")){    
    echo "CPI Tool Loaded Successfully!";
}
else{
    echo "not loaded";
}
 
define("MODULE_PAYMENT_HSBC_ID","myid");
define("MODULE_PAYMENT_HSBC_HASH","my hash key");
define("MODULE_PAYMENT_HSBC_TESTMODE","Test");
 
function process_button(){
    global $order, $currency;
  
    $country_codes=array
    (
    'GB'=>'826',
    );
  
  //Currency code setup
  $currency_code = $currency;
  if (!in_array($currency_code, array('EUR', 'GBP', 'HKD', 'JPY', 'USD'))) {
    $currency_code = 'USD';
  }
  $curr=array('EUR'=>978,'HKD'=>344,'JPY'=>392,'GBP'=>826,'USD'=>840);
  
  $currency_code= "826";
    
  //Total setup without .     
  $total= "55";
//  $total=round($total,2);
//  $total=number_format($total, 2, '.', '');

  //Generation of the order_id  
  srand ((float) microtime() * 10000000);
  $r1 = rand(100,999);
  $t1 = date("yz-his");
  
  $sequence = $t1.$r1;
  $sequence = "Order $t1";
  
  //POST time
  /*
  $time=time();
  
  //Change the 0 if your server is located at a different GMT time  
  
 // $time=($time+(0*3600));
 // $time=$time*1000;
  
  $time = $time."000";
  */
  
  $time=time();
 $time*=1000;
     
  $post_1=array(
 
                           'CpiDirectResultUrl'=>'https://www.winstonsolicitors.co.uk/Complete.php',
                           'CpiReturnUrl'=>'https://www.winstonsolicitors.co.uk/Complete.php',
                           'OrderDesc'=>'Our order',
                           'OrderId'=>'Orderid8368',
                           'PurchaseAmount'=>$total, //
                           'PurchaseCurrency'=>$currency_code,
                           'StorefrontId'=>'UK54587401GBP',
                           'TimeStamp'=>$time,
                           'TransactionType'=>'Auth',
                           'MerchantData'=>session_id(),
                           'BillingAddress1'=>'12 Downsway',                                                       
                           'BillingCity'=>'Southwak',                                                     
                           //'BillingCountry'=>$order->billing['country']['title'],                                                    
                           'BillingCountry'=>'826',
                           'BillingCounty'=>'Surrey',                                                          
                           'BillingFirstName'=>'Dan',
                           'BillingLastName'=>'Heaver',
                           'BillingPostal'=>'BN41 4WC',
                           'ShopperEmail'=>'rusagar@hotmail.com',
                           'ShippingAddress1'=>'24 Downsway',                                                 
                           'ShippingCity'=>'Southwark',                                                              
                           //'ShippingCountry'=>$order->delivery['country']['title'],                                                          
                           'ShippingCountry'=>'826',                                                      
                           'ShippingCounty'=>'Surrey',                                                      
                           'ShippingFirstName'=>'Dan',
                           'ShippingLastName'=>'Heaver',
                           'ShippingPostal'=>'BN41 4WC'
                         );
                                                       
 //Test Order                         
      if (MODULE_PAYMENT_HSBC_TESTMODE == 'Test') $post_1['Mode']='T';
      else $post_1['Mode']='P';
                                                           
        reset($post_1);
         
      //Sets hidden fields on the form 
          $cmd="";
          $process_button_string='';
          while(list($k,$v)=each($post_1)){
              //echo $k. " - ".$v."<br>";
                  //$process_button_string.=tep_draw_hidden_field($k,$v);
                   $process_button_string.='<input type="hidden" name="'.$k.'" value="'.$v.'">';
          }
          
          //Gets the hash
          $hash = getHash($post_1);
          
         //echo "My Hash -- ".$hash;
        $process_button_string.='<input type="hidden" name="OrderHash" value="'.$hash.'">';
       //$process_button_string.='<input type="hidden" name="OrderHash" value="IDJjY5lpaYO15sO6Mb8DgW/4T9g=">';
    return $process_button_string;
}
    
//Function to generate a hash to perform the POST or to check received parameters
function getHash($fields){
        $cmd="";
        reset($fields);
        while(list($k,$v)=each($fields)){
                $cmd.=" \"$v\" ";
        }  
        //Path where the TestHash.e executable is located
        //$path='D:/wamp/www/hsbc_payment/cgi-bin/hsbc'; 
        //$path='/home/wakie/public_html/catalog/cgi-bin/hsbc';  
        $path ='/home/sites/winstonsolicitors.co.uk/cgi-bin';  
        $path ='/home/sites/winstonsolicitors.co.uk/cgi-bin';  
               
        putenv("LD_LIBRARY_PATH=$path");        
        //Executes the TestHash to get the hash
        
        $cmd="$path/TestHash.e \"".MODULE_PAYMENT_HSBC_HASH."\" $cmd 2>&1";
        //$cmd="$path/TestHash.e \""."2DsIdINvHawcRZdLrugRDq/9pXq5MaXq"."\" $cmd 2>&1";
        //$cmd="$path/TestHash.e \2DsIdINvHawcRZdLrugRDq/9pXq5MaXq\" $cmd 2>&1";
        //echo $cmd;
        //$ret=exec($cmd, $output);
        $ret=exec($cmd);        
        //echo "Genreated Hash: ".$ret; 
        $ret=split(':',$ret);      
        //Returns the hash
        $hash=trim($ret[1]);  
        echo "Hash Key (If successfull!) >> ".$hash;  
        
         return($hash);}
?>


<form name="cpi_results" method="POST" action="https://www.cpi.hsbc.com/servlet">
<p><label for="total">Total:</label><input name="total" id="total" type="text"></p>

  <?=process_button();?>
  <input type="submit" name="Submit" value="Submit">
</form>
The above code works perfectly however it doesn't allow the total to be input by a user instead it's stated as

Code: Select all

      
  $total= "55";
I need to make this $total reference the text input box...

Any help would be greatly appreciated :)

Re: Reference values from a text box

Posted: Thu Jun 17, 2010 6:37 am
by internet-solution
In PHP, you can read the value of an input element after the form is posted. So in this case you will need a hopping page to achieve what you want. It should work like this -

first form gets amount + user address etc -- post to > your current page, you need to modify this so it reads info from $_POST variables and put them in $post_1 array -- auto submit to > https://www.cpi.hsbc.com/servlet

I have worked with BarclayCard and their documentation included some usefule examples. Have you checked HSBC payment documentation?

Re: Reference values from a text box

Posted: Thu Jun 17, 2010 7:12 am
by dark_uk
Thank you for the reply! although I'm a bit lost to how I to make a text box on a previous page that will POST its contents to

Code: Select all

'PurchaseAmount'=>$total,