Difference between revisions of "Extension talk:PayPal.php"

From Organic Design wiki
(code notes)
 
(Info)
Line 1: Line 1:
<php>
+
== Installation ==
<?php
+
To install the extension, just save the code into your extensions directory as usual and include it from your LocalSettings.php file. You should also create a file of the same name as the extension but with the .log file-extension instead of .php (e.g. PayPal.log) and make it writable by the web server to allow IPN responses or debugging information to be logged there.
  
// read the post from PayPal system and add 'cmd'
+
To test IPN, see [[PayPal]] for how to set up a paypal test environment, and then enable the IPN functionality and create your forms as in the following LocalSettings.php example:
$req = 'cmd=_notify-validate';
+
{{code|<php>
 +
include('extensions/PayPal.php');
 +
$wgPayPalIPN    = true; # Enable handling of IPN posts from PayPal
 +
$wgUseAjax      = true; # Enable the Ajax updater to dynamically update the form totals after IPN post received
 +
$wgPayPalTest  = 'aran@zenovia.net'; # <--------------- THIS IS YOUR TEST BUYER ACCOUNT
 +
$ipbwiki_paypal[1] = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
 +
<input type="hidden" name="cmd" value="_xclick">
 +
<input type="hidden" name="business" value="aran@organicdesign.co.nz">
 +
<input type="hidden" name="item_name" value="Donation">
 +
<input type="hidden" name="no_shipping" value="1">
 +
<input type="hidden" name="cn" value="Optional Comments">
 +
<input type="hidden" name="currency_code" value="USD">
 +
<input type="hidden" name="tax" value="0">
 +
<input type="hidden" name="bn" value="PP-DonationsBF">
 +
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!">
 +
</form>';
 +
$ipbwiki_paypal[2] = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
 +
<input type="hidden" name="cmd" value="_xclick">
 +
<input type="hidden" name="business" value="aran@organicdesign.co.nz">
 +
<input type="hidden" name="item_name" value="Other thing">
 +
<input type="hidden" name="no_shipping" value="1">
 +
<input type="hidden" name="cn" value="Optional Comments">
 +
<input type="hidden" name="currency_code" value="USD">
 +
<input type="hidden" name="tax" value="0">
 +
<input type="hidden" name="bn" value="PP-DonationsBF">
 +
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!">
 +
</form>';
 +
</php>}}
  
foreach ($_POST as $key => $value) {
+
== Creating a form ==
$value = urlencode(stripslashes($value));
+
The following couple of lines of wikitext will render the two forms defined in LocalSettings above. The parameter supplied is the text which will be rendered along with the form (if the first characters of the text are a number followed by a space, then the number is treated as the number of the form which should be rendered). If IPN is enabled then this text should contain tokens $1, $2 and $3 which will be replaced respectively by total amount donated, total number of donations made and the current state of the transaction (if returning from paypal after purchase - empty otherwise).
$req .= "&$key=$value";
+
<pre>
}
+
<paypal>There have been $2 donations totalling $$1. $3</paypal>
  
// post back to PayPal system to validate
+
<paypal>2 There have been $2 donations totalling $$1. $3</paypal>
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
+
</pre>
$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);
 
  
 
+
== IPN Database Table ==
 
+
The transactions are stored in a database table called ''templatePayPalIPN'' which is structured as follows:
if (!$fp) {
+
<pre>
// HTTP ERROR
+
+------------+---------------+------+-----+---------+-------+
} else {
+
| Field      | Type          | Null | Key | Default | Extra |
fputs ($fp, $header . $req);
+
+------------+---------------+------+-----+---------+-------+
while (!feof($fp)) {
+
| ipn_id    | varchar(32)   | NO  | PRI |        |      |
$res = fgets ($fp, 1024);
+
| ipn_date  | tinytext      | YES  |    | NULL    |      |
if (strcmp ($res, "VERIFIED") == 0) {
+
| ipn_item  | int(11)       | NO  |    |        |      |
// check the payment_status is Completed
+
| ipn_from  | tinytext      | YES  |    | NULL    |      |
// check that txn_id has not been previously processed
+
| ipn_amount | decimal(10,0) | YES  |    | NULL    |      |
// check that receiver_email is your Primary PayPal email
+
| ipn_status | tinytext      | YES  |    | NULL    |      |
// check that payment_amount/payment_currency are correct
+
+------------+---------------+------+-----+---------+-------+
// process payment
+
</pre>
 
+
Here is an example of the tables content after it has been populated with some paypal sandbox transactions made from the form examples shown above:
 
+
<pre>
// echo the response
+
+-------------------+---------------------------+----------+-------------------------------+------------+------------+
echo "The response from IPN was: <b>" .$res ."</b><br><br>";
+
| ipn_id            | ipn_date                  | ipn_item | ipn_from                      | ipn_amount | ipn_status |
 
+
+-------------------+---------------------------+----------+-------------------------------+------------+------------+
//loop through the $_POST array and print all vars to the screen.
+
| 6C047440J05621040 | 15:11:38 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        22 | Completed  |
 
+
| 23M49673DB6083320 | 17:00:51 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |          8 | Completed  |
foreach($_POST as $key => $value){
+
| 18183268SP9363925 | 17:03:16 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        33 | Completed  |
 
+
| 7U934835WE0858113 | 17:06:54 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        21 | Completed  |
        echo $key." = ". $value."<br>";
+
| 68W35106NH479463N | 17:16:52 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        12 | Completed  |
 
+
| 83A03941MM395870F | 17:19:33 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        22 | Completed  |
 
+
| 44384998LU7025730 | 17:22:12 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |        22 | Pending    |
 
+
| 3Y781051NB0709135 | 17:37:27 Dec 12, 2007 PST |        2 | donations@organicdesign.co.nz |        44 | Completed  |
}
+
+-------------------+---------------------------+----------+-------------------------------+------------+------------+
 
+
</pre>
 
 
}
 
else if (strcmp ($res, "INVALID") == 0) {
 
// log for manual investigation
 
 
 
// echo the response
 
echo "The response from IPN was: <b>" .$res ."</b>";
 
 
 
  }
 
 
 
}
 
fclose ($fp);
 
}
 
 
 
