Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Ravens PHP Scripts And Web Hosting Forum Index -> NukeSentinel™ v2.5.x
Author Message
bugsy
Worker
Worker


Joined: May 08, 2007
Posts: 130

PostPosted: Tue Jul 22, 2008 10:23 am Reply with quote

Hi I am trying to use NSN Paypal for managing subscriptions in my site. Site is powered by RN2.2, but this "NSN Paypal" is not working - that is - after payment has been made the user is not getting added to the alloted nsn group for that subscription plan.

I came across few posts in this forum only and I have tried two solutions
a)
Code:
$bypassNukeSentinelInvalidIPCheck = false; 
I changed it to
Code:
$bypassNukeSentinelInvalidIPCheck = true; 
.... problem persisted.

b) I commented out
Code:
// Invalid user agent

if($nsnst_const['user_agent']=="none" AND !stristr($_SERVER['PHP_SELF'], "backend.php") AND ($nsnst_const['remote_ip'] != $nsnst_const['server_ip'])) {
  echo abget_template("abuse_invalid2.tpl");
  die();
}
in the nukesentinel.php in includes folder - still nothing happened.

Later I remembered that I have not activated the Paypal IPN notification so tried to activate it but what i came across is this..
Quote:
Instant Payment Notification Preferences
Activate Instant Payment Notification

Before activating Instant Payment Notification, please read the instructions to ensure that you can support the notification system.

Instant Payment Notification integrates PayPal payment notification and authentication with your website's back-end operations. Check this box to activate Instant Payment Notification, and enter the URL at which you will receive the payment notifications below.

Notification URL:
....basically i need to give a notification url.

Now which url should i post here? Thing is I am using two Purchase Plans with NSN Paypal so they have two ipn scripts - so if i give one then wont the other be ignored?

Secondly suppose i want to use subscription in another site of mine then how come only one IPN system will work?

Do i need to just activate it by keeping the url blank?

Any ides?

Thanks in advance..
 
View user's profile Send private message Visit poster's website
Gremmie
Former Moderator in Good Standing


Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA

PostPosted: Tue Jul 22, 2008 11:06 am Reply with quote

Well if you have not activated IPN, the two changes you made are irrelevant. Paypal needs to "call back" to your site before NSN Paypal can do its thing. Likewise, a blank URL isn't going to help you either.

As for which URL. If you really do have 2 or more scripts running off IPN then you will need to combine them for them to work together.

_________________
Only registered users can see links on this board! Get registered or login! - An Event Calendar for PHP-Nuke
Only registered users can see links on this board! Get registered or login! - A Google Maps Nuke Module 
View user's profile Send private message
bugsy
PostPosted: Tue Jul 22, 2008 3:12 pm Reply with quote

Hi the Block (one user will click) script is this one....and a note just mentions that if you have two subscriptions running you need to rename the ipn scripts...
Code:
<?php


/********************************************************/
/* NSN PayPal Purchase Intergration                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright © 2000-2005 by NukeScripts Network         */
/********************************************************/

if (eregi("block-Purchase.php", $_SERVER['PHP_SELF'])) {
    Header("Location: index.php");
    die();
}

$idofgroup = 0;
$freetrial = 0; // # of free trial days
$urlforgroup = "URL_TO_GROUP_ACCESS"; // Set to the url your new group members will have access to
$ppemail = "...."; // Replace with your registered paypal email address
$paytitle = "...."; // Replace PAYMENT_TITLE with your service title
$payamount = "...."; // Replace PAYMENT_AMOUNT with the cost of your service
$curr = "...."; // Replace CURRENCY_TYPE with your currency type (i.e. USD, MUST be a PayPal supported currency)

