1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP and Paypal IPN Subscription and recurring payments

Discussion in 'PHP' started by asgsoft, Jun 19, 2007.

  1. #1
    Hi

    I am about to start a membership website where you have to subscribe to gain access.

    I have a user registration system in place but how do I encoperate Paypal IPN Subscription and recurring payments in it?

    Any Ideas or samples or tutorials out there?

    Thanks
     
    asgsoft, Jun 19, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    So I guess the user has to keep up the subscription for access right ??

    Do you know how standard IPN works, like one time buttons ??

    And how much help do you want, ideas or actual code ??
     
    krakjoe, Jun 19, 2007 IP
  3. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #3
    First question is a yes
    second question is a yes too

    and third question is I need sample code to show me how to go about it
     
    asgsoft, Jun 19, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Well the subscriptions don't work differently to the normal payments everytime the subscription is renewed on paypal ( or cancelled or whatever ) your ipn script will be executed. Obviously there are differences, but its not like you have to learn anything new there.

    So then, in the most basic terms, you incorporate a normal ipn system ( there is a ipn handler on krakjoe.com that uses curl if you wanna save yourself ten minutes ), and a field in the members table ( enum ) to dictate wether the users payment is active, upon the inital ipn you update the enum field to one ( or whatever is appropropriate, depending on how many IPN status's you wanna handle ), and everytime your ipn script is executed after that update your enum field accordingly.

    If you implement the way you think will work and then post or pm me the code, I'll happily look over it for you, but really the only code I could post that I know will be useful at this point ( as you obviously know how to program already ) would be the ipn handler, which I already wrote ......
     
    krakjoe, Jun 19, 2007 IP
    samantha pia likes this.
  5. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #5
    OK where can I get a list of all the required parameters so I know what I need to work with?


    forexample, how do i set the page it returns to when they renew/cancel or even scubscribe for the first time?
     
    asgsoft, Jun 19, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    krakjoe, Jun 21, 2007 IP
    asgsoft likes this.
  7. debiannerd

    debiannerd Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hello,

    I am now emerging from two days of coding and unsuccess in setting up recurring payment with IPN - I have read all the IPN manual and also suggested on this site

    Ok first off, let me start with saying that I have in the past and already succeed in setting up IPN (without "recurring payment"). So to this point it worked with no flaws.

    Since two days, I am trying to set a recurring option but without success.

    I use two codes for managing my IPN script

    One called paypal.php and paypal.class

    the paypal. php is the one where I set my variables,
    
    include ("../conf/conn.php");
    include ("../conf/paypalconf.php");
    
    require_once('paypal.class.php');
    $p = new paypal_class;
    $p->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    
    $this_script = 'MYDOMAIN.COM/paypal.php';
    
    if (empty($_GET['action'])) $_GET['action'] = 'process';  
    
    switch ($_GET['action']) {
        
       case 'process': 
          $p->add_field('username', $_POST['paypalemail']);
          $p->add_field('business', $owner_paypal_email);
          $p->add_field('return', $this_script.'?action=success');
          $p->add_field('cancel_return', $this_script.'?action=cancel');
          $p->add_field('notify_url', $this_script.'?action=ipn');
          $p->add_field('item_name', $item_name.$_POST['login']);
          $p->add_field('amount', $price);
    //MY THREE VARIABLES FOR RECURING
    $p->add_field('reattempt', '1');
    $p->add_field('recur_times', '1');
    $p->add_field('recurring', '1');
          $p->add_field('custom', $_POST['login']);
          $p->submit_paypal_post();
          break;
          
       case 'success':
     
          echo "Thanks for your order, your invoice will be automatically activated once payment is received<br />";
          echo "Click <a href='index.php?action=li'>here</a> to continue</a>";
          break;
          
       case 'cancel':
     
          echo "The order was canceled. Please retry again<br />";
          
          break;
          
       case 'ipn':
          
          if ($p->validate_ipn()) {
          	mysql_query("UPDATE subscriptions SET activated='1' WHERE login='".$p->ipn_data['custom']."'");
    
          }
          break;
     }     
    
    ?>
    
    Code (markup):
    Ok so, as I said this code worked with no problem, but now I added three more variables whih I assumed would set up a "monthly recuring payment, but with no success

    $p->add_field('reattempt', '1');
    $p->add_field('recur_times', '1');
    $p->add_field('recurring', '1');

    Am I doing anything wrong.

    For more information, here is the paypal.class I use
    
    <?php
    class paypal_class {
        
       var $last_error;                 
       
       var $ipn_log;                    
       var $ipn_log_file;               
       var $ipn_response;                  
       var $ipn_data = array();         
       
       var $fields = array();           
    
       
       function paypal_class() {
          
          $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
          
          $this->last_error = '';
          
          $this->ipn_log_file = 'ipn_log.txt';
          $this->ipn_log = true;
          $this->ipn_response = '';
    
          $this->add_field('rm','2');       
          $this->add_field('cmd','_xclick'); 
          
       }
       
       function add_field($field, $value) {
          
          $this->fields["$field"] = $value;
       }
    
       function submit_paypal_post() {
    
          echo "<html>\n";
          echo "<head><title>Processing Payment...</title></head>\n";
          echo "<body onLoad=\"document.form.submit();\">\n";
          echo "<center><h3>Please wait, your order is being processed...</h3></center>\n";
          echo "<form method=\"post\" name=\"form\" action=\"".$this->paypal_url."\">\n";
    
          foreach ($this->fields as $name => $value) {
             echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
          }
     
          echo "</form>\n";
          echo "</body></html>\n";
        
       }
       
       function validate_ipn() {
          $url_parsed=parse_url($this->paypal_url);        
          $post_string = '';    
          foreach ($_POST as $field=>$value) { 
             $this->ipn_data["$field"] = $value;
             $post_string .= $field.'='.urlencode($value).'&'; 
          }
          $post_string.="cmd=_notify-validate";
          $fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30); 
          if(!$fp) {
             $this->last_error = "fsockopen error no. $errnum: $errstr";
             $this->log_ipn_results(false);       
             return false;
             
          } else { 
             fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n"); 
             fputs($fp, "Host: $url_parsed[host]\r\n"); 
             fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
             fputs($fp, "Content-length: ".strlen($post_string)."\r\n"); 
             fputs($fp, "Connection: close\r\n\r\n"); 
             fputs($fp, $post_string . "\r\n\r\n"); 
    		 while(!feof($fp)) { 
                $this->ipn_response .= fgets($fp, 1024); 
             } 
    
             fclose($fp);
    
          }
          
          if (eregi("VERIFIED",$this->ipn_response)) {
             $this->log_ipn_results(true);
             return true;       
             
          } else {
      
             $this->last_error = 'IPN Validation Failed.';
             $this->log_ipn_results(false);   
             return false;
             
          }
          
       }
       
       function log_ipn_results($success) {
           
          if (!$this->ipn_log) return;
    
          $text = '['.date('m/d/Y g:i A').'] - '; 
        
          if ($success) $text .= "SUCCESS!\n";
          else $text .= 'FAIL: '.$this->last_error."\n";
          
          $text .= "IPN POST Vars from Paypal:\n";
          foreach ($this->ipn_data as $key=>$value) {
             $text .= "$key=$value, ";
          }
     
          $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;
          
          $fp=fopen($this->ipn_log_file,'a');
          fwrite($fp, $text . "\n\n"); 
    
          fclose($fp);
       }
    
       function dump_fields() {
          
          echo "<h3>paypal_class->dump_fields() Output:</h3>";
          echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
                <tr>
                   <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
                   <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
                </tr>"; 
          
          ksort($this->fields);
          foreach ($this->fields as $key => $value) {
             echo "<tr><td>$key</td><td>".urldecode($value)." </td></tr>";
          }
     
          echo "</table><br>"; 
       }
    }         
    
    Code (markup):
    Thanks a lot for your all help.
     
    debiannerd, Nov 11, 2007 IP
  8. vivek291186

    vivek291186 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i am using same code for recuring but i cant done recuring in this problam . i hava fazing same prblm .i need you help . so send me any code or define me how i can enable recuring in my code

    thanks
    vivek khatri
     
    vivek291186, Jul 8, 2009 IP
  9. vivek291186

    vivek291186 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    hi sir i need ur help

    sir i m working on recuring in paypal but i couldnt find any solution.

    please send me any code that i m use

    or give me any idea

    i check ur pdf file

    but i got error like
    secuty error in code
    or
    invalid token id

    thanks
     
    vivek291186, Jul 8, 2009 IP
  10. naseemkm

    naseemkm Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello krakjoe's and all..


    Can you please help me to answer my doubt.
    I need to setup paypal auto renewal for my customers in website.
    actually customer will get 30 days website access on every month payment.

    if the monthly auto payment is ok we can increase "website access" days by 30.
    if no it will not add any more days to customers account.

    that means i need to RUN a SCRIPT from my own website to check and increase or stop customers "website access" days after each auto payment.


    can we set up paypal auto renewal like this?
    is there any method to run a script (mywebsite.com/renewalscript.php?customerid=123&payment=ok) after every auto renewal ?
    or do you have any other idea to overcome this ?

    please advice me
     
    naseemkm, May 24, 2010 IP
  11. nicm0

    nicm0 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    humm i have the exact same question, how is this done?
     
    nicm0, Oct 14, 2010 IP