# return some bullshet to paypal
 
global $wgOut,$wgParser;
 
if ($expand) $text = $wgParser->preprocess($text,new Title(),new ParserOptions());
 
$wgOut->disable();
 
wfResetOutputBuffers();
 
header('Content-Type: application/octet-stream');
 
if ($save) header("Content-Disposition: attachment; filename=\"$save\"");
 
echo $text;
 
}
 
</php>
 

Revision as of 01:57, 13 December 2007

Installation

To install the extension, just save the code into your extensions directory as usual and include it from your LocalSettings.php file. You should also create a file of the same name as the extension but with the .log file-extension instead of .php (e.g. PayPal.log) and make it writable by the web server to allow IPN responses or debugging information to be logged there.

To test IPN, see PayPal for how to set up a paypal test environment, and then enable the IPN functionality and create your forms as in the following LocalSettings.php example:

{{{1}}}

Creating a form

The following couple of lines of wikitext will render the two forms defined in LocalSettings above. The parameter supplied is the text which will be rendered along with the form (if the first characters of the text are a number followed by a space, then the number is treated as the number of the form which should be rendered). If IPN is enabled then this text should contain tokens $1, $2 and $3 which will be replaced respectively by total amount donated, total number of donations made and the current state of the transaction (if returning from paypal after purchase - empty otherwise).

<paypal>There have been $2 donations totalling $$1. $3</paypal>

<paypal>2 There have been $2 donations totalling $$1. $3</paypal>

IPN Database Table

The transactions are stored in a database table called templatePayPalIPN which is structured as follows:

+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| ipn_id     | varchar(32)   | NO   | PRI |         |       | 
| ipn_date   | tinytext      | YES  |     | NULL    |       | 
| ipn_item   | int(11)       | NO   |     |         |       | 
| ipn_from   | tinytext      | YES  |     | NULL    |       | 
| ipn_amount | decimal(10,0) | YES  |     | NULL    |       | 
| ipn_status | tinytext      | YES  |     | NULL    |       | 
+------------+---------------+------+-----+---------+-------+

Here is an example of the tables content after it has been populated with some paypal sandbox transactions made from the form examples shown above:

+-------------------+---------------------------+----------+-------------------------------+------------+------------+
| ipn_id            | ipn_date                  | ipn_item | ipn_from                      | ipn_amount | ipn_status |
+-------------------+---------------------------+----------+-------------------------------+------------+------------+
| 6C047440J05621040 | 15:11:38 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         22 | Completed  | 
| 23M49673DB6083320 | 17:00:51 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |          8 | Completed  | 
| 18183268SP9363925 | 17:03:16 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         33 | Completed  | 
| 7U934835WE0858113 | 17:06:54 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         21 | Completed  | 
| 68W35106NH479463N | 17:16:52 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         12 | Completed  | 
| 83A03941MM395870F | 17:19:33 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         22 | Completed  | 
| 44384998LU7025730 | 17:22:12 Dec 12, 2007 PST |        1 | donations@organicdesign.co.nz |         22 | Pending    | 
| 3Y781051NB0709135 | 17:37:27 Dec 12, 2007 PST |        2 | donations@organicdesign.co.nz |         44 | Completed  | 
+-------------------+---------------------------+----------+-------------------------------+------------+------------+