global $user, $cookie, $user_prefix, $db, $nukeurl;
if (is_user($user)) {
    cookiedecode($user);
    $username = $cookie[1];
    $uid = $cookie[0];
    $oid = $cookie[0]."-".time();
    $oid = str_pad($oid, 19, "0", STR_PAD_LEFT);
    $oid = "P".$oid;

    $pp_formgen = "\n\n<form target='_blank' action='https://www.paypal.com/cgi-bin/webscr' method='POST'>\n";
    $pp_formgen .= "<input type='hidden' name='cmd' value='_ext-enter'>\n";
    $pp_formgen .= "<input type='hidden' name='redirect_cmd' value='_xclick'>\n";
    $pp_formgen .= "<input type='hidden' name='business' value=\"$ppemail\">\n";
    $pp_formgen .= "<input type='hidden' name='item_name' value=\"$paytitle\">\n";
    $pp_formgen .= "<input type='hidden' name='amount' value=\"$payamount\">\n";
    $pp_formgen .= "<input type='hidden' name='txn_type' value='web_accept'>\n";
    $pp_formgen .= "<input type='hidden' name='tax' value='0'>\n";
    $pp_formgen .= "<input type='hidden' name='no_shipping' value='1'>\n";
    $pp_formgen .= "<input type='hidden' name='no_note' value='1'>\n";
    $pp_formgen .= "<input type='hidden' name='custom' value=\"$uid\">\n";
    $pp_formgen .= "<input type='hidden' name='invoice' value=\"$oid\">\n";
    $pp_formgen .= "<input type='hidden' name='on0' value='Group ID'>\n";
    $pp_formgen .= "<input type='hidden' name='os0' value=\"$idofgroup\">\n";
    $pp_formgen .= "<input type='hidden' name='currency_code' value=\"$curr\">\n";
    $pp_formgen .= "<input type='hidden' name='return' value=\"$urlforgroup\">\n";
    $pp_formgen .= "<input type='hidden' name='cancel_return' value=\"$nukeurl\">\n";
    // If you want more then one block/ipn combo you will need to change the name of the ipn.php file
    $pp_formgen .= "<input type='hidden' name='notify_url' value=\"$nukeurl/pp_ipn.php\">\n";
    $pp_formgen .= "<input type='image' SRC='images/paypal.png' height='33' width='88' alt='Pay via PayPal' title='Pay via PayPal' border='0' name='submit'>\n";
    $pp_formgen .= "</form>\n\n";
    $ft_formgen = "\n\n<hr>\n";
    $ft_formgen .= "\n\n<form target='_blank' action=\"$nukeurl/ft_ipn.php\" method='POST'>\n";
    $ft_formgen .= "<input type='hidden' name='custom' value=\"$uid\">\n";
    $ft_formgen .= "<input type='hidden' name='os0' value=\"$idofgroup\">\n";
    $ft_formgen .= "<input type='hidden' name='freetrial' value=\"$freetrial\">\n";
    // Replace URL_TO_GROUP_ACCESS url to the service being purchased
    $ft_formgen .= "<input type='hidden' name='return' value=\"$urlforgroup\">\n";
    $ft_formgen .= "<input type='image' src='images/trial.png' height='33' width='88' alt='$freetrial Day(s) Free Trial' title='$freetrial Day(s) Free Trial' border='0' name='submit'>\n";
    $ft_formgen .= "</form>\n\n";

} else {
    $pp_formgen = "<b>PLEASE LOGIN!</b><br><br>";
}
    // Replace WHAT_THE_USER_IS_BUYING with the description of the service the user is purchasing
    $content = "<CENTER>Service Description";
    $content .= $pp_formgen;
    if ($freetrial > 0) { $content .= $ft_formgen; }
    $content .= "</CENTER>";

?>
Pl note
Quote:
// If you want more then one block/ipn combo you will need to change the name of the ipn.php file


Now here is the basic ipn script
Code:
<?php


/********************************************************/
/* NSN PayPal Purchase Intergration                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright © 2000-2005 by NukeScripts Network         */
/********************************************************/

require_once("config.php");
require_once("db/db.php");
require_once("includes/nsngr_func.php");
$livefeed = 1; // Set to 0 to run test payments thru PayPal's sandbox
$nuke_config = $db->sql_fetchrow($db->sql_query("SELECT * FROM ".$prefix."_config"));

