paypal_ipn.php
- bonelifer
- Administrator
- Posts: 477
- Joined: 24 Jun 2006, 17:48
- Real name: William
- Location: htpc.MythBuntu
paypal_ipn.php
I'm not sure exactly how to use this other than the the obvious of using an include to include it.
Re: paypal_ipn.php
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:
Now you have to set your paypal fields:
Then you have to assign the template variables:
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.
Now we need to check if this is an IPN request:
Now, all of the paypal data is saved in $paypal->ipn_data. Here's an example of how we use it:
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!
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.
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!
Donate to phpBBModders.net - we're poor!
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
Igor, would please explain as detailed as possible on how to apply the code you showed above?
Re: paypal_ipn.php
Even more detailed?
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
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
Donate to phpBBModders.net - we're poor!
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
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
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
I feel something's wrong/didn't understand something...
This is how my file looks like now:
And this is the HTML:
This is how my file looks like now:
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_ipn = new paypal_ipn();
$paypal->set_field('business', $donate->config['account']);
$paypal->set_field('item_name', $donate->config['slogan']);
$paypal->set_field('no_shipping', 1);
$paypal->set_field('return', $donate->config['success_url']);
$paypal->set_field('cancel_return', $donate->config['cancel_url']);
$paypal->set_field('notify_url', $server_url . $cute_url->build(array('ipn_location')));
$paypal->set_field('tax', 0);
$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';
$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();
?>
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 -->
- tumba25
- Supporter
- Posts: 1052
- Joined: 24 Oct 2007, 13:10
- Real name: Jari Kanerva
- Location: Kokkola, Finland.
- Contact:
Re: paypal_ipn.php
And what error do you get?
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
no error, I just want to make sure I used it correctly.
- tumba25
- Supporter
- Posts: 1052
- Joined: 24 Oct 2007, 13:10
- Real name: Jari Kanerva
- Location: Kokkola, Finland.
- Contact:
Re: paypal_ipn.php
Seems right after a quick look. Try it and see if it works.
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
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?
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?
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();
?>
- tumba25
- Supporter
- Posts: 1052
- Joined: 24 Oct 2007, 13:10
- Real name: Jari Kanerva
- Location: Kokkola, Finland.
- Contact:
Re: paypal_ipn.php
To know if the transaction was successful you need to setWhere 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
$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.
Code: Select all
<input type="hidden" name="notify_url" value="{PAYPAL_NOTIFY_URL}" />
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.
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
Can you give me a sample code, I rely a lot on samples and when I see them I know what to do?
- tumba25
- Supporter
- Posts: 1052
- Joined: 24 Oct 2007, 13:10
- Real name: Jari Kanerva
- Location: Kokkola, Finland.
- Contact:
Re: paypal_ipn.php
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.
I have been looking at it but have not had time to finish it yet. I'll post a notice here when its done.
- Dugi
- New member
- Posts: 22
- Joined: 11 Nov 2008, 11:40
- Real name: Dugagjin
- Location: Prishtina, Kosova
Re: paypal_ipn.php
Okay, thanks a lot