CodeIgniter Forums
Paypal_Lib + Monthly Subscription - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Paypal_Lib + Monthly Subscription (/thread-32413.html)

Pages: 1 2


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]bondjp[/eluser]
Hi, i've managed to get the library to work with my app when a user wants to buy some credits. What i'm not getting is how i setup the library to accept monthly payments.

I know that for monthly payments we have to use
Code:
$this->add_field('cmd','_xclick-subscriptions');
instead of
Code:
$this->add_field('cmd','_xclick');

How do i do it?
Thanks.


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]pickupman[/eluser]
I just recently did something similar on a project. I added to the Paypal_lib a add_subscription() method to handle the extra fields like recurring period and such.
Code:
/**
      * Add fields for a subscription button
      * Defaults
      * a3 = $10
      * p3 = 12
      * t3 = months
      * sra = 1 reattempt failed payment
      * @param array of key=>values for fields
      * @return bool
      */
     public function add_subscription( $options = array() ){
        
        //Merge default values with values passed to function
        $options = array_merge( array('a3' => '10',
                                      'p3' => '12',
                                      't3' => 'M',
                                      'sra'  => TRUE,
                                      'custom' => rand()
                                    ), $options);
                                    
        foreach ($options as $key => $value) {
            $this->add_field($key, $value);
        }
       return TRUE;
        
     }

I still use add_field for business_name. I created a class property for paypal command. Then I can pass _xclick or subscription for the form type.


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]bondjp[/eluser]
Thanks for posting.
I'm a newbie so i'm finding a bit hard to merge this.
Ok i can take this code and insert it in Paypal_Lib.
How can i then make my controller to work?
Here's where i will handle the monthly subscriptios:

Controller

Code:
........

function vip(){
    if ($this->tank_auth->is_logged_in())  
        {
        $userid=$this->uri->segment(3);
        
        $this->paypal_lib->add_field('business', '[email protected]');
        $this->paypal_lib->add_field('return', site_url('membership/success/'.$userid));
        $this->paypal_lib->add_field('cancel_return', site_url('membership/cancel/'.$userid));
        $this->paypal_lib->add_field('notify_url', site_url('membership/ipnvip/'.$userid)); // <-- IPN url
        $this->paypal_lib->add_field('custom', $userid); // User ID
        

        $this->paypal_lib->add_field('item_name', 'VIP MEMBERSHIP');
        $this->paypal_lib->add_field('item_number', '3');
        
        
        $this->paypal_lib->paypal_auto_form();
        } else {
                redirect('home');
                }
  }



Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]pickupman[/eluser]
You are on the right track. I think I may have added an initialize method as well, so I could pass an array to configure it. I also created a clear() method to reset the form fields. I was having multiple forms for subscriptions. I have in a controller:
Code:
//Setup subscription button
        $config['paypal_cmd'] = '_xclick-subscriptions';
        $this->paypal_lib->initialize($config); //set paypal cmd for subscriptions
        
        $subscription = array('a3' => '300',
                              'p3' => '1',
                              't3' => 'Y',
                              'src'=> '1'
                            );
        
        $this->paypal_lib->add_field('item_name','Membership');
        $this->paypal_lib->add_field('business', '[email protected]');
        $this->paypal_lib->button('Subscribe');
        $this->paypal_lib->add_subscription($subscription); //add array of subscription values
        $data['paypal_form'] .= $this->paypal_lib->paypal_form();



Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]bondjp[/eluser]
Ok, just tried and i'm getting this error:

Fatal error: Call to undefined method Paypal_Lib::initialize() in /home/availabl/public_html/app/controllers/membership.php on line 109

in this line
Code:
$this->paypal_lib->initialize($config); //set paypal cmd for subscriptions



Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]pickupman[/eluser]
Yeah, I mentioned I added that.
Code:
/**
     * Initialize the configuration variables
     * @param array
     * @return void
     */
     public function initialize( $options = array() ){
        if(is_array($options)){
            foreach($options as $key => $value){
                $this->$key = $value;
            }
        }
        
        // populate $fields array with a few default values.  See the paypal
        // documentation for a list of fields and their data types. These defaul
        // values can be overwritten by the calling script.
        $this->add_field('rm','2');              // Return method = POST
        $this->add_field('cmd',$this->paypal_cmd);
        
        return;

You will have to create the class property paypal cmd
Code:
class Paypal_lib{
  var $paypal_cmd = '_x-click'; //define property


}



Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]bondjp[/eluser]
Ok, did some more digging and found that i get this error:
Fatal error: Call to undefined method Paypal_Lib::initialize() in /home/availabl/public_html/app/controllers/membership.php on line 109

[
Code:
$this->paypal_lib->initialize($config); //set paypal cmd for subscriptions

Only on my live site, my localhost does the processing ok and i can get the order complete in sandbox.

Can someone tell me why it's giving me this error only on my live site?
Thanks.


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]pickupman[/eluser]
It's the casing of the class/filename. Windows doesn't care, but need to make sure the class is named
Paypal_lib (lowercase "l" in lib), and make sure filename is the same way.


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]bondjp[/eluser]
[quote author="pickupman" date="1279843805"]It's the casing of the class/filename. Windows doesn't care, but need to make sure the class is named
Paypal_lib (lowercase "l" in lib), and make sure filename is the same way.[/quote]

You're right. Changed to Paypal_lib and it worked! Thanks!

Now to finish i have 2 problems lol:

1- Getting this: Fatal error: ob_start() [<a href='ref.outcontrol'>ref.outcontrol</a>]: Cannot use output buffering in output buffering display handlers in /home/availabl/public_html/sys/libraries/Exceptions.php on line 162

When displaying this:
Code:
$this->button('Click here if you\'re not automatically redirected...');

        echo '&lt;html&gt;' . "\n";
        echo '&lt;head&gt;&lt;title>Processing Payment...&lt;/title&gt;&lt;/head>' . "\n";
        echo '&lt;body&gt;' . "\n";
        echo '<p>Please wait, your order is being processed and you will be redirected to the paypal website.</p>' . "\n";
        echo $this->paypal_form('paypal_auto_form');
        echo '&lt;/body&gt;&lt;/html>';

I have compression enable so how can i get this going without the error?


2- I don't have any ipn log file after the completed transaction. I only receive the email.
Is this normal?

Thanks.


Paypal_Lib + Monthly Subscription - El Forum - 07-22-2010

[eluser]pickupman[/eluser]
1.In order to use output compression you need to change the echos'. Add each line to a string, and pass that off to a view.
Code:
$data['form'] = '&lt;html&gt;' ."\n";
$data['form'] .= '&lt;head&gt;'; //..rest

$this->load->view('paypal_form', $data);

//paypal_form view
echo $form;

2. In order to use the log, enable the option in config/paypallib_config.php. Make sure the file exists and has write permissions (0777). I edited the code and created a paypal_log table. Then I just insert the log details into the table. I can view in my dashboard that way. Lazy opening files I guess.