paypal_ipn.php

Support for the phpbbmodders_lib.
User avatar
bonelifer
Administrator
Administrator
Posts: 477
Joined: 24 Jun 2006, 17:48
Real name: William
Location: htpc.MythBuntu

paypal_ipn.php

Post by bonelifer »

I'm not sure exactly how to use this other than the the obvious of using an include to include it.
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: paypal_ipn.php

Post by igorw »

Hehe, okay. This is a tricky one. :) First of all, there's some documentation on PayPal's IPN [url=https://www.paypal.com/ipn]over here[/url].

There are two parts, one part is generating the form of submission. Let's start with that:

Okay, after including the file we have to make a new instance of the class:

Code: Select all

$paypal_ipn = new paypal_ipn();


Now you have to set your paypal fields:

Code: Select all

      $site_root = generate_board_url(true);

      $paypal->set_field('business', $paypal_settings['email']);
      $paypal->set_field('item_name', 'phpBBModders.net donation');
      $paypal->set_field('no_shipping', 1);
      $paypal->set_field('return', $site_root . $cute_url->build(array('about', 'donate', 'success')));
      $paypal->set_field('cancel_return', $site_root . $cute_url->build(array('about', 'donate', 'cancel')));
      $paypal->set_field('notify_url', $site_root . $cute_url->build(array('ipn_location')));
      $paypal->set_field('tax', 0);
      $paypal->set_field('custom', $user->data['user_id']);


Then you have to assign the template variables:

Code: Select all

      $template->assign_vars(array(
         'CURRENCY_SELECT'   => form_select($this->paypal_currencies),

         'S_ACTION'         => $paypal->paypal_url,
         'S_HIDDEN_FIELDS'   => $paypal->hidden_fields(),
      ));


Now, part two is the more interesting, complex and frustrating part. The actual IPN stuff. You also have to have a class instantiated, as above. Note that this is a separate page.

One thing we have to do is change the paypal URL to HTTP (instead of HTTPS), because they are too stupid to fix it. :roll:

Code: Select all

            // set to http, but only for ipn
            $paypal->paypal_url = 'http://www.paypal.com/cgi-bin/webscr';


Now we need to check if this is an IPN request:

Code: Select all

            if ($paypal->check_ipn())
            {


Now, all of the paypal data is saved in $paypal->ipn_data. Here's an example of how we use it:

Code: Select all

                     $message = vsprintf($user->lang['DONATE_POST_DATA'], array_map('utf8_normalize_nfc', array(
                        $paypal->ipn_data['first_name'],
                        $paypal->ipn_data['last_name'],
                        $paypal->ipn_data['mc_gross'],
                        $paypal->ipn_data['mc_currency'],
                        $paypal->ipn_data['payer_status'],
                        $paypal->ipn_data['payment_status'],
                        $paypal->ipn_data['txn_id'],
                        isset($paypal->ipn_data['memo']) ? $paypal->ipn_data['memo'] : '',
                     )));


You need to do some checks first though. For example on the $paypal->ipn_data['payment_status'], which can be either "Pending" or "Completed".

But you best check the paypal documentation for more information. You can also set the paypal URL to 'http://www.sandbox.paypal.com/cgi-bin/webscr' and sign up at [url=https://developer.paypal.com/]developer.paypal.com[/url] to get access to the sandbox, which will let you test your application with fake money.

I hope that helps!
xCrazy
New member
New member
Posts: 1
Joined: 15 Mar 2009, 01:45

Re: paypal_ipn.php

Post by xCrazy »

thanks!!
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

Igor, would please explain as detailed as possible on how to apply the code you showed above? :)
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: paypal_ipn.php

Post by igorw »

Even more detailed? :o

1. Include the file
2. Instantiate paypal_ipn
3. Assign fields (as seen above)
4. Output a form pointing to $paypal->paypal_url with the $paypal->hidden_fields() as hidden fields

notify_url is the callback location that is called by paypal when the transaction is completed. You can do steps 1-3 on that page (you can use $paypal->check_ipn() to make sure it's an IPN request) and use the information from $paypal->ipn_data as you wish.

Information about the available data can be found here: https://cms.paypal.com/cgi-bin/marketin ... 91F0M006Y4
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

This was detailed enough now, thanks a lot, I'll try and report back if it works or anything else keeps preventing me from success :D
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

I feel something's wrong/didn't understand something...

This is how my file looks like now:

Code: Select all

<?php
&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;/*
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@package&nbsp;-&nbsp;DS&nbsp;PayPal&nbsp;Donation&nbsp;Page&nbsp;MOD
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@version&nbsp;1.0.1
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@copyright&nbsp;2010&nbsp;(c)&nbsp;Dugi.
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@license&nbsp;http://opensource.org/licenses/gpl-license.php&nbsp;GNU&nbsp;Public&nbsp;License
&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@ignore
&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;define('IN_PHPBB',&nbsp;true);
&
nbsp;&nbsp;&nbsp;&nbsp;$current_dir&nbsp;=&nbsp;'./';
&
nbsp;&nbsp;&nbsp;&nbsp;$phpbb_root_path&nbsp;=&nbsp;$current_dir&nbsp;.&nbsp;'../';
&
nbsp;&nbsp;&nbsp;&nbsp;$phpEx&nbsp;=&nbsp;substr(strrchr(__FILE__,&nbsp;'.'),&nbsp;1);
&
nbsp;&nbsp;&nbsp;&nbsp;include($phpbb_root_path&nbsp;.&nbsp;'common.'&nbsp;.&nbsp;$phpEx);
&
nbsp;&nbsp;&nbsp;&nbsp;include($phpbb_root_path&nbsp;.&nbsp;'includes/functions_display.'&nbsp;.&nbsp;$phpEx);
&
nbsp;&nbsp;&nbsp;&nbsp;include($phpbb_root_path&nbsp;.&nbsp;'donate/functions_donate.'&nbsp;.&nbsp;$phpEx);
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Start&nbsp;session&nbsp;management
&nbsp;&nbsp;&nbsp;&nbsp;$user->session_begin();
&
nbsp;&nbsp;&nbsp;&nbsp;$auth->acl($user->data);
&
nbsp;&nbsp;&nbsp;&nbsp;$user->setup('mods/donation_mod');
&
nbsp;&nbsp;&nbsp;&nbsp;$server_url&nbsp;=&nbsp;generate_board_url()&nbsp;.&nbsp;'/';
&
nbsp;&nbsp;&nbsp;&nbsp;$url&nbsp;=&nbsp;$server_url&nbsp;.&nbsp;'donate/index.'&nbsp;.&nbsp;$phpEx;
&
nbsp;&nbsp;&nbsp;&nbsp;if(!strstr($_SERVER['HTTP_REFERER'],&nbsp;$url))
&
nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trigger_error($user->lang['CONFIRM_ERROR']);
&
nbsp;&nbsp;&nbsp;&nbsp;}
&
nbsp;&nbsp;&nbsp;&nbsp;$donate&nbsp;=&nbsp;new&nbsp;donate(true);
&
nbsp;&nbsp;&nbsp;&nbsp;$paypal_ipn&nbsp;=&nbsp;new&nbsp;paypal_ipn();

&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('business',&nbsp;$donate->config['account']);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('item_name',&nbsp;$donate->config['slogan']);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('no_shipping',&nbsp;1);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('return',&nbsp;$donate->config['success_url']);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('cancel_return',&nbsp;$donate->config['cancel_url']);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('notify_url',&nbsp;$server_url&nbsp;.&nbsp;$cute_url->build(array('ipn_location')));
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('tax',&nbsp;0);
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->set_field('custom',&nbsp;$user->data['user_id']);
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$template->assign_vars(array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'S_ACTION'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;$paypal->paypal_url,
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'S_HIDDEN_FIELDS'&nbsp;&nbsp;&nbsp;=>&nbsp;$paypal->hidden_fields(),
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;));
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($paypal->check_ipn())
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->paypal_url&nbsp;=&nbsp;'http://www.paypal.com/cgi-bin/webscr';
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;=&nbsp;vsprintf($user->lang['DONATE_POST_DATA'],&nbsp;array_map('utf8_normalize_nfc',&nbsp;array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['first_name'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['last_name'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['mc_gross'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['mc_currency'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['payer_status'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['payment_status'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$paypal->ipn_data['txn_id'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isset($paypal->ipn_data['memo'])&nbsp;?&nbsp;$paypal->ipn_data['memo']&nbsp;:&nbsp;'',
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)));
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Output&nbsp;the&nbsp;confirmation&nbsp;page
&nbsp;&nbsp;&nbsp;&nbsp;page_header($user->lang['DONATION_CONFIRMATION'],&nbsp;false);
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;$template->set_filenames(array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'body'&nbsp;=>&nbsp;'donate/confirm_body.html')
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;page_footer();
?>


And this is the HTML:

Code: Select all

<!-- INCLUDE overall_header.html -->
<form action='{S_ACTION}' method='post'>
<div class="panel">
   <div class="inner"><span class="corners-top"><span></span></span>
   <h2>{L_DONATION_CONFIRMATION}</h2>
        <fieldset class="fields2">
            <span class="style1"><p>{L_DONATION_CONFIRMATION_MSG}<strong>{S_DONATE_USER_AMOUNT} {S_DONATE_CURRENCY}</strong>.</p></div>
        </fieldset>
   <span class="corners-bottom"><span></span></span><div></div>
</div>
   <div class="panel">
            <div class="inner"><span class="corners-top"><span></span></span>
                <fieldset class="submit-buttons">
                   {S_HIDDEN_FIELDS}
                    <input type='submit' class="button1" name='submit' value='{L_COMPLETE_DONATION}'>
                </fieldset>
                </form>
                <span class="corners-bottom"><span></span></span>
         </div>
<!-- INCLUDE donate/overall_footer_donate.html -->
User avatar
tumba25
Supporter
Supporter
Posts: 1058
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: paypal_ipn.php

Post by tumba25 »

And what error do you get?
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

no error, I just want to make sure I used it correctly.
User avatar
tumba25
Supporter
Supporter
Posts: 1058
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: paypal_ipn.php

Post by tumba25 »

Seems right after a quick look. Try it and see if it works.
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

It works, but I still do not understand the concept of PayPal IPN even after reading the whole documentation, can someone please explain in simply english? :lol:

I think you know my issue from the topic at [url=http://www.phpbb.com/community/viewtopic.php?f=71&t=2108973]phpBB[/url], and igor suggested me to use PayPal IPN. I did so and I still don't know what's the effect of this, I'm just getting the <input type="hidden" name="amount (just an example)" > like using them manually in the HTML. How am I checking if the user came from PayPal using PayPal IPN, this is my question.

P.S: I'm facing problems on understanding the documentation of PayPal IPN, since English isn't my primary language.

This is how my code looks NOW, is this the correct usage?

Code: Select all

<?php
    
/**
    /*
    * @package - DS PayPal Donation Page MOD
    * @version 1.0.1
    * @copyright 2010 (c) Dugi.
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    *
    */
    
    
/**
    * @ignore
    */
    define('IN_PHPBB', true);
    $current_dir = './';
    $phpbb_root_path = $current_dir . '../';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
    include($phpbb_root_path . 'donate/functions_donate.' . $phpEx);
    
    
// Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('mods/donation_mod');
    $server_url = generate_board_url() . '/';
    $url = $server_url . 'donate/index.' . $phpEx;
    if(!strstr($_SERVER['HTTP_REFERER'], $url))
    {
        trigger_error($user->lang['CONFIRM_ERROR']);
    }
    $donate = new donate(true);
    $paypal = new paypal_ipn();
    $option = new option();
    $paypal->paypal_url = ($donate->config['enable_debug'] == true) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr'; 

      $paypal
->set_field('business', $donate->config['account']);
      $paypal->set_field('item_name', $donate->config['slogan']);
      $paypal->set_field('amount', $donate->config['submitted_amount']);
       $paypal->set_field('currency_code', $donate->config['default_currency']);
      $paypal->set_field('no_shipping', 1);
      $paypal->set_field('return', $donate->config['success_url'] . "?key=" . $option->grab_key());
      $paypal->set_field('cancel_return', $donate->config['cancel_url']);
      $paypal->set_field('notify_url', $donate->config['cancel_url']);
      $paypal->set_field('tax', 0);
      $paypal->set_field('bn', 'PP-DonationsBF');
      $paypal->set_field('custom', $user->data['user_id']);
    
      $template
->assign_vars(array(
         'S_ACTION'          => $paypal->paypal_url,
         'S_HIDDEN_FIELDS'   => $paypal->hidden_fields(),
      ));
            
      if 
($paypal->check_ipn())
      {
            $paypal->paypal_url = 'http://www.paypal.com/cgi-bin/webscr'; // do_NOT_change to https
          $message = vsprintf($user->lang['DONATE_POST_DATA'], array_map('utf8_normalize_nfc', array(
          $paypal->ipn_data['first_name'],
          $paypal->ipn_data['last_name'],
          $paypal->ipn_data['mc_gross'],
          $paypal->ipn_data['mc_currency'],
          $paypal->ipn_data['payer_status'],
          $paypal->ipn_data['payment_status'],
          $paypal->ipn_data['txn_id'],
          isset($paypal->ipn_data['memo']) ? $paypal->ipn_data['memo'] : '',
          )));
      }
    
    
// Output the confirmation page
    page_header($user->lang['DONATION_CONFIRMATION'], false);
    
    $template
->set_filenames(array(
        'body' => 'donate/confirm_body.html')
        );
    
    page_footer
();
?>
User avatar
tumba25
Supporter
Supporter
Posts: 1058
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: paypal_ipn.php

Post by tumba25 »

To know if the transaction was successful you need to set

Code: Select all

<input type="hidden" name="notify_url" value="{PAYPAL_NOTIFY_URL}" />
Where PAYPAL_NOTIFY_URL is set to some script that should be called by PayPal to inform you about the moniez. This page should not have any output, it's only for PayPal to communicate with you.

In that file you run something like this

Code: Select all

$paypal = new paypal_ipn();
$success = $paypal->check_ipn();

$success is true if that message is from PayPal and false if not.
If you put $_REQUEST in some file you'll see what data you get from PayPal. You get invoice_id and teansaction_id amongst others. I don't remember all. Then you'll have to act accordingly depending on the state of $success.
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

Can you give me a sample code, I rely a lot on samples and when I see them I know what to do? :)
User avatar
tumba25
Supporter
Supporter
Posts: 1058
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: paypal_ipn.php

Post by tumba25 »

I have been looking at this library and it don't seem to work as expected. Might be that PayPal has done some changes, I don't know yet.

I have been looking at it but have not had time to finish it yet. I'll post a notice here when its done.
User avatar
Dugi
New member
New member
Posts: 23
Joined: 11 Nov 2008, 11:40
Real name: Dugagjin
Location: Prishtina, Kosova

Re: paypal_ipn.php

Post by Dugi »

Okay, thanks a lot :D
Post Reply