Category Archives: PHP Tutorial

Integrating paypal to PHP website

While searching for paypal integration, I got this beautiful class from internet.

http://www.micahcarrick.com/php-paypal-ipn-integration-class.html

Thanks Micah Carrick, it is perfect for my work.

I am not explaining much about this integration because if you download it from the above link, it is well documented in the file.

So I don’t want to repeat it here.

Installing IIS and WAMP on same computer

To run IIS and wamp server on the same computer all you need to do is to change the port address.

First, install IIS on your machine , start IIS manager, click on websites–>Right click on Default Websites  and select Properties —> Select Website –> TCP Port- enter port number, say 1025.  Now you can run IIS at http://localhost:1025/

Then install Wamp server, it will run on http://localhost/

How to install ioncube in your server

Some scripts are encoded with ioncube. So to run the script, you need ioncube installed at your server. To install ioncube at your server, use the following instructions:

  1. Go to this page http://www.ioncube.com/loaders.php and download the package suitable for your operating system
  2. Unpack it and upload it to your server in the directory ioncube. The package contains the directory ioncube with loaders for different versions of operating system
  3. Launch the Loader Wizard script in your browser. For example: http://yourdomain/ioncube/loader-wizard.php
  4. Select for run time installation, if your server is Shared ( not dedicated ).
  5. Follow the instructions there and the installation will be done successfully.
  6. Contact the script provider if you do experience any problems running encoded files.
  7. For security reasons it is strongly advised to remove the Wizard script ( loader-wizard.php )  from your server after the installation of the ionCube Loader

How to integrate Cyberbit payment gateway to a PHP Website

I could not find a guide to integrate cyberbit payment gateway to a website. So I thought to write one.

First you need a merchant account with cybernet . When you start an account you will receive a test account with user name and password. Also you will receive some documents. It explains almost everything – how to integrate it. They also give you hash key for you to use in the code.

On the programming side, you have to create a form, so that the customers can enter  card holder details and shipping details. Then POST the data to cyberbit payment gateway.

Following is an example of Form  (taken from the document provided by Cyberbit)

<form method=”POST” action=”https://test.xxxxx.xx/xxxx.php”>
<input type=”hidden” value=”1″ name=”transtype”>
<input type=”hidden” value=”sdf6a6yr3f3df33″ name=”secret”>
<input type=”hidden” value=”https://www.cyberbit.eu/bjarne_test/accept.php”
name=”accepturl”>
<input type=”hidden” value=”CyberTest” name=”merchantid”>
<input type=”hidden” value=”test3″ name=”InternalorderId”>
<input type=”hidden” value=”978″ name=”currencycode”>
<input type=”hidden” value=”100″ name=”amountcleared”>
<input type=”hidden” value=”28006f49d5ffc3a60adbe4898594e749ee34b055″
name=”hash”>
<input type=”hidden” value=”cardholder@email.com” name=”owneremail”>
<input type=”hidden” value=”some street” name=”owneraddress”>
<input type=”hidden” value=”123″ name=”owneraddressnumber”>
<input type=”hidden” value=”London” name=”ownercity”>
<input type=”hidden” value=”OO” name=”ownerstate”>
<input type=”hidden” value=”GB” name=”ownercountry”>
<input type=”hidden” value=”Larry” name=”ownerfirstname”>
<input type=”hidden” value=”Smith” name=”ownerlastname”>
<input type=”hidden” value=”123456″ name=”ownerzip”>
<input type=”hidden” value=”xxxx” name=”ownerphone”>
<input type=”hidden” value='”Item Number”;”Item Description”;”Amount”;”Price”‘
name=”header”>
<input type=”hidden” value='”1″;”Blue car”;”1″;”1.000,00″‘ name=”orderline1″>
<input type=”hidden” value='”2″;”Red bike”;”2″;”250,00″‘ name=”orderline2″>
<input type=”hidden” value='”Shipping”;”150,00″‘ name=”shipping”>
<input type=”hidden” value='”Total”;”1.650,00″‘ name=”total”>
<input type=”submit” value=”Make Payment”>
</form>

All the variables are well defined in cyberbit document. In the above example all the values are defined. In real situation you have to take the input values.

Once the transaction is made cyberbit sends some data back to merchant’s server.  To receive that data we need to create a file. The URL of that file is called callback URL.

For safety ( I guess) in the test account, we don’t have the option to set callback URL. If we send the callback URL to cyberbit, they will update the callback URL info.

The server sends an xml file with all the relevant details of transaction  held. It also sends a fingerprint. we can calculate the fingerprint on our side and can match both fingerprints.

Some web servers, like Apache, will add slashes to all characters that could break communication. These are characters like “, ‘, `, etc. And since the XML sent in the xml post parameter does contain double-quotes it is possible that your web server added an escape character to the double-quote to  sanitize the string, so it would look like this \” instead of just “. This would result in a totally different SHA1 hash result as your XML string is slightly different than the one cyberbit used when they calculated the SHA1 code.

To avoid this, you can try stripping slashes form the XML string before you try an calculate the hash. In PHP this is very simply, simply do stripslashes($_POST[‘xml’]);

If both matches, we can parse the xml and take the data we need and use it.

There are 2 other pages, called Accept URL and Decline URL.

Accept URL is the URL of the file we create to direct the customer upon successful completion of payment.

Decline URl is the URL of the file we create to direct the customer if the payment is declined.

We can set both Accept URL and Decline URL in cyberbet account.

An interesting thing is the shipping –> name, phone and email is not yet used for the payment form, so we can’t send those data. They said they have plans to add them to the payment form.

I will try to add some more notes, when I get time.

How to Detemine The Number Of Fridays In A Month

The following code determines the number of Fridays in a month :

$year = 2008;
$month = 1; //Aug
$day = 1;
$startDate = mktime(0, 0, 0, $month, $day, $year);
$NoD = date( “t”, $startDate);

While(date( “D”, $startDate) != “Fri”) //find first Friday
{
$day++;
$startDate = mktime(0, 0, 0, $month, $day, $year);
}
$day–;
$days = ceil(($NoD-$day)/7);
echo “$days Fridays”;

Replace the $month=1; to the number of the month you want.

Function to Get the variable name

This is a PHP Function to get the name of the variable (not the value) :

function getvarname(&$var)
{
$ret = ”;
$tmp = $var;
$var = md5(uniqid(rand(), TRUE));

$key = array_keys($GLOBALS);
foreach ( $key as $k )
if ( $GLOBALS[$k] === $var )
{
$ret = $k;
break;
}

$var = $tmp;
return $ret;
}

$another = ‘test’;
$testvar = ‘test’;
echo getvarname($testvar); //echoes ‘testvar’
?>

How to display how many weeks and days left when a start date and an end date is given?

Following code in php displays how many weeks and days left when a start date and an end date is given:
function daysWeeks($start,$end)
{
$days_left=$end-$start;
$weeks = (int) ($days_left / 7) ;
$days = $days_left % 7;
echo “$weeks weeks and $days days left!”;
}
$start=”05/08/2008″;
$end=”25/08/2008″;
daysWeeks($start,$end);