###################
# Start Site Values
###################
$ppemail = "...."; // Replace with your registered paypal email address
// For unlimited leave these set to 0
$ppyears = 0; // Set to the number of years you want the membership to last for
$ppmonths = 1; // Set to the number of months you want the membership to last for
$ppdays = 0; // Set to the number of days you want the membership to last for
$pphours = 0; // Set to the number of hours you want the membership to last for
$ppminutes = 0; // Set to the number of minutes you want the membership to last for

###################
# Start PayPal IPN
###################
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($HTTP_POST_VARS as $key => $value) {
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
}
if($livefeed > 0) {
  // 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 ('www.paypal.com', 80, $errno, $errstr, 30);
} else {
  // post back to PayPal Sandbox system to validate, used for testing IPN
  $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 ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
}

//YOU RECIEVE EMAILS WHEN THIS SCRIPT IS ACCESSED
$req2 = eregi_replace("&","\r\n",$req);
$req2 = urldecode($req2);
mail($nuke_config['adminmail'], "Testing", $req2, "From: ".$nuke_config['adminmail']."\r\n");

// assign posted variables to local variables
// note: additional IPN variables also available -- see IPN documentation
$receiver_email = $HTTP_POST_VARS['receiver_email'];//
$custom = $HTTP_POST_VARS['custom'];//
$option_name1 = $HTTP_POST_VARS['option_name1'];//
$option_selection1 = $HTTP_POST_VARS['option_selection1'];//
$reason_code = $HTTP_POST_VARS['reason_code'];//
$payment_status = $HTTP_POST_VARS['payment_status'];//
$txn_type = $HTTP_POST_VARS['txn_type'];//
$payer_status = $HTTP_POST_VARS['payer_status'];
$payment_type = $HTTP_POST_VARS['payment_type'];
$amount = $HTTP_POST_VARS['mc_gross'];


########################
# Start IPN Validator
########################
if (!$fp) {
  echo "Problem: Error Number: $errno Error String: $errstr";
  exit;
} else {
  fputs($fp, $header . $req);
  while(!feof($fp)) {
    $res = fgets($fp, 1024);
    $res = trim($res);
    if (strcasecmp($res, "VERIFIED") == 0) {
      if ($receiver_email == "$ppemail"){
        if(!empty($custom)) {
          if(!empty($invoice)) {
            if ($payment_status == "Completed"){
              $ydate = time();
              if($ppyears == 0 AND $ppmonths == 0 AND $ppdays == 0 AND $pphours == 0 AND $ppminutes == 0) {
                $xdate = "0";
              } else {
                $xdate = strtotime(date("Y-m-d H:i:00", mktime(date("H")+$pphours, date("i")+$ppminutes, 0, date("m")+$ppmonths, date("d")+$ppdays, date("Y")+$ppyears)));
              }
              $zdate = $xdate - $ydate;
              if(is_ingroup($custom,$option_selection1)) {
                list($expire) = $db->sql_fetchrow($db->sql_query("SELECT `edate` FROM `".$prefix."_nsngr_users` WHERE `uid`='$custom' AND `gid`='$option_selection1'"));
                if($expire > $ydate AND $xdate == 0) {
                    $db->sql_query("UPDATE `".$prefix."_nsngr_users` SET `edate`='0' WHERE `uid`='$custom' AND `gid`='$option_selection1'");
                } elseif($expire > $ydate AND $xdate > 0) {
                    $db->sql_query("UPDATE `".$prefix."_nsngr_users` SET `edate`=`edate`+$zdate WHERE `uid`='$custom' AND `gid`='$option_selection1'");
                } else {
                    $db->sql_query("UPDATE `".$prefix."_nsngr_users` SET `sdate`='$ydate', `edate`='$xdate' WHERE `uid`='$custom' AND `gid`='$option_selection1'");
                }
              } else {
                list($u_name) = $db->sql_fetchrow($db->sql_query("SELECT `username` FROM `".$user_prefix."_users` WHERE `user_id`='$custom'"));
                list($phpBB) = $db->sql_fetchrow($db->sql_query("SELECT `phpBB` FROM `".$prefix."_nsngr_groups` WHERE `gid`='$option_selection1'"));
                $db->sql_query("INSERT INTO `".$prefix."_nsngr_users` VALUES ('$option_selection1', '$custom', '$u_name', '0', '0', '$ydate', '$xdate')");
                $db->sql_query("INSERT INTO `".$prefix."_bbuser_group` VALUES ('$phpBB', '$custom', '0')");
              }
            } elseif ($payment_status == "Completed" && $txn_type == "reversal"){
              list($phpBB) = $db->sql_fetchrow($db->sql_query("SELECT `phpBB` FROM `".$prefix."_nsngr_groups` WHERE `gid`='$option_selection1'"));
              $db->sql_query("DELETE FROM `".$prefix."_nsngr_users` WHERE `gid`='$option_selection1' AND `uid`='$custom'");
              $db->sql_query("DELETE INTO `".$prefix."_bbuser_group` WHERE `group_id`='$phpBB' AND `user_id`='$custom'");
            }
          }
        }
      }
    } else {//if (strcmp($res, "INVALID") == 0)
      // log for manual investigation
      mail($nuke_config['adminmail'], "IPN Failure", $req2, "From: ".$nuke_config['adminmail']."\r\n");
    }
  }
  fclose($fp);
}


?>


Welll in the above ipn script for both plans lets say as i want to keep monthly renewal - actually nothing needs to be changed to merge them - its basically same as per coding.

But in the block file it does mention
Quote:
// If you want more then one block/ipn combo you will need to change the name of the ipn.php file



Another thing - i use paypal for other payments also - so in case someone sends me payment for something else and who is not a member of my site -- will paypal call the ipn script url then also? Will that not create problem in the user database?

It also means that i can use ipn feature only with one site i guess in paypal!
 
Gremmie
PostPosted: Tue Jul 22, 2008 3:34 pm Reply with quote

Paypal only has 1 URL for your Paypal login ID. Therefore it is your job in your IPN script to figure out what needs to be done. For example, I run a (modified) version of the popular Nuke Treasury script. I also sell items on my site using Paypal buttons and shopping cart. And people also send me donations without using NukeTreasury or the Paypal buttons. I had to modify the NukeTreasury ipn script to take all of this into account.

Again, Paypal just calls that one script you tell it about. It is up to you to figure out what happened and act accordingly. So it is indeed possible to use IPN with more than one site/feature.
 
bugsy
PostPosted: Wed Jul 23, 2008 5:33 am Reply with quote

ok - i get the idea- basically having a centralized ipn with various rules in place for all sites and all kinds of paypal integration....but what is bothering me is that note
Quote:
// If you want more then one block/ipn combo you will need to change the name of the ipn.php file
...to me it looks like it says i need to have two separate ipns for two blocks....but then there must be a way to communicate with paypal - which it does not mention!
 
Gremmie
PostPosted: Wed Jul 23, 2008 7:08 am Reply with quote

That comment is not right unless it is assuming you are using multiple Paypal ID's. Again, as far as I know, you only get 1 IPN URL for your paypal account.
 
fkelly
Former Moderator in Good Standing


Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Wed Jul 23, 2008 7:31 am Reply with quote

While scripts like the NSN one and Treasury are a great starting point, they do not remove the necessity for you to understand how Paypal works when you need to do something "unusual". You need to set up an account in Paypal developer and print out a list of the IPN variables and their possible settings. Then study the IPN scripts as well as how the ipnlog.txt file gets written to when Paypal "returns" completed transactions to you site. Then learn how to modify that log file to help you diagnose any problems. You also will need an account in the Paypal Sandbox to use in testing. And you will have to get familiar with the forums in the Paypal developer central because setting up accounts properly on Paypal is complicated and there a lot of useful suggestions there.

Now specifically as to pp_ipn.php the other posters are correct, you will need to combine it and detect what situations are occurring. Here is some code that I use in the diagnosis stage:

Code:
foreach ($_POST as $key => $value) {

   $a = $key;
   kaching!a = $value;
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
//  $logvar .= "$key=$value";
}


What I do is accumulate the POST variables that get sent back from Paypal into the $logvar and then output them into ipnlog.txt. That way I can see exactly what Paypal is sending back and determine what logic should be used to process it. For instance, there is a variable called $item_name that gets sent back from Paypal (you have to send the item_name the user chooses to Paypal in the first place of course). So I have code like this:


Code:
    if ($item_name == 'century') {

      $sql = 'UPDATE nuke_century SET paymentstatus = \'paid \',  paymenttype = \'paypal\', paymentdate = \'' . $datepd . '\'  WHERE user_id =  \''. $custom .'\' AND invoice = \'' .$invoice . '\'';
      $result = $db->sql_query($sql);
   }
   if ($item_name == 'membership') {
      $sql = 'UPDATE nuke_household SET status = \'paid\'
       WHERE Household_Number = \''.$option_name1.'\'';
       $result = $db->sql_query($sql);
   }
and I have tables for "century" and "household" that get updated depending on what gets sent back from Paypal. 

Also, because pp_ipn.php gets executed "silently" in the background, you will not see any errors.  The only way you really will be able to diagnose problems is by the absence of anything getting written to ipnlog.txt.  I wasted a whole day recently when Sandbox was having problems and not writing anything back.  Then I checked the Forums on Paypal developer and found out that a whole bunch of people were affected and there was nothing I could do until Paypal fixed the problem on their end. 

 
View user's profile Send private message Visit poster's website
bugsy
PostPosted: Wed Jul 23, 2008 7:33 am Reply with quote

@ Gremmie..I know...i found that out too..! There is another subscription module available which has some internal adjusted single ipn but multiple plan support facility ws script i think- i might give that a shot too. I mean looks like that this script is not going to help me much if i want to run multiple plans in the site..i mean unless as you said i combine the scripts into one....but regarding this script i might try to have a go with a single one only and test both plans as i said...both ipn scripts will say the same thing if i want to keep 1 month renewal for both...only i need it to assign to different groups.
 
bugsy
PostPosted: Wed Jul 23, 2008 7:54 am Reply with quote

@ fkelly...thanks for that lead...will check the paypal forum also...and will try to fiddle around with your code and see if i can figure it out at my end.....just wondering...for this issue if i need help of a pro developer where should i look for that person...here in RN2.2 jobs forum or in the paypal developer forum?

Actually i will like to have a ipn system to work for multiple sites and at the same time i will like to leave out certain transactions from being pinged back lets say...looks like a coding job for a pro developer...
 
fkelly
PostPosted: Wed Jul 23, 2008 3:07 pm Reply with quote

There is a "for hire" forum. I would start there. I haven't seen anything like that on the Paypal forums but then again I never looked. I meant to mention that the Paypal forums have some very excellent "sticky" posts at the top of the forums that explain in great detail how the IPN and other features work.

The basic concepts are not that difficult. You pass given variables over to Paypal, it validates and processes the variables and sends them back to you as POST variables. There is a "handshake" process in pp_ipn.php where you confirm the transactions with Paypal and then it is finished. Some of the variables that get sent back and forth are customizable ... that is you can put in them what you want and Paypal will send them back that way. So, if you sell "widgets" and "midgets" you can fill item_name with either and you will get that back. Then update your widgets table or your midgets table accordingly.
 
bugsy
PostPosted: Thu Jul 24, 2008 8:08 am Reply with quote

ok thats sounds great and doable i guess Smile never quite tried to see how the ipn works and was looking at a packaged solution - but as you suggest i guess i should first do the research and fiddle around with the codes to see where it takes me and will surely visit the paypal forum and try to find more.
 
bugsy
PostPosted: Thu Jul 24, 2008 8:13 am Reply with quote

I guess you are talking about Only registered users can see links on this board! Get registered or login! and not Only registered users can see links on this board! Get registered or login! first one is official one..both looks good to me!
 
bugsy
PostPosted: Thu Jul 24, 2008 8:28 am Reply with quote

ok got it Smile

Only registered users can see links on this board! Get registered or login!
 
Display posts from previous:       
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Ravens PHP Scripts And Web Hosting Forum Index -> NukeSentinel™ v2.5.x

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©