Post: Paypal+Database Tutorial
05-10-2011, 01:05 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

Well at a time i have been wondering how to do just the same thing for my gaming server. And after about a week of learning about paypals IPN and how everything works along with some help of a few books i have i was able to figure it out :] Well here we go. I hope this tutorial will help anyone in need of a donation system for ether a game or a website (Mines for both)

Now i know this isnt so hard to do but there are hundreds of people out there who need help.. Well here it is!

Lets get started!
Sign into paypal and you will see this screen
1. You must login or register to view this content.

Click on "Sell on your website"
2. You must login or register to view this content.

Scroll down a bit and select your button Happy
3. You must login or register to view this content.

Fill in your settings!
4. You must login or register to view this content.

Have your advance settings EXACTLY like mine!


Then generate the button
5. You must login or register to view this content.

Copy and paste everything
6. You must login or register to view this content.

Now for this next part you need 2 things

1. A webhost (To host the IPN.php file we will be making)
2. MySql Database (To store the information)

Now i will not be going into detail on where to get ether so i will just head straight into it! :]

[size=medium]Creating the DataBase[/size]

Make the database and click go
1. You must login or register to view this content.

Name it VIP and we need 3 fields
3. You must login or register to view this content.

Make everything look like it does here! PS: Click SAVE nor GO!
4. You must login or register to view this content.

If you see this CONGRATS! You have a data base Happy
5. You must login or register to view this content.

[size=medium]Modifying the IPN for our button and database[/size]

Here is the original code from paypal

    
<?php

// PHP 4.1

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($req) . "

";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>


Now we need to have it do the following:
1. connect to our database
2. If paid for, insert data

To do this Paypal has already created a if statement for VERIFIED. This will allow us to send certain data to us when the payment is finished.

look at this part of the code located in about the middle of the code
    
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];


We need to add out text fields to this section so that we can grab the data.

Here is what we will add under $payer_email = $_POST['payer_email'];

    $id = $_POST['option_selection1'];
$name = $_POST['option_selection2'];


$id is our steam id (The first Text Field)
$name is our username (The second Text Field)

Now change this

    if (strcmp ($res, "VERIFIED") == 0) {

}


To this

        if ($payment_status =="Completed"){
if($payment_amount=="3.00"&&$payment_currency=="USD")
{
$con = mysql_connect("HOSTNAME","DB USERNAME","DA PASSWORD");
mysql_select_db("DATABASE", $con);
$sql = mysql_query("UPDATE gcusers SET pgroup = 'vip' WHERE steamid = '$id' AND pname = '$name'");
}
}


[size=medium]What is this doing?[/size]

    if (strcmp ($res, "VERIFIED") == 0) {

if ($payment_status =="Completed"){ //is the payment complete?
if($payment_amount=="3.00"&&$payment_currency=="USD") // 0.02 is the ammount we want from the person. MAKE SURE ITS THE SAME AS THE BUTTON. USD is the payment currency THESE BOTH HAVE TO BE CORRECT FOR THE DATA TO GO THROUGH
{
$con = mysql_connect("HOSTNAME","DB USERNAME","DB PASSWORD"); // Connect to our database Make sure you change. HOSTNAME, DB USERNAME, DB PASSWORD to your database information
mysql_select_db("DATABASE", $con); //Select the Database. Again change DATABASE to your database info
$sql = mysql_query("INSERT INTO vip (Username, SteamID, isVIP) VALUES ($id, $name, '1'Winky Winky");//insert our values into our database
//$sql = mysql_query("UPDATE vip SET isVIP = '1' WHERE SteamID = '$id' AND Username = '$name'"); Use this if you want to update a current table and not make one. Just remove the insert values and remove the // in front of this line
}
}

}


Save the file as ipn.php and upload it to your webhost

[size=medium]NOW GO BACK INTO PAYPAL!!![/size]

1. You must login or register to view this content.

2.Click my selling tools on the left navigation bar

3.Under "Getting paid and managing my risk" Click "update" for "Instant payment notifications"

Click "Edit" then enter your information
4. You must login or register to view this content.

5.Click save!!

6.Now place your button somewhere where you want to sell your item and BAM!!! You my friend will have a automatic system that will insert and or update your customers information into a database for you to access later :] :yeye:

Well i hope this tutorial has helped you! :] cURL is a better choice since it is a bit safer but i intend this to be used for beginers. Now of course its free but i would LOVE it if you would please donate so that i may buy a domain name and keep my website/gaming server up Happy :thumbsup:

paypal: [email][email protected][/email]

Enjoy everyone and please comment on ideas or errors you might have ill try my best to help!! :hehe:




source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo