Sunday, October 16, 2011

Paypal Auto Donation - PHP

Here is an auto donation script using paypal as payment option, hope someone has use for it.

PHP Code.


PHP Code:
<?php/**/
// CONFIGURATION REQUIRED FOR PARAMETERS BELOW
// THIS PHP SCRIPT CAN BE RUN FROM YOUR BROWSER WITH "?test=true" PARAMETER TO TEST CONNECTION WITH THE SQL DATABASE
/**/
$paypal_email "myname@domain.com"// You need to set this to your own PayPal email account (at which you will be receiving the payments), this needs to be set in order to verify that the payment was actually delivered to your account$server_name "localhost\SQLEXPRESS"// set this to your SQL Server Name$database_name "accounts"// set this to your Database Name$database_user "user"// set this to your Database User$database_pass "pass"// set this to your Database Password$table "payments"// set this to your Payments Table
/**/
// Functions
/**/
function Error($message// write the message to your php error log, and stop processing the script any further (die){
    
error_log($message);
    die();
}
function 
SQLErrors() // get list of all SQL errors, and return it as one string{
    
$text=""; if(($errors=sqlsrv_errors())!=null)foreach($errors as $error)$text.=$error['message']."\n";
    return 
$text;
}
function 
SQLConnect($server$database$user$pass// connect to SQL Server Database{
    
$params=array('Database'=>$database'UID'=>$user'PWD'=>$pass);
    
$sql=sqlsrv_connect($server$params);
    if(!
$sql)Error("Can't connect to SQL Server (".$server.") Database (".$database."), errors: ".SQLErrors()."\n");
    return 
$sql;
}
function 
SQLDisconnect($sql// disconnect from SQL Server Database{
    
sqlsrv_close($sql);
}
function 
AddPayment($sql$table$user_id$e_mail$payment$currency$transaction_id$test_mode// Add Payment by inserting a Row in the payments Table{
    
$command="INSERT INTO ".$table." (user_id, e_mail, payment, currency, transaction_id) SELECT ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM ".$table." WHERE user_id=? AND transaction_id=? AND e_mail=?)"// command for inserting a payment if it doesn't exist yet
    
$statement=sqlsrv_query($sql$command, array($user_id$e_mail$payment$currency$transaction_id,   $user_id$transaction_id$e_mail));
    if(!
$statement)Error("Could not add payment, errors: ".SQLErrors()."\n");
    
sqlsrv_free_stmt($statement);
    if(
$test_mode)echo("Test Payment Added Successfully!\n"); // if we're running in test mode, then display success message in the browser}
function 
PayPalVerify() // test if PayPal verifies the Payment information{
    
$ok=false;

    
$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\r\n";
    
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    
$header .= "Content-Length: ".strlen($req)."\r\n\r\n";
    
$fp fsockopen('ssl://www.paypal.com'443$errno$errstr30);
    if(
$fp)
    {
        
fputs($fp$header.$req);
        while(!
feof($fp))
        {
            
$res=fgets($fp1024);
            if(
strcmp($res"VERIFIED")==0$ok=true;
        }
        
fclose($fp);
    }
    if(!
$ok)Error("PayPal did not verify the payment");
}
/**/
// Main Codes
/**/
$test_mode = ($_GET['test']!=null); // you can test the script by manually running it from the browser with "?test=true" added at the end of the address, when enabled this will test if the script can properly access your SQL Database by adding a test payment, on success, it will display "Test Payment Added Successfully!", on error, you can investigate your php error log

// connect to SQL Database
$sql=SQLConnect($server_name$database_name$database_user$database_pass);
// if PayPal confirms the paymentif(!$test_mode)PayPalVerify();
// get information about the payment (this data is sent to us by PayPal)$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'];$transaction_id $_POST['txn_id'];$receiver_email $_POST['receiver_email'];$payer_email $_POST['payer_email'];$custom $_POST['custom']; if($custom == null$custom = -1;
// if the money was sent to our address (case insensitive)if(!$test_mode)if(strcasecmp($receiver_email$paypal_email)!=0Error("Someone is sending us fake payments");

if(
$test_mode true : (strcmp($payment_status"Completed")==0)) // if the status of payment is Completed{
    if(
$test_mode) {$custom=-1$payer_email="test@test.test"$payment_amount=0$payment_currency="test"$transaction_id="test";} // setup test data when in test mode

    
$user_id $custom;
    
$e_mail $payer_email;
    
$payment round($payment_amount);
    
$currency $payment_currency;
    
AddPayment($sql$table$user_id$e_mail$payment$currency$transaction_id$test_mode);
}
SQLDisconnect($sql);/**/?>

MSSQL Table.

PHP Code:
CREATE TABLE [accounts].[dbo].[payments]
(
    [
user_id] [intNOT NULL,
    [
e_mail] [nvarchar](128NOT NULL,
    [
payment] [intNOT NULL,
    [
currency] [varchar](5NOT NULL,
    [
transaction_id] [varchar](32NOT NULL,
    [
date] [datetimeNOT NULL DEFAULT GetDate(),

Have fun,

Cheers.

 
Design by Free WordPress Themes | Blogger Theme by Lasantha - Premium Blogger Templates